Bug 691

Summary: Function - isIdentical() return true even though puzzleState is not identical
Product: River-Crossing-Puzzle-Solver Reporter: tsunychau2-c
Component: LogicAssignee: tsunychau2-c
Status: RESOLVED FIXED    
Severity: enhancement    
Priority: ---    
Version: v0.9.5   
Hardware: PC   
OS: Mac OS   

Description tsunychau2-c 2022-11-05 20:16:48 HKT
Way to Reproduce:
1. Create two unequal PuzzleState

PuzzleState A:
ArrayList<String> TlandA = new ArrayList<String>(Arrays.asList("tiger", "grass"));
		ArrayList<String> TlandB = new ArrayList<String>(Arrays.asList("farmer", "sheep"));
		PuzzleState tps = new PuzzleState(TlandA,TlandB,1);

PuzzleState B:
ArrayList<String> ClandA = new ArrayList<String>(Arrays.asList("tiger", "grass","cow"));
		ArrayList<String> ClandB = new ArrayList<String>(Arrays.asList("farmer", "sheep"));
		PuzzleState cps = new PuzzleState(ClandA,ClandB,1);

2. Get the return value of function isIdentical()
i.e. boolean result = tps.isIdentical(cps);

Actual Result:

True

Expect Result:

False
Comment 1 tsunychau2-c 2022-11-05 20:17:07 HKT
Bug is confirmed
Comment 2 tsunychau2-c 2022-11-05 20:18:50 HKT
The original code inside isIdentical() does not consider the size of two puzzleState.

public boolean isIdentical(PuzzleState state) {
		if(!this.landA.containsAll(state.landA))
			return false;
		
		if(!this.landB.containsAll(state.landB))
			return false;
			
		return true;
	}

Modification of code:

public boolean isIdentical(PuzzleState state) {
		if(this.landA.size() != state.landA.size())
			return false;
		
		if(this.landB.size() != state.landB.size()) 
			return false;
		
		if(!this.landA.containsAll(state.landA))
			return false;
		
		if(!this.landB.containsAll(state.landB))
			return false;
			
		return true;
	}