日历
| 日 | 一 | 二 | 三 | 四 | 五 | 六 |
---|
26 | 27 | 28 | 29 | 30 | 31 | 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 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
|
统计
- 随笔 - 2
- 文章 - 6
- 评论 - 5
- 引用 - 0
导航
常用链接
留言簿(1)
随笔档案
文章档案
搜索
最新评论
![](/images/xml.gif)
阅读排行榜
评论排行榜
|
用JAVA写的五子棋代码,没有AI,有检测禁手,永远是黑棋先手。以后有时间再去研究下AI了。
禁手检测没做的十分全面,若有读者发现有是禁手而没有检测出来,望在评论中说出 (*^__^*)
代码附有详细注释。
1 package game4;
2![](/Images/OutliningIndicators/None.gif)
3 import java.awt.*;
4 import java.awt.event.*;
5 import javax.swing.*;
6![](/Images/OutliningIndicators/None.gif)
7 class ChessFrame extends JFrame //整个五子棋框架类
8![](/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](/Images/OutliningIndicators/ContractedBlock.gif) {
9 JMenuBar jmb;
10 JMenu []jm=new JMenu[4];
11 JMenuItem []jm1jmi=new JMenuItem[3];
12 JRadioButtonMenuItem []jm2jrbmi=new JRadioButtonMenuItem[2];
13 ButtonGroup bg1;
14 JRadioButtonMenuItem []jm3jrbmi=new JRadioButtonMenuItem[3];
15 ButtonGroup bg2;
16 JMenuItem jm4jmi1;
17 public ChessFrame()
18![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
19 super("五子棋学习制作·未完成");
20 jmb = new JMenuBar();
21 jm[0] = new JMenu("游戏");
22 jm[1] = new JMenu("棋盘大小");
23 jm[2] = new JMenu("棋盘背景");
24 jm[3] = new JMenu("帮助");
25 jm1jmi[0] = new JMenuItem("游戏开始");
26 jm1jmi[1] = new JMenuItem("游戏重置");
27 jm1jmi[2] = new JMenuItem("游戏结束");
28 jm2jrbmi[0] = new JRadioButtonMenuItem("15 * 15",true);
29 jm2jrbmi[1] = new JRadioButtonMenuItem("19 * 19");
30 bg1 = new ButtonGroup();
31 jm3jrbmi[0] = new JRadioButtonMenuItem("红色",true);
32 jm3jrbmi[1] = new JRadioButtonMenuItem("橘黄色");
33 jm3jrbmi[2] = new JRadioButtonMenuItem("青色");
34 bg2 = new ButtonGroup();
35 jm4jmi1 = new JMenuItem("关于");
36 this.setLocation(255, 175);
37 this.setSize(500, 500);
38 this.setVisible(true);
39 this.setJMenuBar(jmb);
40![](/Images/OutliningIndicators/InBlock.gif)
41 bg1.add(jm2jrbmi[0]);
42 bg1.add(jm2jrbmi[1]);
43
44![](/Images/OutliningIndicators/InBlock.gif)
45 bg2.add(jm3jrbmi[0]);
46 bg2.add(jm3jrbmi[1]);
47 bg2.add(jm3jrbmi[2]);
48![](/Images/OutliningIndicators/InBlock.gif)
49 jm[0].add(jm1jmi[0]);
50 jm[0].add(jm1jmi[1]);
51 jm[0].add(jm1jmi[2]);
52![](/Images/OutliningIndicators/InBlock.gif)
53 jm[1].add(jm2jrbmi[0]);
54 jm[1].add(jm2jrbmi[1]);
55![](/Images/OutliningIndicators/InBlock.gif)
56 jm[2].add(jm3jrbmi[0]);
57 jm[2].add(jm3jrbmi[1]);
58 jm[2].add(jm3jrbmi[2]);
59![](/Images/OutliningIndicators/InBlock.gif)
60 jm[3].add(jm4jmi1);
61![](/Images/OutliningIndicators/InBlock.gif)
62 jmb.add(jm[0]);
63 jmb.add(jm[1]);
64 jmb.add(jm[2]);
65 jmb.add(jm[3]);
66![](/Images/OutliningIndicators/InBlock.gif)
67 this.addWindowListener(new WindowAdapter()
68![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
69 public void windowClosing(WindowEvent e)
70![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
71 dispose();
72 System.exit(0);
73 }
74 });
75 }
76 }
77![](/Images/OutliningIndicators/None.gif)
78 class ChessPanel extends JPanel //棋盘类
79![](/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](/Images/OutliningIndicators/ContractedBlock.gif) {
80 private int maxrows, maxcols; //最大行数,列数
81
82 private int [][]pointState; //棋盘上每个点的状态,0为没有子,1为黑,2为白
83
84 private int start; //初始为0,表示未开始,也表示开始后的结束,1表示开始
85
86 private Color frontChessColor; //记录上次棋子的颜色,初始为白色
87
88 private int unitSize =25; // 棋盘每个单元格的边大小
89
90 private int distanceX ;//棋盘左上起点 X 坐标
91
92 private int distanceY ;//棋盘左上起点 Y 坐标
93
94 private int fiveLine;//5连标记,初始为0;1表示是
95![](/Images/OutliningIndicators/InBlock.gif)
96 private int overline;//长连标记,初始为0;1表示是
97
98 public ChessPanel(int maxrows, int maxcols)
99![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
100 this.setLayout(null);
101 this.start=0;
102 this.frontChessColor=Color.WHITE;
103 this.maxrows = maxrows;
104 this.maxcols = maxcols;
105 this.setBackground(Color.RED);
106 this.setSize((maxrows+1)*unitSize, (maxcols+1)*unitSize);
107 initPointState();
108 initVicAOl();
109 }
110
111 public void initVicAOl() //初始化5连长连标记
112![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
113 this.fiveLine=0;
114 this.overline=0;
115 }
116
117 public int getFiveLine() //得到5连标记
118![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
119 return fiveLine;
120 }
121
122 public int getOverline() //得到长连标记
123![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
124 return overline;
125 }
126
127 public void initPointState() //初始化棋盘点
128![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
129 pointState=new int[maxrows][maxcols];
130 for(int i=0;i<maxrows;i++)
131 for(int j=0;j<maxcols;j++)
132 pointState[i][j]=0;
133 }
134
135 public void setPointState(int x, int y, int tag) //设置此点状态
136![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
137 this.pointState[x][y]=tag;
138 }
139
140 public int getPointState(int x,int y) //获得此点状态
141![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
142 return this.pointState[x][y];
143 }
144
145 public boolean isLayChess(int x,int y) //判断是否能放子
146![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
147 if(this.pointState[x][y]==0)
148 return true;
149 else return false;
150 }
151
152 public void setFrontChessColor(Color c) //设置上次棋子颜色
153![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
154 this.frontChessColor=c;
155 }
156
157 public boolean isFrontChessColorBlack() //检测上次棋子是否为黑色
158![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
159 if(this.frontChessColor.equals(Color.BLACK))
160 return true;
161 else return false;
162 }
163
164 public void setStart(int start) //设置是否开始
165![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
166 this.start=start;
167 }
168
169 public boolean isStart() //检测是否开始
170![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
171 if(this.start==1) return true;
172 else return false;
173 }
174![](/Images/OutliningIndicators/InBlock.gif)
175 public void setMaxRowCol(int maxrows, int maxcols) //设置行数列数
176![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
177 this.maxrows = maxrows;
178 this.maxcols = maxcols;
179 initPointState();
180 }
181
182 public int getMaxrows() //获得最大行数
183![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
184 return maxrows;
185 }
186
187 public int getMaxcols() //获得最大列数
188![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
189 return maxcols;
190 }
191![](/Images/OutliningIndicators/InBlock.gif)
192 public void setColor(Color c) //设置背景颜色
193![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
194 this.setBackground(c);
195 }
196
197 public int getUnitSize() //获得单元格的大小
198![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
199 return unitSize;
200 }
201
202 public int getDistanceX() //获得棋盘左上 X 坐标
203![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
204 return distanceX;
205 }
206
207 public int getDistanceY() //获得棋盘左上 Y 坐标
208![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
209 return distanceY;
210 }
211![](/Images/OutliningIndicators/InBlock.gif)
212 public void paint(Graphics g) //画棋盘
213![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
214 super.paint(g);
215 g.setColor(Color.BLACK);
216 //棋盘左上起点 X 坐标
217 distanceX = (this.getWidth()-(maxcols-1)*unitSize)/2;
218 //棋盘左上起点 Y 坐标
219 distanceY = (this.getWidth()-(maxrows-1)*unitSize)*2/5;
220
221 for (int i = 0; i < maxrows; i++)
222 // 画横线
223 g.drawLine(0 + distanceX, i * unitSize + distanceY, (maxcols - 1)
224 * unitSize + distanceX, i * unitSize + distanceY);
225 for (int i = 0; i < maxcols; i++)
226 // 画竖线
227 g.drawLine(i * unitSize + distanceX, 0 + distanceY, i * unitSize
228 + distanceX, (maxrows - 1) * unitSize + distanceY);
229 int star;
230 star = maxrows / 4 + 1; // 画天元和星
231 g.fillOval((star - 1) * unitSize - 3 + distanceX, (star - 1) * unitSize
232 - 3 + distanceY, 6, 6);
233 g.fillOval((star * 3 - 1) * unitSize - 3 + distanceX, (star - 1)
234 * unitSize - 3 + distanceY, 6, 6);
235 g.fillOval((star - 1) * unitSize - 3 + distanceX, (star * 3 - 1)
236 * unitSize - 3 + distanceY, 6, 6);
237 g.fillOval((star * 3 - 1) * unitSize - 3 + distanceX, (star * 3 - 1)
238 * unitSize - 3 + distanceY, 6, 6);
239 g.fillOval((star * 2 - 1) * unitSize - 3 + distanceX, (star * 2 - 1)
240 * unitSize - 3 + distanceY, 6, 6);
241 }
242
243 public boolean checkIsVictory(int x, int y, int tag)//是否5连或长连
244![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
245 int sum=0; // 连子总数
246 for(int i=1;i<=4;i++) //横向 左
247![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
248 if((y-i)>=0&&pointState[x][y-i]==tag)
249 sum++;
250 else break;
251 }
252 for(int i=1;i<=4;i++) //横向 右
253![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
254 if((y+i)<maxcols&&pointState[x][y+i]==tag)
255 sum++;
256 else break;
257 }
258 if(sum==4) //五连
259![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
260 this.fiveLine=1;
261 return true;
262 }
263 else if(sum>4) //长连
264 this.overline=1;
265
266 sum=0; //还原
267 for(int i=1;i<=4;i++) //纵向 上
268![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
269 if((x-i)>=0&&pointState[x-i][y]==tag)
270 sum++;
271 else break;
272 }
273 for(int i=1;i<=4;i++) //纵向 下
274![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
275 if((x+i)<maxrows&&pointState[x+i][y]==tag)
276 sum++;
277 else break;
278 }
279 if(sum==4) //五连
280![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
281 this.fiveLine=1;
282 return true;
283 }
284 else if(sum>4) //长连
285 this.overline=1;
286
287 sum=0; //还原
288 for(int i=1;i<=4;i++) //斜向 右上
289![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
290 if((x-i)>=0&&(y+i)<maxcols&&pointState[x-i][y+i]==tag)
291 sum++;
292 else break;
293 }
294 for(int i=1;i<=4;i++) //斜向 左下
295![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
296 if((x+i)<maxrows&&(y-i)>=0&&pointState[x+i][y-i]==tag)
297 sum++;
298 else break;
299 }
300 if(sum==4) //五连
301![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
302 this.fiveLine=1;
303 return true;
304 }
305 else if(sum>4) //长连
306 this.overline=1;
307
308 sum=0; //还原
309 for(int i=1;i<=4;i++) //斜向 左上
310![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
311 if((x-i)>=0&&(y-i)>=0&&pointState[x-i][y-i]==tag)
312 sum++;
313 else break;
314 }
315 for(int i=1;i<=4;i++) //斜向 右下
316![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
317 if((x+i)<maxcols&&(y+i)<maxrows&&pointState[x+i][y+i]==tag)
318 sum++;
319 else break;
320 }
321 if(sum==4) //五连
322![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
323 this.fiveLine=1;
324 return true;
325 }
326 else if(sum>4) //长连
327 this.overline=1;
328
329 return false; //上面均没成功,最后返回false
330 }
331 }
332![](/Images/OutliningIndicators/None.gif)
333 class Chess extends Canvas //棋子类,创建的对象即一个棋子,黑白在创建时确定
334![](/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](/Images/OutliningIndicators/ContractedBlock.gif) {
335 private Color c;
336 private int chessSize;
337 public Chess(Color c)
338![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
339 this.c=c;
340 this.chessSize=16;
341 this.setSize(chessSize, chessSize);
342 }
343 public int getChessSize() //获得棋子大小
344![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
345 return chessSize;
346 }
347 public void paint(Graphics g) //画棋子
348![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
349 g.setColor(c);
350 g.fillOval(0, 0, chessSize, chessSize);
351 }
352 }
353![](/Images/OutliningIndicators/None.gif)
354 class MyActionItemListener implements ActionListener,ItemListener//菜单监听
355![](/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](/Images/OutliningIndicators/ContractedBlock.gif) {
356 ChessFrame cf;
357 ChessPanel cp;
358 public MyActionItemListener(ChessFrame cf,ChessPanel cp)
359![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
360 this.cf=cf;
361 this.cp=cp;
362 }
363 public void actionPerformed(ActionEvent e)
364![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
365 if(e.getSource().equals( //游戏开始
366 cf.getJMenuBar().getMenu(0).getMenuComponent(0)))
367![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
368 cp.removeAll();
369 cp.initPointState();
370 cp.initVicAOl();
371 cp.setFrontChessColor(Color.WHITE); //保证每次黑棋先手
372 cp.setStart(1);
373 JOptionPane.showMessageDialog(cp, "游戏开始");
374 cf.jm[1].setEnabled(false);
375 }
376 else if(e.getSource().equals( //游戏重置
377 cf.getJMenuBar().getMenu(0).getMenuComponent(1)))
378![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
379 cp.removeAll();
380 cp.initPointState();
381 cp.initVicAOl();
382 cp.setFrontChessColor(Color.WHITE); //保证每次黑棋先手
383 cp.setStart(0);
384 cf.jm[1].setEnabled(true);
385 }
386 else if(e.getSource().equals( //游戏结束
387 cf.getJMenuBar().getMenu(0).getMenuComponent(2)))
388![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
389 cf.dispose();
390 System.exit(0);
391 }
392 else if(e.getSource().equals( //帮助 关于
393 cf.getJMenuBar().getMenu(3).getMenuComponent(0)))
394![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
395 JOptionPane.showMessageDialog(cp, "Author : ZHY");
396 }
397 }
398 public void itemStateChanged(ItemEvent e)
399![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
400 if(e.getItemSelectable().equals( //棋盘大小 15 * 15
401 cf.getJMenuBar().getMenu(1).getMenuComponent(0)))
402![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
403 cf.setLocation(255, 175);
404 cf.setSize(500, 500);
405 cp.setMaxRowCol(15, 15);
406 cp.setBounds(50, 25, 400, 400);
407 }
408 else if(e.getItemSelectable().equals( //棋盘大小 19 * 19
409 cf.getJMenuBar().getMenu(1).getMenuComponent(1)))
410![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
411 cf.setLocation(180, 100);
412 cf.setSize(650, 650);
413 cp.setMaxRowCol(19, 19);
414 cp.setBounds(72, 40, 500, 500);
415 }
416 else if(e.getItemSelectable().equals( //棋盘背景 红色
417 cf.getJMenuBar().getMenu(2).getMenuComponent(0)))
418![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
419 cp.setColor(Color.RED);
420 }
421 else if(e.getItemSelectable().equals( //棋盘背景 橘黄色
422 cf.getJMenuBar().getMenu(2).getMenuComponent(1)))
423![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
424 cp.setColor(Color.ORANGE);
425 }
426 else if(e.getItemSelectable().equals( //棋盘背景 青色
427 cf.getJMenuBar().getMenu(2).getMenuComponent(2)))
428![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
429 cp.setColor(Color.CYAN);
430 }
431 }
432 }
433![](/Images/OutliningIndicators/None.gif)
434 class MyMouseAdapter extends MouseAdapter //面板监听
435![](/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](/Images/OutliningIndicators/ContractedBlock.gif) {
436 ChessPanel cp;
437 public MyMouseAdapter(ChessPanel cp)
438![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
439 this.cp=cp;
440 }
441 public void mousePressed(MouseEvent e)
442![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
443 Chess chess;
444 int indexX=-1,indexY=-1; //要下棋子的地方
445 int tag=0; //对棋盘点做的标示,初始为0,为1是黑,为2是白
446 if(cp.isStart()&& //是否有开始
447 (e.getX()-cp.getDistanceX())>=0&& //点击点是否在棋盘内
448 (e.getX()-cp.getDistanceX())<=(cp.getUnitSize()*(cp.getMaxcols()-1))&&
449 (e.getY()-cp.getDistanceY())>=0&&
450 (e.getY()-cp.getDistanceY())<=(cp.getUnitSize()*(cp.getMaxrows()-1))
451 )
452![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
453 if(cp.isFrontChessColorBlack()) //判断要下黑子还是白子
454![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
455 chess=new Chess(Color.WHITE);
456 cp.setFrontChessColor(Color.WHITE);
457 tag=2;
458 }
459 else
460![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
461 chess=new Chess(Color.BLACK);
462 cp.setFrontChessColor(Color.BLACK);
463 tag=1;
464 }
465
466 for(int i=0;i<cp.getMaxcols();i++) //确定要下棋子的横坐标
467 if((e.getX()-cp.getDistanceX())>=(i*cp.getUnitSize()-cp.getUnitSize()/2)&&
468 (e.getX()-cp.getDistanceX())<=(i*cp.getUnitSize()+cp.getUnitSize()/2)
469 )
470 indexX=i;
471
472 for(int j=0;j<cp.getMaxrows();j++) //确定要下棋子的纵坐标
473 if((e.getY()-cp.getDistanceY())>=(j*cp.getUnitSize()-cp.getUnitSize()/2)&&
474 (e.getY()-cp.getDistanceY())<=(j*cp.getUnitSize()+cp.getUnitSize()/2)
475 )
476 indexY=j;
477
478 if(tag==0)
479![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
480 JOptionPane.showMessageDialog(cp, "出现异常,无法判断下子颜色,下子失败!");
481 return ;
482 }
483![](/Images/OutliningIndicators/InBlock.gif)
484 if(indexX==-1||indexY==-1) //是否会出现异常
485![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
486 JOptionPane.showMessageDialog(cp, "出现异常,无法确定下子坐标,下子失败!");
487 return ;
488 }
489
490 if(cp.isLayChess(indexY, indexX)) //若可以下子
491![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
492 cp.add(chess);
493 chess.setLocation(indexX*cp.getUnitSize()-chess.getChessSize()/2+cp.getDistanceX(),
494 indexY*cp.getUnitSize()-chess.getChessSize()/2+cp.getDistanceY());
495 cp.setPointState(indexY, indexX, tag);
496
497 Check ci=new Check(cp);
498
499 cp.checkIsVictory(indexY, indexX, tag);//每下一子,做次判断
500
501 if(cp.getFiveLine()==1||(tag==2&&cp.getOverline()==1))
502![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
503 String s;
504 if(tag==1) s="黑方胜利!";
505 else s="白方胜利";
506 JOptionPane.showMessageDialog(cp, s);
507 cp.setStart(0); //一盘结束
508 }
509 else if( tag==1&&
510 (cp.getOverline()==1||ci.isIled(indexY, indexX, tag)) )
511![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
512 String s;
513 s="禁手,白方胜利!";
514 JOptionPane.showMessageDialog(cp, s);
515 cp.setStart(0); //一盘结束
516 }
517 String s;
518 if(tag==1) s="黑方";
519 else s="白方";
520 System.out.println(s+"下子点:"+indexX+" "+indexY);
521 }
522 }
523 }
524 }
525![](/Images/OutliningIndicators/None.gif)
526 public class FiveChess
527![](/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](/Images/OutliningIndicators/ContractedBlock.gif) {
528 public static void main(String[] args)
529![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
530 ChessFrame cf = new ChessFrame();
531 ChessPanel cp = new ChessPanel(15, 15);
532 cf.add(cp);
533 cp.setLocation(50, 25);
534 cf.setLayout(null);
535 for(int i=0;i<3;i++)
536 cf.jm1jmi[i].addActionListener(new MyActionItemListener(cf,cp));
537 for(int i=0;i<2;i++)
538 cf.jm2jrbmi[i].addItemListener(new MyActionItemListener(cf,cp));
539 for(int i=0;i<3;i++)
540 cf.jm3jrbmi[i].addItemListener(new MyActionItemListener(cf,cp));
541 cf.jm4jmi1.addActionListener(new MyActionItemListener(cf,cp));
542 cp.addMouseListener(new MyMouseAdapter(cp));
543 }
544 }
545![](/Images/OutliningIndicators/None.gif)
546 class Check //判断盘面类
547![](/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](/Images/OutliningIndicators/ContractedBlock.gif) {
548 ChessPanel cp;
549 private int [][][]aroundPointState;
550 //直接举例说明其作用,aps(aroundPointState的简写)
551 //aps[0][0][0]=2 表示此次下子点
552 //左边(第三个下标表示,此为同一阳线或阴线上的两个方向(以所下子为分界))
553 //(这里约定 0 表示 上,左上,左,左下 ; 1 表示 下,右下,右,右上)
554 //与其相邻(第一个坐标表示,0为相邻,1表示中间有与所下子相同的子相隔或是空格)
555 //的空格(第二个坐标表示,0为空格,1为与所下子相同的子)
556 //连续有2个。
557 //aps[1][1][1]=3 右边空格相隔连续与所下子相同的子有3个
558 private int [][][]intervalPointState;
559 //此跟上面aps类似,相邻相同子-空白之后的相同子-空白
560
561
562 public Check(ChessPanel cp)
563![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
564 this.cp=cp;
565 aroundPointState =new int[2][2][2];
566 intervalPointState =new int[2][2][2];
567 initAPSIPS();
568 }
569
570 private void initAPSIPS() // 初始化 aroundPointState
571![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
572 for(int i=0;i<2;i++)
573 for(int j=0;j<2;j++)
574 for(int k=0;k<2;k++)
575![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
576 aroundPointState[i][j][k]=0;
577 intervalPointState[i][j][k]=0;
578 }
579 }
580
581 public boolean isIled(int x, int y, int tag)//判断是否为禁手
582![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
583 //约定:X为横轴,XY为45°斜轴,Y为纵轴,YX为135°斜轴
584 //记录该行三的个数,该行四的个数
585 int numThreeX=0,numFourX=0;
586 int numThreeXY=0,numFourXY=0;
587 int numThreeY=0,numFourY=0;
588 int numThreeYX=0,numFourYX=0;
589
590 initAPSIPS(); //初始化
591 doJudge(x, y, tag, 0, -1, 0);//横向 左
592 doJudge(x, y, tag, 0, 1, 1); //右
593 if(isConStraightThree()||isJumpStraightThree()) //是否有活三
594 numThreeX++;
595 if(isContinuousFour()||isStraightFour()) //是否有冲四或活四
596 numFourX++;
597
598 initAPSIPS(); //初始化
599 doJudge(x, y, tag, 1, -1, 0);//升斜向 左下
600 doJudge(x, y, tag, -1, 1, 1); //右上
601 if(isConStraightThree()||isJumpStraightThree()) //是否有活三
602 numThreeXY++;
603 if(isContinuousFour()||isStraightFour()) //是否有冲四或活四
604 numFourXY++;
605
606 initAPSIPS(); //初始化
607 doJudge(x, y, tag, 1, 0, 0);//纵向 上
608 doJudge(x, y, tag, -1, 0, 1); //下
609 if(isConStraightThree()||isJumpStraightThree()) //是否有活三
610 numThreeY++;
611 if(isContinuousFour()||isStraightFour()) //是否有冲四或活四
612 numFourY++;
613
614 initAPSIPS(); //初始化
615 doJudge(x, y, tag, -1, -1, 0);//降斜向 左上
616 doJudge(x, y, tag, 1, 1, 1); //右下
617 if(isConStraightThree()||isJumpStraightThree()) //是否有活三
618 numThreeYX++;
619 if(isContinuousFour()||isStraightFour()) //是否有冲四或活四
620 numFourYX++;
621
622 if( (numThreeX+numThreeXY+numThreeY+numThreeYX)>=2 //三三
623 || (numFourX+numFourXY+numFourY+numFourYX)>=2 ) //四四 类
624 return true; //包含了四四三、四三三类
625
626 return false; //以上不符合,返回false
627 }
628![](/Images/OutliningIndicators/InBlock.gif)
629 //这里为一个通用的方法,计算8个方向的情况
630 //xtag,ytag为方向标示 范围 -1、0、1
631 //waytag为阳线或阴线方向上的2个分方向的标示,以落子点位分界,范围 0、1
632 //x,y,tag为落子点的坐标和颜色标记
633 private boolean doJudge(int x, int y, int tag, int xtag,int ytag, int waytag)
634![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
635 int maxcount; //计算最大循环次数用的
636 if(ytag==0) //纵向时用 x
637![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
638 if(waytag==0) maxcount=x; //上
639 else maxcount=cp.getMaxrows()-1-x; //下
640 }
641 else //其他用 y
642![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
643 if(waytag==0) maxcount=y; //左上、左、左下
644 else maxcount=cp.getMaxcols()-1-y; //右下、右、右上
645 }
646
647 if( ((xtag==1&&(x+xtag)<cp.getMaxrows()) || (xtag==0) || (xtag==-1&&(x+xtag)>=0)) //纵向不越界
648 &&
649 ((ytag==1&&(y+ytag)<cp.getMaxcols()) || (ytag==0) || (ytag==-1&&(y+ytag)>=0)) //横向不越界
650 &&
651 cp.getPointState(x+xtag, y+ytag)==0 //相邻先是空格
652 )
653![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
654 aroundPointState[0][0][waytag]++;
655 for(int i=2;i<=maxcount;i++)
656![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
657 if(aroundPointState[1][1][waytag]==0&&
658 ((xtag==1&&(x+i)<cp.getMaxrows()) || (xtag==0) || (xtag==-1&&(x-i)>=0)) //纵向不越界
659 &&
660 ((ytag==1&&(y+i)<cp.getMaxcols()) || (ytag==0) || (ytag==-1&&(y-i)>=0)) //横向不越界
661 &&
662 cp.getPointState(x+xtag*i, y+ytag*i)==0 //空格
663 )
664 aroundPointState[0][0][waytag]++;
665
666 else if( intervalPointState[0][0][waytag]==0&&
667 ((xtag==1&&(x+i)<cp.getMaxrows()) || (xtag==0) || (xtag==-1&&(x-i)>=0)) //纵向不越界
668 &&
669 ((ytag==1&&(y+i)<cp.getMaxcols()) || (ytag==0) || (ytag==-1&&(y-i)>=0)) //横向不越界
670 &&
671 cp.getPointState(x+xtag*i, y+ytag*i)==tag //相同子
672 )
673 aroundPointState[1][1][waytag]++;
674
675 else if(aroundPointState[1][1][waytag]!=0
676 &&
677 intervalPointState[1][1][waytag]==0
678 &&
679 ((xtag==1&&(x+i)<cp.getMaxrows()) || (xtag==0) || (xtag==-1&&(x-i)>=0)) //纵向不越界
680 &&
681 ((ytag==1&&(y+i)<cp.getMaxcols()) || (ytag==0) || (ytag==-1&&(y-i)>=0)) //横向不越界
682 &&
683 cp.getPointState(x+xtag*i, y+ytag*i)==0 //间隔空格
684 )
685 intervalPointState[0][0][waytag]++;
686
687 else if(intervalPointState[0][0][waytag]!=0&&
688 ((xtag==1&&(x+i)<cp.getMaxrows()) || (xtag==0) || (xtag==-1&&(x-i)>=0)) //纵向不越界
689 &&
690 ((ytag==1&&(y+i)<cp.getMaxcols()) || (ytag==0) || (ytag==-1&&(y-i)>=0)) //横向不越界
691 &&
692 cp.getPointState(x+xtag*i, y+ytag*i)==tag //间隔相同子
693 )
694 intervalPointState[1][1][waytag]++;
695 else
696 break;
697 }
698 }
699
700 else if( ((xtag==1&&(x+xtag)<cp.getMaxrows()) || (xtag==0) || (xtag==-1&&(x+xtag)>=0)) //纵向不越界
701 &&
702 ((ytag==1&&(y+ytag)<cp.getMaxcols()) || (ytag==0) || (ytag==-1&&(y+ytag)>=0)) //横向不越界
703 &&
704 cp.getPointState(x+xtag, y+ytag)==tag //相邻先是相同子
705 )
706![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
707 aroundPointState[0][1][waytag]++;
708 for(int i=2;i<=maxcount;i++)
709![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
710 if(aroundPointState[1][0][waytag]==0&&
711 ((xtag==1&&(x+i)<cp.getMaxrows()) || (xtag==0) || (xtag==-1&&(x-i)>=0)) //纵向不越界
712 &&
713 ((ytag==1&&(y+i)<cp.getMaxcols()) || (ytag==0) || (ytag==-1&&(y-i)>=0)) //横向不越界
714 &&
715 cp.getPointState(x+xtag*i, y+ytag*i)==tag //相同子
716 )
717 aroundPointState[0][1][waytag]++;
718
719 else if( intervalPointState[0][1][waytag]==0&&
720 ((xtag==1&&(x+i)<cp.getMaxrows()) || (xtag==0) || (xtag==-1&&(x-i)>=0)) //纵向不越界
721 &&
722 ((ytag==1&&(y+i)<cp.getMaxcols()) || (ytag==0) || (ytag==-1&&(y-i)>=0)) //横向不越界
723 &&
724 cp.getPointState(x+xtag*i, y+ytag*i)==0 //空格
725 )
726 aroundPointState[1][0][waytag]++;
727
728 else if( aroundPointState[1][0][waytag]!=0
729 &&
730 intervalPointState[1][0][waytag]==0
731 &&
732 ((xtag==1&&(x+i)<cp.getMaxrows()) || (xtag==0) || (xtag==-1&&(x-i)>=0)) //纵向不越界
733 &&
734 ((ytag==1&&(y+i)<cp.getMaxcols()) || (ytag==0) || (ytag==-1&&(y-i)>=0)) //横向不越界
735 &&
736 cp.getPointState(x+xtag*i, y+ytag*i)==tag //间隔相同子
737 )
738 intervalPointState[0][1][waytag]++;
739
740 else if(intervalPointState[0][1][waytag]!=0&&
741 ((xtag==1&&(x+i)<cp.getMaxrows()) || (xtag==0) || (xtag==-1&&(x-i)>=0)) //纵向不越界
742 &&
743 ((ytag==1&&(y+i)<cp.getMaxcols()) || (ytag==0) || (ytag==-1&&(y-i)>=0)) //横向不越界
744 &&
745 cp.getPointState(x+xtag*i, y+ytag*i)==0 //间隔空格
746 )
747 intervalPointState[1][0][waytag]++;
748
749 else
750 break;
751 }
752 }
753 else return false; //相邻为不同子,直接返回
754
755 return true; //最后返回true
756 }
757
758 private boolean isConStraightThree()//判断是否为连活三
759![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
760 //有3种情况,分别列出3种情况,看其阳线或阴线周围情况是否符合
761 //先来是判断是否为连活三,情况都是以左、中、右或上、中、下来列
762 if(aroundPointState[0][1][1]==2
763 &&
764 ((aroundPointState[0][0][0]>=1&&aroundPointState[1][0][1]>=2)
765 ||(aroundPointState[0][0][0]>=2&&aroundPointState[1][0][1]>=1))
766 )
767 return true;
768 else if(aroundPointState[0][1][0]==1&&aroundPointState[0][1][1]==1
769 &&
770 ((aroundPointState[1][0][0]>=1&&aroundPointState[1][0][1]>=2)
771 ||(aroundPointState[1][0][0]>=2&&aroundPointState[1][0][1]>=1))
772 )
773 return true;
774 else if(aroundPointState[0][1][0]==2
775 &&
776 ((aroundPointState[1][0][0]>=1&&aroundPointState[0][0][1]>=2)
777 ||(aroundPointState[1][0][0]>=2&&aroundPointState[0][0][1]>=1))
778 )
779 return true;
780
781 return false; //以上3种情况都不是,非连活三
782 }
783
784 private boolean isJumpStraightThree() //判断是否为跳活三
785![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
786 //有两种情况,左2右1、左1右2 分别列出3种情况,看其阳线或阴线周围情况是否符合
787 //先左2右1 判断子顺序 左、中、右
788 if(aroundPointState[0][1][1]==1&&aroundPointState[1][0][1]==1
789 &&intervalPointState[0][1][1]==1
790 &&
791 ((aroundPointState[0][0][0]>=1&&intervalPointState[1][0][1]>=2)
792 ||(aroundPointState[0][0][0]>=2&&intervalPointState[1][0][1]>=1))
793 )
794 return true;
795
796 else if(aroundPointState[0][1][0]==1&&
797 aroundPointState[0][0][1]==1&&aroundPointState[1][1][1]==1
798 &&
799 ((aroundPointState[1][0][0]>=1&&intervalPointState[0][0][1]>=2)
800 ||(aroundPointState[1][0][0]>=2&&intervalPointState[0][0][1]>=1))
801 )
802 return true;
803
804 else if(aroundPointState[0][0][0]==1
805 &&aroundPointState[1][1][0]==2
806 &&
807 ((intervalPointState[0][0][0]>=1&&aroundPointState[0][0][1]>=2)
808 ||(intervalPointState[0][0][0]>=2&&aroundPointState[0][0][1]>=1))
809 )
810 return true;
811
812 //接下来 左1右2 判断子顺序 左、中、右
813 if(aroundPointState[0][0][1]==1&&aroundPointState[1][1][1]==2
814 &&
815 ((aroundPointState[0][0][0]>=1&&intervalPointState[0][0][1]>=2)
816 ||(aroundPointState[0][0][0]>=2&&intervalPointState[0][0][1]>=1))
817 )
818 return true;
819
820 else if(aroundPointState[0][0][0]==1&&
821 aroundPointState[1][1][0]==1&&aroundPointState[0][1][1]==1
822 &&
823 ((intervalPointState[0][0][0]>=1&&aroundPointState[1][0][1]>=2)
824 ||(intervalPointState[0][0][0]>=2&&aroundPointState[1][0][1]>=1))
825 )
826 return true;
827
828 else if(aroundPointState[0][1][0]==1&&aroundPointState[1][0][0]==1
829 &&intervalPointState[0][1][0]==1
830 &&
831 ((aroundPointState[0][0][1]>=1&&intervalPointState[1][0][0]>=2)
832 ||(aroundPointState[0][0][1]>=2&&intervalPointState[1][0][0]>=1))
833 )
834 return true;
835
836 return false; //以上6种情况都不是,非跳活三
837 }
838
839 private boolean isContinuousFour() //是否为冲四
840![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
841 //这里判断是否为冲四,依旧是列举每种情况,看是否是,4类,每类4中情况
842
843 //左1右3 左1、右3、右2、右1
844 if(aroundPointState[0][0][1]==1&&aroundPointState[1][1][1]==3&&
845 aroundPointState[0][1][0]==0)
846 return true;
847 else if(aroundPointState[0][0][0]==1&&aroundPointState[1][1][0]==1
848 &&intervalPointState[0][1][0]==0&&aroundPointState[0][1][1]==2)
849 return true;
850 else if(aroundPointState[0][1][0]==1&&aroundPointState[1][0][0]==1
851 &&intervalPointState[0][1][0]==1&&aroundPointState[0][1][1]==1)
852 return true;
853 else if(aroundPointState[0][1][0]==2&&aroundPointState[1][0][0]==1
854 &&intervalPointState[0][1][0]==1&&aroundPointState[0][1][1]==0)
855 return true;
856
857 //左2右2 左1、左2、右2、右1
858 if(aroundPointState[0][1][0]==0&&aroundPointState[0][1][1]==1&&
859 aroundPointState[1][0][1]==1&&intervalPointState[0][1][1]==2)
860 return true;
861 else if(aroundPointState[0][1][0]==1&&
862 aroundPointState[0][0][1]==1 &&aroundPointState[1][1][1]==2)
863 return true;
864 else if(aroundPointState[0][0][0]==1&&aroundPointState[1][1][0]==2
865 &&aroundPointState[0][1][1]==1)
866 return true;
867 else if(aroundPointState[0][1][0]==1&&aroundPointState[1][0][0]==1
868 &&intervalPointState[0][1][0]==2&&aroundPointState[0][1][1]==0)
869 return true;
870
871 //左3右1 左1、左2、左3、右1
872 if(aroundPointState[0][1][0]==0&&aroundPointState[0][1][1]==2&&
873 aroundPointState[1][0][1]==1&&intervalPointState[0][1][1]==1)
874 return true;
875 else if(aroundPointState[0][1][0]==1&&aroundPointState[0][1][1]==1
876 &&aroundPointState[1][0][1]==1&&intervalPointState[0][1][1]==1)
877 return true;
878 else if(aroundPointState[0][1][0]==2&&
879 aroundPointState[0][0][1]==1&&aroundPointState[1][1][1]==1)
880 return true;
881 else if(aroundPointState[0][0][0]==1&&aroundPointState[1][1][0]==3
882 &&aroundPointState[0][1][1]==0)
883 return true;
884
885 //4个连一起 左、中间2个、右
886 if(
887 aroundPointState[0][1][1]==3&&( (aroundPointState[0][0][0]==0
888 &&aroundPointState[0][1][0]==0&&aroundPointState[1][0][1]>=1)
889 ||(aroundPointState[0][0][0]>=1&&aroundPointState[1][0][1]==0) )
890 )
891 return true;
892
893 else if(
894 ( (aroundPointState[0][1][0]==1&&aroundPointState[0][1][1]==2)
895 ||(aroundPointState[0][1][0]==2&&aroundPointState[0][1][1]==1) )
896 &&
897 ( (aroundPointState[1][0][0]==0&&aroundPointState[1][0][1]>=1)
898 ||(aroundPointState[1][0][0]>=1&&aroundPointState[1][0][1]==0) )
899 )
900 return true;
901
902 else if(
903 aroundPointState[0][1][0]==3&&( (aroundPointState[0][0][1]==0
904 &&aroundPointState[0][1][1]==0&&aroundPointState[1][0][0]>=1)
905 ||(aroundPointState[0][0][1]>=1&&aroundPointState[1][0][0]==0) )
906 )
907 return true;
908
909 return false; //以上都不是 返回false
910 }
911
912 private boolean isStraightFour() //是否为活四
913![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
914 //判断是否为活四,4种情况,列出 左、中间2个、右
915 if( aroundPointState[0][1][1]==3&&
916 ( (aroundPointState[0][0][0]>=1&&aroundPointState[1][0][1]>=2)
917 ||(aroundPointState[0][0][0]>=2&&aroundPointState[1][0][1]>=1) )
918 )
919 return true;
920 else if(
921 ( (aroundPointState[0][1][0]==1&&aroundPointState[0][1][1]==2)
922 ||(aroundPointState[0][1][0]==2&&aroundPointState[0][1][1]==1) )
923 &&
924 ( (aroundPointState[1][0][0]>=1&&aroundPointState[1][0][1]>=2)
925 ||(aroundPointState[1][0][0]>=2&&aroundPointState[1][0][1]>=1) )
926 )
927 return true;
928 else if( aroundPointState[0][1][0]==3&&
929 ( (aroundPointState[1][0][0]>=1&&aroundPointState[0][0][1]>=2)
930 ||(aroundPointState[1][0][0]>=2&&aroundPointState[0][0][1]>=1) )
931 )
932 return true;
933
934 return false; //以上情况都不是,返回false
935 }
936 }
|