From c40c3fb50e87c87814e0d6b113876e79d2f55939 Mon Sep 17 00:00:00 2001 From: Maurizio Bruetsch Date: Mon, 23 Oct 2017 14:26:03 +0200 Subject: [PATCH 1/2] fixed bug 12 and improved output of Human.java --- src/game/Game.java | 46 ++++++++++++++++++++++++------------------- src/player/Human.java | 6 ++++++ 2 files changed, 32 insertions(+), 20 deletions(-) diff --git a/src/game/Game.java b/src/game/Game.java index 81387b9..dfc5412 100644 --- a/src/game/Game.java +++ b/src/game/Game.java @@ -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; } /** diff --git a/src/player/Human.java b/src/player/Human.java index 84d792e..244523c 100644 --- a/src/player/Human.java +++ b/src/player/Human.java @@ -27,10 +27,16 @@ 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.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!"); + } } + System.out.println(choice); return choice; } From 6394e2d3237efde85b8e6f93bbc052f0aa98038e Mon Sep 17 00:00:00 2001 From: Maurizio Bruetsch Date: Mon, 23 Oct 2017 14:40:40 +0200 Subject: [PATCH 2/2] removed unnecessary output --- src/player/Human.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/player/Human.java b/src/player/Human.java index 244523c..3d73f4f 100644 --- a/src/player/Human.java +++ b/src/player/Human.java @@ -36,7 +36,6 @@ public class Human implements Player { System.out.println("This is not a legal move!"); } } - System.out.println(choice); return choice; }