-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMDL_Ppal.bas
333 lines (264 loc) · 10.5 KB
/
MDL_Ppal.bas
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
Attribute VB_Name = "MDL_Ppal"
Option Explicit
'--------------------------CONSTANTS---------------------------------------------
Public Const tamany_x = 10 'Tamany x de la matriu
Public Const tamany_y = 10 'Tamany y de la matriu
'--------------------------VARIABLES---------------------------------------------
Public m(1 To tamany_x, 1 To tamany_y) As Integer 'matriu principal que contindra
'totes les possibilitats
Public inici_x As Integer 'Marca la posició x d'inici
Public inici_y As Integer 'Marca la posició y d'inici
Public mov As Integer 'Indica el número de moviments que fem
Public Type posi
p_x As Integer
p_y As Integer
numero As Integer
final As Boolean
End Type
Public segon As Boolean
'-------------------------FUNCIONS i PROCEDIMENTS-------------------------------------
Public Function Buscar_Millor_Posicio(ByVal x, ByVal y) As posi
Dim i As Integer
Dim j As Integer
Dim millor_moviment As Integer
Dim posicio_millor As posi
Dim millor_moviment2 As Integer
millor_moviment = 8
millor_moviment2 = 8
posicio_millor.final = False
'Primera possibilitat (dos dreta, un avall)
If y + 2 <= tamany_y Then
If x + 1 <= tamany_x Then
If m(x + 1, y + 2) <= millor_moviment And m(x + 1, y + 2) <> 0 Then
millor_moviment = m(x + 1, y + 2)
posicio_millor.p_x = x + 1
posicio_millor.p_y = y + 2
posicio_millor.numero = millor_moviment
End If
End If
End If
'Segona possibilitat (dos esquerra, un avall)
If y - 2 >= 1 Then
If x + 1 <= tamany_x Then
If m(x + 1, y - 2) <= millor_moviment And m(x + 1, y - 2) <> 0 Then
millor_moviment = m(x + 1, y - 2)
posicio_millor.p_x = x + 1
posicio_millor.p_y = y - 2
posicio_millor.numero = millor_moviment
End If
End If
End If
'Tercera possibilitat (dos dreta, un amunt)
If y + 2 <= tamany_y Then
If x - 1 >= 1 Then
If m(x - 1, y + 2) <= millor_moviment And m(x - 1, y + 2) <> 0 Then
millor_moviment = m(x - 1, y + 2)
posicio_millor.p_x = x - 1
posicio_millor.p_y = y + 2
posicio_millor.numero = millor_moviment
End If
End If
End If
'Cuarta possibilitat (dos esquerra, un amunt)
If y - 2 >= 1 Then
If x - 1 >= 1 Then
If m(x - 1, y - 2) <= millor_moviment And m(x - 1, y - 2) <> 0 Then
millor_moviment = m(x - 1, y - 2)
posicio_millor.p_x = x - 1
posicio_millor.p_y = y - 2
posicio_millor.numero = millor_moviment
End If
End If
End If
'Quinta possibilitat (un esquerra, dos amunt)
If y - 1 >= 1 Then
If x - 2 >= 1 Then
If m(x - 2, y - 1) <= millor_moviment And m(x - 2, y - 1) <> 0 Then
millor_moviment = m(x - 2, y - 1)
posicio_millor.p_x = x - 2
posicio_millor.p_y = y - 1
posicio_millor.numero = millor_moviment
End If
End If
End If
'Sexta possibilitat (un dreta, dos amunt)
If y + 1 <= tamany_y Then
If x - 2 >= 1 Then
If m(x - 2, y + 1) <= millor_moviment And m(x - 2, y + 1) <> 0 Then
millor_moviment = m(x - 2, y + 1)
posicio_millor.p_x = x - 2
posicio_millor.p_y = y + 1
posicio_millor.numero = millor_moviment
End If
End If
End If
'Setena possibilitat (un dreta, dos avall)
If y + 1 <= tamany_y Then
If x + 2 <= tamany_x Then
If m(x + 2, y + 1) <= millor_moviment And m(x + 2, y + 1) <> 0 Then
millor_moviment = m(x + 2, y + 1)
posicio_millor.p_x = x + 2
posicio_millor.p_y = y + 1
posicio_millor.numero = millor_moviment
End If
End If
End If
'Vuitena possibilitat (un esquerra, dos avall)
If y - 1 >= 1 Then
If x + 2 <= tamany_x Then
If m(x + 2, y - 1) <= millor_moviment And m(x + 2, y - 1) <> 0 Then
millor_moviment = m(x + 2, y - 1)
posicio_millor.p_x = x + 2
posicio_millor.p_y = y - 1
posicio_millor.numero = millor_moviment
End If
End If
End If
If posicio_millor.p_x = 0 And posicio_millor.p_y = 0 Then
posicio_millor.final = True
End If
Buscar_Millor_Posicio = posicio_millor
End Function
Public Function Triar_Posicio_Inici() As posi
'Busca la posició inicial en que començara el cavall i la guarda en una variable
Dim x As Integer
Dim y As Integer
Dim posici As posi
x = 0
y = 0
While x = 0
Randomize
x = Int((tamany_x * Rnd) + 1)
Wend
While y = 0
Randomize
y = Int((tamany_y * Rnd) + 1)
Wend
posici.p_x = x
posici.p_y = y
posici.numero = m(x, y)
Triar_Posicio_Inici = posici
End Function
Public Sub Buscar_Posicions_Possibles()
'Omple la matriu amb les possibilitats que te cada casella de la matriu
'Sempre pensant amb el moviment del cavall
Dim x As Integer
Dim y As Integer
Dim pos As Integer
For x = 1 To tamany_x
For y = 1 To tamany_y
pos = 0
'Primera possibilitat (dos dreta, un avall)
If y + 2 <= tamany_y Then
If x + 1 <= tamany_x Then
If (m(x + 1, y + 2) <> 0) Or (segon = False) Then
pos = pos + 1
End If
End If
End If
'Segona possibilitat (dos esquerra, un avall)
If y - 2 >= 1 Then
If x + 1 <= tamany_x Then
If (m(x + 1, y - 2) <> 0) Or (segon = False) Then
pos = pos + 1
End If
End If
End If
'Tercera possibilitat (dos dreta, un amunt)
If y + 2 <= tamany_y Then
If x - 1 >= 1 Then
If (m(x - 1, y + 2) <> 0) Or (segon = False) Then
pos = pos + 1
End If
End If
End If
'Cuarta possibilitat (dos esquerra, un amunt)
If y - 2 >= 1 Then
If x - 1 >= 1 Then
If (m(x - 1, y - 2) <> 0) Or (segon = False) Then
pos = pos + 1
End If
End If
End If
'Quinta possibilitat (un esquerra, dos amunt)
If y - 1 >= 1 Then
If x - 2 >= 1 Then
If (m(x - 2, y - 1) <> 0) Or (segon = False) Then
pos = pos + 1
End If
End If
End If
'Sexta possibilitat (un dreta, dos amunt)
If y + 1 <= tamany_y Then
If x - 2 >= 1 Then
If (m(x - 2, y + 1) <> 0) Or (segon = False) Then
pos = pos + 1
End If
End If
End If
'Setena possibilitat (un dreta, dos avall)
If y + 1 <= tamany_y Then
If x + 2 <= tamany_x Then
If (m(x + 2, y + 1) <> 0) Or (segon = False) Then
pos = pos + 1
End If
End If
End If
'Vuitena possibilitat (un esquerra, dos avall)
If y - 1 >= 1 Then
If x + 2 <= tamany_x Then
If (m(x + 2, y - 1) <> 0) Or (segon = False) Then
pos = pos + 1
End If
End If
End If
If m(x, y) <> 0 Or segon = False Then
m(x, y) = pos
End If
Next
Next
End Sub
Public Sub Omplir_Matriu_Textos()
'Omple totes les caselles de texte amb el seu corresponent a la matriu
'original
Dim i As Integer
Dim j As Integer
Dim k As Integer
k = 1
For i = 1 To tamany_x
For j = 1 To tamany_y
FRM_Cavall.Text1(k).Text = m(i, j)
k = k + 1
Next
Next
FRM_Cavall.L_Moviments.Caption = mov
End Sub
Public Sub Omplir_matriu_picture()
Dim i As Integer
Dim j As Integer
Dim k As Integer
k = 0
For i = 1 To tamany_y
For j = 1 To tamany_y
If m(i, j) = 0 Then FRM_Cavall.Picture1(k).BackColor = &HFF&
k = k + 1
Next
Next
End Sub
Public Sub Reiniciar_pictures()
Dim i As Integer
Dim j As Integer
For i = 0 To 99
FRM_Cavall.Picture1(i).BackColor = &H8000000A
Next
End Sub
Public Sub Posar_Matriu_a_0()
'Posem la matriu principal a 0
Dim i As Integer
Dim j As Integer
For i = 1 To tamany_x
For j = 1 To tamany_y
m(i, j) = 0
Next
Next
End Sub