-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathColorTilePuzzle.java
More file actions
181 lines (162 loc) · 4.3 KB
/
Copy pathColorTilePuzzle.java
File metadata and controls
181 lines (162 loc) · 4.3 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
import java.awt.geom.Point2D;
/**
*
* @author Boaz Sharabi
* This class represent ColorTilePuzzle state
*
*/
public class ColorTilePuzzle implements tile {
private Board board;
private int cost=0; // how much cost to develop this state
private tile parent=null;
private String num_op;
public boolean _isOut=false;
private int time;
public static int count=0; // count the number of instance created
public ColorTilePuzzle(Board board,int cost,tile parent, String num_op) {
this.board=board;
this.cost=cost;
this.parent=parent;
this.num_op=num_op;
this._isOut=false;
count++;
time=count;
}
@Override
public int getTime() {
return time;
}
@Override
public Point2D getFree(){
return board.blank_cell;
}
@Override
public Board getBoard() {
// TODO Auto-generated method stub
return this.board;
}
@Override
public int getCost() {
// TODO Auto-generated method stub
return this.cost;
}
@Override
public void setCost(int cost) {
// TODO Auto-generated method stub
this.cost=cost;
}
public String toString() {
// Loop through all rows
return this.board.toString();
}
@Override
public tile getParent() {
return this.parent;
}
@Override
public tile copy() {
return new ColorTilePuzzle(this.board.copy(),this.cost,this.parent,this.num_op);
}
@Override
public boolean equals(Object b) {
return (b instanceof tile )&&b.toString().equals(this.toString());
}
@Override
public String getNumOp() {
// TODO Auto-generated method stub
return this.num_op;
}
@Override
public tile getArrangedTile() {
// TODO Auto-generated method stub
return new ColorTilePuzzle(this.board.getArranged(), 0, null, "");
}
@Override
public boolean isOut() {
// TODO Auto-generated method stub
return _isOut;
}
@Override
public void out() {
_isOut=true;
}
@Override
public tile move(int direction) {
int x =(int) this.board.getEmpty().getX();
int y = (int) this.board.getEmpty().getY();
// TODO Auto-generated method stub
if(direction==1)
{
// checks that the cell you want to move exists in the board
if(x==board.mat[0].length-1||
// checks that we able to move the cell
this.board.color_cell.get(this.getBoard().mat[y][x+1])==Color.BLACK||
//ensure we never apply an operator and it's inverse in succession
this.getNumOp().contains("R"))
return null;
return move(x,y,x+1,y,"L");
}
else if(direction==2)
{
if(y==board.mat.length-1||
this.board.color_cell.get(this.getBoard().mat[y+1][x])==Color.BLACK||
this.getNumOp().contains("D"))
return null;
return move(x,y,x,y+1,"U");
}
else if(direction==3)
{
if(x==0||this.board.color_cell.get(this.getBoard().mat[y][x-1])==Color.BLACK
||this.getNumOp().contains("L"))
return null;
return move(x,y,x-1,y,"R");
}
else{
if(y==0||this.board.color_cell.get(this.getBoard().mat[y-1][x])==Color.BLACK||
this.getNumOp().contains("U"))
return null;
return move(x,y,x,y-1,"D");
}
}
/**
*
* @param x_dest - of blank piece
* @param y_dest - of blank piece
* @param x_src
* @param y_src
* @param direction
* @return new tile state after sliding piece from src point to dest point
*/
public tile move(int x_dest,int y_dest,int x_src,int y_src, String direction ) {
tile newTile=this.copy();
newTile.getBoard().mat[y_dest][x_dest]=newTile.getBoard().mat[y_src][x_src];
newTile.getBoard().mat[y_src][x_src]=-1;
int cost=1;
if(this.board.color_cell.get(newTile.getBoard().mat[y_dest][x_dest])==Color.RED)
cost=30;
newTile.setCost(cost+this.getCost());
newTile.setNumOp(newTile.getBoard().mat[y_dest][x_dest]+direction);
newTile.getBoard().setEmpty(new Point2D.Double(x_src, y_src));
newTile.setParent(this);
return newTile;
}
@Override
public void setNumOp(String num_op) {
// TODO Auto-generated method stub
this.num_op=num_op;
}
@Override
public void setParent(tile parent) {
// TODO Auto-generated method stub
this.parent=parent;
}
@Override
public void markNoOut() {
this._isOut=false;
}
@Override
public String getKey() {
// TODO Auto-generated method stub
return this.board.getKey();
}
}