一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

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

服務(wù)器之家 - 編程語言 - Java教程 - Java實(shí)現(xiàn)兩人五子棋游戲(五) 判斷是否有一方勝出

Java實(shí)現(xiàn)兩人五子棋游戲(五) 判斷是否有一方勝出

2021-04-15 11:53v_xchen_v Java教程

這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)兩人五子棋游戲,判斷是否有一方勝出,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

之前的兩篇文章:java實(shí)現(xiàn)兩人五子棋游戲(二) 畫出棋盤java實(shí)現(xiàn)兩人五子棋游戲(三) 畫出棋子java實(shí)現(xiàn)兩人五子棋游戲(四) 落子動(dòng)作的實(shí)現(xiàn),可以點(diǎn)擊查看。

Java實(shí)現(xiàn)兩人五子棋游戲(五) 判斷是否有一方勝出

前面我們已經(jīng)畫好了棋盤、棋子并且可以自由的落子了,那么接下來要實(shí)現(xiàn)的功能是判斷是否有五連珠(暫時(shí)不考慮行棋方)。

我們采用遍歷棋盤已經(jīng)落子的位置,查看每個(gè)落子點(diǎn),在它的上下,左右,左下右上,左上右下四個(gè)方向的任一方向上是否有五個(gè)連續(xù)的棋子。

第一步,對棋子類進(jìn)行改造,之前我們的棋子類只有顏色信息和落子狀態(tài),現(xiàn)在要新增一個(gè)int型的數(shù)據(jù),用于記錄遍歷過程中當(dāng)前有幾個(gè)珠子已知連續(xù)。

Java實(shí)現(xiàn)兩人五子棋游戲(五) 判斷是否有一方勝出

chessman.java

?
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
package xchen.test.simplegobang;
 
public class chessman {
  private int color;//1-white,0-black
  private boolean placed = false;
  int matchcount = 1;
   
  public chessman(int color,boolean placed){
    this.color=color;
    this.placed=placed;
  }
   
  public boolean getplaced() {
    return placed;
  }
 
  public void setplaced(boolean placed) {
    this.placed = placed;
  }
 
  public int getcolor() {
    return color;
  }
 
  public void setcolor(int color) {
    this.color = color;
  }
}

第二步,先從一個(gè)方向上判斷是否有五連珠,這里采用左右方向作為嘗試。

添加了一個(gè)iswin函數(shù),用遍歷整個(gè)棋盤上的有效棋子的方式,來進(jìn)行勝出判斷。

drawchessboard.java

?
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
package xchen.test.simplegobang;
 
import java.awt.graphics;
import java.awt.graphics2d;
import java.awt.radialgradientpaint;
import java.awt.image;
import java.awt.toolkit;
import java.awt.event.mouseevent;
import java.awt.event.mouselistener;
import java.awt.color;
import javax.swing.jpanel;
 
public class drawchessboard extends jpanel implements mouselistener{
  final static int black=0;
  final static int white=1;
  public int chesscolor = black;
  int chessman_width=30;
   
  public image boardimg;
  final private int rows = 19;
  chessman[][] chessstatus=new chessman[rows+1][rows+1]; 
   
  public drawchessboard() {
    boardimg = toolkit.getdefaulttoolkit().getimage("res/drawable/chessboard2.png");
    if(boardimg == null)
      system.err.println("png do not exist");
     
    addmouselistener(this);
  }  
  @override
  protected void paintcomponent(graphics g) {
    // todo auto-generated method stub
    super.paintcomponent(g);
     
    int imgwidth = boardimg.getheight(this);
    int imgheight = boardimg.getwidth(this);
    int fwidth = getwidth();
    int fheight= getheight();
     
    int x=(fwidth-imgwidth)/2;
    int y=(fheight-imgheight)/2;
     
    int span_x=imgwidth/rows;
    int span_y=imgheight/rows;
     
    g.drawimage(boardimg, x, y, null);
     
    //畫橫線
    for(int i=0;i<rows;i++)
    {
      g.drawline(x, y+i*span_y, fwidth-x,y+i*span_y);
    }
    //畫豎線
    for(int i=0;i<rows;i++)
    {
      g.drawline(x+i*span_x, y, x+i*span_x,fheight-y);
    }
     
    //畫棋子
    for(int i=0;i<rows+1;i++)
    {
      for(int j=0;j<rows+1;j++)
      {
        if(chessstatus[i][j]!=null&&chessstatus[i][j].getplaced()==true)
        {
          //system.out.println("draw chessman "+i+" "+j);
          int pos_x=x+i*span_x;
          int pos_y=y+j*span_y;
          float radius_b=40;
          float radius_w=80;
          float[] fractions = new float[]{0f,1f};
          java.awt.color[] colors_b = new java.awt.color[]{color.black,color.white};
          color[] colors_w = new color[]{color.white,color.black};
          radialgradientpaint paint;
          if(chessstatus[i][j].getcolor()==1)
          {
            //system.out.println("draw white chess");
            paint = new radialgradientpaint(pos_x-chessman_width/2f, pos_y-chessman_width/2f, radius_w*2, fractions, colors_w);
          }else{
            //system.out.println("draw black chess");
            paint = new radialgradientpaint(pos_x-chessman_width/2f, pos_y-chessman_width/2f, radius_b*2, fractions, colors_b);
          }
          ((graphics2d)g).setpaint(paint);
           
          ((graphics2d)g).filloval(pos_x-chessman_width/2,pos_y-chessman_width/2,chessman_width,chessman_width);
        }
      }
    }
  }
   
  @override
  //當(dāng)用戶按下鼠標(biāo)按鈕時(shí)發(fā)生
  public void mousepressed(mouseevent e) {
    int point_x=e.getx();
    int point_y=e.gety();
     
    int imgwidth = boardimg.getheight(this);
    int imgheight = boardimg.getwidth(this);
    int fwidth = getwidth();
    int fheight= getheight();
     
    int x=(fwidth-imgwidth)/2;
    int y=(fheight-imgheight)/2;
     
    int span_x=imgwidth/rows;
    int span_y=imgheight/rows;
     
    //system.out.println("press");
    int status_x = 0;
    int status_y = 0;
    if(point_x>=x && point_x<=x+imgwidth && point_y>=y && point_y <= y+imgheight)
    {
      //system.out.println("合法");
      for(int i=0;i<rows+1;i++)
      {
        if(point_x>=x-chessman_width/2+1+i*span_x)
        {
          if(point_x<=x+chessman_width/2-1+i*span_x)//如果是width/2會(huì)在中間點(diǎn)出現(xiàn)兩個(gè)匹配值
          {
            //system.out.println("point x "+i+" "+point_x+" "+(x-chessman_width/2+i*span_x)+" "+(x+chessman_width/2+i*span_x));
            status_x = i;
          }
        }
      }
      for(int i=0;i<rows+1;i++)
      {
        if(point_y>=y-chessman_width/2+1+i*span_y)
        {
          if(point_y <= y+chessman_width/2-1+i*span_y)
          {
            //system.out.println("point y "+i+" "+point_y+" "+(y-chessman_width/2+1+i*span_y)+" "+(y+chessman_width/2-1+i*span_y));
            status_y = i;
          }
        }
      }
      chessman chessman = new chessman(black, true);
      chessstatus[status_x][status_y]=chessman;
      repaint();
      if(iswin(status_x, status_y, chessstatus))
      {
        system.out.println("win!!!!!");
      }
    }
  }
  @override
  //當(dāng)用戶按下并松開鼠標(biāo)按鈕時(shí)發(fā)生
  public void mouseclicked(mouseevent e) {
    // todo auto-generated method stub
  }
  @override
  public void mousereleased(mouseevent e) {
    // todo auto-generated method stub
     
  }
  @override
  public void mouseentered(mouseevent e) {
    // todo auto-generated method stub
     
  }
  @override
  public void mouseexited(mouseevent e) {
    // todo auto-generated method stub 
  }
   
  boolean iswin(int point_x,int point_y,chessman[][] cm)
  {
    //int matchcount = 1;//記錄連珠的數(shù)目
     
    //橫向查找
    for(int i=0;i<rows+1;i++)
    {
      for(int j=0;j<rows+1;j++)
      {
        if(chessstatus[i][j]!=null&&chessstatus[i][j].getplaced()==true)
        {
          //system.out.println("iswin:"+i+" "+j);
          //向右側(cè)查找
          for(int n=1;n<=4;n++)
          {
            if((i+n>=0)&&(i+n)<=rows)
            {
              if(chessstatus[i+n][j]!=null&&chessstatus[i+n][j].getplaced()==true)
              {
                chessstatus[i][j].matchcount++;
                system.out.println("pos:"+i+" "+j+" right count++:"+(i+n)+" "+j+" count:"+chessstatus[i][j].matchcount);
                if(chessstatus[i][j].matchcount==5)
                {
                  return true;
                }
              }else
              {
                break;
              }  
            }
          }
          //向左側(cè)查找
          for(int n=1;n<=4;n++)
          {
            if((i-n>=0)&&(i-n)<=rows)
            {
              if(chessstatus[i-n][j]!=null&&chessstatus[i-n][j].getplaced()==true)
              {
                chessstatus[i][j].matchcount++;
                system.out.println("pos:"+i+" "+j+" "+"left count++:"+(i-n)+" "+j+" count:"+chessstatus[i][j].matchcount);
                if(chessstatus[i][j].matchcount==5)
                {
                  return true;
                }
              }else
              {
                if(chessstatus[i-n][j]!=null)
                {
                  chessstatus[i][j].matchcount = 1;
                }
                break;
              }
            }
          }
          chessstatus[i][j].matchcount=1;//refresh count
        }
      }
    }
    return false;
     
  }
}

第三步,主模塊不變,運(yùn)行測試一下我們的算法是否正確

main.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package xchen.test.simplegobang;
 
import java.awt.container;
import javax.swing.jframe;
 
import xchen.test.simplegobang.drawchessboard;
 
public class main extends jframe{
  private drawchessboard drawchessboard;
  public main() {   
    drawchessboard = new drawchessboard();
     
    //frame標(biāo)題
    settitle("單機(jī)五子棋");
     
    container containerpane =getcontentpane();
    containerpane.add(drawchessboard);   
  }
  public static void main(string[] args) {
    main m = new main();
    m.setsize(800, 800);
    m.setvisible(true);
  }
}

Java實(shí)現(xiàn)兩人五子棋游戲(五) 判斷是否有一方勝出

第四步,現(xiàn)在我們一個(gè)方向上的判斷已經(jīng)做好了,接下來補(bǔ)全其他三個(gè)方向上的判斷代碼

補(bǔ)足drawchessboard.java中的iswin()函數(shù)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
package xchen.test.simplegobang;
 
import java.awt.color;
import java.awt.graphics;
import java.awt.graphics2d;
import java.awt.image;
import java.awt.radialgradientpaint;
import java.awt.toolkit;
import java.awt.event.mouseevent;
import java.awt.event.mouselistener;
 
import javax.swing.jpanel;
 
public class drawchessboard extends jpanel implements mouselistener{
  final static int black=0;
  final static int white=1;
  public int chesscolor = black;
  int chessman_width=30;
 
  public image boardimg;
  final private int rows = 19;
  chessman[][] chessstatus=new chessman[rows+1][rows+1]; 
 
  public drawchessboard() {
    boardimg = toolkit.getdefaulttoolkit().getimage("res/drawable/chessboard2.png");
    if(boardimg == null)
      system.err.println("png do not exist");
 
    addmouselistener(this);
  }  
  @override
  protected void paintcomponent(graphics g) {
    // todo auto-generated method stub
    super.paintcomponent(g);
 
    int imgwidth = boardimg.getheight(this);
    int imgheight = boardimg.getwidth(this);
    int fwidth = getwidth();
    int fheight= getheight();
 
    int x=(fwidth-imgwidth)/2;
    int y=(fheight-imgheight)/2;
 
    int span_x=imgwidth/rows;
    int span_y=imgheight/rows;
 
    g.drawimage(boardimg, x, y, null);
 
    //畫橫線
    for(int i=0;i<rows;i++)
    {
      g.drawline(x, y+i*span_y, fwidth-x,y+i*span_y);
    }
    //畫豎線
    for(int i=0;i<rows;i++)
    {
      g.drawline(x+i*span_x, y, x+i*span_x,fheight-y);
    }
 
    //畫棋子
    for(int i=0;i<rows+1;i++)
    {
      for(int j=0;j<rows+1;j++)
      {
        if(chessstatus[i][j]!=null&&chessstatus[i][j].getplaced()==true)
        {
          //system.out.println("draw chessman "+i+" "+j);
          int pos_x=x+i*span_x;
          int pos_y=y+j*span_y;
          float radius_b=40;
          float radius_w=80;
          float[] fractions = new float[]{0f,1f};
          java.awt.color[] colors_b = new java.awt.color[]{color.black,color.white};
          color[] colors_w = new color[]{color.white,color.black};
          radialgradientpaint paint;
          if(chessstatus[i][j].getcolor()==1)
          {
            //system.out.println("draw white chess");
            paint = new radialgradientpaint(pos_x-chessman_width/2f, pos_y-chessman_width/2f, radius_w*2, fractions, colors_w);
          }else{
            //system.out.println("draw black chess");
            paint = new radialgradientpaint(pos_x-chessman_width/2f, pos_y-chessman_width/2f, radius_b*2, fractions, colors_b);
          }
          ((graphics2d)g).setpaint(paint);
 
          ((graphics2d)g).filloval(pos_x-chessman_width/2,pos_y-chessman_width/2,chessman_width,chessman_width);
        }
      }
    }
  }
 
  @override
  //當(dāng)用戶按下鼠標(biāo)按鈕時(shí)發(fā)生
  public void mousepressed(mouseevent e) {
    int point_x=e.getx();
    int point_y=e.gety();
 
    int imgwidth = boardimg.getheight(this);
    int imgheight = boardimg.getwidth(this);
    int fwidth = getwidth();
    int fheight= getheight();
 
    int x=(fwidth-imgwidth)/2;
    int y=(fheight-imgheight)/2;
 
    int span_x=imgwidth/rows;
    int span_y=imgheight/rows;
 
    //system.out.println("press");
    int status_x = 0;
    int status_y = 0;
    if(point_x>=x && point_x<=x+imgwidth && point_y>=y && point_y <= y+imgheight)
    {
      //system.out.println("合法");
      for(int i=0;i<rows+1;i++)
      {
        if(point_x>=x-chessman_width/2+1+i*span_x)
        {
          if(point_x<=x+chessman_width/2-1+i*span_x)//如果是width/2會(huì)在中間點(diǎn)出現(xiàn)兩個(gè)匹配值
          {
            //system.out.println("point x "+i+" "+point_x+" "+(x-chessman_width/2+i*span_x)+" "+(x+chessman_width/2+i*span_x));
            status_x = i;
          }
        }
      }
      for(int i=0;i<rows+1;i++)
      {
        if(point_y>=y-chessman_width/2+1+i*span_y)
        {
          if(point_y <= y+chessman_width/2-1+i*span_y)
          {
            //system.out.println("point y "+i+" "+point_y+" "+(y-chessman_width/2+1+i*span_y)+" "+(y+chessman_width/2-1+i*span_y));
            status_y = i;
          }
        }
      }
      chessman chessman = new chessman(black, true);
      chessstatus[status_x][status_y]=chessman;
      repaint();
      if(iswin(status_x, status_y, chessstatus))
      {
        system.out.println("win!!!!!");
      }
    }
  }
  @override
  //當(dāng)用戶按下并松開鼠標(biāo)按鈕時(shí)發(fā)生
  public void mouseclicked(mouseevent e) {
    // todo auto-generated method stub
  }
  @override
  public void mousereleased(mouseevent e) {
    // todo auto-generated method stub
 
  }
  @override
  public void mouseentered(mouseevent e) {
    // todo auto-generated method stub
 
  }
  @override
  public void mouseexited(mouseevent e) {
    // todo auto-generated method stub 
  }
 
  boolean iswin(int point_x,int point_y,chessman[][] cm)
  {  
    for(int i=0;i<rows+1;i++)
    {
      for(int j=0;j<rows+1;j++)
      {
        //橫向查找
        if(chessstatus[i][j]!=null&&chessstatus[i][j].getplaced()==true)
        {
          //向右側(cè)查找
          for(int n=1;n<=4;n++)
          {
            if((i+n>=0)&&(i+n)<=rows)
            {
              if(chessstatus[i+n][j]!=null&&chessstatus[i+n][j].getplaced()==true)
              {
                chessstatus[i][j].matchcount++;
                system.out.println("pos:"+i+" "+j+" right count++:"+(i+n)+" "+j+" count:"+chessstatus[i][j].matchcount);
                if(chessstatus[i][j].matchcount==5)
                {
                  return true;
                }
              }else
              {
                break;
              }  
            }
          }
          //向左側(cè)查找
          for(int n=1;n<=4;n++)
          {
            if((i-n>=0)&&(i-n)<=rows)
            {
              if(chessstatus[i-n][j]!=null&&chessstatus[i-n][j].getplaced()==true)
              {
                chessstatus[i][j].matchcount++;
                system.out.println("pos:"+i+" "+j+" "+"left count++:"+(i-n)+" "+j+" count:"+chessstatus[i][j].matchcount);
                if(chessstatus[i][j].matchcount==5)
                {
                  return true;
                }
              }else
              {
                if(chessstatus[i-n][j]!=null)
                {
                  chessstatus[i][j].matchcount = 1;
                }
                break;
              }
            }
          }
          chessstatus[i][j].matchcount=1;//refresh count
        }
      }
    }
 
    for(int i=0;i<rows+1;i++)
    {
      for(int j=0;j<rows+1;j++)
      {
        //縱向
        if(chessstatus[i][j]!=null&&chessstatus[i][j].getplaced()==true)
        {
          //向下查找,左上角為坐標(biāo)原點(diǎn),y軸正方向向下
          for(int n=1;n<=4;n++)
          {
            if((j+n>=0)&&(j+n)<=rows)
            {
              if(chessstatus[i][j+n]!=null&&chessstatus[i][j+n].getplaced()==true)
              {
                chessstatus[i][j].matchcount++;
                system.out.println("pos:"+i+" "+j+" up count++:"+(i)+" "+(j+n)+" count:"+chessstatus[i][j].matchcount);
                if(chessstatus[i][j].matchcount==5)
                {
                  return true;
                }
              }else
              {
                break;
              }  
            }
          }
          //向上查找
          for(int n=1;n<=4;n++)
          {
            if((j-n>=0)&&(j-n)<=rows)
            {
              if(chessstatus[i][j-n]!=null&&chessstatus[i][j-n].getplaced()==true)
              {
                chessstatus[i][j].matchcount++;
                system.out.println("pos:"+i+" "+j+" "+"left count++:"+(i)+" "+(j-n)+" count:"+chessstatus[i][j].matchcount);
                if(chessstatus[i][j].matchcount==5)
                {
                  return true;
                }
              }else
              {
                if(chessstatus[i][j-n]!=null)
                {
                  chessstatus[i][j].matchcount = 1;
                }
                break;
              }
            }
          }
          chessstatus[i][j].matchcount=1;//refresh count
        }
      }
    }
 
    //方向:左上右下
    for(int i=0;i<rows+1;i++)
    {
      for(int j=0;j<rows+1;j++)
      {
        //左上
        if(chessstatus[i][j]!=null&&chessstatus[i][j].getplaced()==true)
        {
          //向下查找,左上角為坐標(biāo)原點(diǎn),y軸正方向向下
          for(int n=1;n<=4;n++)
          {
            if((j-n>=0)&&(j-n)<=rows&&(i-n)>=0&&(i-n)<=rows)
            {
              if(chessstatus[i-n][j-n]!=null&&chessstatus[i-n][j-n].getplaced()==true)
              {
                chessstatus[i][j].matchcount++;
                system.out.println("pos:"+i+" "+j+" up count++:"+(i-n)+" "+(j-n)+" count:"+chessstatus[i][j].matchcount);
                if(chessstatus[i][j].matchcount==5)
                {
                  return true;
                }
              }else
              {
                break;
              }  
            }
          }
          //右下
          for(int n=1;n<=4;n++)
          {
            if((j+n>=0)&&(j+n)<=rows&&(i+n)>=0&&(i+n)<=rows)
            {
              if(chessstatus[i+n][j+n]!=null&&chessstatus[i+n][j+n].getplaced()==true)
              {
                chessstatus[i][j].matchcount++;
                system.out.println("pos:"+i+" "+j+" "+"left count++:"+(i+n)+" "+(j+n)+" count:"+chessstatus[i][j].matchcount);
                if(chessstatus[i][j].matchcount==5)
                {
                  return true;
                }
              }else
              {
                if(chessstatus[i+n][j+n]!=null)
                {
                  chessstatus[i][j].matchcount = 1;
                }
                break;
              }
            }
          }
          chessstatus[i][j].matchcount=1;//refresh count
        }
      }
    }
 
    //方向:左下右上
    for(int i=0;i<rows+1;i++)
    {
      for(int j=0;j<rows+1;j++)
      {
        //左下
        if(chessstatus[i][j]!=null&&chessstatus[i][j].getplaced()==true)
        {
          //向下查找,左上角為坐標(biāo)原點(diǎn),y軸正方向向下
          for(int n=1;n<=4;n++)
          {
            if((j+n>=0)&&(j+n)<=rows&&(i-n)>=0&&(i-n)<=rows)
            {
              if(chessstatus[i-n][j+n]!=null&&chessstatus[i-n][j+n].getplaced()==true)
              {
                chessstatus[i][j].matchcount++;
                system.out.println("pos:"+i+" "+j+" up count++:"+(i-n)+" "+(j+n)+" count:"+chessstatus[i][j].matchcount);
                if(chessstatus[i][j].matchcount==5)
                {
                  return true;
                }
              }else
              {
                break;
              }  
            }
          }
          //右上
          for(int n=1;n<=4;n++)
          {
            if((j-n>=0)&&(j-n)<=rows&&(i+n)>=0&&(i+n)<=rows)
            {
              if(chessstatus[i+n][j-n]!=null&&chessstatus[i+n][j-n].getplaced()==true)
              {
                chessstatus[i][j].matchcount++;
                system.out.println("pos:"+i+" "+j+" "+"left count++:"+(i+n)+" "+(j-n)+" count:"+chessstatus[i][j].matchcount);
                if(chessstatus[i][j].matchcount==5)
                {
                  return true;
                }
              }else
              {
                if(chessstatus[i+n][j-n]!=null)
                {
                  chessstatus[i][j].matchcount = 1;
                }
                break;
              }
            }
          }
          chessstatus[i][j].matchcount=1;//refresh count
        }
      }
    }    
 
    return false;  
  }
}

再運(yùn)行一下

Java實(shí)現(xiàn)兩人五子棋游戲(五) 判斷是否有一方勝出

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。

原文鏈接:https://blog.csdn.net/v_xchen_v/article/details/53440319

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 99爱在线观看精品视频 | meyd–456佐山爱在线播放 | 国产精品污双胞胎在线观看 | 奇米888在线看奇米999 | 国产一区二区三区水野朝阳 | 99精品视频只99有精品 | 国产精品视频二区不卡 | 欧美人曾交 | av在线色 | 成年性生交大片免费看 | 国产伦精一区二区三区视频 | 亚洲网视频 | brazzers欧美教师 | 大伊人青草狠狠久久 | 1024免费永久福利视频 | 超级碰在线视频 | 接吻吃胸摸下面啪啪教程 | 天天乐影院 | 艾秋果冻麻豆老狼 | 海绵宝宝第二季全集免费观看 | 欧美成人一区二区三区 | 色播影院性播影院私人影院 | 日本中文字幕在线观看视频 | 婷婷在线网站 | 欧美春宫| 国产日韩精品一区二区三区 | 乌克兰成人性色生活片 | 国产精品久热 | 亚洲国产精品二区久久 | 明星梦淫| 国产原创一区二区 | 亚洲天堂中文字幕 | bt岛www| 我年轻漂亮的继坶2中字在线播放 | 毛片大全免费看 | 91精品综合国产在线观看 | 亚洲系列国产精品制服丝袜第 | 国产精品嫩草影院一二三区 | 亚洲欧美影院 | 青青草原在线 | 超碰成人在线播放 |