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,27 +133,34 @@ public class Game {
// Start the game
this.gameOn = true;
while(gameOn) {
makeMove(output);
currentP = other(currentP);
if(output){
logGame(hist.getLast());
}
int state = checkState(output);
if(state != (-1)){
if(makeMove(output)){
currentP = other(currentP);
if(output){
System.out.println("*******************");
System.out.println("Thanks for Playing!");
System.out.println("*******************");
logGame(hist.getLast());
}
// Uninstall Jansi
AnsiConsole.systemUninstall();
return state; // the result of the game
}
int state = checkState(output);
if(state != (-1)){
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
AnsiConsole.systemUninstall();
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.
*/
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.
int choice = currentP.getP().move(copyBoard());
// Check his choice against the current board.
if (choice < 0 || choice > GAME_COLUMNS || this.board[choice][0] != 0) {
// 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;
//TODO the game gets stopped, but checkState doesn't know what that means!?
this.gameOn = false;
if(output){
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.
int pos = 0;
@ -198,6 +203,7 @@ public class Game {
if(output){
log(currentP.getP().getName() + " made a move!");
}
return true;
}
/**

View file

@ -27,9 +27,14 @@ public class Human implements Player {
}
}
int choice = -1;
// make sure that the user chooses a legal move
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();
if(!options.contains(new Integer(choice))){
System.out.println("This is not a legal move!");
}
}
return choice;
}