Newer
Older
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/**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 Integer defines which character can currently draw on a tile.
*
*/
int currentPlayer;
/**Constructor of the Board class.
* Creates the Tiles and sets the player who starts the game.
*/
Board() {
for(int i = 0; i < tiles.length; i++) tiles[i] = new Tile();
currentPlayer = 1;
}
/**Checks wether the current player has won the game after placing a mark.
* When the player won the game is reset, else the current player is switched.
*/
public void victoryCheck() {
boolean victory = false;
for(int i = 0; i < 3; i++) {
if(currentPlayer == tiles[0+i*3].get_owner() && currentPlayer == tiles[1+i*3].get_owner() && currentPlayer == tiles[2+i*3].get_owner()) victory = true;
if(currentPlayer == tiles[0+i].get_owner() && currentPlayer == tiles[3+i].get_owner() && currentPlayer == tiles[6+i].get_owner()) victory = true;
}
if(currentPlayer == tiles[0].get_owner() && currentPlayer == tiles[4].get_owner() && currentPlayer == tiles[8].get_owner()) victory = true;
if(currentPlayer == tiles[2].get_owner() && currentPlayer == tiles[4].get_owner() && currentPlayer == tiles[6].get_owner()) victory = true;
if(victory) {
System.out.println("You win Player " + currentPlayer);
} else {
currentPlayer = currentPlayer == 1 ? 2 : 1;
}
}