-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReadFEN.java
More file actions
189 lines (175 loc) · 6.17 KB
/
ReadFEN.java
File metadata and controls
189 lines (175 loc) · 6.17 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
/**
* ReadFEN.java
* A program that converts a single line of FEN number into 2d format
* Homework 2
*/
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintStream;
import java.lang.StringBuilder;
public class ReadFEN {
private static final String numStr = "12345678";
private static final String pieceStr = "pnbrqkPNBRQK";
/**
* checks if length is 8
* @param boardArr FEN number made into array
* @return true if there are 8 false if there isnt
*/
private static boolean checkRows(char[] boardArr){
int lenRow = 0;
for (int i =0; i <= boardArr.length-1; i++){
if (boardArr[i] == '/'){
lenRow++;
}
}
return (lenRow == 8);
}
/**
* checks if there are no extraneous characters
* @param boardArr FEN number made into array
* @return true if there are no extraneous character false if there are
*/
private static boolean checkPiece(char[] boardArr){
for (int i = 0; i < boardArr.length; i++){
if (pieceStr.indexOf(boardArr[i]) != -1 && numStr.indexOf(boardArr[i]) != -1){
return false;
}
}
return true;
}
/**
* checks if there are no number characters together
* @param boardArr FEN number made into array
* @return true if there are no number characters together false if there are
*/
private static boolean checkConsNum(char[] boardArr){
for (int i = 0; i < boardArr.length-1; i++){
if (numStr.indexOf(boardArr[i]) > -1 && numStr.indexOf(boardArr[i+1]) > -1){
return false;
}
}
return true;
}
/**
* checks if the row contains 8squares worth of information
* @param boardArr FEN number made into array
* @return true if checkCond is 8 false if checkCond doesnt equal 8
*/
private static boolean checkValRows(String boardStr){
int infoWorthFen = 0;
int infoWorthRow = 0;
String[] checkArr = boardStr.split("/");
for (String row : checkArr){
for (int i = 0; i < row.length(); i++){
if (pieceStr.indexOf(row.toCharArray()[i]) > -1){
infoWorthRow++;
} else if (numStr.indexOf(row.toCharArray()[i]) > -1){
infoWorthRow = infoWorthRow + Integer.parseInt(String.valueOf(row.toCharArray()[i]));
}
if (infoWorthRow == 8){
infoWorthFen++;
infoWorthRow = 0;
}
}
}
return (infoWorthFen == 8);
}
/**
* runs if there are no command line arguments
* @return the FEN number entered from the user
*/
private static String takeUserInput(){
System.out.println("Enter the FEN");
Scanner scan = new Scanner(System.in);
String boardStr = scan.next();
scan.close();
return boardStr;
}
/**
* runs if there are command line arguments
* @param inputFile the file that is inputing FEN to the system
* @return the FEN number on the file
*/
private static String turnFile(String inputFile){
File fenFile = new File(inputFile);
Scanner input = null;
try {
input = new Scanner(fenFile);
} catch (FileNotFoundException e) {
System.out.println("File doesn't exist");
System.exit(0);
}
String rowFen = input.nextLine();
input.close();
return rowFen;
}
/**
* checks if the FEN number is valid
* @param boardArr array of characters from the string of the user input
* @param fenBoard string of the user input
* @return true if the FEN number is valid and false if its not
*/
private static boolean checkFen(char[] boardArr, String fenBoard){
if (checkRows(boardArr) && checkPiece(boardArr) && checkConsNum(boardArr) && checkValRows(fenBoard)){
return true;
}
return false;
}
/**
* turns the string into 2d board format
* @param fenBoard string user input
* @return the 2d board format of the string
*/
public static String turnBoard(String fenBoard){
char[] boardArr = fenBoard.toCharArray();
boolean validFen = checkFen(boardArr, fenBoard);
StringBuilder outputBoard = new StringBuilder();
if (validFen){
String[] gameBoard = fenBoard.split("/");
for (String board : gameBoard){
for (int i = 0; i < board.length(); i++){
if (numStr.indexOf(board.toCharArray()[i]) > -1){
outputBoard.append(".".repeat(Integer.parseInt(String.valueOf(board.toCharArray()[i]))));
} else{
outputBoard.append(board.toCharArray()[i]);
}
}
outputBoard.append('\n');
}
return outputBoard.toString();
}
return "invalid";
}
public static void main(String[] args){
String fenBoard ="";
if (args.length != 0){
String inputFile = args[0];
String outputFile = args[1];
fenBoard = turnFile(inputFile);
String newBoard = turnBoard(fenBoard);
if (newBoard.equals("invalid")){
System.out.println("Invalid FEN");
} else {
File fenFile = new File(outputFile);
PrintStream outBoard = null;
try {
outBoard = new PrintStream(fenFile);
outBoard.print(newBoard);
} catch (FileNotFoundException e) {
System.err.println("No file found");
System.exit(0);
}
outBoard.close();
}
} else {
fenBoard = takeUserInput();
String newBoard = turnBoard(fenBoard);
if (newBoard.equals("invalid")){
System.out.println("Invalid FEN");
} else {
System.out.println(newBoard);
}
}
}
}