-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGame.java
executable file
·55 lines (38 loc) · 1 KB
/
Game.java
1
2
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package test;
public class Game {
private Player player1;
private Player player2;
private Player currentPlayer;
private Board board = new Board();
public Game(Player player1, Player player2) {
this.player1 = player1;
this.player2 = player2;
}
public void playGame() {
currentPlayer = player1;
while (!board.boardIsFull() && board.winner().equals(State.EMPTY)) {
currentPlayer.doMove(board);
switchPlayer();
}
if (board.boardIsFull() || !board.winner().equals(State.EMPTY)) {
finishGame();
}
}
private void switchPlayer() {
if (currentPlayer == player1) {
currentPlayer = player2;
} else {
currentPlayer = player1;
}
}
private void finishGame() {
State winner = board.winner();
if (winner == State.X) {
System.out.println("Congratulations player X, you've won!");
} else if (winner == State.O) {
System.out.println("Congratulations, player O, you've won!");
} else {
System.out.println("Well played guys, it's a draw! Play again!");
}
}
}