/** * This class is an Object that defines the logical enviroment of the application. */ public class Board extends Thread { /** * This Array contains the Tiles on which the players draw. * The tiles are aranged two dimensionally in this form: * 0 1 2 * 3 4 5 * 6 7 8 */ Tile tiles[] = new Tile[9]; /** * This Array contains the Players. */ Player players[] = new Player[2]; /** * This Integer defines which character gets to draw first. */ int firstPlayer; /** * This Integer defines which character can currently draw on a tile. */ int currentPlayer; /** * This Integer tracts the amount of marks that were already placed. */ int marks; /** * Constructor of the Board class. * Creates a board with new players and a set turn order. */ Board() { for(int i = 0; i < players.length; i++) players[i] = new Player(i+1); firstPlayer = 0; initBoard(); } /** * Constructor of the Board class. * Creates a board with set players and an inverted turn order. * * @param players the players of the previous game. * @param firstPlayer the first turn assignment of the previous game. */ Board(Player[] players, int firstPlayer) { this.players = players; this.firstPlayer = firstPlayer == 1 ? 0 : 1; initBoard(); } /** * Creats the tiles and assigns the first turn. */ private void initBoard() { for(int i = 0; i < tiles.length; i++) tiles[i] = new Tile(); currentPlayer = firstPlayer; marks = 0; } /** * Ends the turn by increasing the mark counter and changing the player. * Defines an end state and returns it. * end states: * 0 = no draw, no winner * 1 = current player won * 2 = draw * * @return end state of the turn. */ public int turnEnd() { marks++; int endState = 0; if(marks > 4 && victoryCheck()) { //System.out.println("You win Player " + currentPlayer); players[currentPlayer].playerScoreInc(); endState = 1; } else if(marks == 9) { //System.out.println("A draw"); endState = 2; } else { currentPlayer = currentPlayer == 1 ? 0 : 1; } return endState; } /** * Checks whether the current player has won the game. * * @return current player won */ public boolean victoryCheck() { boolean victory = false; for(int i = 0; i < 3; i++) { if(currentPlayer == tiles[0+i*3].getOwner() && currentPlayer == tiles[1+i*3].getOwner() && currentPlayer == tiles[2+i*3].getOwner()) victory = true; if(currentPlayer == tiles[0+i].getOwner() && currentPlayer == tiles[3+i].getOwner() && currentPlayer == tiles[6+i].getOwner()) victory = true; } if(currentPlayer == tiles[0].getOwner() && currentPlayer == tiles[4].getOwner() && currentPlayer == tiles[8].getOwner()) victory = true; if(currentPlayer == tiles[2].getOwner() && currentPlayer == tiles[4].getOwner() && currentPlayer == tiles[6].getOwner()) victory = true; return victory; } }