-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReadFromFile.java
79 lines (56 loc) · 1.91 KB
/
ReadFromFile.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
import java.io.*;
public class ReadFromFile {
public int dimension = 0;
public int height = 0;
public int width = 0;
private Board board= null;
public Board readFromFile(File file, boolean showGUI){
try{
BufferedReader br = new BufferedReader(new FileReader(file));
dimension = Integer.parseInt(br.readLine());
height= Integer.parseInt(br.readLine());
width = Integer.parseInt(br.readLine());
board = new Board(dimension, height, width, showGUI);
for(int i = 0; i < dimension; i++){
String line = br.readLine();
for(int j = 0; j < line.length(); j++){
if(line.charAt(j) != '.'){
board.getTable()[i][j] = new FilledSquare(toInt(line.charAt(j)), board.getRows()[i], board.getCols()[j], board.getBoxes()[i/height][j/width]);
}
else{
board.getTable()[i][j] = new EmptySquare(0, board.getRows()[i], board.getCols()[j], board.getBoxes()[i/height][j/width]);
}
}
}
for(int i = 0; i < dimension; i++){
for(int j = 0; j < dimension; j++){
this.board.getTable()[i][j].setBoard(board);
board.getRows()[i].getSquare()[j] = board.getTable()[i][j];
board.getCols()[i].getSquare()[j] = board.getTable()[j][i];
board.getBoxes()[i/height][j/width].addToStructure(board.getTable()[i][j]);
}
}
for(int i = 0; i < dimension; i++){
for(int j = 0; j < dimension; j++){
if(board.getTable()[i][j].getValue() != 0){
board.getTable()[i][j].assignFinalNumber(board.getTable()[i][j].getValue());
}
}
}
br.close();
}
catch(Exception e){
e.printStackTrace();
}
return board;
}
private static int toInt(char c) {
if (c < '0' || (c > 'Z' && c < 'a') || c > 'z'){
return 0;
}
if (c >= '0' && c <= '9'){
return c - '0';
}
return (c - 'A') % 32 + 10;
}
}