Bug 687

Summary: Function-GetLegalMoves() return a set of duplicate character
Product: River-Crossing-Puzzle-Solver Reporter: tsunychau2-c
Component: LogicAssignee: tsunychau2-c
Status: RESOLVED FIXED    
Severity: enhancement    
Priority: Normal    
Version: v0.1.0   
Hardware: PC   
OS: Mac OS   

Description tsunychau2-c 2022-11-05 17:57:49 HKT
Steps to Reproduce:

1. Create a Solver object (Solver s = new Solver();)

2. Create an ArrayList to store the moves (ArrayList <Move> result_moves = s.getLegalMoves(p, node);)

3. Print out every moves inside the ArrayList.


Actual Result:

[Move [farmer, farmer] from LandA to LandB, Move [farmer, grass] from LandA to LandB, Move [farmer, tiger] from LandA to LandB]


Expect Result:

[Move [farmer] from LandA to LandB, Move [farmer, grass] from LandA to LandB, Move [farmer, tiger] from LandA to LandB]
Comment 1 tsunychau2-c 2022-11-05 18:13:24 HKT
Bug is confirmed
Comment 2 tsunychau2-c 2022-11-05 18:16:58 HKT
The bug is fixed. There is a mistake on line 91 in the Solver.java. Replace roles with role.

Modification of code:

	public ArrayList<Move> getLegalMoves(Puzzle puzzle, Node currState) {
		ArrayList<Move> moves = new ArrayList<Move>();
		
		RuleSet ruleSet = new RuleSet(puzzle);
		HashSet<String> travelables = puzzle.getTravelables();
		
		PuzzleState puzzleState = currState.getPuzzleState();
		
		
		int origin = puzzleState.getBoatPos();
		int dest = (origin==0)?1:0;
		
		ArrayList<String> roles = puzzleState.getLandByID(origin);
		
		
		//one traveler 
		for(String travler : travelables)
		{
				Move move = new Move(travler,null, origin, dest);
				Response res = ruleSet.isValidMove(puzzleState, move);
				if (res.isValid())
					moves.add(move);
		}
		
		//two travelers
		for(String travler : travelables)
		{
			for(String role : roles)
			{
				if(travler.equals(role))
					continue;
				
				Move move = new Move(travler,role, origin, dest);
				Response res = ruleSet.isValidMove(puzzleState, move);
				if (res.isValid())
					moves.add(move);
			}
		}

					
		return moves;
	}