国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務器之家:專注于服務器技術及軟件下載分享
分類導航

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

服務器之家 - 編程語言 - 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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 婷婷五月色综合 | 亚洲国产精品尤物yw在线观看 | 视频一区二区三区中文字幕 | 亚洲欧美高清 | 国产中文字幕一区 | 成人在线网址 | 中文字幕第9页 | 欧美久久久精品 | 欧美日韩免费在线 | 国产成人久久精品一区二区三区 | 亚洲国产精品久久 | 人人九九精 | 久久精品亚洲精品 | 日韩无在线 | 欧美日韩在线一区二区三区 | 成人久久久精品乱码一区二区三区 | 三级电影网址 | 精品成人一区二区三区 | 91亚洲国产精品 | 成人在线看片 | 精品久久久久久久 | 亚洲午夜一区 | 欧美精品久久 | 亚洲午夜精品视频 | 国产欧美日韩综合精品一区二区 | 国产综合精品一区二区三区 | 人人爱人人草 | 午夜在线电影 | 国产偷窥老熟盗摄视频 | 中日韩午夜理伦电影免费 | av网站在线播放 | 久草久草久草 | 久久99蜜桃综合影院免费观看 | 精品日韩一区二区 | 精品久久久久一区二区国产 | 久久久精品日本 | 欧美精品综合 | 91精品国产91久久综合桃花 | 亚洲一区中文字幕在线观看 | 国产精品99久久久久久www | 精品一二三区 |