From d7074f2a18391bba5e73c77a2c7a9c70dcd15d1b Mon Sep 17 00:00:00 2001 From: Malte Tammena Date: Sun, 15 Oct 2017 12:52:53 +0200 Subject: [PATCH 1/5] Fixing format Signed-off-by: Malte Tammena --- src/game/Game.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/game/Game.java b/src/game/Game.java index 69b629a..6f22892 100644 --- a/src/game/Game.java +++ b/src/game/Game.java @@ -327,8 +327,8 @@ public class Game { * The Player that started the game gets 'X' the other one 'O'. */ private void logGame() { - for(int i=0;i Date: Sun, 15 Oct 2017 13:12:31 +0200 Subject: [PATCH 2/5] Redoing players in the game. Signed-off-by: Malte Tammena --- .gitignore | 1 + src/game/Game.java | 26 +++++++++++++++----------- src/player/Player1.java | 2 +- 3 files changed, 17 insertions(+), 12 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a2e6bd4 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +doc/ diff --git a/src/game/Game.java b/src/game/Game.java index 6f22892..c4f80f3 100644 --- a/src/game/Game.java +++ b/src/game/Game.java @@ -29,6 +29,11 @@ public class Game { */ private final Player p2; + /** + * Player who made the last move. + */ + private Player lastPlayer; + /** * The game's board. * It's size is GAME_COLUMNS * GAME_ROWS. @@ -46,6 +51,9 @@ public class Game { /** * Constructor. * Initialize board and save players. + * + * @param p1 Player One + * @param p2 Player Two */ public Game(Player p1, Player p2) { this.p1 = p1; @@ -68,16 +76,16 @@ public class Game { first = this.p2; second = this.p1; } - boolean turn = true; this.gameOn = true; while(gameOn) { - if(turn){ - makeMove(first,turn); + if(lastPlayer == second){ + lastPlayer = first; + makeMove(first); } else { - makeMove(second,turn); + lastPlayer = second; + makeMove(second); } if(gameOn) { - turn = !turn; logGame(); checkState(); } else { @@ -91,7 +99,7 @@ public class Game { /** * Calls a players functions to make a move. */ - private void makeMove(Player p, boolean turn) { + private void makeMove(Player p) { log(p.getName() + " makes a move!"); // Get a choice from the player, while only giving him a copy of the game. int choice = p.move(copyBoard()); @@ -108,11 +116,7 @@ public class Game { pos++; } // Change the board accordingly. - if(turn){ - this.board[choice][pos] = 1; - } else { - this.board[choice][pos] = 2; - } + this.board[choice][pos] = p.getNr(); } /** diff --git a/src/player/Player1.java b/src/player/Player1.java index f7ae56f..2c5f9c0 100644 --- a/src/player/Player1.java +++ b/src/player/Player1.java @@ -2,7 +2,7 @@ package player; import java.util.Random; -public class Player1 implements Player{ +public class Player1 implements Player { private String name; private Random ran; From be0540a879db436970adad02785e978198b5de76 Mon Sep 17 00:00:00 2001 From: Malte Tammena Date: Sun, 15 Oct 2017 13:20:16 +0200 Subject: [PATCH 3/5] Added a player wrapper PlayerObject Signed-off-by: Malte Tammena --- Makefile | 1 + src/game/Game.java | 10 +++++----- src/game/PlayerObject.java | 40 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 46 insertions(+), 5 deletions(-) create mode 100644 src/game/PlayerObject.java diff --git a/Makefile b/Makefile index 8523c36..2b202b4 100644 --- a/Makefile +++ b/Makefile @@ -13,6 +13,7 @@ LIBARIES= CLASSES=\ src/game/Main.java \ src/game/Game.java \ +src/game/PlayerObject.java \ src/player/Player.java \ src/player/Player1.java \ src/player/Player2.java diff --git a/src/game/Game.java b/src/game/Game.java index c4f80f3..85d2973 100644 --- a/src/game/Game.java +++ b/src/game/Game.java @@ -22,17 +22,17 @@ public class Game { /** * Player One */ - private final Player p1; + private final PlayerObject p1; /** * Player Two */ - private final Player p2; + private final PlayerObject p2; /** * Player who made the last move. */ - private Player lastPlayer; + private PlayerObject lastPlayer; /** * The game's board. @@ -56,8 +56,8 @@ public class Game { * @param p2 Player Two */ public Game(Player p1, Player p2) { - this.p1 = p1; - this.p2 = p2; + this.p1 = new PlayerObject(p1); + this.p2 = new PlayerObject(p2); this.board = new int [GAME_COLUMNS][GAME_ROWS]; this.gameOn = false; } diff --git a/src/game/PlayerObject.java b/src/game/PlayerObject.java new file mode 100644 index 0000000..fdd280a --- /dev/null +++ b/src/game/PlayerObject.java @@ -0,0 +1,40 @@ +package player; + +/** + * Wrapper for the player. + * This is a wrapper for a player. This guarantees, that no + * player will change his Symbol, nor his id. + */ +public class PlayerObject { + + private final Player p; + private final int id; + private String sym; + + /** + * Wraps a player. + * + * @param id The players id. + * @param p The player himself. + */ + public PlayerObject(int id, Player p) { + this.p = p; + this.id = id; + } + + public void setSymbol(String sym) { + this.sym = sym; + } + + public String getSymbol() { + return this.sym; + } + + public Player getP() { + return this.p; + } + + public int getID() { + return this.id; + } +} From b96e0e27999fdf7d686dab15f008b4205c72ccb9 Mon Sep 17 00:00:00 2001 From: Malte Tammena Date: Sun, 15 Oct 2017 13:32:15 +0200 Subject: [PATCH 4/5] Created PlayerObject and implemented into game Signed-off-by: Malte Tammena --- src/game/Game.java | 29 ++++++++++++++--------------- src/game/PlayerObject.java | 4 +++- 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/src/game/Game.java b/src/game/Game.java index 85d2973..b8e389e 100644 --- a/src/game/Game.java +++ b/src/game/Game.java @@ -56,8 +56,8 @@ public class Game { * @param p2 Player Two */ public Game(Player p1, Player p2) { - this.p1 = new PlayerObject(p1); - this.p2 = new PlayerObject(p2); + this.p1 = new PlayerObject(1, p1); + this.p2 = new PlayerObject(2, p2); this.board = new int [GAME_COLUMNS][GAME_ROWS]; this.gameOn = false; } @@ -66,24 +66,22 @@ public class Game { * Starts the game. */ public void start() { + // Set random starting player. Random ran = new Random(); - Player first; - Player second; if (ran.nextBoolean()) { - first = this.p1; - second = this.p2; + lastPlayer = this.p1; } else { - first = this.p2; - second = this.p1; + lastPlayer = this.p2; } + // Start the game this.gameOn = true; while(gameOn) { - if(lastPlayer == second){ - lastPlayer = first; - makeMove(first); + if(lastPlayer == p1){ + lastPlayer = p2; + makeMove(); } else { - lastPlayer = second; - makeMove(second); + lastPlayer = p1; + makeMove(); } if(gameOn) { logGame(); @@ -99,7 +97,8 @@ public class Game { /** * Calls a players functions to make a move. */ - private void makeMove(Player p) { + private void makeMove() { + Player p = lastPlayer.getP(); log(p.getName() + " makes a move!"); // Get a choice from the player, while only giving him a copy of the game. int choice = p.move(copyBoard()); @@ -116,7 +115,7 @@ public class Game { pos++; } // Change the board accordingly. - this.board[choice][pos] = p.getNr(); + this.board[choice][pos] = lastPlayer.getID(); } /** diff --git a/src/game/PlayerObject.java b/src/game/PlayerObject.java index fdd280a..7ab6dfd 100644 --- a/src/game/PlayerObject.java +++ b/src/game/PlayerObject.java @@ -1,4 +1,6 @@ -package player; +package game; + +import player.Player; /** * Wrapper for the player. From b14c1bddbab4dfc3c4dddd11441cf024d957b4cc Mon Sep 17 00:00:00 2001 From: Malte Tammena Date: Sun, 15 Oct 2017 14:17:48 +0200 Subject: [PATCH 5/5] Added GameHistory and changed things. Signed-off-by: Malte Tammena --- Makefile | 2 ++ src/game/Game.java | 49 +++++++++++++++++++++++++-------------- src/game/GameEntry.java | 26 +++++++++++++++++++++ src/game/GameHistory.java | 31 +++++++++++++++++++++++++ 4 files changed, 91 insertions(+), 17 deletions(-) create mode 100644 src/game/GameEntry.java create mode 100644 src/game/GameHistory.java diff --git a/Makefile b/Makefile index 2b202b4..dee4017 100644 --- a/Makefile +++ b/Makefile @@ -13,6 +13,8 @@ LIBARIES= CLASSES=\ src/game/Main.java \ src/game/Game.java \ +src/game/GameHistory.java \ +src/game/GameEntry.java \ src/game/PlayerObject.java \ src/player/Player.java \ src/player/Player1.java \ diff --git a/src/game/Game.java b/src/game/Game.java index b8e389e..5b14463 100644 --- a/src/game/Game.java +++ b/src/game/Game.java @@ -30,9 +30,14 @@ public class Game { private final PlayerObject p2; /** - * Player who made the last move. + * The current player. */ - private PlayerObject lastPlayer; + private PlayerObject currentP; + + /** + * The game's history. + */ + private final GameHistory hist; /** * The game's board. @@ -58,6 +63,7 @@ public class Game { public Game(Player p1, Player p2) { this.p1 = new PlayerObject(1, p1); this.p2 = new PlayerObject(2, p2); + this.hist = new GameHistory(); this.board = new int [GAME_COLUMNS][GAME_ROWS]; this.gameOn = false; } @@ -69,20 +75,15 @@ public class Game { // Set random starting player. Random ran = new Random(); if (ran.nextBoolean()) { - lastPlayer = this.p1; + currentP = this.p1; } else { - lastPlayer = this.p2; + currentP = this.p2; } // Start the game this.gameOn = true; while(gameOn) { - if(lastPlayer == p1){ - lastPlayer = p2; - makeMove(); - } else { - lastPlayer = p1; - makeMove(); - } + makeMove(); + currentP = other(currentP); if(gameOn) { logGame(); checkState(); @@ -94,19 +95,31 @@ public class Game { } } + /** + * Returns the opponent of the given player. + * + * @param p The player. + * @return The opponent of the given player. + */ + private PlayerObject other(PlayerObject p) { + if (p1.equals(p)) { + return p2; + } else { + return p1; + } + } + /** * Calls a players functions to make a move. */ private void makeMove() { - Player p = lastPlayer.getP(); - log(p.getName() + " makes a move!"); // Get a choice from the player, while only giving him a copy of the game. - int choice = p.move(copyBoard()); + 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 by. + // If a player makes a false move, the game punishes him. this.gameOn = false; - log(p.getName() + " made an illegal move and lost!"); + log(currentP.getP().getName() + " made an illegal move and lost!"); return; } // Find the lowest empty field in the board in the choosen column. @@ -115,7 +128,9 @@ public class Game { pos++; } // Change the board accordingly. - this.board[choice][pos] = lastPlayer.getID(); + this.board[choice][pos] = currentP.getID(); + this.hist.add(new GameEntry(currentP, choice, pos)); + log(currentP.getP().getName() + " made a move!"); } /** diff --git a/src/game/GameEntry.java b/src/game/GameEntry.java new file mode 100644 index 0000000..ac40b9f --- /dev/null +++ b/src/game/GameEntry.java @@ -0,0 +1,26 @@ +package game; + +public class GameEntry { + + private final PlayerObject p; + private final int column; + private final int row; + + public GameEntry(PlayerObject p, int column, int row) { + this.p = p; + this.column = column; + this.row = row; + } + + public PlayerObject getPlayer() { + return this.p; + } + + public int getColumn() { + return this.column; + } + + public int getRow() { + return this.row; + } +} diff --git a/src/game/GameHistory.java b/src/game/GameHistory.java new file mode 100644 index 0000000..b7e1834 --- /dev/null +++ b/src/game/GameHistory.java @@ -0,0 +1,31 @@ +package game; + +import java.util.List; +import java.util.ArrayList; + +public class GameHistory { + + private List hist; + + public GameHistory() { + hist = new ArrayList<>(); + } + + public void add(GameEntry ge) { + this.hist.add(ge); + } + + public GameEntry getLast() { + if (this.hist.size() > 0) { + return this.hist.get(this.hist.size() - 1); + } + return null; + } + + public PlayerObject lastPlayer() { + if (this.hist.size() > 0) { + return getLast().getPlayer(); + } + return null; + } +}