-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGame.java
69 lines (59 loc) · 1.42 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import java.awt.Color;
/**
* Represents a model for managing the state
* of the chess board and viewer.
*/
public class Game extends Observable {
/** Represents the chess board. */
private final Board board;
/** Represents a player. */
private final Player player1 = new Player(Color.WHITE);
/** Represents another player. */
private final Player player2 = new Player(Color.BLACK);
/** Represents the current turn. */
private Player turn = player1;
/** Initializes the game. */
public Game() {
board = new TwoDBoard();
board.setup();
}
/** Overridden constructor initializes the game. */
public Game(int numberOfBoards) {
board = new TwoDBoard(numberOfBoards);
board.setup();
}
/**
* Gets the chess board.
*
* @return board currently in use
*/
public Board getBoard() {
return board;
}
/**
* Initializes the controller.
*
* @return turn of current player
*/
public Player getTurn() {
return turn;
}
/**
* Changes turns to the next player.
*/
private void switchTurn() {
if (turn.equals(player1)) turn = player2;
else turn = player1;
}
/**
* Handles piece movement.
*/
public void makeMove(Position start, Position end) {
// Check player moving own piece
if (board.movePiece(start, end)) {
switchTurn(); // Switch turns
this.pingObservers(); // Notify viewer
}
System.out.printf("Current turn: %s\n", (turn == player1) ? "WHITE" : "BLACK");
}
}