我正在编写代码来实现不同的搜索功能来解决 Farmer Wolf Goat Cabbage 问题。我们获得了几个由我们的 main 和 FarmerWolfGoatCabbage 类实现的类。类之一,AbstractSolver 包括该行
Iterable<AState> moves = s.getPossibleMoves();
for (AState move : moves)
if (!closed.contains(move))
addState(move);
这是我的FarmerWolfGoatCabbage类。我基本上想翻译以下功能
public DepthFirstSolver getPossibleMoves1(){
DepthFirstSolver moves = null;
//use getOpposite() and addIfSafe
FarmerWolfGoatState fwgsParent = new FarmerWolfGoatState();
FarmerWolfGoatState fwgsChild = null;
int hash;
// the farmer's current position before crossing the river
Side farmerCurrent = this.farmer;
if(this.wolf == farmerCurrent){
fwgsChild = new FarmerWolfGoatState(this, this.getOpposite(this.farmer),
this.getOpposite(this.wolf), this.goat, this.cabbage);
hash = fwgsChild.hashCode();
if(addIfSafe(hash))
moves.addState(fwgsChild);
System.out.println("W");
}
if(this.cabbage == farmerCurrent){
fwgsChild = new FarmerWolfGoatState(this, this.getOpposite(this.farmer),
this.wolf, this.goat, this.getOpposite(this.cabbage));
hash = fwgsChild.hashCode();
if(addIfSafe(hash))
moves.addState(fwgsChild);
System.out.println("C");
}
if(this.goat == farmerCurrent){
fwgsChild = new FarmerWolfGoatState(this, this.getOpposite(this.farmer),
this.wolf, this.getOpposite(this.goat), this.cabbage);
hash = fwgsChild.hashCode();
fwgsChild.getPosition();
//
if (fwgsChild == null)
System.out.println("NULL");
if(addIfSafe(hash))
//moves.addState(fwgsChild);
System.out.println("G");
}
return moves;
}
进入一个类似的函数,但返回类型为 Iterable
public Iterable<AState> getPossibleMoves()
{
}