From ed10fdb24b3be4d5f4882c4e3ef284b4075ad771 Mon Sep 17 00:00:00 2001 From: Malte Tammena Date: Fri, 13 Oct 2017 14:43:08 +0200 Subject: [PATCH] Working game, needing stateChanges --- Makefile | 3 +- src/game/Game.java | 85 ++++++++++++++++++++++++++++--------- src/game/Main.java | 2 + src/player/Player.java | 3 +- src/player/PlayerBasic.java | 28 ++++++++++++ 5 files changed, 100 insertions(+), 21 deletions(-) create mode 100644 src/player/PlayerBasic.java diff --git a/Makefile b/Makefile index 7037d36..89dfb47 100644 --- a/Makefile +++ b/Makefile @@ -13,7 +13,8 @@ LIBARIES= CLASSES=\ src/game/Main.java \ src/game/Game.java \ -src/player/Player.java +src/player/Player.java \ +src/player/PlayerBasic.java OBJECTS=$($(subst $(CLASSPATH),$(BUILDS),$(CLASSES)):.java=.class) diff --git a/src/game/Game.java b/src/game/Game.java index bab4759..01dbecb 100644 --- a/src/game/Game.java +++ b/src/game/Game.java @@ -24,16 +24,19 @@ public class Game { /** * The board on which is played. * [7] columns, [6] rows - * 0 - Empty Space - * 1 - Player One - * 2 - Player Two + * " " - Empty Space */ - private int[][] board; + private String[][] board; + + /** + * Unplayed pieces in the game. + */ + private int piecesLeft; /** * Is the game still running? */ - private boolean gameOn; + private String gameOn; /** * Constructor. @@ -42,7 +45,12 @@ public class Game { public Game(Player p1, Player p2) { this.p1 = p1; this.p2 = p2; - this.board = new int [7][6]; + this.board = new String[7][6]; + for (int i = 0; i < this.board.length; i++) { + for (int j = 0; j < this.board[i].length; j++) { + this.board[i][j] = " "; + } + } } /** @@ -59,44 +67,83 @@ public class Game { first = this.p2; second = this.p1; } + String symFirst = first.getSymbol(); + String symSecond = symFirst; + while (symSecond.equals(symFirst)) { + symSecond = second.getSymbol(); + } + this.piecesLeft = 42; + gameOn = "running"; + while(gameOn == "running") { + makeMove(first, symFirst); + updateState(); + makeMove(second, symSecond); + updateState(); + } - while(gameOn) { - // TODO: Think of another possibility - makeMove(first, ); + switch(gameOn) { + case "draw": + log("The game ended in a draw"); } } /** * Calls a players functions to make a move. */ - private void makeMove(Player p, int nr) { + private void makeMove(Player p, String symbol) { boolean invalid = true; - int choice; + int choice = 0; + long before = System.nanoTime(); while (invalid) { - log(p.getName() + "makes a move!"); - choice = p.move(); - if (this.board[choice][0] == 0) { + choice = p.move(this.board); + if (this.board[choice][0] == " ") { invalid = false; } } - int pos = 0; - while (this.board[choice][pos + 1] == 0) { - pos++; + long after = System.nanoTime(); + double diff = (after - before) / 1000000000.0; + log(p.getName() + " made a move in " + diff + "seconds"); + addPiece(choice, symbol); + } + + private void addPiece(int column, String symbol) { + int pos = 5; + while (pos > 0 && this.board[column][pos] != " ") { + pos--; + } + this.board[column][pos] = symbol; + this.piecesLeft--; + log("" + this.piecesLeft); + logGame(); + } + + /** + * Check the game state. Update this.gameOn if necessary. + */ + private void updateState() { + if (this.piecesLeft == 0) { + this.gameOn = "draw"; } - this.board[choice][pos] = nr; } /** * Log player actions. */ private void log(String s) { + System.out.println(s); } /** * Prints the current board to stdout. */ private void logGame() { - + String out = ""; + for (int i = 0; i < 6; i++) { + for (int j = 0; j < 7; j++) { + System.out.print(this.board[j][i]); + } + System.out.println(); + } } } diff --git a/src/game/Main.java b/src/game/Main.java index bddd1e6..8413bcc 100644 --- a/src/game/Main.java +++ b/src/game/Main.java @@ -3,6 +3,7 @@ package game; import player.Player; // import player.Player1; // import player.Player2; +import player.PlayerBasic; public class Main { @@ -10,5 +11,6 @@ public class Main { // Player p1 = new Player1(); // Player p2 = new Player2(); // new Game(p1, p2).start(); + new Game(new PlayerBasic(), new PlayerBasic()).start(); } } diff --git a/src/player/Player.java b/src/player/Player.java index f7d6016..95abb4d 100644 --- a/src/player/Player.java +++ b/src/player/Player.java @@ -2,6 +2,7 @@ package player; public interface Player { - public int move(int[][] game); + public int move(String[][] game); public String getName(); + public String getSymbol(); } diff --git a/src/player/PlayerBasic.java b/src/player/PlayerBasic.java new file mode 100644 index 0000000..07e3069 --- /dev/null +++ b/src/player/PlayerBasic.java @@ -0,0 +1,28 @@ +package player; + +import java.util.Random; + +public class PlayerBasic implements Player { + + private String name; + private Random ran; + private char symbol = 'A'; + + public PlayerBasic() { + ran = new Random(); + this.name = "Player " + ran.nextInt(); + } + + public int move(String[][] board) { + return ran.nextInt(7); + } + + public String getName() { + return name; + } + + public String getSymbol() { + this.symbol++; + return "" + this.symbol; + } +}