-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGuess.java
More file actions
172 lines (141 loc) · 3.93 KB
/
Copy pathGuess.java
File metadata and controls
172 lines (141 loc) · 3.93 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
public class Guess {
private Board board;
//private ListStack l = new ListStack();
public Guess(Board b) {
// TODO Auto-generated constructor stub
this.board = b;
}
public Board execute() throws ConflictException {
// TODO Auto-generated method stub
Board b = board;
if(!checkFull(b)) {
int p = -1;
p = findNextPebble(p, b);
b = recursion(p, board); //start recursion from left-up most point on the board
}
return b;
}
private int findNextPebble(int position, Board b) {
int i = position;
try{
i = position + 1;
for(; b.getColor(i) == Board.NOTHING; i++)
;
}
catch(Exception ex){
//System.out.println("Reach limit");
return -1;
}
return i;
}
private synchronized Board recursion(int p, Board temp) throws ConflictException {
// TODO Auto-generated method stub
/*try {
Thread.sleep(200);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}*/
Board b = temp.clone();
FillBoard fb1 = new FillBoard(b.clone());
b = fb1.execute();
Coordinates c = new Coordinates(p,b.getSize());
int color = b.getColor(p);
try{
b = tryPosition(c.getLeft(), b.clone(), color);
b = tryPosition(c.getUp(), b.clone(), color);
b = tryPosition(c.getRight(), b.clone(), color);
b = tryPosition(c.getDown(), b.clone(), color);
}
catch(ConflictException ex){
System.out.println("conflicting");
throw new ConflictException("ex");
}
//b.printBoard();
FillBoard fb2 = new FillBoard(b.clone());
b = fb2.execute();
//System.out.println("FB finished");
int next = this.findNextPebble(p, temp);
if(next == -1)
next = findNextPebble(-1, b);
//System.out.println("Next pebble is " + next);
if(!checkFull(b)){
b = recursion(next, b.clone());
}
Board result = b.clone();
//System.out.println("Totally finished");
return result;
}
private synchronized Board tryPosition(Coordinates c, Board b,int color) throws ConflictException {
Board temp = b.clone();
if(c.inRange())
if(b.getColor(c.toPosition()) == Board.NOTHING){
if(checkAvailability(c, color, temp))
temp.addPebble(c.toPosition(), Board.InverseColor(color));
else temp.addPebble(c.toPosition(), color);
}
return temp;
}
private synchronized boolean checkAvailability(Coordinates c,int color, Board b) {
if(c.getLeft().inRange()) {
if(b.getColor(c.getLeft().toPosition()) == color) {
if(c.getLeft().getLeft().inRange()) {
if(b.getColor(c.getLeft().getLeft().toPosition()) == color) {
return false;
}
}
else if(c.getRight().inRange()) {
if(b.getColor(c.getRight().toPosition()) == color)
return false;
}
}
}
if(c.getUp().inRange()) {
if(b.getColor(c.getUp().toPosition()) == color) {
if(c.getUp().getUp().inRange()) {
if(b.getColor(c.getUp().getUp().toPosition()) == color) {
return false;
}
}
else if(c.getDown().inRange()) {
if(b.getColor(c.getDown().toPosition()) == color)
return false;
}
}
}
if(c.getRight().inRange()) {
if(b.getColor(c.getRight().toPosition()) == color) {
if(c.getRight().getRight().inRange()) {
if(b.getColor(c.getRight().getRight().toPosition()) == color) {
return false;
}
}
else if(c.getLeft().inRange()) {
if(b.getColor(c.getLeft().toPosition()) == color)
return false;
}
}
}
if(c.getDown().inRange()) {
if(b.getColor(c.getDown().toPosition()) == color) {
if(c.getDown().getDown().inRange()) {
if(b.getColor(c.getDown().getDown().toPosition()) == color) {
return false;
}
}
else if(c.getUp().inRange()) {
if(b.getColor(c.getUp().toPosition()) == color)
return false;
}
}
}
return true;
}
private synchronized boolean checkFull(Board b) {
int n = b.getNumbers();
for(int i = 0; i < n; i++)
if(b.getColor(i) == Board.NOTHING)
return false;
return true;
}
}