Merge remote-tracking branch 'origin/master' into malte-alternative

This commit is contained in:
Malte Tammena 2017-10-23 15:20:00 +02:00
commit c8c8c8d973
2 changed files with 32 additions and 21 deletions

View file

@ -133,7 +133,7 @@ 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){ if(output){
logGame(hist.getLast()); logGame(hist.getLast());
@ -149,11 +149,18 @@ public class Game {
AnsiConsole.systemUninstall(); AnsiConsole.systemUninstall();
return state; // the result of the game 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,9 +27,14 @@ 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.println("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!");
}
} }
return choice; return choice;
} }