-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChessGame.java
More file actions
259 lines (210 loc) · 7.63 KB
/
Copy pathChessGame.java
File metadata and controls
259 lines (210 loc) · 7.63 KB
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
package edu.unlv.mis768.finalproject;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.ArrayList;
import javafx.scene.layout.GridPane;
/**
* This Class controls the flow of the chess game.
* @author William Brasic and Sergio Torres
*
*/
public class ChessGame {
// initializing fields
private Player player1;
private Player player2;
private ChessBoard chessBoard;
private Player currentTurn;
private GameState gameState;
private ArrayList<Move> moves = new ArrayList<Move>();
/**
* This method initializes the game and resets the chess board
* @param player1
* @param player2
*/
void startGame(Player player1, Player player2){
// set player1 and player 1
this.player1 = player1;
this.player2 = player2;
// reset chess board
chessBoard = new ChessBoard();
// if player1 is white then they start the game
if (player1.isWhitePlayer())
this.currentTurn = player1;
// otherwise player2 will start the game
else
this.currentTurn = player2;
}
/**
* Getter for the first player name.
* @return
*/
public Player getPlayer1() {
return this.player1;
}
/**
* Getter for the second player name in the chessgame.
* @return
*/
public Player getPlayer2() {
return this.player2;
}
/**Getter for the ChessBoard array object.
* @return
*/
public ChessBoard getChessBoard() {
return this.chessBoard;
}
/*Getter for the ChessBoard current player turn.
* @return
*/
public Player getCurrentTurn() {
return this.currentTurn;
}
/**
* Getter for the state of the game
* @return
*/
public GameState getGameState(){
return this.gameState;
}
/**
* Setter for state of the game
* @param gameState
*/
public void setGameState(GameState gameState){
this.gameState = gameState;
}
/**
* Method to determine if the game is in play or not
* @return boolean value of true if the game is in play; true otherwise
*/
public boolean gameInPlay(){
return this.getGameState() == GameState.IN_PLAY;
}
/**
* Method to execute move by player_i
* @param move
* @param player
* @return flag which is a boolean value of true if move executed; false otherwise.
* @throws
*/
public boolean executeMove(Move move, Player player){
// initialize flag
boolean flag = true;
//initialize ending piece.
Piece endingPiece = null;
// get the piece that is to be moved
Piece startingPiece = move.getStartingSquare().getPiece();
// get ending square to evaluate for null value.
Square endingSquare = move.getEndingSquare();
// determine if a piece has been killed. Update value of ending piece if square not empty.
if (endingSquare != null) {
if (endingSquare.hasPiece()) {
endingPiece = move.getEndingSquare().getPiece();
endingPiece.setKilledPiece(true);
move.setKilledPiece(endingPiece);
}
}
// if there is no piece on the square return false
if (startingPiece == null) {
flag = false;
return flag;
}
// if it is NOT player_i's turn, then return false because they cannot currently move
if (player != currentTurn) {
flag = false;
return flag;
}
// if player_i is playing white (black) and player_i tries to move black (white) this is NOT valid
if (startingPiece.isWhitePiece() == player.isWhitePlayer()) {
flag = false;
return flag;
}
// if the piece tries to move in some fashion which is invalid then set flag to false
if (!startingPiece.legalMove(chessBoard, move.getStartingSquare(),
move.getEndingSquare())) {
flag = false;
return flag;
}
// // castling?
// if (startingPiece != null &&
// && startingPiece.isCastlingMove()) {
// move.setCastlingMove(true);
// }
// move desired piece from starting square to ending square
move.getEndingSquare().setPiece(move.getStartingSquare().getPiece());
// set starting square to null as the desired piece has been moved from it
move.getStartingSquare().setPiece(null);
// store move
moves.add(move);
// end game if endingPiece is a king
if (endingPiece != null && endingPiece instanceof King) {
if (player.isWhitePlayer()) {
this.setGameState(GameState.WHITE_WIN);
}
else {
this.setGameState(GameState.BLACK_WIN);
}
// create print writer object to write all the games moves to
PrintWriter movesFile = null;
try {
movesFile = new PrintWriter("Moves_of_Previous_Game.txt");
// print the issue if there is one
} catch (FileNotFoundException e) {
e.printStackTrace();
}
// loop through the moves ArrayList printing each element to the file
for (int k = 0; k < moves.size(); k ++) {
movesFile.print(k);
}
}
// set the current turn to the other player
if (this.currentTurn == player1) {
this.currentTurn = player2;
}
else {
this.currentTurn = player1;
}
// return true if move executed
return true;
}
/**
* Method to determine if player_i can make the desired move
* @param player
* @param startX
* @param startY
* @param endX
* @param endY
* @return boolean value of true if player_i can make the desired move, false otherwise
*/
public boolean playerLegalMove(Player player, int startX,
int startY, int endX, int endY){
// initialize the starting and ending squares to null
Square startingSquare = null;
Square endingSquare = null;
// try to set the starting square
try {
startingSquare = chessBoard.validSquare(startX, startY);
// if validSquare method evaluates the false for starting square, inform player_i
} catch (Exception e) {
System.out.println("ERROR: Invalid starting square");
}
// try to set ending square
try {
endingSquare = chessBoard.validSquare(endX, endY);
// if validSquare method evaluates to false for ending square, inform player_i
} catch (Exception e) {
System.out.println("ERROR: Invalid ending square");
}
//Check if both values were assigned a value.
if (startingSquare != null && endingSquare != null) {
// if the above are okay then create a move object of class Move
Move move = new Move(player, startingSquare, endingSquare);
// return true if moves is made, false otherwise.
return this.executeMove(move, player);
}
else {
return false;
}
}
}