-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChessBoard.java
More file actions
157 lines (115 loc) · 5.08 KB
/
ChessBoard.java
File metadata and controls
157 lines (115 loc) · 5.08 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
//------------------------------------------------------------------------------------------------------------
// Keerthi Krishnan, kvkrishn
// HW2
// A Program that uses a linked list in order to place pieces on a chessboard, find a specific piece, and tell whether
// another piece is in the attacking range of this piece.
//------------------------------------------------------------------------------------------------------------
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 ChessBoard {
// Public variables used for location, pieces, rows, columns, and types
public int row;
public int col;
public String []area;
public String []objects;
public char type = '1';
//LinkedList and ChessPiece object created
public LinkedList ChessBoard1;
private ChessPiece object;
// this method reads the input file, traverses the linked list, applying any function necessary and prints the solution to the output file.
private void IOReaderandWriter(String[] args) throws FileNotFoundException, IOException{
int row1 = 0;
int col1 = 0;
// goes to open files
// takes in input file as first argument, takes in output file as second agument, uses BufferedReader to go through input file
FileReader input = new FileReader(args[0]);
PrintWriter output = new PrintWriter(new FileWriter(args[1]));
BufferedReader reader = new BufferedReader(input);
String line = "";
// reads lines from the input file, uses colons and whitespace as delimiter for the input and extracts the data
while((line = reader.readLine()) != null ) {
if(line == null) {
break;
}
String[] inputScan = line.split(":");
// gets the information about the row and column
area = inputScan[0].split(" ");
row = Integer.parseInt(area[0]);
col = Integer.parseInt(area[1]);
// linked list object made
ChessBoard1 = new LinkedList();
// gets rid of whitespace in the input file
objects = inputScan[1].split(" ");
String c = "";
// iterates through object array and sets each argument as the type, row, and column
for(int i=1;i<objects.length;i=i+3){
c = objects[i];
type = c.charAt(0);
row1 = Integer.parseInt(objects[i+1]);
col1 = Integer.parseInt(objects[i+2]);
// calls the addPiece method from the linked list and uses method pieceInit in order to enter in the types of pieces into the linked list
ChessBoard1.addPiece(this.piecesInit(type, row1, col1));
}
// checks for validity aka whether any 2 pieces are on the chessboard. If it is, it continues, otherwise it prints invalid.
if(ChessBoard1.checkPiece()) {
object = ChessBoard1.findPiece(row,col); // Given the row and column, find the piece at the location.
// if there is a piece there, print out the piece
if(object != null) {
output.print(object);
// if this piece is in the attacking range of another piece, print out y which stands for yes. Otherwise print n for no.
if(ChessBoard1.attacking(object)) {
output.print(" y ");
} else {
output.print(" n ");
}
} else {
output.print("-");
}
} else {
output.print("Invalid");
}
output.println();
}
// close the input file, and the output file
input.close();
output.close();
}
// adds in chesspieces through switch cases. Basically identifies type of piece and creates a new object from their respective classes.
public ChessPiece piecesInit(char type, int row, int col) {
switch(type){
case 'q':
case 'Q':
return new Queen(row, col, type);
case 'k':
case 'K':
return new King(row, col, type);
case 'n':
case 'N':
return new Knight(row, col, type);
case 'r':
case 'R':
return new Rook(row, col, type);
case 'b':
case 'B':
return new Bishop(row, col, type);
default:
return null;
}
}
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 ChessBoard.jar <input file> <output file>");
System.exit(1);
}
// create a new ChessBoard object
ChessBoard chess = new ChessBoard();
// apply the input file reader and output file writer function to ChessBoard object
chess.IOReaderandWriter(args);
}
}