game: fixed AI can't play and freezes game bug

This commit is contained in:
Nathan Woodburn 2023-05-09 13:37:38 +10:00
parent dd4e360d8e
commit 6edfd9266a
Signed by: nathanwoodburn
GPG Key ID: 203B000478AD0EF1
2 changed files with 38 additions and 13 deletions

View File

@ -385,9 +385,13 @@ public class Player {
/** /**
* Do a calculated move * Do a calculated move
*/ */
public void doAIMove(State state){ public Boolean doAIMove(State state){
String bestMove = createAIMove(state); String bestMove = createAIMove(state);
if (bestMove.equals("")){
System.out.println("No AI moves");
return false;
}
char pieceType = bestMove.charAt(0); char pieceType = bestMove.charAt(0);
String coordStr = bestMove.substring(2); String coordStr = bestMove.substring(2);
int y = Integer.parseInt(coordStr.split(",")[0]); int y = Integer.parseInt(coordStr.split(",")[0]);
@ -396,6 +400,7 @@ public class Player {
lastMove = coord; lastMove = coord;
state.placePiece(coord, pieceType); state.placePiece(coord, pieceType);
state.nextPlayer(); state.nextPlayer();
return true;
} }
/** /**

View File

@ -269,19 +269,23 @@ public class Game extends Application {
* Do a full AI game. This is good to visualize the AI * Do a full AI game. This is good to visualize the AI
*/ */
void AIGame(){ void AIGame(){
while (!currentGame.isPhaseOver()){ while (!game_over){
currentGame.getCurrentPlayer().doAIMove(currentGame); if (!currentGame.getCurrentPlayer().doAIMove(currentGame)){
currentGame.nextPlayer();
}
if (currentGame.isPhaseOver()){
if (currentGame.getCurrentPhase() == 'E') {
currentGame.scorePhase();
currentGame.cleanBoard();
currentGame.distributeResources();
currentGame.nextPhase();
}
else {
game_over = true;
}
}
} }
currentGame.scorePhase();
currentGame.cleanBoard();
currentGame.distributeResources();
currentGame.nextPhase();
while (!currentGame.isPhaseOver()){
currentGame.getCurrentPlayer().doAIMove(currentGame);
}
currentGame.scorePhase();
sendMessage("Game over!",true); sendMessage("Game over!",true);
game_over = true;
refresh(); refresh();
} }
@ -472,6 +476,9 @@ public class Game extends Application {
Label mapLabel = new Label("Select Map:"); Label mapLabel = new Label("Select Map:");
Label aiLabel = new Label("How many AI players:"); Label aiLabel = new Label("How many AI players:");
Label stuckLabel = new Label("Stuck?");
Button stuckButton = new Button("Skip my turn");
// Numeric select for AI // Numeric select for AI
ComboBox aiSelector = new ComboBox(); ComboBox aiSelector = new ComboBox();
aiSelector.getItems().add("0"); aiSelector.getItems().add("0");
@ -528,8 +535,21 @@ public class Game extends Application {
} }
}); });
stuckButton.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent e) {
currentGame.nextPlayer();
StringBuilder message = new StringBuilder("You skipped your turn");
while (currentGame.getCurrentPlayer().isAI()) {
message.append("\n").append(doAIMove());
}
sendMessage(message.toString());
refresh();
}
});
HBox hb = new HBox(); HBox hb = new HBox();
hb.getChildren().addAll(mapLabel,mapSelector,aiLabel,aiSelector,newLabel, twoPlayer,threePlayer,fourPlayer); hb.getChildren().addAll(mapLabel,mapSelector,aiLabel,aiSelector,newLabel, twoPlayer,threePlayer,fourPlayer,stuckLabel,stuckButton);
hb.setSpacing(10); hb.setSpacing(10);
hb.setLayoutX(50); hb.setLayoutX(50);
hb.setLayoutY(WINDOW_HEIGHT - 50); hb.setLayoutY(WINDOW_HEIGHT - 50);