-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayer.java
59 lines (49 loc) · 1.67 KB
/
Player.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
package TicTacToe;
import java.util.Random;
import TicTacToe.Square.Piece;
/**
* This abstract class is for a generic player (used by the ComputerPlayer and HumanPlayer classes).
* This inheritance allows them to be held in homogeneous data structures (e.g. arrays of type
* Player). This class also factors out some common code.
*/
public abstract class Player {
private Piece _piece;
/**
* The constructor chooses a random piece for the player.
*/
public Player() {
_piece = Piece.values()[(new Random()).nextInt(2)];
}
/**
* This constructor is called when the player is the second to be created. The first player
* passes in their piece, and this constructor chooses the opposite to it. This is so that both
* players don't randomly choose the same piece.
*/
public Player(Piece piece) {
_piece = TicTacToe.flipPiece(piece);
}
/**
* This method places a piece on the real board.
*/
public void move(TicTacToe game, int boardSize, int x, int y) {
game.getBoard().setPreviousMove(new int[] { x, y });
game.getBoard().getBoardArray()[x][y].placePiece(_piece, boardSize, false);
// First move flag is used so the empty board isn't highlighted – that would confuse the
// user.
if (game.getBoard().getIsFirstMove()) {
game.getBoard().setIsFirstMove(false);
}
}
/**
* This method gets the player's piece.
*/
public Piece getPiece() {
return _piece;
}
// This method is abstract because computer players don't have names and will always return
// "computer".
public abstract String getName();
// This method is abstract because the computer and the human player will implement it
// differently.
public abstract boolean isComputer();
}