我正在编写一个简单的tictactoe 游戏,其中每个玩家都可以是人类或人工智能。因此,为了创建一个新的游戏实例,我所做的只是:
Player p1=new RLPlayer(world);
gameInstance = new GameInstance(this, p1, new HumanPlayer(this, world), world);
但是,如果我做类似的事情
Player p1=new RLPlayer(world);
gameInstance = new GameInstance(this, p1, new HumanPlayer(this, world), world);
Player p2=new RLPlayer(world);
gameInstance = new GameInstance(this, p2, new HumanPlayer(this, world), world);
只进行了 1 场比赛。我发现这是因为对于 HumanPlayer,它在等待移动输入时停止并且什么也不做,如果之后有代码,它就会跳到那个位置。如何让执行等待输入?还是我的方法完全错误?我有一种感觉。
public class HumanPlayer implements Player {
private GameGUI gameGUI;
private TicTacToeWorld world;
public HumanPlayer(GameGUI gameGUI, TicTacToeWorld world){
this.gameGUI = gameGUI;
this.world = world;
}
public boolean makeMove() {
gameGUI.setMoveInputEnabled(true);
return false;
}
public boolean makeMove(int move){ //false means no move made
if(!world.isMoveLegal(move)) return false;
world.makeMove(move);
gameGUI.setMoveInputEnabled(false);
return true;
}
public void observeMove(){
}
}
只是为了一些背景。我认为问题出在上述问题上。
public class GameInstance {
private Player[] players;
private TicTacToeWorld world;
private GameGUI gameGUI;
public GameInstance(GameGUI gameGUI, Player player1, Player player2, TicTacToeWorld world){
this.gameGUI = gameGUI;
players = new Player[]{player1,player2};
this.world = world;
runGame();
}
public void runGame(){
gameGUI.updateGUI(world.getWorldState());
while(!world.isGameOver()){
if(!makePlayerMove())return;
gameGUI.updateGUI(world.getWorldState());
}
}
public boolean makePlayerMove(){
int[] state;
state = world.getWorldState();
if(players[(state[0]-1)%2].makeMove()){
players[state[0]%2].observeMove();
return true;
}
else
return false;
}
public void makePlayerMove(int move){ //force move AI OR input human move
int[] state;
state = world.getWorldState();
if( players[(state[0]-1)%2].makeMove(move) ){ //if move was made e.g. AI. Human player would return false;
gameGUI.updateGUI(world.getWorldState());
players[state[0]%2].observeMove();
runGame();
}
}
}