-
Notifications
You must be signed in to change notification settings - Fork 0
/
Player.java
185 lines (161 loc) · 5.91 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import java.awt.Graphics2D;
import java.awt.Point;
import java.util.ArrayList;
public abstract class Player {
private final Board board; //playing board
private final int startPos; // 0 for starting on top, 1 for starting on bottom
private final int team; // 0 for white-team, or 1 for black-team
private Player opponent; //the opposing player
private ArrayList<Piece> alives; //currently alive pieces
private ArrayList<Piece> deads; //currently dead pieces
private boolean moveWasMade;
public Player(Board aBoard, int aStartPos, int aTeam) { //player constructor
board = aBoard;
startPos = aStartPos;
team = aTeam;
}
public void executeMove(Piece piece, Point point) {
executeMove(piece, point, null);
}
//TODO: update readme to include 3rd param option
public void executeMove(Piece piece, Point point, String promoteTo) { //moves a piece and kills the opposition
moveWasMade = true;
if(piece instanceof King && ((King) piece).validCastles() != null) { //detect if castling
for(Point p : ((King) piece).validCastles()) {
if(p.equals(point)) { //script for castling TODO: fix for if there is no castle at that side???
if(point.x < (board.getNumTiles() / 2)) { //castling with left piece
pieceAt(new Point(0, point.y))
.setPosition(new Point(point.x + 1, point.y));
} else { //castling with right piece
pieceAt(new Point(board.getNumTiles() - 1, point.y))
.setPosition(new Point(point.x - 1, point.y));
}
}
}
}
piece.setPosition(point); //move the piece
if(piece instanceof Pawn && point.y == ((startPos == 0)? (board.getNumTiles() - 1) : 0)) {
//TODO: it might be worth making cases pieces Pieces rather than Strings
piece.setIsAlive(false); //remove the old piece
alives.remove(piece);
Piece newPiece;
switch (promoteTo) {
case "Rook":
newPiece = new Rook(board, this, point.x, point.y);
break;
case "Knight":
newPiece = new Knight(board, this, point.x, point.y);
break;
case "Bishop":
newPiece = new Bishop(board, this, point.x, point.y);
break;
case "Queen":
newPiece = new Queen(board, this, point.x, point.y);
break;
default:
newPiece = piece;
newPiece.setIsAlive(true);
break;
}
alives.add(newPiece);
newPiece.setImg();
newPiece.resize();
}
piece = getOpponent().pieceAt(point); //takes any opponent piece
if(piece != null) {
piece.setIsAlive(false);
getOpponent().getAlives().remove(piece);
getOpponent().getDeads().add(piece);
}
}
public void draw(Graphics2D g2) {
for(Piece p : alives) {
p.draw(g2);
}
for(Piece p : deads) {
p.draw(g2);
}
}
public void resize() {
for(Piece p : alives) {
p.resize();
}
for(Piece p : deads) {
p.resize();
}
}
public void createPieces() {
alives = new ArrayList<Piece>();
deads = new ArrayList<Piece>();
for(int i=0; i<board.getNumTiles(); i++) { //add the pawns
alives.add(new Pawn(board, this, i, (startPos * (board.getNumTiles() - 3) + 1)));
}
//add the powerful pieces
int i=0;
alives.add(new Rook(board, this, i++, (startPos * (board.getNumTiles() - 1))));
alives.add(new Knight(board, this, i++, (startPos * (board.getNumTiles() - 1))));
alives.add(new Bishop(board, this, i++, (startPos * (board.getNumTiles() - 1))));
alives.add(new Queen(board, this, i++, (startPos * (board.getNumTiles() - 1))));
alives.add(new King(board, this, i++, (startPos * (board.getNumTiles() - 1))));
alives.add(new Bishop(board, this, i++, (startPos * (board.getNumTiles() - 1))));
alives.add(new Knight(board, this, i++, (startPos * (board.getNumTiles() - 1))));
alives.add(new Rook(board, this, i++, (startPos * (board.getNumTiles() - 1))));
for(Piece p : alives) { //initalize piece images
p.setImg();
}
}
public Piece pieceAt(Point point) { //returns the piece at a given tile
if(point == null) return null;
for(Piece p : alives) {
if(point.equals(p.getTile())) {
return p;
}
}
return null;
}
public Point tileAt(Point p) { //returns the tile which the coordinate is upon
if(p == null || p.x < board.getPos().x || p.y < board.getPos().y ||
p.x > (board.getPos().x + board.getWidth()) || p.y > (board.getPos().y + board.getHeight())) {
return new Point(-1, -1);
}
return new Point(((int)(p.x - board.getPos().x) / (int) board.getTileWidth()),
((int)(p.y - board.getPos().y) / (int) board.getTileHeight()));
}
public boolean onBoard(Point p) { //returns if a tile is upon the board
if(p.x < 0 || p.y < 0 || p.x >= board.getNumTiles() || p.y >= board.getNumTiles()) return false;
return true;
}
//setter methods
public void setOpponent(Player aOpponent) {
opponent = aOpponent;
}
public void setMoveWasMade(boolean setTo) {
moveWasMade = setTo;
}
public void setAlives(ArrayList<Piece> aAlives) {
alives = aAlives;
}
//getter methods
public int getTeam() {
return team;
}
public int getStartPos() {
return startPos;
}
public Board getBoard() {
return board;
}
public ArrayList<Piece> getAlives() {
return alives;
}
public ArrayList<Piece> getDeads() {
return deads;
}
public Player getOpponent() {
return opponent;
}
public boolean getMoveWasMade() {
return moveWasMade;
}
public abstract void makeMove() throws InterruptedException; //players have to have makeMove()
}