This repository has been archived by the owner on Aug 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FileOperator.java
262 lines (233 loc) · 7.46 KB
/
FileOperator.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
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
260
261
262
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
import java.lang.StringIndexOutOfBoundsException;
import java.io.PrintWriter;
class FileOperator {
private static char TOM_RUTE_TEGN = '.';
private static int rowsInBox = 0;
private static int columnsInBox = 0;
// public static void main(String[] args) throws FileNotFoundException, IncorrectCharacterInFile, IllegalValueIntervalException, IncorrectAmountOfSymbols, TheBoardIsToLargeException{
// Brett board = lesFil(args[0]);
// board.opprettDatastruktur();
// board.solve();
// Rute[][] doubleArrayBoard = board.getBoard();
// //printOut(doubleArrayBoard);
// System.out.println("Sudoku size: " + board.getSudoku().hentAntallLosninger());
// PrintWriter out = new PrintWriter(new File(args[1]));
// while (SudokuBeholder.counter != board.getSudoku().hentAntallLosninger()) {
// // printOut(board.getSudoku().taUt().getASolution2D());
// out.println((SudokuBeholder.counter + 1) + ": " + board.getSudoku().taUt().getSolutionForOutput());
// }
// out.close();
// // Test loop to print out solutions in terminal.
// // while (SudokuBeholder.counter != board.getSudoku().hentAntallLosninger()) {
// // printOut(board.getSudoku().taUt().getASolution2D());
// // System.out.println(board.getSudoku().taUt().getSolutionForOutput());
// // }
// }
/**
* Reads information about the board from a file.
*
* @param String Name of the file with the info about the board.
*/
public static Brett lesFil(File file) throws FileNotFoundException, IncorrectCharacterInFile, IllegalValueIntervalException, IncorrectAmountOfSymbols, TheBoardIsToLargeException{
Scanner read = new Scanner(file);
rowsInBox = Integer.parseInt(read.nextLine());
columnsInBox = Integer.parseInt(read.nextLine());
if (rowsInBox > 8 || columnsInBox > 8) {
throw new TheBoardIsToLargeException();
}
int LENGTH = rowsInBox * columnsInBox;
Brett board = new Brett(rowsInBox, columnsInBox);
int i = 0;
while(i < LENGTH) {
String row = read.nextLine();
Rute[] cellArray = new Rute[LENGTH];
if (row.length() > LENGTH) throw new IncorrectAmountOfSymbols();
for (int j = 0; j < LENGTH; j++) {
cellArray[j] = new Rute(tegnTilVerdi(row.charAt(j)), board);
if (cellArray[j].getNr() < 0 || cellArray[j].getNr() > LENGTH) {
throw new IllegalValueIntervalException();
}
}
board.fillIn(cellArray, i);
i++;
}
return board;
}
/**
* Prints out the board on the screen.
*
* @param Rute[][] Two-dimensional array of cells.
*/
public static void printOut(Rute[][] board) {
int tmpBox = 0;
try{
for (int i = 0; i < board.length; i++) {
int c1;
int c2 = 0;
if (i%rowsInBox == 0) {
c1 = i;
tmpBox = i;
if (i != 0) {
int a = 0;
int b = 0;
System.out.println();
while (b <= rowsInBox - 1) {
while (a < columnsInBox) {
System.out.print("-");
a++;
}
b++;
if (b <= rowsInBox - 1) System.out.print("+");
a = 0;
}
}
}else{
c1 = tmpBox;
}
System.out.println();
for (int j = 0; j < board[i].length; j++) {
if (c2 < columnsInBox) {
System.out.print(verdiTilTegn(board[i][j].getNr(), ' '));
c2++;
} else if (c2 == columnsInBox) {
c2 = 0;
System.out.print("|");
c1++;
System.out.print(verdiTilTegn(board[i][j].getNr(), ' '));
c2++;
}
}
}
System.out.println();
}catch (UgyldigVerdiUnntak e) {
System.out.println("Error! Incorrect char!");
}
}
/**
* Prints out the board on the screen.
*
* @param Rute[][] Two-dimensional array of cell-values.
*/
public static void printOut(int[][] board) {
int tmpBox = 0;
try{
for (int i = 0; i < board.length; i++) {
int c1;
int c2 = 0;
if (i%rowsInBox == 0) {
c1 = i;
tmpBox = i;
if (i != 0) {
int a = 0;
int b = 0;
System.out.println();
while (b <= rowsInBox - 1) {
while (a < columnsInBox) {
System.out.print("-");
a++;
}
b++;
if (b <= rowsInBox - 1) System.out.print("+");
a = 0;
}
}
}else{
c1 = tmpBox;
}
System.out.println();
for (int j = 0; j < board[i].length; j++) {
if (c2 < columnsInBox) {
System.out.print(verdiTilTegn(board[i][j], ' '));
c2++;
} else if (c2 == columnsInBox) {
c2 = 0;
System.out.print("|");
c1++;
System.out.print(verdiTilTegn(board[i][j], ' '));
c2++;
}
}
}
System.out.println();
}catch (UgyldigVerdiUnntak e) {
System.out.println("Error! Incorrect char!");
}
}
/**
* Prints all the solutions to a given file.
*
* @param String File name.
*/
public void saveToFile(String filename) throws FileNotFoundException{
}
/**
* Help method to get the int parameter of the board, ex: rows, columns, cells in a box,
* based on the provided parameter to the method.
* @param String "row", "col"
* @return int rowsInBox, columnsInBox.
*/
public int getParameter(String key) {
if (key.equals("row")) {
return rowsInBox;
} else if (key.equals("col")) {
return columnsInBox;
} else {
return -1;
}
}
// imported from http://heim.ifi.uio.no/inf1010/2016/oblig/8/filer/2hjelpemetoder.java
/**
* Oversetter et tegn (char) til en tallverdi (int)
*
* @param tegn tegnet som skal oversettes
* @return verdien som tegnet tilsvarer
*/
public static int tegnTilVerdi(char tegn) throws IncorrectCharacterInFile {
if (tegn == TOM_RUTE_TEGN) { // tom rute, DENNE KONSTANTEN MAA DEKLARERES
return 0;
} else if ('1' <= tegn && tegn <= '9') { // tegn er i [1, 9]
return tegn - '0';
} else if ('A' <= tegn && tegn <= 'Z') { // tegn er i [A, Z]
return tegn - 'A' + 10;
} else if (tegn == '@') { // tegn er @
return 36;
} else if (tegn == '#') { // tegn er #
return 37;
} else if (tegn == '&') { // tegn er &
return 38;
} else if ('a' <= tegn && tegn <= 'z') { // tegn er i [a, z]
return tegn - 'a' + 39;
} else { // tegn er ugyldig
throw new IncorrectCharacterInFile(tegn);
}
}
/**
* Oversetter en tallverdi (int) til et tegn (char)
*
* @param verdi verdien som skal oversettes
* @param tom tegnet som brukes for aa representere 0 (tom rute)
* @return tegnet som verdien tilsvarer
*/
public static char verdiTilTegn(int verdi, char tom) throws UgyldigVerdiUnntak {
if (verdi == 0) { // tom
return tom;
} else if (1 <= verdi && verdi <= 9) { // tegn er i [1, 9]
return (char) (verdi + '0');
} else if (10 <= verdi && verdi <= 35) { // tegn er i [A, Z]
return (char) (verdi + 'A' - 10);
} else if (verdi == 36) { // tegn er @
return '@';
} else if (verdi == 37) { // tegn er #
return '#';
} else if (verdi == 38) { // tegn er &
return '&';
} else if (39 <= verdi && verdi <= 64) { // tegn er i [a, z]
return (char) (verdi + 'a' - 39);
} else { // tegn er ugyldig
throw new UgyldigVerdiUnntak(verdi); // HUSK DEFINISJON AV UNNTAKSKLASSE
}
}
}