Bug 691 - Function - isIdentical() return true even though puzzleState is not identical
Summary: Function - isIdentical() return true even though puzzleState is not identical
Status: RESOLVED FIXED
Alias: None
Product: River-Crossing-Puzzle-Solver
Classification: Unclassified
Component: Logic (show other bugs)
Version: v0.9.5
Hardware: PC Mac OS
: --- enhancement
Assignee: tsunychau2-c
URL:
Depends on:
Blocks:
 
Reported: 2022-11-05 20:16 HKT by tsunychau2-c
Modified: 2022-11-05 20:19 HKT (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
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;
	}