我正在研究一個代碼來實現不同的搜索功能來解決農夫狼山羊白菜問題。我們給了我們的主要和FarmerWolfGoatCabbage類實現的幾個類。其中一個類別,AbstractSolver包括線如何將我的一個函數轉換爲可重用函數<T>?
Iterable<AState> moves = s.getPossibleMoves();
for (AState move : moves)
if (!closed.contains(move))
addState(move);
這裏是我的FarmerWolfGoatCabbage class.I基本上要
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;
}
翻譯以下功能到一個類似的功能,但與可迭代返回類型
public Iterable<AState> getPossibleMoves()
{
}
如果這是一項家庭作業,您應該添加作業標籤:) – Jack