-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChessMoves.java
More file actions
254 lines (190 loc) · 8.25 KB
/
ChessMoves.java
File metadata and controls
254 lines (190 loc) · 8.25 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
/*-----------------------------------------------------------------------------------------------------------
Keerthi Krishnan
kvkrishn
File: ChessMoves.java
This program performs the basic task of finding a piece and validating moves as well as checking if a repsective king is in check
------------------------------------------------------------------------------------------------------------- */
import java.util.Scanner;
import java.lang.*;
import java.io.*;
import java.util.*;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.File;
public class ChessMoves {
// row, column, 3 string arrays, and 2 int arrays created as well as a LinkedList and ChessPiece object created
public int row;
public int col;
public String []ChessBoard; //stores all locations
public String []coordinates; //stores all the moves
// public String []moves;
private int startingPoint[]; // created to store the given move coordinates
private int endingPoint[]; // stores the end coordinates to where the chesspieces wanna move
public char type = '1'; //states type of chess piece
// public int count = 0;
//creates objects of LinkedListChess and ChessPiece class
public LinkedList Chesspieces;
private void readInputFile(String[] args) throws FileNotFoundException, IOException{
// row and col for each chess piece location
int row = 0;
int col = 0;
// open files
FileReader file = new FileReader(args[0]);
PrintWriter out = new PrintWriter(new FileWriter(args[1]));
BufferedReader reader = new BufferedReader(file);
String line = "";
// read lines from in, extract and print tokens from each line
while((line = reader.readLine()) != null ) {
if(line == null) {
break;
}
String[] scan = line.split(":");
//locations into scan array
ChessBoard = scan[0].split(" ");
Chesspieces = new LinkedList();
//chess pieces into scan array
String piece = "";
for(int j=0;j<ChessBoard.length;j=j+3){
piece = ChessBoard[j];
type = piece.charAt(0);
row = Integer.parseInt(ChessBoard[j+1]);
col = Integer.parseInt(ChessBoard[j+2]);
Chesspieces.addPiece(this.createPiece(type, row, col)); //adds all pieces in chess board
}
coordinates = scan[1].split(" ");
canMove(Chesspieces, out);
}
file.close();
out.close();
}
/* This method basically checks whether a move is valid or not based on multiple conditions such as the color, whether it is in the attacking range, etc. */
private void canMove(LinkedList chess, PrintWriter output) {
boolean isLegal = false;
boolean whiteCheck = true;
for(int i=1;i<coordinates.length;i+=4) {
isLegal = false;
startingPoint = new int[2];
endingPoint = new int[2];
startingPoint[0] = Integer.parseInt(coordinates[i]); // stores column of starting coordinates
startingPoint[1] = Integer.parseInt(coordinates[i+1]); // stores rows of starting coordinates
endingPoint[0] = Integer.parseInt(coordinates[i+2]); // stores destination column of move
endingPoint[1] = Integer.parseInt(coordinates[i+3]); // stores destination row of move
Node startingNode = chess.findPiece(startingPoint[0], startingPoint[1]); // finds if piece exists and stores it in node
Node endingNode = chess.findPiece(endingPoint[0], endingPoint[1]); // finds if piece exists and stores it in a node
if(startingNode == null){
isLegal = false;
output.println(startingPoint[0] + " " + startingPoint[1] + " " + endingPoint[0] + " " + endingPoint[1] + " illegal");
break;
}
if(chess.CheckKing(!whiteCheck)){
isLegal = false;
output.println(startingPoint[0] + " " + startingPoint[1] + " " + endingPoint[0] + " " + endingPoint[1] + " illegal");
break;
} else {
isLegal = true;
}
// if the starting node is null, then a piece does not exist and it is illegal
ChessPiece pieceStart = startingNode.getData();
boolean whiteColor = pieceStart.getColor() == 'w' ? true : false;
if(whiteColor != whiteCheck){
isLegal = false;
output.println(startingPoint[0] + " " + startingPoint[1] + " " + endingPoint[0] + " " + endingPoint[1] + " illegal");
break;
}
if(pieceStart.isValidMove(startingPoint, endingPoint) == false) {
isLegal = false;
output.println(startingPoint[0] + " " + startingPoint[1] + " " + endingPoint[0] + " " + endingPoint[1] + " illegal");
break;
}
// if the color of the piece is black and it is the first move then it is illegal
if(pieceStart.getColor() == 'b' && i == 1){
isLegal = false;
output.println(startingPoint[0] + " " + startingPoint[1] + " " + endingPoint[0] + " " + endingPoint[1] + " illegal");
break;
}
// if the destination node is null, then check the move and set the new location at the destination node
if(endingNode == null){
if(chess.checkMove(startingPoint, endingPoint, startingNode)){
pieceStart.setLocation(endingPoint);
isLegal = true;
} else {
// otherwise, print illegal
isLegal = false;
output.println(startingPoint[0] + " " + startingPoint[1] + " " + endingPoint[0] + " " + endingPoint[1] + " illegal");
break;
}
} else {
ChessPiece piecesEnd = endingNode.getData(); // stores the data that is in the ending node
// if the color of the starting piece is equal to the color of the ending piece, then print illegal
if(pieceStart.getColor() == piecesEnd.getColor()) {
isLegal = false;
output.println(startingPoint[0] + " " + startingPoint[1] + " " + endingPoint[0] + " " + endingPoint[1] + " illegal");
break;
} else {
// if it is not the same color, then check the move and if check move returns true, then set isLegal to true
if(chess.checkMove(startingPoint, endingPoint, startingNode)){
isLegal = true;
} else {
// else print illegal
isLegal = false;
output.println(startingPoint[0] + " " + startingPoint[1] + " " + endingPoint[0] + " " + endingPoint[1] + " illegal");
break;
}
// if the piece is able to attack the end piece, then delete that node and set the location of the starting piece to the end piece
if(pieceStart.isAttacking(piecesEnd)) {
chess.deletePiece(endingNode);
pieceStart.setLocation(piecesEnd.getLocation());
Node p1 = Chesspieces.findPiece(piecesEnd.getRow(), piecesEnd.getCol());
}
}
}
whiteCheck = !whiteCheck;
if(chess.CheckKing(!whiteCheck)){
isLegal = false;
output.println(startingPoint[0] + " " + startingPoint[1] + " " + endingPoint[0] + " " + endingPoint[1] + " illegal");
break;
} else {
isLegal = true;
}
// whiteCheck = !whiteCheck;
}
// if isLegal is true, then print legal wherever it is true
if(isLegal == true)
output.println("legal");
return;
}
/* creates pieces in linked list based on type */
public ChessPiece createPiece(char type, int row, int col) {
switch(type){
case 'k':
case 'K':
return new King(row, col, type);
case 'q':
case 'Q':
return new Queen(row, col, type);
case 'r':
case 'R':
return new Rook(row, col, type);
case 'b':
case 'B':
return new Bishop(row, col, type);
case 'n':
case 'N':
return new Knight(row, col, type);
default:
return null;
}
}
//main method
public static void main(String[] args) throws IOException {
// check number of command line arguments is at least 2
if(args.length < 2){
System.out.println("Usage: java –jar ChessMoves.jar <input file> <output file>");
System.exit(1);
}
//creates new object ChessBoard
ChessMoves chess = new ChessMoves();
//calls input file reader method
chess.readInputFile(args);
}
}