Java做的五子棋
两年前做的,程序在玩家胜利后会有一点问题,有些棋子还在原来的位置上,那时候没找出来是什么原因,现在也不想管它了,拿上来也许对有些朋友有用。不过要事先说明的是:要在j2sdk1.4.0下才能编译通过,在更高的版本上就要修改下程序才行,这可不是我的错,是java的版本兼容有问题;最好用个工具编译执行它,如:EditPlus(它虽然是个文本编辑工具,但可以编译执行源程序的,要在工具菜单参数选择里设置下,当然要有java的sdk了),如果直接在dos下执行可能图片加载不上来。
两年前做的,程序在玩家胜利后会有一点问题,有些棋子还在原来的位置上,那时候没找出来是什么原因,现在也不想管它了,拿上来也许对有些朋友有用。不过要事先说明的是:要在j2sdk1.4.0下才能编译通过,在更高的版本上就要修改下程序才行,这可不是我的错,是java的版本兼容有问题;最好用个工具编译执行它,如:EditPlus(它虽然是个文本编辑工具,但可以编译执行源程序的,要在工具菜单参数选择里设置下,当然要有java的sdk了),如果直接在dos下执行可能图片加载不上来。
与本博客中的用flash实现的五子棋的实现原理不同,这个要简单很多,这个10×10的,标准应该是15×15。
贴个图(比较丑,不过我想你关心的是程序实现原理。):
再来这里下载源码:java实现五子棋源码
同样把代码贴上:
展开查看源码
1import javax.swing.*;
2import java.awt.*;
3import java.awt.event.*;
4import java.util.*;
5import javax.swing.event.*;
6
7public class wuziqi
8{
9 public static void main(String[] args)
10 {
11 SimpleFrame frame=new SimpleFrame();
12 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
13 frame.show();
14 }
15}
16
17class AboutFrame extends JFrame
18{
19 public AboutFrame()
20 {
21 setSize(400,300);
22 setResizable(false);
23 setTitle("关于");
24 Toolkit tk=Toolkit.getDefaultToolkit();
25 Image ff=tk.getImage("false.gif");
26 setIconImage(ff);
27 Dimension screenSize=tk.getScreenSize();
28 setLocation((screenSize.width-400)/2,(screenSize.height-300)/2);
29 FontPanel fpanel=new FontPanel();
30 Container con=getContentPane();
31 con.add(fpanel);
32 }
33}
34class FontPanel extends JPanel
35{
36 public void paintComponent(Graphics g)
37 {
38 super.paintComponent(g);
39 Font f=new Font("宋体",Font.BOLD,22);
40 g.setFont(f);
41 g.drawString("五子棋)",10,30);
42 f=new Font("楷体_GB2312",Font.PLAIN,18);
43 g.setFont(f);
44 g.drawString("设计者:Rick carter",10,120);
45 g.drawString("本程序仅供大家参考及学习,",10,225);
46 g.drawString("未经本人允许请勿用于任何商业活动!",10,250);
47 }
48}
49
50class SimpleFrame extends JFrame
51{
52 public SimpleFrame()
53 {
54 setSize(WIDTH,HEIGHT);
55 setResizable(false);
56 setTitle("五子棋(10*10)");
57 Toolkit tk=Toolkit.getDefaultToolkit();
58 Image img=tk.getImage("map.gif");
59 setIconImage(img);
60 Dimension screenSize=tk.getScreenSize();
61 setLocation((screenSize.width-WIDTH)/2,(screenSize.height-HEIGHT)/2);
62
63 final AboutFrame aboutFrame=new AboutFrame();
64
65 ImagePanel panel=new ImagePanel();
66 Container contentPane=getContentPane();
67 contentPane.add(panel);
68
69 JMenuBar menuBar=new JMenuBar();
70 setJMenuBar(menuBar);
71
72 JMenu gameMenu=new JMenu("游戏(G)"); //创建游戏菜单
73 gameMenu.setMnemonic('G');
74
75 //创建游戏子菜单及监听器
76 JMenuItem replayItem=new JMenuItem("重玩",'R');
77 replayItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_R,InputEvent.CTRL_MASK));
78 replayItem.addActionListener(new AbstractAction("重玩")
79 {
80 public void actionPerformed(ActionEvent event)
81 {
82 ImagePanel.restart();
83 }
84 } );
85 JMenuItem optionItem=new JMenuItem("选项",'O');
86 optionItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,InputEvent.CTRL_MASK));
87 optionItem.addActionListener(new AbstractAction("选项")
88 {
89 public void actionPerformed(ActionEvent event)
90 {
91
92 }
93 } );
94
95
96 JMenuItem exitItem=new JMenuItem("退出",'E');
97 exitItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_E,InputEvent.CTRL_MASK));
98 exitItem.addActionListener(new AbstractAction("退出")
99 {
100 public void actionPerformed(ActionEvent event)
101 {
102 System.exit(0);
103 }
104 } );
105 gameMenu.add(replayItem);
106 gameMenu.add(optionItem);
107 gameMenu.addSeparator();
108 gameMenu.add(exitItem);
109 menuBar.add(gameMenu);
110
111 JMenu helpMenu=new JMenu("帮助(H)"); //创建帮助菜单
112 helpMenu.setMnemonic('H');
113 //创建帮助子菜单及监听器
114 JMenuItem aboutItem=new JMenuItem("关于",'A');
115 aboutItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A,InputEvent.CTRL_MASK));
116 aboutItem.addActionListener(new AbstractAction("关于")
117 {
118 public void actionPerformed(ActionEvent event)
119 {
120 aboutFrame.show();
121 }
122 });
123 helpMenu.add(aboutItem);
124 menuBar.add(helpMenu);
125
126 }
127 public static final int WIDTH=560;
128 public static final int HEIGHT=450;
129}
130
131final class ImagePanel extends JPanel
132{
133 public ImagePanel()
134 {
135 image=Toolkit.getDefaultToolkit().getImage("map.gif");
136 green=Toolkit.getDefaultToolkit().getImage("green.gif");
137 red=Toolkit.getDefaultToolkit().getImage("red.gif");
138 position=Toolkit.getDefaultToolkit().getImage("position.gif");
139 ired=Toolkit.getDefaultToolkit().getImage("ired.gif");
140 MediaTracker tracker=new MediaTracker(this);
141 tracker.addImage(image,1); //棋盘
142 tracker.addImage(green,2); //绿棋子
143 tracker.addImage(red,3); //红棋子
144 tracker.addImage(position,4); //鼠标位置
145 tracker.addImage(ired,5);
146 try
147 {
148 tracker.waitForID(1);
149 tracker.waitForID(2);
150 tracker.waitForID(3);
151 tracker.waitForID(4);
152 tracker.waitForID(5);
153 }
154 catch(InterruptedException exception)
155 {}
156
157 //注册监听器
158 addMouseListener(new MouseHandler());
159 addMouseMotionListener(new MouseMotionHandler());
160
161 for(i=0;i<10;i++) //初始化棋盘状态
162 for(j=0;j<10;j++)
163 ball[i][j] = 0;
164 for(i=0;i<10;i++) //设定水平方向的获胜位置
165 for(j=0;j<6;j++)
166 {
167 for(k=0;k<5;k++)
168 {
169 ptable[j+k][i][count] = true;
170 ctable[j+k][i][count] = true;
171 }
172 count++;
173 }
174 for(i=0;i<10;i++) //设定垂直方向的获胜位置
175 for(j=0;j<6;j++)
176 {
177 for(k=0;k<5;k++)
178 {
179 ptable[i][j+k][count] = true;
180 ctable[i][j+k][count] = true;
181 }
182 count++;
183 }
184 for(i=0;i<6;i++) //设定正对角线方向的获胜位置
185 for(j=0;j<6;j++)
186 {
187 for(k=0;k<5;k++)
188 {
189 ptable[j+k][i+k][count] = true;
190 ctable[j+k][i+k][count] = true;
191 }
192 count++;
193 }
194 for(i=0;i<6;i++) //设定反对角线方向的获胜位置
195 for(j=9;j>=4;j--)
196 {
197 for(k=0;k<5;k++)
198 {
199 ptable[j-k][i+k][count] = true;
200 ctable[j-k][i+k][count] = true;
201 }
202 count++;
203 }
204 computer = false;
205 player = true;
206 }
207
208 //绘制窗体面板
209 public void paintComponent(Graphics g)
210 {
211 super.paintComponent(g);
212 int FrameWidth=getWidth();
213 int FrameHeight=getHeight();
214 int x=(FrameWidth-400)/2;
215 int y=FrameHeight-400;
216 int px=mousex-(mousex-x)%40;
217 int py=mousey-(mousey-y)%40;
218 if(px<x)
219 px=x;
220 if(py<y)
221 py=y;
222 if(px>(x+360))
223 px=x+360;
224 if(py>(y+360))
225 py=y+360;
226 g.drawImage(image,x,y,null);
227 g.drawImage(position,px,py,null);
228 Font f=new Font("楷体_GB2312",Font.BOLD,20);
229 g.setFont(f);
230 g.setColor(new Color(204,66,204));
231 g.drawString("计算机",5,60);
232 g.setColor(new Color(00,99,00));
233 g.drawString("玩家",495,60);
234 g.drawImage(red,20,75,null);
235 g.drawImage(green,500,75,null);
236 g.setColor(Color.black);
237 switch(str)
238 {
239 case 0:
240 g.drawString("你先下!",480,140);
241 break;
242 case 1:
243 g.drawString("等待!",480,140);
244 break;
245 case 2:
246 g.drawString("该你了.",480,140);
247 break;
248 case 3:
249 g.drawString("你嬴了!",480,140);
250 break;
251 case 4:
252 g.drawString("你输了!",480,140);
253 break;
254 case 5:
255 g.drawString("平 局!",480,140);
256 break;
257 }
258 for(i=0;i<10;i++)
259 for(j=0;j<10;j++)
260 {
261 if(ball[i][j]==1)
262 {
263 px=i*40+3+x;
264 py=j*40+3+y;
265 g.drawImage(green,px,py,null);
266 }
267 if(ball[i][j]==2)
268 {
269 px=i*40+3+x;
270 py=j*40+3+y;
271 g.drawImage(red,px,py,null);
272 }
273 }
274 g.drawImage(ired,a*40+x+3,b*40+y+3,null);
275 }
276
277 //变量
278 private Image image;
279 private Image position;
280 private Image green;
281 private Image red;
282 private Image ired;
283 private int mousex,mousey;
284 private JButton replayButton;
285
286 private static int ball[][]=new int [10][10];
287 private static boolean ptable[][][]=new boolean [10][10][192],ctable[][][]=new boolean [10][10][192];
288 private static int pgrades[][]=new int [10][10],cgrades[][]=new int [10][10],cgrade,pgrade;
289 private static int win[][]=new int [2][192];
290 private static int pcount,ccount; //记录计算机与玩家各下了多少棋子
291 private static boolean player,computer,start=true,over=false,pwin,cwin,tie;
292 private static int i,j,k,n,m,count,a=50,b=50;
293 private static int mat,nat,mde,nde;
294 private static int str=0;
295
296 private class replayAction implements ActionListener
297 {
298 public void actionPerformed(ActionEvent event)
299 {
300 restart();
301 }
302 }
303
304 //鼠标移动事件
305 private class MouseMotionHandler implements MouseMotionListener
306 {
307 public void mouseMoved(MouseEvent event)
308 {
309 mousex=event.getX();
310 mousey=event.getY();
311 repaint();
312 }
313 public void mouseDragged(MouseEvent event) {}
314 }
315
316 //鼠标点击事件
317 private class MouseHandler extends MouseAdapter
318 {
319 public void mousePressed(MouseEvent event)
320 {
321 mousex=event.getX();
322 mousey=event.getY();
323
324 if(!over)
325 if(player)
326 {
327 int FrameWidth=getWidth();
328 int FrameHeight=getHeight();
329 int x=(FrameWidth-400)/2;
330 int y=FrameHeight-400-5;
331 if(mousex>x&&mousex<(x+400)&&mousey>y&&mousey<(y+400))
332 { m=(int)((mousex-x)/40);
333 n=(int)((mousey-y)/40);
334 while(ball[m][n]==0) //如果该棋子为空
335 { ball[m][n]=1; //设为玩家的棋子
336 pcount++;
337 str=1;
338 if((ccount==50)&&(pcount==50))
339 { tie=true;
340 over=true;
341 str=5;
342 }
343 for(i=0;i<192;i++)
344 {
345 if(ptable[m][n][i]&&win[0][i]!=7)
346 win[0][i]++;
347 if(ctable[m][n][i])
348 {
349 ctable[m][n][i]=false;
350 win[1][i]=7; //计算机在第i种获胜可能中不可能再获胜,就置为7
351 }
352 }
353 if(!over)
354 {
355 for(i=0;i<=1;i++)
356 for(j=0;j<192;j++)
357 {
358 if(win[i][j] == 5)
359 if(i==0) //玩家获胜
360 {
361 pwin=true;
362 cwin=false;
363 over=true;
364 str=3;
365 break;
366 }
367 else //计算机获胜
368 {
369 cwin=true;
370 pwin=false;
371 over=true;
372 str=4;
373 break;
374 }
375 if(over)
376 break;
377 }
378 }
379
380 player=false;
381 computer=true;
382 repaint();
383 if(over)
384 {
385 if(tie)
386 JOptionPane.showConfirmDialog(
387 ImagePanel.this,
388 "平局!","确认信息",
389 JOptionPane.DEFAULT_OPTION,
390 JOptionPane.INFORMATION_MESSAGE);
391 else
392 if(pwin)
393 JOptionPane.showConfirmDialog(
394 ImagePanel.this,
395 "恭喜你!你嬴了。","确认信息",
396 JOptionPane.DEFAULT_OPTION,
397 JOptionPane.INFORMATION_MESSAGE);
398 else
399 if(cwin)
400 JOptionPane.showConfirmDialog(
401 ImagePanel.this,
402 "你输了!","确认信息",
403 JOptionPane.DEFAULT_OPTION,
404 JOptionPane.INFORMATION_MESSAGE);
405 restart();
406
407 }
408 }
409 }
410 }
411 //电脑下
412 computerdo();
413 }
414 }
415
416 public void computerdo()
417 {
418 if(!over)
419 if(computer)
420 {
421 for(i=0;i<=9;i++) //计算玩家在空格子上的获胜分数
422 for(j=0;j<=9;j++)
423 {
424 pgrades[i][j]=0;
425 if(ball[i][j]==0)
426 for(k=0;k<192;k++)
427 if(ptable[i][j][k])
428 {
429 switch(win[0][k])
430 {
431 case 1:
432 pgrades[i][j]+=5;
433 break;
434 case 2:
435 pgrades[i][j]+=50;
436 break;
437 case 3:
438 pgrades[i][j]+=100;
439 break;
440 case 4:
441 pgrades[i][j]+=400;
442 break;
443 }
444 }
445 }
446 for(i=0;i<=9;i++) //计算计算机在空格子上的获胜分数
447 for(j=0;j<=9;j++)
448 {
449 cgrades[i][j]=0;
450 if(ball[i][j]==0)
451 for(k=0;k<192;k++)
452 if(ctable[i][j][k])
453 {
454 switch(win[1][k])
455 {
456 case 1:
457 cgrades[i][j]+=5;
458 break;
459 case 2:
460 cgrades[i][j]+=50;
461 break;
462 case 3:
463 cgrades[i][j]+=100;
464 break;
465 case 4:
466 cgrades[i][j]+=400;
467 break;
468 }
469 }
470 }
471 if(start) //游戏开始
472 {
473 if(ball[4][4]==0)
474 {
475 m = 4;
476 n = 4;
477 }
478 else
479 {
480 m = 5;
481 n = 5;
482 }
483 start = false;
484 }
485 else
486 {
487 for(i=0;i<10;i++)
488 for(j=0;j<10;j++)
489 if(ball[i][j]==0)
490 {
491 if(cgrades[i][j]>=cgrade)
492 {
493 cgrade = cgrades[i][j];
494 mat = i;
495 nat = j;
496 }
497 if(pgrades[i][j]>=pgrade)
498 {
499 pgrade = pgrades[i][j];
500 mde = i;
501 nde = j;
502 }
503 }
504 if(pgrade>=cgrade&&pgrade>100) //防守
505 {
506 m = mde;
507 n = nde;
508 }
509 else //攻击
510 {
511 m = mat;
512 n = nat;
513 }
514 }
515 cgrade = 0;
516 pgrade = 0;
517 ball[m][n] = 2; //设定为计算机的棋子
518 a=m;
519 b=n;
520 ccount++;
521 str=2;
522 if((ccount==50)&&(pcount==50))
523 {
524 tie = true;
525 over = true;
526 }
527 for(i=0;i<192;i++)
528 {
529 if(ctable[m][n][i] && win[1][i] != 7)
530 win[1][i]++;
531 if(ptable[m][n][i])
532 {
533 ptable[m][n][i] = false;
534 win[0][i]=7;
535 }
536 }
537 if(!over)
538 {
539 for(i=0;i<=1;i++)
540 for(j=0;j<192;j++)
541 {
542 if(win[i][j]==5)
543 if(i==0) //玩家获胜
544 {
545 pwin=true;
546 cwin=false;
547 over=true;
548 str=3;
549 break;
550 }
551 else //计算机获胜
552 {
553 cwin=true;
554 pwin=false;
555 over=true;
556 str=4;
557 break;
558 }
559 if(over)
560 break;
561 }
562 }
563 player=true; //换玩家下
564 computer=false;
565 repaint();
566 if(over)
567 {
568 if(tie)
569 JOptionPane.showConfirmDialog(
570 ImagePanel.this,
571 "平局!","确认信息",
572 JOptionPane.DEFAULT_OPTION,
573 JOptionPane.INFORMATION_MESSAGE);
574 else
575 if(pwin)
576 JOptionPane.showConfirmDialog(
577 ImagePanel.this,
578 "恭喜你!你嬴了。","确认信息",
579 JOptionPane.DEFAULT_OPTION,
580 JOptionPane.INFORMATION_MESSAGE);
581 else
582 if(cwin)
583 JOptionPane.showConfirmDialog(
584 ImagePanel.this,
585 "你输了!","确认信息",
586 JOptionPane.DEFAULT_OPTION,
587 JOptionPane.INFORMATION_MESSAGE);
588 restart();
589
590 }
591 }
592 }
593
594 public static void restart()
595 {
596 for(i=0;i<10;i++)
597 for(j=0;j<10;j++)
598 {
599 pgrades[i][j]=0;
600 cgrades[i][j]=0;
601 ball[i][j]=0;
602 }
603 count=0;
604 for(i=0;i<10;i++) //设定水平方向的获胜位置
605 for(j=0;j<6;j++)
606 {
607 for(k=0;k<5;k++)
608 {
609 ptable[j+k][i][count] = true;
610 ctable[j+k][i][count] = true;
611 }
612 count++;
613 }
614 for(i=0;i<10;i++) //设定垂直方向的获胜位置
615 for(j=0;j<6;j++)
616 {
617 for(k=0;k<5;k++)
618 {
619 ptable[i][j+k][count] = true;
620 ctable[i][j+k][count] = true;
621 }
622 count++;
623 }
624 for(i=0;i<6;i++) //设定正对角线方向的获胜位置
625 for(j=0;j<6;j++)
626 {
627 for(k=0;k<5;k++)
628 {
629 ptable[j+k][i+k][count] = true;
630 ctable[j+k][i+k][count] = true;
631 }
632 count++;
633 }
634 for(i=0;i<6;i++) //设定反对角线方向的获胜位置
635 for(j=9;j>=4;j--)
636 {
637 for(k=0;k<5;k++)
638 {
639 ptable[j-k][i+k][count] = true;
640 ctable[j-k][i+k][count] = true;
641 }
642 count++;
643 }
644 for(i=0;i<=1;i++)
645 for(j=0;j<192;j++)
646 win[i][j]=0;
647 computer=false;
648 player=true;
649 str=0;
650 count=0;
651 ccount=0;
652 pcount=0;
653 start=true;
654 over=false;
655 pwin=false;
656 cwin=false;
657 tie=false;
658 a=b=50;
659 }
660}
1import javax.swing.*;
2import java.awt.*;
3import java.awt.event.*;
4import java.util.*;
5import javax.swing.event.*;
6
7public class wuziqi
8{
9 public static void main(String[] args)
10 {
11 SimpleFrame frame=new SimpleFrame();
12 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
13 frame.show();
14 }
15}
16
17class AboutFrame extends JFrame
18{
19 public AboutFrame()
20 {
21 setSize(400,300);
22 setResizable(false);
23 setTitle("关于");
24 Toolkit tk=Toolkit.getDefaultToolkit();
25 Image ff=tk.getImage("false.gif");
26 setIconImage(ff);
27 Dimension screenSize=tk.getScreenSize();
28 setLocation((screenSize.width-400)/2,(screenSize.height-300)/2);
29 FontPanel fpanel=new FontPanel();
30 Container con=getContentPane();
31 con.add(fpanel);
32 }
33}
34class FontPanel extends JPanel
35{
36 public void paintComponent(Graphics g)
37 {
38 super.paintComponent(g);
39 Font f=new Font("宋体",Font.BOLD,22);
40 g.setFont(f);
41 g.drawString("五子棋)",10,30);
42 f=new Font("楷体_GB2312",Font.PLAIN,18);
43 g.setFont(f);
44 g.drawString("设计者:Rick carter",10,120);
45 g.drawString("本程序仅供大家参考及学习,",10,225);
46 g.drawString("未经本人允许请勿用于任何商业活动!",10,250);
47 }
48}
49
50class SimpleFrame extends JFrame
51{
52 public SimpleFrame()
53 {
54 setSize(WIDTH,HEIGHT);
55 setResizable(false);
56 setTitle("五子棋(10*10)");
57 Toolkit tk=Toolkit.getDefaultToolkit();
58 Image img=tk.getImage("map.gif");
59 setIconImage(img);
60 Dimension screenSize=tk.getScreenSize();
61 setLocation((screenSize.width-WIDTH)/2,(screenSize.height-HEIGHT)/2);
62
63 final AboutFrame aboutFrame=new AboutFrame();
64
65 ImagePanel panel=new ImagePanel();
66 Container contentPane=getContentPane();
67 contentPane.add(panel);
68
69 JMenuBar menuBar=new JMenuBar();
70 setJMenuBar(menuBar);
71
72 JMenu gameMenu=new JMenu("游戏(G)"); //创建游戏菜单
73 gameMenu.setMnemonic('G');
74
75 //创建游戏子菜单及监听器
76 JMenuItem replayItem=new JMenuItem("重玩",'R');
77 replayItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_R,InputEvent.CTRL_MASK));
78 replayItem.addActionListener(new AbstractAction("重玩")
79 {
80 public void actionPerformed(ActionEvent event)
81 {
82 ImagePanel.restart();
83 }
84 } );
85 JMenuItem optionItem=new JMenuItem("选项",'O');
86 optionItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,InputEvent.CTRL_MASK));
87 optionItem.addActionListener(new AbstractAction("选项")
88 {
89 public void actionPerformed(ActionEvent event)
90 {
91
92 }
93 } );
94
95
96 JMenuItem exitItem=new JMenuItem("退出",'E');
97 exitItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_E,InputEvent.CTRL_MASK));
98 exitItem.addActionListener(new AbstractAction("退出")
99 {
100 public void actionPerformed(ActionEvent event)
101 {
102 System.exit(0);
103 }
104 } );
105 gameMenu.add(replayItem);
106 gameMenu.add(optionItem);
107 gameMenu.addSeparator();
108 gameMenu.add(exitItem);
109 menuBar.add(gameMenu);
110
111 JMenu helpMenu=new JMenu("帮助(H)"); //创建帮助菜单
112 helpMenu.setMnemonic('H');
113 //创建帮助子菜单及监听器
114 JMenuItem aboutItem=new JMenuItem("关于",'A');
115 aboutItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A,InputEvent.CTRL_MASK));
116 aboutItem.addActionListener(new AbstractAction("关于")
117 {
118 public void actionPerformed(ActionEvent event)
119 {
120 aboutFrame.show();
121 }
122 });
123 helpMenu.add(aboutItem);
124 menuBar.add(helpMenu);
125
126 }
127 public static final int WIDTH=560;
128 public static final int HEIGHT=450;
129}
130
131final class ImagePanel extends JPanel
132{
133 public ImagePanel()
134 {
135 image=Toolkit.getDefaultToolkit().getImage("map.gif");
136 green=Toolkit.getDefaultToolkit().getImage("green.gif");
137 red=Toolkit.getDefaultToolkit().getImage("red.gif");
138 position=Toolkit.getDefaultToolkit().getImage("position.gif");
139 ired=Toolkit.getDefaultToolkit().getImage("ired.gif");
140 MediaTracker tracker=new MediaTracker(this);
141 tracker.addImage(image,1); //棋盘
142 tracker.addImage(green,2); //绿棋子
143 tracker.addImage(red,3); //红棋子
144 tracker.addImage(position,4); //鼠标位置
145 tracker.addImage(ired,5);
146 try
147 {
148 tracker.waitForID(1);
149 tracker.waitForID(2);
150 tracker.waitForID(3);
151 tracker.waitForID(4);
152 tracker.waitForID(5);
153 }
154 catch(InterruptedException exception)
155 {}
156
157 //注册监听器
158 addMouseListener(new MouseHandler());
159 addMouseMotionListener(new MouseMotionHandler());
160
161 for(i=0;i<10;i++) //初始化棋盘状态
162 for(j=0;j<10;j++)
163 ball[i][j] = 0;
164 for(i=0;i<10;i++) //设定水平方向的获胜位置
165 for(j=0;j<6;j++)
166 {
167 for(k=0;k<5;k++)
168 {
169 ptable[j+k][i][count] = true;
170 ctable[j+k][i][count] = true;
171 }
172 count++;
173 }
174 for(i=0;i<10;i++) //设定垂直方向的获胜位置
175 for(j=0;j<6;j++)
176 {
177 for(k=0;k<5;k++)
178 {
179 ptable[i][j+k][count] = true;
180 ctable[i][j+k][count] = true;
181 }
182 count++;
183 }
184 for(i=0;i<6;i++) //设定正对角线方向的获胜位置
185 for(j=0;j<6;j++)
186 {
187 for(k=0;k<5;k++)
188 {
189 ptable[j+k][i+k][count] = true;
190 ctable[j+k][i+k][count] = true;
191 }
192 count++;
193 }
194 for(i=0;i<6;i++) //设定反对角线方向的获胜位置
195 for(j=9;j>=4;j--)
196 {
197 for(k=0;k<5;k++)
198 {
199 ptable[j-k][i+k][count] = true;
200 ctable[j-k][i+k][count] = true;
201 }
202 count++;
203 }
204 computer = false;
205 player = true;
206 }
207
208 //绘制窗体面板
209 public void paintComponent(Graphics g)
210 {
211 super.paintComponent(g);
212 int FrameWidth=getWidth();
213 int FrameHeight=getHeight();
214 int x=(FrameWidth-400)/2;
215 int y=FrameHeight-400;
216 int px=mousex-(mousex-x)%40;
217 int py=mousey-(mousey-y)%40;
218 if(px<x)
219 px=x;
220 if(py<y)
221 py=y;
222 if(px>(x+360))
223 px=x+360;
224 if(py>(y+360))
225 py=y+360;
226 g.drawImage(image,x,y,null);
227 g.drawImage(position,px,py,null);
228 Font f=new Font("楷体_GB2312",Font.BOLD,20);
229 g.setFont(f);
230 g.setColor(new Color(204,66,204));
231 g.drawString("计算机",5,60);
232 g.setColor(new Color(00,99,00));
233 g.drawString("玩家",495,60);
234 g.drawImage(red,20,75,null);
235 g.drawImage(green,500,75,null);
236 g.setColor(Color.black);
237 switch(str)
238 {
239 case 0:
240 g.drawString("你先下!",480,140);
241 break;
242 case 1:
243 g.drawString("等待!",480,140);
244 break;
245 case 2:
246 g.drawString("该你了.",480,140);
247 break;
248 case 3:
249 g.drawString("你嬴了!",480,140);
250 break;
251 case 4:
252 g.drawString("你输了!",480,140);
253 break;
254 case 5:
255 g.drawString("平 局!",480,140);
256 break;
257 }
258 for(i=0;i<10;i++)
259 for(j=0;j<10;j++)
260 {
261 if(ball[i][j]==1)
262 {
263 px=i*40+3+x;
264 py=j*40+3+y;
265 g.drawImage(green,px,py,null);
266 }
267 if(ball[i][j]==2)
268 {
269 px=i*40+3+x;
270 py=j*40+3+y;
271 g.drawImage(red,px,py,null);
272 }
273 }
274 g.drawImage(ired,a*40+x+3,b*40+y+3,null);
275 }
276
277 //变量
278 private Image image;
279 private Image position;
280 private Image green;
281 private Image red;
282 private Image ired;
283 private int mousex,mousey;
284 private JButton replayButton;
285
286 private static int ball[][]=new int [10][10];
287 private static boolean ptable[][][]=new boolean [10][10][192],ctable[][][]=new boolean [10][10][192];
288 private static int pgrades[][]=new int [10][10],cgrades[][]=new int [10][10],cgrade,pgrade;
289 private static int win[][]=new int [2][192];
290 private static int pcount,ccount; //记录计算机与玩家各下了多少棋子
291 private static boolean player,computer,start=true,over=false,pwin,cwin,tie;
292 private static int i,j,k,n,m,count,a=50,b=50;
293 private static int mat,nat,mde,nde;
294 private static int str=0;
295
296 private class replayAction implements ActionListener
297 {
298 public void actionPerformed(ActionEvent event)
299 {
300 restart();
301 }
302 }
303
304 //鼠标移动事件
305 private class MouseMotionHandler implements MouseMotionListener
306 {
307 public void mouseMoved(MouseEvent event)
308 {
309 mousex=event.getX();
310 mousey=event.getY();
311 repaint();
312 }
313 public void mouseDragged(MouseEvent event) {}
314 }
315
316 //鼠标点击事件
317 private class MouseHandler extends MouseAdapter
318 {
319 public void mousePressed(MouseEvent event)
320 {
321 mousex=event.getX();
322 mousey=event.getY();
323
324 if(!over)
325 if(player)
326 {
327 int FrameWidth=getWidth();
328 int FrameHeight=getHeight();
329 int x=(FrameWidth-400)/2;
330 int y=FrameHeight-400-5;
331 if(mousex>x&&mousex<(x+400)&&mousey>y&&mousey<(y+400))
332 { m=(int)((mousex-x)/40);
333 n=(int)((mousey-y)/40);
334 while(ball[m][n]==0) //如果该棋子为空
335 { ball[m][n]=1; //设为玩家的棋子
336 pcount++;
337 str=1;
338 if((ccount==50)&&(pcount==50))
339 { tie=true;
340 over=true;
341 str=5;
342 }
343 for(i=0;i<192;i++)
344 {
345 if(ptable[m][n][i]&&win[0][i]!=7)
346 win[0][i]++;
347 if(ctable[m][n][i])
348 {
349 ctable[m][n][i]=false;
350 win[1][i]=7; //计算机在第i种获胜可能中不可能再获胜,就置为7
351 }
352 }
353 if(!over)
354 {
355 for(i=0;i<=1;i++)
356 for(j=0;j<192;j++)
357 {
358 if(win[i][j] == 5)
359 if(i==0) //玩家获胜
360 {
361 pwin=true;
362 cwin=false;
363 over=true;
364 str=3;
365 break;
366 }
367 else //计算机获胜
368 {
369 cwin=true;
370 pwin=false;
371 over=true;
372 str=4;
373 break;
374 }
375 if(over)
376 break;
377 }
378 }
379
380 player=false;
381 computer=true;
382 repaint();
383 if(over)
384 {
385 if(tie)
386 JOptionPane.showConfirmDialog(
387 ImagePanel.this,
388 "平局!","确认信息",
389 JOptionPane.DEFAULT_OPTION,
390 JOptionPane.INFORMATION_MESSAGE);
391 else
392 if(pwin)
393 JOptionPane.showConfirmDialog(
394 ImagePanel.this,
395 "恭喜你!你嬴了。","确认信息",
396 JOptionPane.DEFAULT_OPTION,
397 JOptionPane.INFORMATION_MESSAGE);
398 else
399 if(cwin)
400 JOptionPane.showConfirmDialog(
401 ImagePanel.this,
402 "你输了!","确认信息",
403 JOptionPane.DEFAULT_OPTION,
404 JOptionPane.INFORMATION_MESSAGE);
405 restart();
406
407 }
408 }
409 }
410 }
411 //电脑下
412 computerdo();
413 }
414 }
415
416 public void computerdo()
417 {
418 if(!over)
419 if(computer)
420 {
421 for(i=0;i<=9;i++) //计算玩家在空格子上的获胜分数
422 for(j=0;j<=9;j++)
423 {
424 pgrades[i][j]=0;
425 if(ball[i][j]==0)
426 for(k=0;k<192;k++)
427 if(ptable[i][j][k])
428 {
429 switch(win[0][k])
430 {
431 case 1:
432 pgrades[i][j]+=5;
433 break;
434 case 2:
435 pgrades[i][j]+=50;
436 break;
437 case 3:
438 pgrades[i][j]+=100;
439 break;
440 case 4:
441 pgrades[i][j]+=400;
442 break;
443 }
444 }
445 }
446 for(i=0;i<=9;i++) //计算计算机在空格子上的获胜分数
447 for(j=0;j<=9;j++)
448 {
449 cgrades[i][j]=0;
450 if(ball[i][j]==0)
451 for(k=0;k<192;k++)
452 if(ctable[i][j][k])
453 {
454 switch(win[1][k])
455 {
456 case 1:
457 cgrades[i][j]+=5;
458 break;
459 case 2:
460 cgrades[i][j]+=50;
461 break;
462 case 3:
463 cgrades[i][j]+=100;
464 break;
465 case 4:
466 cgrades[i][j]+=400;
467 break;
468 }
469 }
470 }
471 if(start) //游戏开始
472 {
473 if(ball[4][4]==0)
474 {
475 m = 4;
476 n = 4;
477 }
478 else
479 {
480 m = 5;
481 n = 5;
482 }
483 start = false;
484 }
485 else
486 {
487 for(i=0;i<10;i++)
488 for(j=0;j<10;j++)
489 if(ball[i][j]==0)
490 {
491 if(cgrades[i][j]>=cgrade)
492 {
493 cgrade = cgrades[i][j];
494 mat = i;
495 nat = j;
496 }
497 if(pgrades[i][j]>=pgrade)
498 {
499 pgrade = pgrades[i][j];
500 mde = i;
501 nde = j;
502 }
503 }
504 if(pgrade>=cgrade&&pgrade>100) //防守
505 {
506 m = mde;
507 n = nde;
508 }
509 else //攻击
510 {
511 m = mat;
512 n = nat;
513 }
514 }
515 cgrade = 0;
516 pgrade = 0;
517 ball[m][n] = 2; //设定为计算机的棋子
518 a=m;
519 b=n;
520 ccount++;
521 str=2;
522 if((ccount==50)&&(pcount==50))
523 {
524 tie = true;
525 over = true;
526 }
527 for(i=0;i<192;i++)
528 {
529 if(ctable[m][n][i] && win[1][i] != 7)
530 win[1][i]++;
531 if(ptable[m][n][i])
532 {
533 ptable[m][n][i] = false;
534 win[0][i]=7;
535 }
536 }
537 if(!over)
538 {
539 for(i=0;i<=1;i++)
540 for(j=0;j<192;j++)
541 {
542 if(win[i][j]==5)
543 if(i==0) //玩家获胜
544 {
545 pwin=true;
546 cwin=false;
547 over=true;
548 str=3;
549 break;
550 }
551 else //计算机获胜
552 {
553 cwin=true;
554 pwin=false;
555 over=true;
556 str=4;
557 break;
558 }
559 if(over)
560 break;
561 }
562 }
563 player=true; //换玩家下
564 computer=false;
565 repaint();
566 if(over)
567 {
568 if(tie)
569 JOptionPane.showConfirmDialog(
570 ImagePanel.this,
571 "平局!","确认信息",
572 JOptionPane.DEFAULT_OPTION,
573 JOptionPane.INFORMATION_MESSAGE);
574 else
575 if(pwin)
576 JOptionPane.showConfirmDialog(
577 ImagePanel.this,
578 "恭喜你!你嬴了。","确认信息",
579 JOptionPane.DEFAULT_OPTION,
580 JOptionPane.INFORMATION_MESSAGE);
581 else
582 if(cwin)
583 JOptionPane.showConfirmDialog(
584 ImagePanel.this,
585 "你输了!","确认信息",
586 JOptionPane.DEFAULT_OPTION,
587 JOptionPane.INFORMATION_MESSAGE);
588 restart();
589
590 }
591 }
592 }
593
594 public static void restart()
595 {
596 for(i=0;i<10;i++)
597 for(j=0;j<10;j++)
598 {
599 pgrades[i][j]=0;
600 cgrades[i][j]=0;
601 ball[i][j]=0;
602 }
603 count=0;
604 for(i=0;i<10;i++) //设定水平方向的获胜位置
605 for(j=0;j<6;j++)
606 {
607 for(k=0;k<5;k++)
608 {
609 ptable[j+k][i][count] = true;
610 ctable[j+k][i][count] = true;
611 }
612 count++;
613 }
614 for(i=0;i<10;i++) //设定垂直方向的获胜位置
615 for(j=0;j<6;j++)
616 {
617 for(k=0;k<5;k++)
618 {
619 ptable[i][j+k][count] = true;
620 ctable[i][j+k][count] = true;
621 }
622 count++;
623 }
624 for(i=0;i<6;i++) //设定正对角线方向的获胜位置
625 for(j=0;j<6;j++)
626 {
627 for(k=0;k<5;k++)
628 {
629 ptable[j+k][i+k][count] = true;
630 ctable[j+k][i+k][count] = true;
631 }
632 count++;
633 }
634 for(i=0;i<6;i++) //设定反对角线方向的获胜位置
635 for(j=9;j>=4;j--)
636 {
637 for(k=0;k<5;k++)
638 {
639 ptable[j-k][i+k][count] = true;
640 ctable[j-k][i+k][count] = true;
641 }
642 count++;
643 }
644 for(i=0;i<=1;i++)
645 for(j=0;j<192;j++)
646 win[i][j]=0;
647 computer=false;
648 player=true;
649 str=0;
650 count=0;
651 ccount=0;
652 pcount=0;
653 start=true;
654 over=false;
655 pwin=false;
656 cwin=false;
657 tie=false;
658 a=b=50;
659 }
660}
作者:Rick Carter
出处:http://pains.cnblogs.com/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。