本文实例为大家分享了java五子棋游戏的具体代码,供大家参考,具体内容如下
ai思路:通过判断棋盘上每个空位的分数,去分数最高的几个点,随机下棋
分数计算思路:能成5个说明能赢了,给最高分
不能成5个,对方能成5个,说明对方要赢了,给第二高分
能成活4,给第三高分
能成活3,给第四高分
能成冲4,给第五高分
能成冲3,给第六高分
能成活2,给第七高分
能成冲2,给第八高分
其他,给最低分
分数设定可自己定义。
因为是去年写的了,思路记得大概就是这样。最近根据书上写了个棋类游戏的设计框架,待完善后再发上来,应该会更新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;
/**
* 棋子个数
*/
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分数
*/
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 ; // 电脑最大分数
private static int comx, comy; // 玩家下子坐标
private final int huo = 1 ;
private final int chong = 2 ;
private static int chesscou = 0 ;
/**
* 记录找到的分数一样的棋子,随机下这些棋子中的一个,以防步法固定
*/
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
* 坐标 @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()); // 电脑根据分值一样的点随机走,防止每次都走相同的步数
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)) // 电脑能赢,故给分最高,因为可以结束,所以不再检测
{
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再来一盘吧!!!" , "结束" , 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你赢了!!!" , "结束" , joptionpane.error_message);
initto();
} else if (flagcomputer) {
joptionpane.showmessagedialog( null , "哈哈哈哈!!!\n你输了!!!" , "结束" , 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;
}
}
|
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/a249900679/article/details/51052103