Repository navigation
Expand file tree
/
Copy pathEPuzzleState.java
More file actions
208 lines (168 loc) · 4.68 KB
/
Copy pathEPuzzleState.java
File metadata and controls
208 lines (168 loc) · 4.68 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
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
<<<<<<< HEAD
public class EPuzzleState extends SearchState {
// display the board as an array
private int[] board = new int[];
// constructor
public EPuzzleState(int[] b) {
this.board = b;
}
// accessor for puzzle state
public int[] getBoard() {
return board;
}
// goal method
public boolean goalP(Searcher searcher) {
PuzzleSearch pS = (PuzzleSearch) searcher;
int[] target = pS.getTarget(); // get target amount
return (target == board); // return if, may have to look up the syntax
// for this
}
// same state
public int[] SameState(SearchState s){
PuzzleState s1 = (PuzzleState) s;
return (board = s1.getBoard())s;
}
// fuk knows how getSuccessors works
// wait i might know ..
// check tomorrow too
public <ArrayList> getSuccessors(Searcher searcher){
EPuzzleState ep = (EPuzzleState) searcher;
int bS = ep.getBoard(); //get the state of the board
//find where the gap is
}
}
}
=======
import java.util.*;
public class EPuzzleState extends SearchState {
// display the board as an array
private int[][] board = new int[3][3];
// constructor
public EPuzzleState(int[][] b) {
this.board = b;
}
// accessor for puzzle state
public int[][] getBoard() {
return board;
}
// goal method
//compare current state with the goal
public boolean goalP(Search searcher) {
PuzzleSearch pS = (PuzzleSearch) searcher;
int[][] target = pS.getTarget(); // get target amount
for (int i = 0; i < board.length; i++){
for(int j = 0; j < board[i].length; j++){
if (board[i][j] != target[i][j]){
return false;
}
}
}
return true;
}
// same state
//check the board and current state and if one tile doesnt match return false
public boolean sameState(SearchState s) {
EPuzzleState s1 = (EPuzzleState) s;
int[][] s2 = s1.getBoard();
for(int i = 0; i < s2.length; i++){
for(int j = 0; j < s2.length; j++){
if (board[i][j] != s2[i][j]){
return false;
}
}
}
return true;
}
public ArrayList<SearchState> getSuccessors(Search searcher){
PuzzleSearch pS = (PuzzleSearch) searcher;
//int tS = pS.getTarget(); //get the target
EPuzzleState cB = new EPuzzleState(board);
int[][] copyBoard = new int[3][3];
int[][] p1 = new int[3][3];
int[][] p2 = new int[3][3];
int[][] p3 = new int[3][3];
int[][] p4 = new int[3][3];
//create many boards each representing a different move
for(int i = 0; i < 3; i++){
copyBoard[i] = board[i].clone();
p1[i] = board[i].clone();
p2[i] = board[i].clone();
p3[i] = board[i].clone();
p4[i] = board[i].clone();
}
int xGap=0; //x co-ord of the gap
int yGap=0; //y co-ord of the gap
ArrayList<EPuzzleState> pslis=new ArrayList<EPuzzleState>(); // the list of puzzle states
ArrayList<SearchState> slis=new ArrayList<SearchState>();
//find the empty space, gap, in the board
for (int i = 0; i < copyBoard.length; i++){
for (int j = 0; j < copyBoard.length; j++){
if (copyBoard[i][j] == 0){
xGap = i;
yGap = j;
}
}
}
//POSSIBLE WAY OF SOlVING
//create 4 additional boards, P1, P2, P3, P4
//at each of the boundary cases, edit one board per boundary case
//pass the array as a parameter in EPuzzleState
//eg pslis.add(newEPuzzleState[P1]);
//boundary cases, when y != 2, x !=2, y !=0, x!= 0
//possible moves at any given state
//move up
if (xGap != 0 ){
for(int i = 0; i < 3; i++){
p1[i] = board[i].clone();
}
p1[xGap][yGap] = p1[xGap - 1][yGap];
p1[xGap -1][yGap] = 0;
pslis.add(new EPuzzleState(p1));
System.out.println("x !=0 reached");
}
//move down
if (xGap != 2){
for(int i = 0; i < 3; i++){
p2[i] = board[i].clone();
}
p2[xGap][yGap] = p2[xGap+1][yGap];
p2[xGap +1][yGap] = 0;
pslis.add(new EPuzzleState(p2));
System.out.println("x !=2 reached");
}
//move left
if (yGap != 0){
for(int i = 0; i < 3; i++){
p3[i] = board[i].clone();
}
p3[xGap][yGap] = p3[xGap][yGap -1];
p3[xGap][yGap-1] =0;
pslis.add(new EPuzzleState(p3));
System.out.println("y !=0 reached");
}
//move right
if(yGap != 2){
for(int i = 0; i < 3; i++){
p4[i] = board[i].clone();
}
p4[xGap][yGap] =p4[xGap][yGap+1];
p4[xGap][yGap+1]=0;
pslis.add(new EPuzzleState(p4));
System.out.println("y !=2 reached");
}
//cast puzzle states to search states for slis
for (EPuzzleState ps:pslis)slis.add((SearchState)ps);
return slis;
}
public String toString(){
String output = new String();
for(int i = 0; i <board.length;i++){
output = output +"\n";
for(int j = 0; j < board.length ; j++){
output = output+board[i][j];
}
}
return output;
}
}
>>>>>>> 62145eca4e0b88139ae369a83bcbdfb25573182a