-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathKaydirma.pde
More file actions
296 lines (260 loc) · 5.77 KB
/
Kaydirma.pde
File metadata and controls
296 lines (260 loc) · 5.77 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
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
class Oyun {
int[][] saha;
public int x;
public int y;
public int boyut;
public Oyun(int n, boolean bitmisMi) {
boyut = n;
saha = new int[boyut][boyut];
if (bitmisMi) {
int sayi = 1;
for (int i = 0; i < boyut; i++) {
for (int j = 0; j < boyut; j++) {
saha[i][j] = sayi;
sayi++;
}
}
saha[boyut-1][boyut-1] = 0;
x = boyut-1;
y = boyut-1;
} else {
int[] sayilar = new int[boyut*boyut];
for (int i = 0; i < sayilar.length; i++) {
sayilar[i] = i;
}
for (int i = 0; i < sayilar.length; i++) {
int tmp = sayilar[i];
int r = (int) random(sayilar.length);
sayilar[i] = sayilar[r];
sayilar[r] = tmp;
}
int indeks = 0;
for (int i = 0; i < boyut; i++) {
for (int j = 0; j < boyut; j++) {
saha[i][j] = sayilar[indeks];
if (saha[i][j] == 0) {
x = i;
y = j;
}
indeks++;
}
}
}
}
void karistir(int k) {
for (int i = 0; i < k; i++) {
hamleYap(rastgeleGecerliHamle());
}
}
void yazdir() {
for (int i = 0; i < boyut; i++) {
for (int j = 0; j < boyut; j++) {
print("" + saha[i][j] + " ");
}
println();
}
}
boolean oyunBittiMi() {
int sayi = 1;
for (int i = 0; i < boyut; i++) {
for (int j = 0; j < boyut; j++) {
if (saha[i][j] == sayi || (i == boyut - 1 && j == boyut - 1)) {
sayi++;
} else {
return false;
}
}
}
return true;
}
// hamlenin geçerli olduğu varsayılıyor
void hamleYap(int h) {
if (h == 0) {
degistir(x, y, x-1, y);
x--;
} else if (h == 1) {
degistir(x, y, x, y-1);
y--;
} else if (h == 2) {
degistir(x, y, x+1, y);
x++;
} else if (h == 3) {
degistir(x, y, x, y+1);
y++;
}
}
// h = 0 -> yukarı, x--
// h = 1 -> sol, y--
// h = 2 -> aşağı, x++
// h = 3 -> sağ, y++
boolean gecerliHamleMi(int h) {
if (h == 0) {
return x > 0;
} else if (h == 1) {
return y > 0;
} else if (h == 2) {
return x < boyut - 1;
} else if (h == 3) {
return y < boyut - 1;
} else {
return false;
}
}
public int rastgeleGecerliHamle() {
int[] sayilar = new int[4];
for (int i = 0; i < sayilar.length; i++) {
sayilar[i] = i;
}
for (int i = 0; i < sayilar.length; i++) {
int tmp = sayilar[i];
int r = (int) random(sayilar.length);
sayilar[i] = sayilar[r];
sayilar[r] = tmp;
}
for (int i = 0; i < sayilar.length; i++) {
if (gecerliHamleMi(sayilar[i])) {
return sayilar[i];
}
}
return -1;
}
public String kodla() {
String k = "";
for (int i = 0; i < boyut; i++) {
for (int j = 0; j < boyut; j++) {
if (i == boyut - 1 && j == boyut - 1) {
k += saha[i][j];
} else {
k += saha[i][j] + "-";
}
}
}
return k;
}
private void degistir(int i1, int j1, int i2, int j2) {
int tmp = saha[i1][j1];
saha[i1][j1] = saha[i2][j2];
saha[i2][j2] = tmp;
}
}
class Zeka {
HashMap<String, Durum> defter = new HashMap<String, Durum>();
Oyun o;
public Zeka(Oyun o) {
this.o = o;
}
public void verilenOyundaHamleYap(Oyun o) {
String s = o.kodla();
Durum d = defter.get(s);
int rh = -1;
if (d != null) {
int h = d.enIyiHamleNe();
if (h == -1) {
rh = o.rastgeleGecerliHamle();
} else {
rh = h;
}
} else {
d = new Durum();
defter.put(s, d);
rh = o.rastgeleGecerliHamle();
}
println("Hamle : " + rh);
o.hamleYap(rh);
}
public void oyna(int k, int jmax, int kardmax) {
for (int kard = 0; kard < kardmax; kard++) {
for (int i = 0; i < k; i++) {
o = new Oyun(o.boyut, true);
o.karistir(kard);
for (int j = 0; j < jmax; j++) {
hamleYap();
//o.yazdir();
//println();
if (o.oyunBittiMi()) {
o = new Oyun(o.boyut, true);
o.karistir(kard);
}
}
}
}
}
public void hamleYap() {
String s = o.kodla();
Durum d = defter.get(s);
int rh = -1;
//println(s);
if (d != null) {
int h = d.enIyiHamleNe();
if (h == -1) {
rh = o.rastgeleGecerliHamle();
} else {
rh = h;
}
} else {
d = new Durum();
defter.put(s, d);
rh = o.rastgeleGecerliHamle();
}
o.hamleYap(rh);
String s2 = o.kodla();
Durum d2 = defter.get(s2);
if (d2 != null) {
int deger = d2.degeriNe();
if (deger != Integer.MAX_VALUE) {
d.guncelle(rh, deger+1);
}
}
if (o.oyunBittiMi()) {
d.guncelle(rh, 1);
}
}
}
class Durum {
// public int[][] saha;
public int[] hamleler;
public Durum() {
hamleler = new int[4];
for (int i = 0; i < 4; i++) {
hamleler[i] = Integer.MAX_VALUE;
}
}
public void guncelle(int h, int d) {
if (hamleler[h] > d) {
hamleler[h] = d;
}
}
public int degeriNe() {
int min = Integer.MAX_VALUE;
for (int i = 0; i < 4; i++) {
if (min > hamleler[i]) {
min = hamleler[i];
}
}
return min;
}
public int enIyiHamleNe() {
int min = Integer.MAX_VALUE;
int in = -1;
for (int i = 0; i < 4; i++) {
if (min > hamleler[i]) {
min = hamleler[i];
in = i;
}
}
return in;
}
}
void setup() {
Oyun o1 = new Oyun(3, true);
Zeka z = new Zeka(o1);
println("zeka eğitiliyor...");
z.oyna(100, 500, 50);
o1 = new Oyun(3, true);
o1.karistir(50);
while (!o1.oyunBittiMi()) {
z.verilenOyundaHamleYap(o1);
o1.yazdir();
}
println("son");
}