-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpipe_solver.pde
More file actions
144 lines (141 loc) · 3.85 KB
/
pipe_solver.pde
File metadata and controls
144 lines (141 loc) · 3.85 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
ArrayList<Integer> path = new ArrayList<Integer>();
Tile start;
Tile end;
boolean solution_found = false;
void find_path_to_endpoint(Tile s){
if(s == null){
return;
}
Tile next_start = null;
for(int i = 0; i<4; i++){
s.rotation = i; // 4 states( 0, 1, 2, 3 )
end.rotation = i;
if(s.type == 1 && i>=2){
continue;
}
//if the start and end are connected to the same tile then the path must have been found
if(s.id == end.id && s.connected() && end.connected()){
path.add(s.type*1000 + i*100 +s.id);
solution_found = true;
return;
}
if(s.connected()){
path.add(s.type*1000+i*100+s.id);
//move start
switch(s.type){
case 1:
switch(s.entrance){// 1 = left, 2 = up, 3 = right, 4 = down
case 1:
if(s.x != canvas_definition - 1){
next_start = new Tile(s.id + 1 , s.entrance);
}
break;
case 2:
if(s.y != canvas_definition - 1){
next_start = new Tile(s.id + canvas_definition, s.entrance);
}
break;
case 3:
if(s.x != 0){
next_start = new Tile(s.id - 1, s.entrance);
}
break;
case 4:
if(s.y != 0){
next_start = new Tile(s.id - canvas_definition, s.entrance);
}
break;
}
break;
case 2:
switch(s.entrance){
case 1:
switch(s.rotation){
case 0:
if(s.y != 0){
next_start = new Tile(s.id - canvas_definition, 4);
}
break;
case 3:
if(s.y != canvas_definition - 1){
next_start = new Tile(s.id + canvas_definition, 2);
}
break;
}
break;
case 2:
switch(s.rotation){
case 0:
if(s.x != 0){
next_start = new Tile(s.id - 1, 3);
}
break;
case 1:
if(s.x != canvas_definition - 1){
next_start = new Tile(s.id + 1, 1);
}
break;
}
break;
case 3:
switch(s.rotation){
case 1:
if(s.y != 0){
next_start = new Tile(s.id - canvas_definition, 4);
}
break;
case 2:
if(s.y != canvas_definition - 1){
next_start = new Tile(s.id + canvas_definition, 2);
}
break;
}
break;
case 4:
switch(s.rotation){
case 2:
if(s.x != canvas_definition - 1){
next_start = new Tile(s.id + 1, 1);
}
break;
case 3:
if(s.x != 0){
next_start = new Tile(s.id - 1, 3);
}
break;
}
break;
}
break;
case 3:
break;
case 4:
break;
}
}
boolean already_explored = false;
try{
for(int j: path){
if(j%100 == next_start.id){
already_explored = true;
break;
}
}
} catch(Exception e){}
if(!already_explored){
find_path_to_endpoint(next_start);
}
if(!solution_found){
path.remove(Integer.valueOf(s.type*1000+i*100+s.id));
}
else{
break;
}
}
}
void solve(int cur_pipe){
int id = cur_pipe%100;
int x = id%canvas_definition;
int y = id/canvas_definition;
cells[x][y] = cur_pipe/100;
}