服务器之家

服务器之家 > 正文

java实现五子棋小游戏

时间:2019-12-14 15:04     来源/作者:hebedich

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
package Gomoku;
 
import java.awt.Toolkit;
 
import javax.swing.JFrame;
 
 
public class GomokuFrame extends JFrame
{
  //定义一个操作面板
  OperatorPane op=null;
   
  public GomokuFrame()
  {
    //设置名称
    this.setTitle("五子棋");
    //设置窗口大小
    this.setSize(510,510);
    //设置窗口位置
      //取得电脑屏幕大小
    int computer_width=Toolkit.getDefaultToolkit().getScreenSize().width;
    int computer_height=Toolkit.getDefaultToolkit().getScreenSize().height;
    System.out.println("电脑屏幕的宽度:\n"+computer_width+"\n电脑屏幕的高度:\n"+computer_height);
        //居中
    this.setLocation((computer_width-510)/2, (computer_height-510)/2);   
     
    //实例化幕布
    op=new OperatorPane();
    //导入幕布
    this.add(op);
    //添加鼠标监听
    this.addMouseListener(op);
     
    //设置窗口的显示
    this.setVisible(true);
    //设置窗口的正常关闭
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }
   
   //执行测试
  public static void main(String[] args)
  {
    new GomokuFrame();
  }
     
}
?
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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
package Gomoku;
 
 
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
 
 
 
import java.awt.image.BufferedImage;
import java.awt.image.BufferedImageFilter;
 
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
 
public class OperatorPane extends JPanel implements MouseListener,Runnable
{
  //定义背景图片对象
  Image imageBackground = null;
  //定义棋盘的格子的行数
  int boardrows=18;
  //定义棋盘的格子的列数
  int boardcols=18;
  //定义棋盘的格子的大小
  int boardsize=20;
  //保存棋子坐标
  int x=0,y=0;
  //保存之前下过的全部棋子坐标
  // 其中数据内容 0: 表示这个点并没有棋子, 1: 表示这个点是黑子, 2:表示这个点是白子
  int allchess[][]=new int [19][19];
  //标记下一步下黑棋还是白棋
  boolean isBlack=true;
  //判断游戏是否能够继续
  boolean canPlay=true;
  //保存显示信息
  String message="黑方先行";
  // 保存最多拥有多少时间(秒)
  int maxTime = 0;
  // 做倒计时的线程类
  Thread t = new Thread(this);
  // 保存黑方与白方的剩余时间
  int blackTime = 0;
  int whiteTime = 0;
  // 保存双方剩余时间的显示信息
  String blackMessage = "无限制";
  String whiteMessage = "无限制";
   
   
  @SuppressWarnings("deprecation")
  public OperatorPane()
  {
 
    t.start();
    t.suspend();
     
     imageBackground=new ImageIcon("image/background.jpg").getImage();
  }
   
  public void paint(Graphics g)
  {
    //双缓冲技术
    BufferedImage b1=new BufferedImage(495,475,BufferedImage.TYPE_INT_ARGB);
    Graphics g2=b1.createGraphics();
     
    // 画出背景图片
    g2.drawImage(imageBackground, 0, 0,495,475,null);
     
     
    //画出棋盘线
    Color c=g2.getColor();
    g2.setColor(Color.BLACK);
    for(int i=0;i<=boardrows;i++)
    {
      g2.drawLine(10,50+boardsize*i,10+boardsize*boardrows,50+boardsize*i);
    }
    for(int i=0;i<=boardcols;i++)
    {
      g2.drawLine(10+boardsize*i,50,10+boardsize*i,50+boardsize*boardcols);
    }
     
    //画出三三位置
    g2.fillOval(67, 107, 6, 6);
    g2.fillOval(67, 347, 6, 6);
    g2.fillOval(307, 107, 6, 6);
    g2.fillOval(307, 347, 6, 6);
    //画出附点
    g2.fillOval(67, 227, 6, 6);
    g2.fillOval(307, 227, 6, 6);
    g2.fillOval(187, 107, 6, 6);
    g2.fillOval(187, 347, 6, 6);
     
    //画出天元
    g2.fillOval(187, 227, 6, 6);
     
     
     
     
    //画出文字提示
    /*Font f=new Font("黑体", Font.BOLD, 24);
    g.setFont(f);*/
    g2.setFont(new Font("黑体", Font.BOLD, 20));
    g2.setColor(Color.BLACK);
    g2.drawString("游戏信息:"+message, 130, 40);
    g2.setFont(new Font("宋体", Font.ITALIC, 15));
    g2.drawString("黑方时间:"+blackMessage,25, 445);
    g2.drawString("白方时间:"+whiteMessage,245, 445);
    
    //绘制全部棋子
    for(int i=0;i<=boardrows;i++)
    {
      for(int j=0;j<=boardcols;j++)
      {
        //存储黑棋
        if(allchess[i][j]==1)
        {
          int tempX=i*20-10;
          int tempY=j*20+30;
          //画出黑棋
          g2.setColor(Color.BLACK);
          g2.fillOval(tempX+12, tempY+13, 15, 15);
        }
         
        //存储白棋
        if(allchess[i][j]==2)
        {
          int tempX=i*20-10;
          int tempY=j*20+30;
          //画出白棋
          g2.setColor(Color.BLACK);
          g2.drawOval(tempX+12, tempY+13, 15, 15);
          g2.setColor(Color.WHITE);
          g2.fillOval(tempX+12, tempY+13, 15, 15);
        }
      }
 
    }
   
    g2.setColor(c);
     
    g.drawImage(b1,0,0,this);
  }
   
   
  private boolean checkWin()
  {
    boolean flag=false;
     
    int color = allchess[x][y];
     
    /*// 保存共有相同颜色多少棋子相连
    int count1=1;
    int count2=1;
    int count3=1;
    int count4=1;
     
    // 判断横向是否有5个棋子相连,特点 纵坐标 是相同, 即allChess[x][y]中y值是相同
    // 通过循环来做棋子相连的判断     
    int i = 1;
    while (color == allchess[x+i][y])
    {
      count1++;
      i++;
    }
    //将i值复位
    i = 1;
    while (color == allchess[x-i][y])
    {
      count1++;
      i++;
    }
    if(count1 >= 5)
    {
      flag = true;
    }
     
     
    //判断纵向 ,即allChess[x][y]中x值是相同
    int j = 1;
    while (color == allchess[x][y+j])
    {
      count2++;
      j++;
    }
    //将j值复位
    j = 1;
    while (color == allchess[x][y-j])
    {
      count2++;
      j++;
    }
    if(count2>= 5)
    {
      flag = true;
    }
     
    //判断斜向"\"
    int m1=1;
    int n1=1;
    while (color == allchess[x+m1][y+n1])
    {
      count3++;
      m1++;
      n1++;
    }
    m1=1;
    n1=1;
    while (color == allchess[x-m1][y-n1])
    {
      count3++;
      m1++;
      n1++;
    }
    if(count3>= 5)
    {
      flag = true;
    }
     
    //判断斜向"/"
    int m2=1;
    int n2=1;
    while (color == allchess[x+m2][y-n2])
    {
      count4++;
      m2++;
      n2++;
    }
    m2=1;
    n2=1;
    while (color == allchess[x-m2][y+n2])
    {
      count4++;
      m2++;
      n2++;
    }
    if(count4>= 5)
    {
      flag = true;
    }
     
*/
    int count;
    //横向判断
    count=this.checkCount(1, 0, color);
    if(count>=5)
    {
      flag = true;
    }
    else
    {
      //纵向判断
      count=this.checkCount(0, 1, color);
      if(count>=5)
      {
        flag = true;
      }
      else
      {
        //斜向“/”
        count=this.checkCount(1, 1, color);
        if(count>=5)
        {
          flag = true;
        }
        else
        {
          //斜向“\”
          count=this.checkCount(1, -1, color);
          if(count>=5)
          {
            flag = true;
          }
        }
      }
    }
     
     
     
    return flag;
  }
   
  private int checkCount(int xChange,int yChange,int color)
  {
    int count=1;
    int tempX=xChange;
    int tempY=yChange;
     
    while (color==allchess[x+xChange][y+yChange])
    {
      count++;
      if(xChange!=0)
      {
        xChange++;
      }
      if(yChange!=0)
      {
        if(yChange<0)
        {
          yChange--;
        
        else
          {
          yChange++;
          }
         
      }
       
    }
    //复位
    xChange=tempX;
    yChange=tempY;
    while (color==allchess[x-xChange][y-yChange])
    {
      count++;
      if(xChange!=0)
      {
        xChange++;
      }
      if(yChange!=0)
      {
        if(yChange<0)
        {
          yChange--;
        
        else
        {
          yChange++;
        }
      
    }
    return count;
 
  }
 
   
   
   
 
  public void mouseClicked(MouseEvent e)
  {
     
    System.out.println("x:"+e.getX()+"y:"+e.getY());
    x=e.getX();
    y=e.getY();
    if(x>=10&&x<=(10+boardsize*boardrows+20)&&y>=50&&y<=(50+boardsize*boardcols+40))
    {
      //System.out.println("点在棋盘内。");
      x=(x-10)/20;
      y=(y-50-20)/20;
       
       
      if(canPlay==true)
      {
        //判断当前要下什么颜色
        if(allchess[x][y]==0)
        {
          if(isBlack==true)
          {
            allchess[x][y]=1;
            isBlack=false;
            message="轮到白方";
          }
          else
          {
            allchess[x][y]=2;
            isBlack=true;
            message="轮到黑方";
          }
 
          // 判断这个棋子是否和其他的棋子连成5连,即判断游戏是否结束
          boolean winFlag=this.checkWin();
          if(winFlag==true)
          {
 
            JOptionPane.showMessageDialog(this,"游戏结束!"+
                (allchess[x][y]==1?"黑方":"白方")+"获胜。");
            canPlay=false;
          }
        }
        else
        {
          JOptionPane.showMessageDialog(this,"当前位置已经有棋子,请重新落子!");
        }
      }
      this.repaint();
    }
     
     
    //点击,游戏开始按钮
        //重新开始新的游戏
    if(e.getX()>=400&&e.getX()<=470&&e.getY()>=80&&e.getY()<=110)
    {
      int result=JOptionPane.showConfirmDialog(this, "设置完成,是否重新开始游戏?");
      if(result==0)
      {
        //重新开始的操作,allchess[][]数组中的信息全部为0
        //清空棋盘
        for (int i = 0; i < 19; i++)
        {
          for (int j = 0; j < 19; j++)
          {
            allchess[i][j] = 0;
          }
        }
        //另一种方式 allChess = new int[19][19];
        blackTime = maxTime;
        whiteTime = maxTime;
        if (maxTime > 0)
        {
          blackMessage = maxTime / 3600 + ":"
              + (maxTime / 60 - maxTime / 3600 * 60) + ":"
              + (maxTime - maxTime / 60 * 60);
          whiteMessage = maxTime / 3600 + ":"
              + (maxTime / 60 - maxTime / 3600 * 60) + ":"
              + (maxTime - maxTime / 60 * 60);
          t.resume();
        }
        else
        {
          blackMessage = "无限制";
          whiteMessage = "无限制";
        }
        message = "黑方先行";
        isBlack = true;
        this.canPlay = true;
        this.repaint();
      }
    }
     
    //点击 游戏设置按钮
    if(e.getX()>=400&&e.getX()<=470&&e.getY()>=130&&e.getY()<=160)
    {
      String input = JOptionPane
          .showInputDialog("请输入游戏的最大时间(单位:分钟),如果输入0,表示没有时间限制:");
      try {
        maxTime = Integer.parseInt(input) * 60;
        if (maxTime < 0)
        {
          JOptionPane.showMessageDialog(this, "请输入正确信息,不允许输入负数!");
        }
        if (maxTime == 0)
        {
          int result = JOptionPane.showConfirmDialog(this,
              "设置完成,是否重新开始游戏?");
          if (result == 0)
          {
            for (int i = 0; i < 19; i++)
            {
              for (int j = 0; j < 19; j++)
              {
                allchess[i][j] = 0;
              }
            }
            // 另一种方式 allChess = new int[19][19];
            message = "黑方先行";
            isBlack = true;
            blackTime = maxTime;
            whiteTime = maxTime;
            blackMessage = "无限制";
            whiteMessage = "无限制";
            this.canPlay = true;
            this.repaint();
          }
        }
        if (maxTime > 0)
        {
          int result = JOptionPane.showConfirmDialog(this,
              "设置完成,是否重新开始游戏?");
          if (result == 0)
          {
            for (int i = 0; i < 19; i++)
            {
              for (int j = 0; j < 19; j++)
              {
                allchess[i][j] = 0;
              }
            }
            // 另一种方式 allChess = new int[19][19];
            message = "黑方先行";
            isBlack = true;
            blackTime = maxTime;
            whiteTime = maxTime;
            blackMessage = maxTime / 3600 + ":"
                + (maxTime / 60 - maxTime / 3600 * 60) + ":"
                + (maxTime - maxTime / 60 * 60);
            whiteMessage = maxTime / 3600 + ":"
                + (maxTime / 60 - maxTime / 3600 * 60) + ":"
                + (maxTime - maxTime / 60 * 60);
            t.resume();
            this.canPlay = true;
            this.repaint();
          }
        }
      }
      catch (NumberFormatException e1)
      {
        // TODO Auto-generated catch block
        JOptionPane.showMessageDialog(this, "请正确输入信息!");
      }
    }
     
    //点击 游戏说明按钮
    if(e.getX()>=400&&e.getX()<=470&&e.getY()>=180&&e.getY()<=210)
    {
      JOptionPane.showMessageDialog(this,"这个一个五子棋游戏程序,黑白双方轮流下棋,当某一方连到五子时,游戏结束。");
    }
     
    //点击 认输按钮
    if(e.getX()>=400&&e.getX()<=470&&e.getY()>=280&&e.getY()<=310)
    {
      int result=JOptionPane.showConfirmDialog(this,"是否确定认输?");
      if (result == 0)
      {
        if (isBlack)
        {
          JOptionPane.showMessageDialog(this, "黑方已经认输,游戏结束!");
        }
        else
        {
          JOptionPane.showMessageDialog(this, "白方已经认输,游戏结束!");
        }
        canPlay = false;
      }
    }
     
    //点击 关于按钮
    if(e.getX()>=400&&e.getX()<=470&&e.getY()>=330&&e.getY()<=360)
    {
      JOptionPane.showMessageDialog(this,"本游戏由南木工作室制作,有相关问题可以访问www.yiyiinformation.com");
    }
     
    //点击 退出按钮
    if(e.getX()>=400&&e.getX()<=470&&e.getY()>=380&&e.getY()<=410)
    {
      JOptionPane.showMessageDialog(this, "游戏结束");
      System.exit(0);
    }
     
  }
   
   
   
  //************************//
  @Override
  public void mouseEntered(MouseEvent arg0)
  {
    // TODO Auto-generated method stub
     
  }
 
  @Override
  public void mouseExited(MouseEvent arg0)
  {
    // TODO Auto-generated method stub
     
  }
 
  @Override
  public void mousePressed(MouseEvent arg0)
  {
    // TODO Auto-generated method stub
     
  }
 
  @Override
  public void mouseReleased(MouseEvent arg0) {
    // TODO Auto-generated method stub
     
  }
 
  @Override
  public void run()
  {
    if (maxTime > 0)
    {
      while (true)
      {
        if (isBlack)
        {
          blackTime--;
          if (blackTime == 0)
          {
            JOptionPane.showMessageDialog(this, "黑方超时,游戏结束!");
          }
        }
        else
        {
          whiteTime--;
          if (whiteTime == 0)
          {
            JOptionPane.showMessageDialog(this, "白方超时,游戏结束!");
          }
        }
        blackMessage = blackTime / 3600 + ":"
            + (blackTime / 60 - blackTime / 3600 * 60) + ":"
            + (blackTime - blackTime / 60 * 60);
        whiteMessage = whiteTime / 3600 + ":"
            + (whiteTime / 60 - whiteTime / 3600 * 60) + ":"
            + (whiteTime - whiteTime / 60 * 60);
        this.repaint();
        try
        {
          Thread.sleep(1000);
        }
        catch (InterruptedException e)
        {
          // TODO Auto-generated catch block
          e.printStackTrace();
        }
        System.out.println(blackTime + " -- " + whiteTime);
      }
    }
  }
     
}
   
 
  

演示图:

java实现五子棋小游戏

以上所述就是本文的全部内容了,希望能够对大家熟练掌握java有所帮助。

标签:

相关文章

热门资讯

玄元剑仙肉身有什么用 玄元剑仙肉身境界等级划分
玄元剑仙肉身有什么用 玄元剑仙肉身境界等级划分 2019-06-21
男生常说24816是什么意思?女生说13579是什么意思?
男生常说24816是什么意思?女生说13579是什么意思? 2019-09-17
配置IIS网站web服务器的安全策略配置解决方案
配置IIS网站web服务器的安全策略配置解决方案 2019-05-23
Nginx服务器究竟是怎么执行PHP项目
Nginx服务器究竟是怎么执行PHP项目 2019-05-24
华为nova5pro和p30pro哪个好 华为nova5pro和华为p30pro对比详情
华为nova5pro和p30pro哪个好 华为nova5pro和华为p30pro对比详情 2019-06-22
返回顶部