-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParser.java
More file actions
152 lines (133 loc) · 3.49 KB
/
Parser.java
File metadata and controls
152 lines (133 loc) · 3.49 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
import java.awt.List;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Scanner;
public class Parser {
private TComponent mainBoard;
private ArrayList<TComponent> Tiles;
private TComponent file;
private TComponent fileMark;
private int frowN;
private int fmaxRowLen;
private boolean valid;
public Parser(){
frowN = 0;
fmaxRowLen = 0;
Tiles = new ArrayList<TComponent>();
file = new TComponent();
fileMark = new TComponent();
mainBoard = new TComponent();
valid = true;
}
public void orderTiles(){
Collections.sort(Tiles, new TCompCompare());
}
public boolean isMainSingle(){
return mainBoard.isSingle();
}
public boolean isValid(){
return valid;
}
public void parseFile(String filename) throws FileNotFoundException{
Scanner myScanner = new Scanner(new File(filename));
ArrayList senten= new ArrayList();
frowN = 0;
while(myScanner.hasNext()){
String bline = myScanner.nextLine();
senten.add(bline);
if(bline.length() > fmaxRowLen){
fmaxRowLen = bline.length();
}
frowN++;
}
for(int i = 0; i < frowN; i++){
for(int j = 0; j < fmaxRowLen; j++){
char value = senten.get(i).toString().charAt(j);
if( value !=' '){
Grid g = new Grid(i, j, value);
file.add(g);
}
}
}
Iterator iter = file.getComp().iterator();
while(iter.hasNext()){
Grid fileG = (Grid)iter.next();
Grid newG = new Grid(fileG.getRow(), fileG.getCol(), fileG.getValue());
fileMark.add(newG);
}
}
public TComponent getFile(){
return file;
}
public void setTiles(){
Iterator iter = file.getComp().iterator();
while(iter.hasNext()){
Grid fileG = (Grid)iter.next();
Grid fileMarkG = fileMark.getGrid(fileG.getRow(), fileG.getCol());
if(!fileMarkG.isMarked()){
TComponent cpt = new TComponent();
cpt.add(fileG);
probe(cpt, fileG);
Tiles.add(cpt);
fileMarkG.mark();
}
}
orderTiles();
}
public ArrayList<TComponent> getTiles(){
return Tiles;
}
//checked
public void probe(TComponent cp, Grid g){
int r = g.getRow();
int c = g.getCol();
if(file.getGrid(r-1, c).getValue()!=' ' && !(fileMark.getGrid(r-1, c).isMarked())){
cp.add(file.getGrid(r-1, c));
fileMark.getGrid(r-1, c).mark();
probe(cp, file.getGrid(r-1, c));
}
if(file.getGrid(r, c-1).getValue()!=' ' && !(fileMark.getGrid(r, c-1).isMarked())){
cp.add(file.getGrid(r, c-1));
fileMark.getGrid(r, c-1).mark();
probe(cp, file.getGrid(r, c-1));
}
if(file.getGrid(r+1, c).getValue()!=' ' && !(fileMark.getGrid(r+1, c).isMarked())){
cp.add(file.getGrid(r+1, c));
fileMark.getGrid(r+1, c).mark();
probe(cp, file.getGrid(r+1, c));
}
if(file.getGrid(r, c+1).getValue()!=' ' && !(fileMark.getGrid(r, c+1).isMarked())){
cp.add(file.getGrid(r, c+1));
fileMark.getGrid(r, c+1).mark();
probe(cp, file.getGrid(r, c+1));
}
}
//Get the main board out of all Tiles and remove it from the set of Tiles
public void setMainBoard(){
int ind = 0;
int totalGrid = 0;
for(int i=0; i< Tiles.size(); i++){
totalGrid += Tiles.get(i).size();
if(Tiles.get(i).size()>=mainBoard.size()){
mainBoard = Tiles.get(i);
ind = i;
}
}
if( (2*mainBoard.size())!= totalGrid){
valid = false;
}
Tiles.remove(ind);
}
public TComponent getMainBoard(){
return mainBoard;
}
public void updateTiles() {
for(int i = 0; i < Tiles.size(); i++){
Tiles.get(i).update();
}
}
}