一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - Java教程 - Java版AI五子棋游戲

Java版AI五子棋游戲

2021-06-02 13:40elims小熊 Java教程

這篇文章主要為大家詳細(xì)介紹了Java版AI五子棋游戲,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了java五子棋游戲的具體代碼,供大家參考,具體內(nèi)容如下

ai思路:通過判斷棋盤上每個空位的分?jǐn)?shù),去分?jǐn)?shù)最高的幾個點,隨機下棋
分?jǐn)?shù)計算思路:能成5個說明能贏了,給最高分
不能成5個,對方能成5個,說明對方要贏了,給第二高分
能成活4,給第三高分
能成活3,給第四高分
能成沖4,給第五高分
能成沖3,給第六高分
能成活2,給第七高分
能成沖2,給第八高分
其他,給最低分
分?jǐn)?shù)設(shè)定可自己定義。

因為是去年寫的了,思路記得大概就是這樣。最近根據(jù)書上寫了個棋類游戲的設(shè)計框架,待完善后再發(fā)上來,應(yīng)該會更新ai思路
下面是去年寫的ai五子棋的代碼:

?
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
package fivechessclient;
 
import java.awt.cursor;
import java.awt.dimension;
import java.awt.graphics;
import java.awt.event.mouseadapter;
import java.awt.event.mouseevent;
import java.awt.event.mousemotionadapter;
import java.awt.image.bufferedimage;
import java.io.file;
import java.util.arraylist;
import java.util.random;
 
import javax.imageio.imageio;
import javax.swing.jframe;
import javax.swing.joptionpane;
import javax.swing.jpanel;
 
/**
 *
 * @classname: game
 * @description: ai五子棋
 * @author xiaoxiong
 * @date 2015年7月3日 下午8:59:02
 *
 */
public class game {
 bufferedimage table;
 bufferedimage black;
 bufferedimage white;
 
 bufferedimage selected;
 /**
 * 棋子個數(shù)
 */
 private static int board_size = 15;
 /**
 * 棋盤寬高
 */
 private final int table_width = 535;
 private final int table_height = 536;
 /**
 * 棋盤15等分
 */
 private final int rate = table_width / board_size;
 /**
 * 棋盤外邊距
 */
 private final int x_offset = 5;
 private final int y_offset = 6;
 /**
 * 棋盤
 */
 private int[][] board = new int[board_size][board_size];
 /**
 * ai分?jǐn)?shù)
 */
 private int[][] computerscore = new int[board_size][board_size];
 // private int[][] gamerscore = new int[board_size][board_size];
 
 jframe f = new jframe("五子棋--小熊");
 
 chessboard chessboard = new chessboard();
 
 private static int selectedx = -1;
 private static int selectedy = -1;
 private static int computerx = -1;
 private static int computery = -1;
 
 private static boolean flaggamer = false; // 記錄玩家是否贏了
 private static boolean flagcomputer = false; // 記錄電腦是否贏了
 
 private static int computerscore = 0; // 電腦最大分?jǐn)?shù)
 private static int comx, comy; // 玩家下子坐標(biāo)
 
 private final int huo = 1;
 private final int chong = 2;
 private static int chesscou = 0;
 /**
 * 記錄找到的分?jǐn)?shù)一樣的棋子,隨機下這些棋子中的一個,以防步法固定
 */
 private arraylist<chessxy> chesslist = new arraylist<chessxy>();
 
 random rand = new random();
 
 /**
 *
 * @title: initto @description: 重置游戲 @param @return void @throws
 */
 public void initto() {
 for (int i = 0; i < board_size; ++i) {
 for (int j = 0; j < board_size; ++j) {
 board[i][j] = 0;
 computerscore[i][j] = 100000;
 }
 }
 chesscou = 0;
 computerx = -1;
 computery = -1;
 flaggamer = false;
 flagcomputer = false;
 }
 
 /**
 *
 * @title: isrun @description: 判斷該位置是否可以走 @param @param x @param @param
 * y @param @return @return boolean @throws
 */
 public boolean isrun(int x, int y) {
 if (board[x][y] == 0) {
 return true;
 }
 return false;
 }
 
 /**
 *
 * @title: iswin @description: 判斷下該子是否能贏 @param @param f 顏色 @param @param x
 * 坐標(biāo) @param @param y @param @return @return boolean @throws
 */
 public boolean iswin(int f, int x, int y) {
 int i, count = 1;
 boolean up, down, right, left, rup, lup, rdown, ldown;
 up = down = right = left = rup = lup = rdown = ldown = true;
 /**
 *
 * 上下
 *
 */
 for (i = 1; i < 5; ++i) {
 if ((y + i) < board_size) {
 if (board[x][y + i] == f && down)
 count++;
 else
 down = false;
 }
 if ((y - i) >= 0) {
 if (board[x][y - i] == f && up)
 count++;
 else
 up = false;
 }
 }
 if (count >= 5) {
 return true;
 }
 count = 1;
 /**
 *
 * 左右
 *
 */
 for (i = 1; i < 5; ++i) {
 if ((x + i) < board_size) {
 if (board[x + i][y] == f && right)
 count++;
 else
 right = false;
 }
 if ((x - i) >= 0) {
 if (board[x - i][y] == f && left)
 count++;
 else
 left = false;
 }
 }
 if (count >= 5) {
 return true;
 }
 count = 1;
 /**
 *
 * 左上右下
 *
 */
 for (i = 1; i < 5; ++i) {
 if ((x + i) < board_size && (y + i) < board_size) {
 if (board[x + i][y + i] == f && rdown)
 count++;
 else
 rdown = false;
 }
 if ((x - i) >= 0 && (y - i) >= 0) {
 if (board[x - i][y - i] == f && lup)
 count++;
 else
 lup = false;
 }
 }
 if (count >= 5) {
 return true;
 }
 count = 1;
 /**
 *
 * 右上左下
 *
 */
 for (i = 1; i < 5; ++i) {
 if ((x + i) < board_size && (y - i) >= 0) {
 if (board[x + i][y - i] == f && rup)
 count++;
 else
 rup = false;
 }
 if ((x - i) >= 0 && (y + i) < board_size) {
 if (board[x - i][y + i] == f && ldown)
 count++;
 else
 ldown = false;
 }
 }
 if (count >= 5) {
 return true;
 }
 
 return false;
 }
 
 /**
 *
 * @title: computer_ai @description: ai下棋 @param @return void @throws
 */
 public void computer_ai() {
 computerscore = 0;
 for (int i = 0; i < board_size; ++i) {
 for (int j = 0; j < board_size; ++j) {
 computerscore[i][j] = 0;
 // gamerscore[i][j] = 0;
 }
 }
 getscore();
 for (int i = 0; i < board_size; ++i) {
 for (int j = 0; j < board_size; ++j) {
 if (computerscore[i][j] == computerscore) {
 chessxy chess = new chessxy(i, j);
 chesslist.add(chess);
 }
 }
 }
 int n = rand.nextint(chesslist.size()); // 電腦根據(jù)分值一樣的點隨機走,防止每次都走相同的步數(shù)
 comx = chesslist.get(n).x;
 comy = chesslist.get(n).y;
 chesslist.clear();
 }
 
 /**
 *
 * @title: getscore @description: 評分 @param @return void @throws
 */
 public void getscore() {
 for (int i = 0; i < board_size; ++i) {
 for (int j = 0; j < board_size; ++j) {
 if (board[i][j] == 0) {
 if (iswin(2, i, j)) // 電腦能贏,故給分最高,因為可以結(jié)束,所以不再檢測
 {
 computerscore = 13;
 computerscore[i][j] = 13;
 
 return;
 } else if (iswin(1, i, j)) // 電腦不能贏,玩家能贏,要阻止,所以給12分
 {
 computerscore = 12;
 computerscore[i][j] = 12;
 } else if (ishuoorchong(2, i, j, 4, huo)) // 電腦玩家都不能贏,電腦能形成活四,給11分
 {
 computerscore = (computerscore > 11 ? computerscore : 11);
 computerscore[i][j] = 11;
 } else if (ishuoorchong(2, i, j, 4, chong)) // 電腦玩家都不能贏,電腦能形成沖四,給10分
 {
 computerscore = (computerscore > 10 ? computerscore : 10);
 computerscore[i][j] = 10;
 } else if (ishuoorchong(1, i, j, 4, huo)) // 電腦玩家都不能贏,玩家能形成活四,給9分
 {
 computerscore = (computerscore > 9 ? computerscore : 9);
 computerscore[i][j] = 9;
 } else if (ishuoorchong(2, i, j, 3, huo)) // 電腦玩家都不能贏,電腦能形成活三,給8分
 {
 computerscore = (computerscore > 8 ? computerscore : 8);
 computerscore[i][j] = 8;
 } else if (ishuoorchong(1, i, j, 4, chong)) // 電腦玩家都不能贏,玩家能形成沖四,給7分
 {
 computerscore = (computerscore > 7 ? computerscore : 7);
 computerscore[i][j] = 7;
 } else if (ishuoorchong(2, i, j, 3, chong)) // 電腦玩家都不能贏,電腦能形成沖三,給6分
 {
 computerscore = (computerscore > 6 ? computerscore : 6);
 computerscore[i][j] = 6;
 } else if (ishuoorchong(2, i, j, 2, huo)) // 電腦玩家都不能贏,電腦能形成活二,給5分
 {
 computerscore = (computerscore > 5 ? computerscore : 5);
 computerscore[i][j] = 5;
 } else if (ishuoorchong(1, i, j, 3, chong)) // 電腦玩家都不能贏,玩家能形成沖三,給4分
 {
 computerscore = (computerscore > 4 ? computerscore : 4);
 computerscore[i][j] = 4;
 } else if (ishuoorchong(1, i, j, 2, huo)) // 電腦玩家都不能贏,玩家能形成活二,給3分
 {
 computerscore = (computerscore > 3 ? computerscore : 3);
 computerscore[i][j] = 3;
 } else if (ishuoorchong(2, i, j, 2, chong)) // 電腦玩家都不能贏,電腦能形成沖二,給2分
 {
 computerscore = (computerscore > 2 ? computerscore : 2);
 computerscore[i][j] = 2;
 } else if (ishuoorchong(1, i, j, 2, chong)) // 電腦玩家都不能贏,玩家能形成沖二,給1分
 {
 computerscore = (computerscore > 1 ? computerscore : 1);
 computerscore[i][j] = 1;
 } else {
 computerscore[i][j] = 0;
 }
 }
 }
 }
 }
 
 /**
 *
 * @title: ishuoorchong @description: 判斷是否為活 @param @param f @param @param
 * x @param @param y @param @param num @param @param
 * horc @param @return @return boolean @throws
 */
 private boolean ishuoorchong(int f, int x, int y, int num, int horc) // 活
 {
 
 num += 1;
 int i, count = 1;
 boolean terminal1 = false;
 boolean terminal2 = false;
 boolean up, down, right, left, rup, lup, rdown, ldown;
 up = down = right = left = rup = lup = rdown = ldown = true;
 /**
 *
 * 上下
 *
 */
 for (i = 1; i < num; ++i) {
 if ((y + i) < board_size) {
 if (board[x][y + i] == f && down)
 count++;
 else {
 if (board[x][y + i] == 0 && down) {
 terminal1 = true;
 }
 down = false;
 }
 }
 if ((y - i) >= 0) {
 if (board[x][y - i] == f && up)
 count++;
 else {
 if (board[x][y - i] == 0 && up) {
 terminal2 = true;
 }
 up = false;
 }
 }
 }
 if (count == num - 1 && horc == huo && terminal1 && terminal2) {
 return true;
 }
 if (count == num - 1 && horc == chong && ((terminal1 && !terminal2) || (!terminal1 && terminal2))) {
 return true;
 }
 count = 1;
 terminal1 = false;
 terminal2 = false;
 /* 左右 */
 for (i = 1; i < num; ++i) {
 if ((x + i) < board_size) {
 if (board[x + i][y] == f && right)
 count++;
 else {
 if (board[x + i][y] == 0 && right) {
 terminal1 = true;
 }
 right = false;
 }
 }
 if ((x - i) >= 0) {
 if (board[x - i][y] == f && left)
 count++;
 else {
 if (board[x - i][y] == 0 && left) {
 terminal2 = true;
 }
 left = false;
 }
 }
 }
 if (count == num - 1 && horc == huo && terminal1 && terminal2) {
 return true;
 }
 if (count == num - 1 && horc == chong && ((terminal1 && !terminal2) || (!terminal1 && terminal2))) {
 return true;
 }
 count = 1;
 terminal1 = false;
 terminal2 = false;
 /**
 *
 * 左上右下
 *
 */
 for (i = 1; i < num; ++i) {
 if ((x + i) < board_size && (y + i) < board_size) {
 if (board[x + i][y + i] == f && rdown)
 count++;
 else {
 if (board[x + i][y + i] == 0 && rdown) {
 terminal1 = true;
 }
 rdown = false;
 }
 }
 if ((x - i) >= 0 && (y - i) >= 0) {
 if (board[x - i][y - i] == f && lup)
 count++;
 else {
 if (board[x - i][y - i] == 0 && lup) {
 terminal2 = true;
 }
 lup = false;
 }
 }
 }
 if (count == num - 1 && horc == huo && terminal1 && terminal2) {
 return true;
 }
 if (count == num - 1 && horc == chong && ((terminal1 && !terminal2) || (!terminal1 && terminal2))) {
 return true;
 }
 count = 1;
 terminal1 = false;
 terminal2 = false;
 /**
 *
 * 右上左下
 *
 */
 for (i = 1; i < num; ++i) {
 if ((x + i) < board_size && (y - i) >= 0) {
 if (board[x + i][y - i] == f && rup)
 count++;
 else {
 if (board[x + i][y - i] == 0 && rup) {
 terminal1 = true;
 }
 rup = false;
 }
 }
 if ((x - i) >= 0 && (y + i) < board_size) {
 if (board[x - i][y + i] == f && ldown)
 count++;
 else {
 if (board[x - i][y + i] == 0 && ldown) {
 terminal2 = true;
 }
 ldown = false;
 }
 }
 }
 if (count == num - 1 && horc == huo && terminal1 && terminal2) {
 return true;
 }
 if (count == num - 1 && horc == chong && ((terminal1 && !terminal2) || (!terminal1 && terminal2))) {
 return true;
 }
 
 return false;
 }
 
 public void init() throws exception {
 table = imageio.read(new file("image/board.jpg"));
 black = imageio.read(new file("image/black.gif"));
 white = imageio.read(new file("image/white.gif"));
 selected = imageio.read(new file("image/selected.gif"));
 
 for (int i = 0; i < board_size; ++i) {
 for (int j = 0; j < board_size; ++j) {
 board[i][j] = 0;
 computerscore[i][j] = 0;
 }
 }
 chessboard.setpreferredsize(new dimension(table_width, table_height));
 
 chessboard.addmouselistener(new mouseadapter() {
 public void mouseclicked(mouseevent e) {
 int xpos = (int) ((e.getx() - x_offset) / rate);
 int ypos = (int) ((e.gety() - y_offset) / rate);
 // system.out.println("1 " + xpos + " " + ypos);
 if (isrun(xpos, ypos)) {
 flaggamer = iswin(1, xpos, ypos);
 board[xpos][ypos] = 1;
 chesscou++;
 // do //電腦下棋,隨機
 // {
 // comx = (rand.nextint(535) - x_offset) / rate;
 // comy = (rand.nextint(536) - y_offset) / rate;
 // } while (!isrun(comx, comy));
 if (chesscou == (board_size * board_size)) {
 joptionpane.showmessagedialog(null, "不相上下!!!\n再來一盤吧!!!", "結(jié)束", joptionpane.error_message);
 initto();
 } else {
 computer_ai(); // 電腦下棋,ai算法
 chesscou++;
 // system.out.println("2 " + comx + " " + comy);
 flagcomputer = iswin(2, comx, comy);
 board[comx][comy] = 2;
 computerx = comx;
 computery = comy;
 }
 }
 chessboard.repaint();
 if (flaggamer) {
 joptionpane.showmessagedialog(null, "厲害厲害!!!\n你贏了!!!", "結(jié)束", joptionpane.error_message);
 initto();
 } else if (flagcomputer) {
 joptionpane.showmessagedialog(null, "哈哈哈哈!!!\n你輸了!!!", "結(jié)束", joptionpane.error_message);
 initto();
 }
 }
 
 public void mouseexited(mouseevent e) {
 selectedx = -1;
 selectedy = -1;
 chessboard.repaint();
 }
 });
 chessboard.addmousemotionlistener(new mousemotionadapter() {
 public void mousemoved(mouseevent e) {
 selectedx = (e.getx() - x_offset) / rate;
 selectedy = (e.gety() - y_offset) / rate;
 
 chessboard.repaint();
 }
 });
 f.add(chessboard);
 f.setcursor(new cursor(cursor.hand_cursor));
 f.setdefaultcloseoperation(jframe.exit_on_close);
 f.setresizable(false);
 f.pack();
 f.setvisible(true);
 }
 
 public static void main(string[] args) throws exception {
 game game = new game();
 game.init();
 }
 
 @suppresswarnings("serial")
 class chessboard extends jpanel {
 public void paint(graphics g) {
 g.drawimage(table, 0, 0, null);
 if (selectedx >= 0 && selectedy >= 0) {
 g.drawimage(selected, selectedx * rate + x_offset, selectedy * rate + y_offset, null);
 }
 if (computerx >= 0 && computery >= 0) {
 g.drawimage(selected, computerx * rate + x_offset, computery * rate + y_offset, null);
 }
 for (int i = 0; i < board_size; ++i) {
 for (int j = 0; j < board_size; ++j) {
 if (board[i][j] == 1) {
 g.drawimage(black, i * rate + x_offset, j * rate + y_offset, null);
 }
 if (board[i][j] == 2) {
 g.drawimage(white, i * rate + x_offset, j * rate + y_offset, null);
 }
 }
 }
 }
 }
}
 
class chessxy {
 int x;
 int y;
 
 public chessxy(int x, int y) {
 this.x = x;
 this.y = y;
 }
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。

原文鏈接:https://blog.csdn.net/a249900679/article/details/51052103

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 男人狂躁女人gif动态图 | 国产九九在线 | 精品成人片深夜 | 国产卡一卡二卡四卡无卡 | 天美影视传媒mv直接看 | 5555国产在线观看精品 | 亚洲人成网站在线观看妞妞网 | 国产在线观看色 | 午夜私人福利影院 | 国产大片51精品免费观看 | 国产欧美在线播放 | 成年女人免费 | 动漫人物差差插曲漫画 | 日本道三区播放区 | 亚洲精品乱码久久久久久蜜桃图片 | 日日爱669| 韩国激情网 | 婷综合| 16男男gaygays | 亚洲精品高清中文字幕完整版 | 男人把大ji巴放进男人免费视频 | 欧美一区二区三区久久久 | 国产精品制服丝袜白丝www | 草草国产成人免费视频 | 美女脱了内裤让男生尿囗 | 日本aa大片在线播放免费看 | 亚洲va欧美va国产综合久久 | 4455四色永久免费 | 九九精品免费视频 | 女女同性做爰xxoo亲吻 | 国产18在线| 日本暖暖在线视频 | 77色视频在线 | 国产婷婷成人久久av免费高清 | 天堂欧美 | 国产一成人精品福利网站 | 513热点网深夜影院影院诶 | 蝴蝶传媒免费安装 | 小妇人电影免费完整观看2021 | 天堂8在线天堂资源在线 | 国产高清日韩 |