服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|JavaScript|易语言|

服务器之家 - 编程语言 - JAVA教程 - java实现单机版五子棋

java实现单机版五子棋

2021-04-16 11:46lin14543 JAVA教程

这篇文章主要为大家详细介绍了java实现单机版五子棋源码,以及五子棋游戏需要的实现,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

这个小游戏是我和我姐们儿的java课程设计,也是我做的第一个java项目,适合初学者,希望能帮到那些被java课设所困扰的孩纸们~~~

一、该游戏需要实现

1、设计主框架,界面。

2、利用actionlistener接口实现按钮事件的监听。

3、重新开始功能的实现。

4、悔棋功能的实现。

5、退出功能的实现。

6、棋盘中棋子点类的定义。

7、利用mouselistener接口实现事件监听,并实现接口里的所有方法。

8、当鼠标移动到棋盘上的交点上,且该点上无棋子时能够变成小手形状。

9、点击棋盘时,利用if语句判断该点是否点在交点上,并利用foreach语句和棋子类中的getx(),gety()方法遍历每一个棋子的位置判断该点是否有棋子。

10、当判断到可以在所点击的点上下子时,画棋子时利用for循环遍历已有的每一个点并利用graphics类的setcolor设置颜色,利用graphics类的filloval方法设置形状大小。

11、当画完棋子时要及时判断输赢,用棋子所在索引和for循环遍历最后一个棋子的各个方向,如果有在同一条直线上的棋子个数大于等于五的即当前棋子所代表的那方赢。

12、胜负已定的时候,能够弹出相应的信息。

二、功能代码实现

2.1进入游戏

?
1
2
3
4
public static void main(string[] args) {
  startchessjframe f=new startchessjframe();//创建主框架
  f.setvisible(true);//显示主框架
 }

2.2初始化,定义一些要用到的量。

?
1
2
3
4
5
private chessboard chessboard;//对战面板
 private panel toolbar;//工具条面板
 private button startbutton;//设置开始按钮
 private button backbutton;//设置悔棋按钮
 private button exitbutton;//设置退出按钮

2.3界面的构造方法(游戏的框架)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public startchessjframe(){
  settitle("单机版五子棋");//设置标题
  chessboard=new chessboard();//初始化面板对象,创建和添加菜单
  myitemlistener lis=new myitemlistener();//初始化按钮事件监听器内部类
  toolbar=new panel();//工具面板栏实例化
  startbutton=new button("重新开始");
  backbutton=new button("悔棋");
  exitbutton=new button("退出");//三个按钮初始化
  toolbar.setlayout(new flowlayout(flowlayout.left));//将工具面板按钮用flowlayout布局
  toolbar.add(backbutton);
  toolbar.add(startbutton);
  toolbar.add(exitbutton);//将三个按钮添加到工具面板上
  startbutton.addactionlistener(lis);
  backbutton.addactionlistener(lis);
  exitbutton.addactionlistener(lis);//将三个按钮事件注册监听事件
  add(toolbar,borderlayout.south);//将工具面板布局到界面南方也就是下面
  add(chessboard);//将面板对象添加到窗体上
  setdefaultcloseoperation(jframe.exit_on_close);//设置界面关闭事件
  pack();//自适应大小
 }

2.4按钮的实现与监听(构造方法内部)

?
1
2
3
4
5
6
7
8
9
10
11
12
myitemlistener lis=new myitemlistener();//初始化按钮事件监听器内部类
  toolbar=new panel();//工具面板栏实例化
  startbutton=new button("重新开始");
  backbutton=new button("悔棋");
  exitbutton=new button("退出");//三个按钮初始化
  toolbar.setlayout(new flowlayout(flowlayout.left));//将工具面板按钮用flowlayout布局
  toolbar.add(backbutton);
  toolbar.add(startbutton);
  toolbar.add(exitbutton);//将三个按钮添加到工具面板上
  startbutton.addactionlistener(lis);
  backbutton.addactionlistener(lis);
  exitbutton.addactionlistener(lis);//将三个按钮事件注册监听事件

2.5按钮事件的监听

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
private class myitemlistener implements actionlistener{
  public void actionperformed(actionevent e) {
   object obj=e.getsource();//获取事件源
   if(obj==startbutton){
    system.out.println("重新开始...");//重新开始
    //jfiveframe.this内部类引用外部类
    chessboard.restartgame();
   }else if(obj==exitbutton){
    system.exit(0);//结束应用程序
   }else if(obj==backbutton){
    system.out.println("悔棋...");//悔棋
    chessboard.goback();
   }   
  }  
 }

2.6重新开始按钮的功能实现

?
1
2
3
4
5
6
7
8
9
public void restartgame(){//清除棋子
  for(int i=0;i<chesslist.length;i++)
   chesslist[i]=null;
  /*恢复游戏相关的变量值*/
  isback=true;
  gameover=false;//游戏是否结束
  chesscount=0;//当前棋盘的棋子个数
  repaint(); 
 }

2.7悔棋按钮的功能实现

?
1
2
3
4
5
6
7
8
9
10
11
12
public void goback(){
  if(chesscount==0)
   return ;
  chesslist[chesscount-1]=null;
  chesscount--;
  if(chesscount>0){
   xindex=chesslist[chesscount-1].getx();
   yindex=chesslist[chesscount-1].gety();
  }
  isback=!isback;
  repaint();
 }

2.8当棋盘根据需要变大或变小时窗口应随之发生改变

?
1
2
3
4
5
//dimension:矩形chessboard类内部
 public dimension getpreferredsize(){
  return new dimension(margin*2+grid_span*cols,margin*2+grid_span*rows);
 
pack();//自适应大小startchessboard类内部

2.9定义棋子类

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import java.awt.*;
public class point {
 private int x;//棋子在棋盘中的x索引值
 private int y;//棋子在棋盘中的y索引值
 private color color;//颜色
 public static int diameter=30;//直径
 public point(int x,int y,color color){
  this.x=x;
  this.y=y;
  this.color=color;
 }
 //得到棋子在棋盘中的x索引值
 public int getx(){
  return x;
 }
 //得到棋子在棋盘中的y索引值
 public int gety(){
  return y;
 }
 //得到棋子颜色
 public color getcolor(){
  return color;
 }
}

三、功能部分代码实现

3.1初始化,定义一些要用到的量。

?
1
2
3
4
5
6
7
8
9
public static int margin=30;//边距
 public static int grid_span=35;//网格间距
 public static int rows=18;//棋盘行数
 public static int cols=18;//棋盘列数
 point[] chesslist=new point[(rows+1)*(cols+1)];//初始化每个数组元素为null
 boolean isback=true;//默认开始是黑棋先下
 boolean gameover=false;//游戏是否结束
 int chesscount;//当前棋盘的棋子个数
 int xindex,yindex;//当前刚下棋子的索引

3.2棋盘对象的构造方法

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public chessboard(){
  setbackground(color.light_gray);//设置背景颜色为灰色
  addmouselistener(this);//添加事件监听器
  addmousemotionlistener(new mousemotionlistener() {//匿名内部类
    
   @override
   public void mousemoved(mouseevent e) {
    int x1=(e.getx()-margin+grid_span/2)/grid_span;
    int y1=(e.gety()-margin+grid_span/2)/grid_span;//将鼠标单击的坐标位置转化为网格索引
    if(x1<0||x1>rows||y1<0||y1>cols||gameover||findchess(x1,y1)){//游戏已经结束,不能下;落在棋盘外,不能下;x,y位置已经有棋子存在,不能下
     setcursor(new cursor(cursor.default_cursor));//设置成默认形状
    }else{
     setcursor(new cursor(cursor.hand_cursor));//设置成手型
    }
   }   
   @override
   public void mousedragged(mouseevent e) {
   }
  });
 }

3.3设置鼠标监听器,变小手(在构造方法内部)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
addmousemotionlistener(new mousemotionlistener() {//匿名内部类
    
   @override
   public void mousemoved(mouseevent e) {
    int x1=(e.getx()-margin+grid_span/2)/grid_span;
    int y1=(e.gety()-margin+grid_span/2)/grid_span;//将鼠标单击的坐标位置转化为网格索引
    if(x1<0||x1>rows||y1<0||y1>cols||gameover||findchess(x1,y1)){//游戏已经结束,不能下;落在棋盘外,不能下;x,y位置已经有棋子存在,不能下
     setcursor(new cursor(cursor.default_cursor));//设置成默认形状
    }else{
     setcursor(new cursor(cursor.hand_cursor));//设置成手型
    }
   }   
   @override
   public void mousedragged(mouseevent e) {
   }
  });

3.4点击棋盘时的鼠标按压事件

?
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
public void mousepressed(mouseevent e) {//鼠标按键在组件上按下时调用
  if(gameover)//游戏已经结束,不能下
   return ;
  string colorname=isback ? "黑棋" : "白棋";
  xindex=(e.getx()-margin+grid_span/2)/grid_span;
  yindex=(e.gety()-margin+grid_span/2)/grid_span;//将鼠标单击的坐标位置转化为网格索引
  if(xindex<0||xindex>rows||yindex<0||yindex>cols)//棋子落在棋盘外,不能下
   return ;
  if(findchess(xindex,yindex))//x,y位置已经有棋子存在,不能下
   return ;  
  point ch=new point(xindex,yindex,isback ? color.black : color.white);
  chesslist[chesscount++]=ch;
  repaint();//通知系统重新绘制
  if(iswin()){
   string msg=string.format("恭喜,%s赢啦~", colorname);
   joptionpane.showmessagedialog(this, msg);
   gameover=true;  
  }
  else if(chesscount==(cols+1)*(rows+1))
  {
   string msg=string.format("棋鼓相当,棒棒哒~");
   joptionpane.showmessagedialog(this,msg);
   gameover=true;
  }
  isback=!isback;
 }

3.5绘制棋盘,棋子还有红框框

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public void paintcomponent(graphics g){
  super.paintcomponent(g);//画棋盘
  for(int i=0;i<=rows;i++){//画横线
   g.drawline(margin, margin+i*grid_span, margin+cols*grid_span, margin+i*grid_span);
  }
  for(int i=0;i<=cols;i++){//画直线
   g.drawline(margin+i*grid_span, margin, margin+i*grid_span,margin+rows*grid_span);
  }
  /*画棋子*/
  for(int i=0;i<chesscount;i++){
   int xpos=chesslist[i].getx()*grid_span+margin;//网格交叉的x坐标
   int ypos=chesslist[i].gety()*grid_span+margin;//网格交叉的y坐标
   g.setcolor(chesslist[i].getcolor());//设置颜色
   g.filloval(xpos-point.diameter/2, ypos-point.diameter/2, point.diameter, point.diameter);
   if(i==chesscount-1){
    g.setcolor(color.red);//标记最后一个棋子为红色
    g.drawrect(xpos-point.diameter/2, ypos-point.diameter/2, point.diameter, point.diameter);
   }
  }
 }

3.6判断输赢

 

?
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
/*判断哪方赢*/
 private boolean iswin(){
  int continuecount=1;//连续棋子的个数
  for(int x=xindex-1;x>=0;x--){//横向向左寻找
   color c=isback ? color.black : color.white;
   if(getchess(x,yindex,c)!=null){
    continuecount++;
   }else
    break;
  }
  for(int x=xindex+1;x<=rows;x++){//横向向右寻找
   color c=isback ? color.black : color.white;
   if(getchess(x,yindex,c)!=null){
    continuecount++;
   }else
    break;
  }
  if(continuecount>=5){//判断记录数大于等于五,即表示此方获胜
   return true;
  }else
   continuecount=1;
  //
  for(int y=yindex-1;y>=0;y--){//纵向向上寻找
   color c=isback ? color.black : color.white;
   if(getchess(xindex,y,c)!=null){
    continuecount++;
   }else
    break;
  }
  for(int y=yindex+1;y<=rows;y++){//纵向向下寻找
   color c=isback ? color.black : color.white;
   if(getchess(xindex,y,c)!=null){
    continuecount++;
   }else
    break;
  }
  if(continuecount>=5){//判断记录数大于等于五,即表示此方获胜
   return true;
  }else
   continuecount=1;
  //
  for(int x=xindex+1,y=yindex-1;y>=0&&x<=cols;x++,y--){//右下寻找
   color c=isback ? color.black : color.white;
   if(getchess(x,y,c)!=null){
    continuecount++;
   }else
    break;
  }
  for(int x=xindex-1,y=yindex+1;y<=rows&&x>=0;x--,y++){//左上寻找
   color c=isback ? color.black : color.white;
   if(getchess(x,y,c)!=null){
    continuecount++;
   }else
    break;
  }
  if(continuecount>=5){//判断记录数大于等于五,即表示此方获胜
   return true;
  }else
   continuecount=1;
  //
  for(int x=xindex-1,y=yindex-1;y>=0&&x>=0;x--,y--){//左下寻找
   color c=isback ? color.black : color.white;
   if(getchess(x,y,c)!=null){
    continuecount++;
   }else
    break;
  }
  for(int x=xindex+1,y=yindex+1;y<=rows&&x<=cols;x++,y++){//右上寻找
   color c=isback ? color.black : color.white;
   if(getchess(x,y,c)!=null){
    continuecount++;
   }else
    break;
  }
  if(continuecount>=5){//判断记录数大于等于五,即表示此方获胜
   return true;
  }else
   continuecount=1;
  return false;  
 }

3.7弹出相应消息框(在鼠标按压函数内部)

?
1
2
3
4
5
6
7
8
9
10
11
if(iswin()){
   string msg=string.format("恭喜,%s赢啦~", colorname);
   joptionpane.showmessagedialog(this, msg);
   gameover=true;  
  }
  else if(chesscount==(cols+1)*(rows+1))//平局
  {
   string msg=string.format("棋鼓相当,棒棒哒~");
   joptionpane.showmessagedialog(this,msg);
   gameover=true;
  }

3.8上面用到的一个判断某点是否有棋子的函数

?
1
2
3
4
5
6
7
private boolean findchess(int x,int y){
  for(point c:chesslist){
   if(c!=null&&c.getx()==x&&c.gety()==y)
    return true;
  }
  return false;
 }

3.9因为该棋盘类实现了鼠标监听接口monselistener,所以要重写该接口内的所有方法,其它方法如下

?
1
2
3
4
5
6
7
8
9
10
11
12
@override
 public void mouseclicked(mouseevent e) {//鼠标按键在组件上单击(按下并释放)时调用
 }
 @override
 public void mousereleased(mouseevent e) {////鼠标按键在组件上释放时调用
 }
 @override
 public void mouseentered(mouseevent e) {//鼠标进入组件时调用
 }
 @override
 public void mouseexited(mouseevent e){//鼠标离开组件时调用 
 }

四、运行结果

java实现单机版五子棋

java实现单机版五子棋

五、代码汇总

该游戏总共建了三个类,一个是界面startchessjframe,一个是棋盘类chessboard,一个是棋子类point

5.1startchessjframe类

?
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
package chess.lcc.com;
import javax.swing.*;
 
import java.awt.event.*;
import java.awt.*;
/*
 * 五子棋的主框架,程序启动类
 */
public class startchessjframe extends jframe {
 private chessboard chessboard;//对战面板
 private panel toolbar;//工具条面板
 private button startbutton;//设置开始按钮
 private button backbutton;//设置悔棋按钮
 private button exitbutton;//设置退出按钮
 
  
 public startchessjframe(){
  settitle("单机版五子棋");//设置标题
  chessboard=new chessboard();//初始化面板对象,创建和添加菜单
  myitemlistener lis=new myitemlistener();//初始化按钮事件监听器内部类
  toolbar=new panel();//工具面板栏实例化
  startbutton=new button("重新开始");
  backbutton=new button("悔棋");
  exitbutton=new button("退出");//三个按钮初始化
  toolbar.setlayout(new flowlayout(flowlayout.left));//将工具面板按钮用flowlayout布局
  toolbar.add(backbutton);
  toolbar.add(startbutton);
  toolbar.add(exitbutton);//将三个按钮添加到工具面板上
  startbutton.addactionlistener(lis);
  backbutton.addactionlistener(lis);
  exitbutton.addactionlistener(lis);//将三个按钮事件注册监听事件
  add(toolbar,borderlayout.south);//将工具面板布局到界面南方也就是下面
  add(chessboard);//将面板对象添加到窗体上
  setdefaultcloseoperation(jframe.exit_on_close);//设置界面关闭事件
  pack();//自适应大小
 }
 private class myitemlistener implements actionlistener{
  public void actionperformed(actionevent e) {
   object obj=e.getsource();//获取事件源
   if(obj==startbutton){
    system.out.println("重新开始...");//重新开始
    //jfiveframe.this内部类引用外部类
    chessboard.restartgame();
   }else if(obj==exitbutton){
    system.exit(0);//结束应用程序
   }else if(obj==backbutton){
    system.out.println("悔棋...");//悔棋
    chessboard.goback();
   }   
  }  
 }
 public static void main(string[] args) {
  startchessjframe f=new startchessjframe();//创建主框架
  f.setvisible(true);//显示主框架
 }
}

5.2chessboard类

?
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
package chess.lcc.com;
 
import javax.swing.*;
 
import java.awt.*;
import java.awt.event.mouselistener;
import java.awt.event.mousemotionlistener;
import java.awt.event.mouseevent;
/*五子棋-棋盘类*/
public class chessboard extends jpanel implements mouselistener{
 public static int margin=30;//边距
 public static int grid_span=35;//网格间距
 public static int rows=15;//棋盘行数
 public static int cols=15;//棋盘列数
 point[] chesslist=new point[(rows+1)*(cols+1)];//初始化每个数组元素为null
 boolean isback=true;//默认开始是黑棋先下
 boolean gameover=false;//游戏是否结束
 int chesscount;//当前棋盘的棋子个数
 int xindex,yindex;//当前刚下棋子的索引
 public chessboard(){
  setbackground(color.light_gray);//设置背景颜色为黄色
  addmouselistener(this);//添加事件监听器
  addmousemotionlistener(new mousemotionlistener() {//匿名内部类
    
   @override
   public void mousemoved(mouseevent e) {
    int x1=(e.getx()-margin+grid_span/2)/grid_span;
    int y1=(e.gety()-margin+grid_span/2)/grid_span;//将鼠标单击的坐标位置转化为网格索引
    if(x1<0||x1>rows||y1<0||y1>cols||gameover||findchess(x1,y1)){//游戏已经结束,不能下;落在棋盘外,不能下;x,y位置已经有棋子存在,不能下
     setcursor(new cursor(cursor.default_cursor));//设置成默认形状
    }else{
     setcursor(new cursor(cursor.hand_cursor));//设置成手型
    }
   }
    
   @override
   public void mousedragged(mouseevent e) {
   }
  });
 }
 /*绘制*/
 public void paintcomponent(graphics g){
  super.paintcomponent(g);//画棋盘
  for(int i=0;i<=rows;i++){//画横线
   g.drawline(margin, margin+i*grid_span, margin+cols*grid_span, margin+i*grid_span);
  }
  for(int i=0;i<=cols;i++){//画直线
   g.drawline(margin+i*grid_span, margin, margin+i*grid_span,margin+rows*grid_span);
  }
  /*画棋子*/
  for(int i=0;i<chesscount;i++){
   int xpos=chesslist[i].getx()*grid_span+margin;//网格交叉的x坐标
   int ypos=chesslist[i].gety()*grid_span+margin;//网格交叉的y坐标
   g.setcolor(chesslist[i].getcolor());//设置颜色
   g.filloval(xpos-point.diameter/2, ypos-point.diameter/2, point.diameter, point.diameter);
   if(i==chesscount-1){
    g.setcolor(color.red);//标记最后一个棋子为红色
    g.drawrect(xpos-point.diameter/2, ypos-point.diameter/2, point.diameter, point.diameter);
   }
  }
 }
  
 
 
 @override
 public void mousepressed(mouseevent e) {//鼠标按键在组件上按下时调用
  if(gameover)//游戏已经结束,不能下
   return ;
  string colorname=isback ? "黑棋" : "白棋";
  xindex=(e.getx()-margin+grid_span/2)/grid_span;
  yindex=(e.gety()-margin+grid_span/2)/grid_span;//将鼠标单击的坐标位置转化为网格索引
  if(xindex<0||xindex>rows||yindex<0||yindex>cols)//棋子落在棋盘外,不能下
   return ;
  if(findchess(xindex,yindex))//x,y位置已经有棋子存在,不能下
   return ;
   
  point ch=new point(xindex,yindex,isback ? color.black : color.white);
  chesslist[chesscount++]=ch;
  repaint();//通知系统重新绘制
  if(iswin()){
   string msg=string.format("恭喜,%s赢啦~", colorname);
   joptionpane.showmessagedialog(this, msg);
   gameover=true;  
  }
  else if(chesscount==(cols+1)*(rows+1))
  {
   string msg=string.format("棋鼓相当,棒棒哒~");
   joptionpane.showmessagedialog(this,msg);
   gameover=true;
  }
  isback=!isback;
 }
  
 @override
 public void mouseclicked(mouseevent e) {//鼠标按键在组件上单击(按下并释放)时调用
 }
 
 @override
 public void mousereleased(mouseevent e) {////鼠标按键在组件上释放时调用
 }
 
 @override
 public void mouseentered(mouseevent e) {//鼠标进入组件时调用
 }
 
 @override
 public void mouseexited(mouseevent e){//鼠标离开组件时调用  
 }
  
 private boolean findchess(int x,int y){
  for(point c:chesslist){
   if(c!=null&&c.getx()==x&&c.gety()==y)
    return true;
  }
  return false;
 }
  
 /*判断那方赢*/
 private boolean iswin(){
  int continuecount=1;//连续棋子的个数
  for(int x=xindex-1;x>=0;x--){//横向向左寻找
   color c=isback ? color.black : color.white;
   if(getchess(x,yindex,c)!=null){
    continuecount++;
   }else
    break;
  }
  for(int x=xindex+1;x<=rows;x++){//横向向右寻找
   color c=isback ? color.black : color.white;
   if(getchess(x,yindex,c)!=null){
    continuecount++;
   }else
    break;
  }
  if(continuecount>=5){//判断记录数大于等于五,即表示此方获胜
   return true;
  }else
   continuecount=1;
  //
  for(int y=yindex-1;y>=0;y--){//纵向向上寻找
   color c=isback ? color.black : color.white;
   if(getchess(xindex,y,c)!=null){
    continuecount++;
   }else
    break;
  }
  for(int y=yindex+1;y<=rows;y++){//纵向向下寻找
   color c=isback ? color.black : color.white;
   if(getchess(xindex,y,c)!=null){
    continuecount++;
   }else
    break;
  }
  if(continuecount>=5){//判断记录数大于等于五,即表示此方获胜
   return true;
  }else
   continuecount=1;
  //
  for(int x=xindex+1,y=yindex-1;y>=0&&x<=cols;x++,y--){//右下寻找
   color c=isback ? color.black : color.white;
   if(getchess(x,y,c)!=null){
    continuecount++;
   }else
    break;
  }
  for(int x=xindex-1,y=yindex+1;y<=rows&&x>=0;x--,y++){//左上寻找
   color c=isback ? color.black : color.white;
   if(getchess(x,y,c)!=null){
    continuecount++;
   }else
    break;
  }
  if(continuecount>=5){//判断记录数大于等于五,即表示此方获胜
   return true;
  }else
   continuecount=1;
  //
  for(int x=xindex-1,y=yindex-1;y>=0&&x>=0;x--,y--){//左下寻找
   color c=isback ? color.black : color.white;
   if(getchess(x,y,c)!=null){
    continuecount++;
   }else
    break;
  }
  for(int x=xindex+1,y=yindex+1;y<=rows&&x<=cols;x++,y++){//右上寻找
   color c=isback ? color.black : color.white;
   if(getchess(x,y,c)!=null){
    continuecount++;
   }else
    break;
  }
  if(continuecount>=5){//判断记录数大于等于五,即表示此方获胜
   return true;
  }else
   continuecount=1;
  return false;  
 }
 private point getchess(int xindex,int yindex,color color){
  for(point c:chesslist){
   if(c!=null&&c.getx()==xindex&&c.gety()==yindex&&c.getcolor()==color)
    return c;
  }
  return null;
 }
 public void restartgame(){//清除棋子
  for(int i=0;i<chesslist.length;i++)
   chesslist[i]=null;
  /*恢复游戏相关的变量值*/
  isback=true;
  gameover=false;//游戏是否结束
  chesscount=0;//当前棋盘的棋子个数
  repaint(); 
 }
 public void goback(){
  if(chesscount==0)
   return ;
  chesslist[chesscount-1]=null;
  chesscount--;
  if(chesscount>0){
   xindex=chesslist[chesscount-1].getx();
   yindex=chesslist[chesscount-1].gety();
  }
  isback=!isback;
  repaint();
 }
 //dimension:矩形
 public dimension getpreferredsize(){
  return new dimension(margin*2+grid_span*cols,margin*2+grid_span*rows);
 }
  
  
 
}

5.3point类

?
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
package chess.lcc.com;
 
import java.awt.*;
 
public class point {
 private int x;//棋子在棋盘中的x索引值
 private int y;//棋子在棋盘中的y索引值
 private color color;//颜色
 public static int diameter=30;//直径
 public point(int x,int y,color color){
  this.x=x;
  this.y=y;
  this.color=color;
 }
 //得到棋子在棋盘中的x索引值
 public int getx(){
  return x;
 }
 //得到棋子在棋盘中的y索引值
 public int gety(){
  return y;
 }
 //得到棋子颜色
 public color getcolor(){
  return color;
 }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/lin14543/article/details/51867892

延伸 · 阅读

精彩推荐