-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathQuad.java
208 lines (103 loc) · 2.72 KB
/
Quad.java
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
import java.io.*;
class Quad implements Serializable
{
public final static boolean EMPTY=true;
public final static byte UP = 0 ;
public final static byte RIGHT = 1 ;
public final static byte DOWN = 2 ;
public final static byte LEFT = 3 ;
private final static byte DEF = -1;//Valor atribuido por Default às partes do Quad
public final static byte NPARTES = 4;
private byte [] parte=new byte [NPARTES];
private boolean empty=false;
private boolean explosiva=false;
public Quad (Quad q)
{
for(byte c=UP;c<=LEFT;c++)
this.parte[c] = q.getParte(c);
empty = q.isEmpty();
explosiva=q.isExplosiva();
}
public Quad (int nHips)//quad aleatorio
{
empty=false;
for (byte c=UP;c<=LEFT;c++) parte[c]=(byte)(Math.random()*nHips);
}
public Quad ()
{
empty=true;
explosiva=false;
for (byte c=UP;c<=LEFT;c++) parte[c]=DEF;//por default
}
public Quad (byte [] parte)
{
empty=false;
this.parte=parte;
}
public void resetQuad()
{
empty=true;
for(int cnt=UP;cnt<=LEFT;cnt++)
parte[cnt]=DEF;
}
// Avalia se dois quads são iguais numa posição
// em termos de formato e não vazio ou explosivo
public boolean eIgual(Quad q)
{
boolean res=false;
for(int cnt=UP;cnt<=LEFT;cnt++)
{
res=res || (this.parte[UP]==q.parte[UP] && this.parte[RIGHT]==q.parte[RIGHT] && this.parte[DOWN]==q.parte[DOWN] && this.parte[LEFT]==q.parte[LEFT]);
q.shiftRight();
}
return(res);
}
public void setEmpty(boolean empty)
{
this.empty=empty;
if(empty==true)
for (byte c=UP;c<=LEFT;c++)parte[c]=DEF;
}
public void shiftRight()
{
byte [] newParte = new byte [NPARTES];
System.arraycopy(parte,UP,newParte,RIGHT,LEFT);
newParte[UP]=parte[LEFT];
parte=newParte;
}
public void shiftLeft()//Tem de ser manual pois com o Arraycopy torna-se complicado
{
byte [] newParte = new byte [NPARTES];
newParte[RIGHT]=parte[DOWN];
newParte[UP]=parte[RIGHT];
newParte[LEFT]=parte[UP];
newParte[DOWN]=parte[LEFT];
parte=newParte;
}
public byte getUp() {return parte[UP ];}
public byte getRight(){return parte[RIGHT];}
public byte getDown() {return parte[DOWN ];}
public byte getLeft() {return parte[LEFT ];}
public boolean isExplosiva()
{
return(explosiva);
}
public void setExplosiva(boolean exp)
{
this.explosiva=exp;
}
public boolean isEmpty(){return empty;}
public byte getParte(byte part){return parte[part];}
public String toString()
{
return
(
" Cima :" +parte[UP ]+
" Direita :" +parte[RIGHT]+
" Baixo :" +parte[DOWN ]+
" Esquerda :"+parte[LEFT ]+". "
+ "Vazio =" +empty
+ " Explosiva="+explosiva
);
}
}