fixed bug 12 and improved output of Human.java

This commit is contained in:
Maurizio Bruetsch 2017-10-23 14:26:03 +02:00
parent 4d3b14c3cc
commit c40c3fb50e
2 changed files with 32 additions and 20 deletions

View file

@ -133,27 +133,34 @@ public class Game {
// Start the game // Start the game
this.gameOn = true; this.gameOn = true;
while(gameOn) { while(gameOn) {
makeMove(output); if(makeMove(output)){
currentP = other(currentP); currentP = other(currentP);
if(output){
logGame(hist.getLast());
}
int state = checkState(output);
if(state != (-1)){
if(output){ if(output){
System.out.println("*******************"); logGame(hist.getLast());
System.out.println("Thanks for Playing!");
System.out.println("*******************");
} }
// Uninstall Jansi int state = checkState(output);
AnsiConsole.systemUninstall(); if(state != (-1)){
return state; // the result of the game if(output){
} System.out.println("*******************");
System.out.println("Thanks for Playing!");
System.out.println("*******************");
}
// Uninstall Jansi
AnsiConsole.systemUninstall();
return state; // the result of the game
}
} else{
// an illegal move was made
if(output){
System.out.println(other(currentP).getP().getName() + " wins.");
}
return other(currentP).getID(); // TODO this only works if the players hav IDs 1 and 2
}
} }
// Uninstall Jansi // Uninstall Jansi
AnsiConsole.systemUninstall(); AnsiConsole.systemUninstall();
System.out.println("BUG!"); System.out.println("BUG!");
return checkState(false); // it is impossible to reach this, but it makes the compiler happy ;) return checkState(false); // this is impossible to reach this, but it makes the compiler happy ;)
} }
/** /**
@ -173,19 +180,17 @@ public class Game {
/** /**
* Calls a players functions to make a move. * Calls a players functions to make a move.
*/ */
private void makeMove(boolean output) { private boolean makeMove(boolean output) {
// Get a choice from the player, while only giving him a copy of the game. // Get a choice from the player, while only giving him a copy of the game.
int choice = currentP.getP().move(copyBoard()); int choice = currentP.getP().move(copyBoard());
// Check his choice against the current board. // Check his choice against the current board.
if (choice < 0 || choice > GAME_COLUMNS || this.board[choice][0] != 0) { if (choice < 0 || choice > GAME_COLUMNS || this.board[choice][0] != 0) {
// If a player makes a false move, the game punishes him. // If a player makes a false move, the game punishes him.
// TODO: Fix this, causes -1 return of checkstate in Simulation mode, breaks the game. this.gameOn = false;
// this.gameOn = false;
//TODO the game gets stopped, but checkState doesn't know what that means!?
if(output){ if(output){
log(currentP.getP().getName() + " made an illegal move and lost!"); log(currentP.getP().getName() + " made an illegal move and lost!");
} }
return; return false;
} }
// Find the lowest empty field in the board in the choosen column. // Find the lowest empty field in the board in the choosen column.
int pos = 0; int pos = 0;
@ -198,6 +203,7 @@ public class Game {
if(output){ if(output){
log(currentP.getP().getName() + " made a move!"); log(currentP.getP().getName() + " made a move!");
} }
return true;
} }
/** /**

View file

@ -27,10 +27,16 @@ public class Human implements Player {
} }
} }
int choice = -1; int choice = -1;
// make sure that the user chooses a legal move
while (!options.contains(new Integer(choice))) { while (!options.contains(new Integer(choice))) {
System.out.print("Choose a move from " + options + ": "); System.out.print("Choose a move from " + options + ": ");
System.out.flush();
choice = sc.nextInt(); choice = sc.nextInt();
if(!options.contains(new Integer(choice))){
System.out.println("This is not a legal move!");
}
} }
System.out.println(choice);
return choice; return choice;
} }