From 770e78dc4a58ecbf9e34254da93c4343450830b9 Mon Sep 17 00:00:00 2001 From: Malte Tammena Date: Sun, 15 Oct 2017 12:49:14 +0200 Subject: [PATCH] Did some comments and minor change. Signed-off-by: Malte Tammena --- src/game/Game.java | 41 +++++++++++++++++++++++++++-------------- 1 file changed, 27 insertions(+), 14 deletions(-) diff --git a/src/game/Game.java b/src/game/Game.java index e279952..69b629a 100644 --- a/src/game/Game.java +++ b/src/game/Game.java @@ -30,9 +30,11 @@ public class Game { private final Player p2; /** - * The board on which is played. - * [GAME_COLUMNS] columns, [GAME_ROWS] rows - * " " - Empty Space + * The game's board. + * It's size is GAME_COLUMNS * GAME_ROWS. + * 0 - Empty Space + * 1 - Player One's piece + * 2 - Player Two's piece */ private int[][] board; @@ -49,7 +51,7 @@ public class Game { this.p1 = p1; this.p2 = p2; this.board = new int [GAME_COLUMNS][GAME_ROWS]; - gameOn = true; + this.gameOn = false; } /** @@ -67,6 +69,7 @@ public class Game { second = this.p1; } boolean turn = true; + this.gameOn = true; while(gameOn) { if(turn){ makeMove(first,turn); @@ -90,19 +93,21 @@ public class Game { */ private void makeMove(Player p, boolean turn) { 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()); - + // 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. this.gameOn = false; log(p.getName() + " made an illegal move and lost!"); return; } - + // Find the lowest empty field in the board in the choosen column. int pos = 0; - while (pos < (board[0].length-1) && this.board[choice][pos + 1] == 0) { + while (pos < (board[0].length - 1) && this.board[choice][pos + 1] == 0) { pos++; } - + // Change the board accordingly. if(turn){ this.board[choice][pos] = 1; } else { @@ -111,10 +116,16 @@ public class Game { } /** - * + * Checks the board for game-ending conditions. + * The game is won, if one of the players manages to get four + * of his pieces in a row. + * The game is drawn when the board is full, that is, when every + * one of the 42 pieces are played. + * The function changes variables accordingly. */ private void checkState() { // --- CHECK WIN --- + // Check rows outer:for (int i = 0; i < GAME_COLUMNS; i++) { int count1 = 0; int count2 = 0; @@ -141,6 +152,7 @@ public class Game { } } } + // Check columns outer:for (int j = 0; j < GAME_ROWS; j++) { int count1 = 0; int count2 = 0; @@ -167,6 +179,7 @@ public class Game { } } } + // Check diagonals from lower left to upper right. boolean reverse = false; int count1 = 0; int count2 = 0; @@ -218,6 +231,7 @@ public class Game { break outer; } } + // Check diagonals from lower right to upper left. reverse = false; count1 = 0; count2 = 0; @@ -271,7 +285,6 @@ public class Game { break outer; } } - // --- CHECK DRAW --- boolean draw = true; outer:for (int i = 0; i < GAME_COLUMNS; i++) { @@ -288,9 +301,9 @@ public class Game { } /** - * Deep copies the board. + * Creates a deep copy of the board. * - * @return A deep copy of the board + * @return A deep copy of the board. */ private int[][] copyBoard() { int[][] ret = new int[GAME_COLUMNS][GAME_ROWS]; @@ -310,8 +323,8 @@ public class Game { } /** - * Prints the current board to stdout. The Player that started the game gets 'X' the other one - * 'O'. + * Prints the current board to stdout. + * The Player that started the game gets 'X' the other one 'O'. */ private void logGame() { for(int i=0;i