zoukankan      html  css  js  c++  java
  • Java实例---黑白五子棋[单机版]

    程序分析

    FiveChessFrame.java

      1 package com.ftl.frame;
      2 
      3 import java.awt.Color;
      4 import java.awt.Font;
      5 import java.awt.Graphics;
      6 import java.awt.Toolkit;
      7 import java.awt.event.MouseEvent;
      8 import java.awt.event.MouseListener;
      9 import java.awt.image.BufferedImage;
     10 import java.io.File;
     11 import java.io.IOException;
     12 
     13 import javax.imageio.ImageIO;
     14 import javax.swing.JFrame;
     15 import javax.swing.JOptionPane;
     16 
     17 public class FiveChessFrame extends JFrame implements MouseListener, Runnable {
     18 
     19     // 获取屏幕高
     20     int height = (int) Toolkit.getDefaultToolkit().getScreenSize().getHeight();
     21 
     22     // 获取屏幕宽
     23     int width = (int) Toolkit.getDefaultToolkit().getScreenSize().getWidth();
     24 
     25     // 获取图片
     26     BufferedImage image = null;
     27 
     28     // 保存X坐标
     29     int x = 0;
     30 
     31     // 保存Y坐标
     32     int y = 0;
     33 
     34     // 标识黑白棋子
     35     boolean isBlack = true;
     36 
     37     // 表示当前游戏是否可以就
     38     boolean canPlay = true;
     39 
     40     // 保存显示的信息
     41     String message = "黑方";
     42 
     43     // 游戏设置(线程操作) 最大时间/秒
     44     int maxTime = 0;
     45 
     46     // 倒计时的线程
     47     Thread t = new Thread(this);
     48 
     49     // 保存黑白方的剩余时间
     50     int blackTime = 0;
     51     int whiteTime = 0;
     52 
     53     // 保存双方剩余时间的显示信息
     54 
     55     String blackMessage = "无限制";
     56     String whiteMessage = "无限制";
     57 
     58     // 二维数组保存所有的图标
     59     // 其中数据内容 0 :空 1: 黑 2: 白
     60     int[][] allChess = new int[19][19];
     61 
     62     public FiveChessFrame() {
     63 
     64         // 设置窗体大xiao
     65         this.setSize(500, 500);
     66 
     67         // 设置标题
     68         this.setTitle("五子棋");
     69 
     70         // 设置关闭窗口即关闭程序
     71         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     72 
     73         // 设置可以放大窗体
     74         this.setResizable(false);
     75 
     76         // 设置位置
     77         this.setLocation((width - 500) / 2, (height - 500) / 2);
     78 
     79         // 增加鼠标监听
     80         this.addMouseListener(this);
     81 
     82         try {
     83             image = ImageIO.read(new File("gcg.jpg"));
     84         } catch (IOException e) {
     85             // TODO 自动生成的 catch 块
     86             e.printStackTrace();
     87         }
     88         //现场开始 
     89         t.start();
     90         t.suspend();
     91         
     92         //防止黑屏幕
     93         this.repaint();
     94         
     95         // 设置是否可见
     96         this.setVisible(true);
     97     }
     98 
     99     // 画画窗口
    100     public void paint(Graphics g) {
    101         // 双缓冲技术
    102         // BufferedImage bi = new BufferedImage(500, 500,
    103         // BufferedImage.TYPE_INT_ARGB);
    104         // Graphics g = bi.createGraphics();
    105         //
    106         Font fontOld = g.getFont();
    107         g.drawImage(image, 1, 20, this);
    108         g.setFont(new Font("黑体", Font.BOLD, 20));
    109         g.drawString("游戏信息:" + message, 160, 60);
    110         g.setFont(new Font("宋体", 10, 20));
    111         g.drawString("黑方时间: " + blackMessage, 40, 470);
    112         g.drawString("白方时间: " + whiteMessage, 260, 470);
    113 
    114         // 画出底线
    115         for (int i = 0; i < 19; i++) {
    116             g.drawLine(10, 70 + 20 * i, 370, 70 + 20 * i);
    117             g.drawLine(10 + 20 * i, 70, 10 + 20 * i, 430);
    118         }
    119 
    120         // 标注位
    121         g.fillOval(68, 128, 4, 4);
    122         g.fillOval(308, 128, 4, 4);
    123         g.fillOval(68, 368, 4, 4);
    124         g.fillOval(308, 368, 4, 4);
    125         g.fillOval(308, 248, 4, 4);
    126         g.fillOval(188, 128, 4, 4);
    127         g.fillOval(68, 248, 4, 4);
    128         g.fillOval(188, 368, 4, 4);
    129         g.fillOval(188, 248, 4, 4); // 圆点
    130 
    131         // 绘制棋子
    132         // g.fillOval(x, y, 10, 10);
    133         // x = (x-10)/20 * 20 + 10; y = (y-70)/20 * 20 + 70;
    134 
    135         for (int i = 0; i < 19; i++) {
    136             for (int j = 0; j < 19; j++) {
    137                 // 黑子
    138                 if (allChess[i][j] == 1) {
    139                     int tempX = i * 20 + 10;
    140                     int tempY = j * 20 + 70;
    141                     g.fillOval(tempX - 7, tempY - 7, 14, 14);
    142                 }
    143                 // 白子
    144                 if (allChess[i][j] == 2) {
    145                     int tempX = i * 20 + 10;
    146                     int tempY = j * 20 + 70;
    147                     g.setColor(Color.WHITE);
    148                     g.fillOval(tempX - 7, tempY - 7, 14, 14);
    149                     g.setColor(Color.BLACK);
    150                     g.drawOval(tempX - 7, tempY - 7, 14, 14);
    151                 }
    152             }
    153         }
    154         // g2.drawImage(bi, 0, 0, this);
    155 
    156     }
    157 
    158     @Override
    159     public void mouseClicked(MouseEvent e) {
    160         // TODO 自动生成的方法存根
    161 
    162     }
    163 
    164     @Override
    165     public void mouseEntered(MouseEvent e) {
    166         // TODO 自动生成的方法存根
    167 
    168     }
    169 
    170     @Override
    171     public void mouseExited(MouseEvent e) {
    172         // TODO 自动生成的方法存根
    173 
    174     }
    175 
    176     @Override
    177     public void mousePressed(MouseEvent e) {
    178 
    179         if (canPlay == true) {
    180             // 获取X坐标
    181             x = e.getX();
    182             // 获取Y坐标
    183             y = e.getY();
    184             // 棋盘范围
    185             if (x >= 10 && x <= 370 && y >= 70 && y <= 430) {
    186                 // 重新绘制,重新执行paint方法
    187                 x = (x - 10) / 20;
    188                 y = (y - 70) / 20;
    189 
    190                 if (allChess[x][y] == 0) {
    191 
    192                     // 判读棋子颜色
    193                     if (isBlack == true) {
    194                         allChess[x][y] = 1;
    195                         isBlack = false;
    196                         message = "白方";
    197                     } else {
    198                         allChess[x][y] = 2;
    199                         isBlack = true;
    200                         message = "黑方";
    201                     }
    202 
    203                     // 判断是否连线结束
    204                     boolean winFlag = this.checkWin();
    205                     if (winFlag == true) {
    206                         JOptionPane.showMessageDialog(this, "游戏结束!"
    207                                 + (allChess[x][y] == 1 ? "黑方" : "白方") + "获胜");
    208                         canPlay = false;
    209                     }
    210                 } else {
    211                     JOptionPane.showMessageDialog(this, "当前位置已经有棋子,请重新 落子");
    212                 }
    213                 this.repaint();
    214             }
    215         }
    216         // 开始游戏
    217         if (e.getX() >= 400 && e.getX() <= 470 && e.getY() >= 70
    218                 && e.getY() <= 100) {
    219             int result = JOptionPane.showConfirmDialog(this, "是否重新开始游戏?");
    220             if (result == 0) {
    221                 // 棋盘清空,allChess数组的数据归0 (双层循环也可以)
    222                 // 游戏信息的显示改回开始位置
    223                 // canplay = true, isBlack = true,黑方开始游戏
    224 
    225                 allChess = new int[19][19];
    226                 message = "黑方";
    227                 isBlack = true;
    228                 canPlay = true;
    229                 // 初始化白方和黑房时间
    230                 blackTime = maxTime;
    231                 whiteTime = maxTime;
    232                 if (maxTime > 0) {
    233                     blackMessage = maxTime / 3600 + ":"
    234                             + (maxTime / 60 - maxTime / 3600 * 60) + ":"
    235                             + (maxTime - maxTime / 60 * 60);
    236                     whiteMessage = blackMessage;
    237                     t.resume();
    238                 }
    239                 else {
    240                     blackMessage = "无限制";
    241                     whiteMessage = "无限制";
    242                 }
    243                 this.repaint();
    244             }
    245 
    246             if (result == 1) {
    247                 JOptionPane.showMessageDialog(this, "游戏继续");
    248             }
    249             if (result == 2) {
    250                 JOptionPane.showMessageDialog(this, "请继续游戏!!!");
    251             }
    252         }
    253         // 游戏设置
    254         if (e.getX() >= 400 && e.getX() <= 470 && e.getY() >= 120
    255                 && e.getY() <= 150) {
    256             String input = JOptionPane.showInputDialog(this,
    257                     "请输入游戏的最大时间?(分钟),) 0表示没有时间限制");
    258             try {
    259                 maxTime = Integer.parseInt(input) * 60;
    260                 if (maxTime < 0) {
    261                     JOptionPane.showMessageDialog(this, "时间不能为负,请输入正确时间信息!!!");
    262                 }
    263 
    264                 if (maxTime == 0) {
    265                     int result = JOptionPane.showConfirmDialog(this,
    266                             "设置完成,是否重新开始游戏?");
    267                     if (result == 0) {
    268                         // 棋盘清空,allChess数组的数据归0 (双层循环也可以)
    269                         // 游戏信息的显示改回开始位置
    270                         // canplay = true, isBlack = true,黑方开始游戏
    271 
    272                         allChess = new int[19][19];
    273                         message = "黑方";
    274                         isBlack = true;
    275                         canPlay = true;
    276                         blackTime = maxTime;
    277                         whiteTime = maxTime;
    278                         blackMessage = "无限制";
    279                         whiteMessage = "无限制";
    280                         this.repaint();
    281                     }
    282 
    283                     if (result == 1) {
    284                         JOptionPane.showMessageDialog(this, "游戏继续");
    285                     }
    286                     if (result == 2) {
    287                         JOptionPane.showMessageDialog(this, "请继续游戏!!!");
    288                     }
    289                 }
    290 
    291                 if (maxTime > 0) {
    292                     int result = JOptionPane.showConfirmDialog(this,
    293                             "设置完成,是否重新开始游戏?");
    294                     if (result == 0) {
    295                         // 棋盘清空,allChess数组的数据归0 (双层循环也可以)
    296                         // 游戏信息的显示改回开始位置
    297                         // canplay = true, isBlack = true,黑方开始游戏
    298 
    299                         allChess = new int[19][19];
    300                         message = "黑方";
    301                         isBlack = true;
    302                         canPlay = true;
    303                         blackTime = maxTime;
    304                         whiteTime = maxTime;
    305                         blackMessage = maxTime / 3600 + ":"
    306                                 + (maxTime / 60 - maxTime / 3600 * 60) + ":"
    307                                 + (maxTime - maxTime / 60 * 60);
    308                         whiteMessage = blackMessage;
    309                         t.resume();
    310                         this.repaint();
    311                     }
    312 
    313                     if (result == 1) {
    314                         JOptionPane.showMessageDialog(this, "游戏继续");
    315                     }
    316                     if (result == 2) {
    317                         JOptionPane.showMessageDialog(this, "请继续游戏!!!");
    318                     }
    319                 }
    320 
    321             } catch (Exception e2) {
    322                 JOptionPane.showMessageDialog(this, "请输入正确时间信息!!!");
    323             }
    324 
    325             canPlay = true;
    326         }
    327         // 游戏说明
    328         if (e.getX() >= 400 && e.getX() <= 470 && e.getY() >= 170
    329                 && e.getY() <= 200) {
    330             JOptionPane.showMessageDialog(this, "游戏说明");
    331         }
    332         // 认输
    333         if (e.getX() >= 400 && e.getX() <= 470 && e.getY() >= 270
    334                 && e.getY() <= 300) {
    335             int result = JOptionPane.showConfirmDialog(this, "是否认输?");
    336             if (result == 0) {
    337                 if (isBlack) {
    338                     JOptionPane.showMessageDialog(this, "黑方,已经认输,游戏结束!");
    339                 } else {
    340                     JOptionPane.showMessageDialog(this, "白方,已经认输,游戏结束!");
    341                 }
    342                 canPlay = false;
    343             }
    344             if (result == 1) {
    345                 JOptionPane.showMessageDialog(this, "游戏继续");
    346             }
    347             if (result == 2) {
    348                 JOptionPane.showMessageDialog(this, "请继续游戏!!!");
    349             }
    350         }
    351         // 关于
    352         if (e.getX() >= 400 && e.getX() <= 470 && e.getY() >= 320
    353                 && e.getY() <= 350) {
    354             JOptionPane.showMessageDialog(this, "关于");
    355         }
    356         // 退出
    357         if (e.getX() >= 400 && e.getX() <= 470 && e.getY() >= 370
    358                 && e.getY() <= 400) {
    359             JOptionPane.showMessageDialog(this, "退出");
    360             System.exit(0);
    361         }
    362     }
    363 
    364     @Override
    365     public void mouseReleased(MouseEvent e) {
    366         // TODO 自动生成的方法存根
    367 
    368     }
    369 
    370     public boolean checkWin() {
    371     
    372 //        boolean flag = false;
    373 //        int i = 1;
    374 //        int count = 1;
    375 //        int color = allChess[x][y]; // 判断黑白
    376 //        // 判断横向      是否有5个棋子相连,即 allChess[x][y]的y 是相同的
    377 //        while (color == allChess[x + i][y]) {
    378 //            count++;
    379 //            i++;
    380 //        }
    381 //        i = 1;
    382 //        while (color == allChess[x - i][y]) {
    383 //            count++;
    384 //            i++;
    385 //        }
    386 //        if (count >= 5) {
    387 //            flag = true;
    388 //        }
    389 //        
    390 //        
    391 //        //纵向        
    392 //        int i2 = 1;
    393 //        int count2 = 1;
    394 //        while (color == allChess[x][y + i2]) {
    395 //            count2++;
    396 //            i2++;
    397 //        }
    398 //        i2 = 1;
    399 //        while (color == allChess[x][y - i2]) {
    400 //            count2++;
    401 //            i2++;
    402 //        }
    403 //        if (count2 >= 5) {
    404 //            flag = true;
    405 //        }
    406 //        
    407 //        //斜方向(右上 + 坐下)
    408 //        int i3 = 1;
    409 //        int count3= 1;
    410 //        while (color == allChess[x + i3][y - i3]) {
    411 //            count3++;
    412 //            i3++;
    413 //        }
    414 //        i3 = 1;
    415 //        while (color == allChess[x - i3][y + i3]) {
    416 //            count3++;
    417 //            i3++;
    418 //        }
    419 //        if (count3 >= 5) {
    420 //            flag = true;
    421 //        }
    422 //        
    423 //        //左上 + 右下
    424 //        int i4 = 1;
    425 //        int count4= 1;
    426 //        while (color == allChess[x - i4][y - i4]) {
    427 //            count4++;
    428 //            i4++;
    429 //        }
    430 //        i4 = 1;
    431 //        while (color == allChess[x + i4][y + i4]) {
    432 //            count4++;
    433 //            i4++;
    434 //        }
    435 //        if (count4 >= 5) {
    436 //            flag = true;
    437 //        }
    438 //        
    439 //        
    440 
    441         boolean flag = false;
    442         int count = 1;
    443         int color = allChess[x][y]; // 判断黑白
    444         count = this.checkCount(1, 0, color);
    445         if (count >= 5) {
    446             flag = true;
    447         } else { // 纵向
    448             count = this.checkCount(0, 1, color);
    449             if (count >= 5) {
    450                 flag = true;
    451             }else{//左下右上
    452                 count = this.checkCount(1, -1, color);
    453                 if (count >= 5) {
    454                     flag = true;
    455                 }else{
    456                     count = this.checkCount(1, 1, color);  //右下左上
    457                     if (count >= 5) {
    458                         flag = true;
    459                     } 
    460                 }
    461             }
    462         }
    463         
    464         return flag;
    465     }
    466 
    467     
    468     public int checkCount( int xChange,  int yChange, int color)
    469     {
    470         int count = 1;
    471         int tempX = xChange;
    472         int tempY = yChange;
    473           
    474         while ( x + xChange >= 0 && x + xChange <=18 &&
    475                 y + yChange >= 0 && y + yChange <= 18
    476                 && color == allChess[x + xChange][y + yChange]) {
    477             count++;
    478             if(xChange!= 0 )
    479             {
    480                 xChange++;   //需要变化的
    481             }
    482             if(yChange!= 0 )   
    483             { 
    484                 if(yChange > 0)
    485                     yChange++;  //需要变化的
    486                 else{
    487                     yChange--;
    488                 }
    489             }
    490         }
    491             // i4 = 1
    492             xChange = tempX;
    493             yChange = tempY;
    494             while ( x + xChange >= 0 && x + xChange <=18 &&
    495                     y + yChange >= 0 && y + yChange <= 18
    496                     &&color == allChess[x - xChange][y - yChange])
    497             {
    498                 count++;
    499                 if(xChange != 0 )
    500                 {
    501                     xChange++;   
    502                 }
    503                 if(yChange != 0 )   
    504                 { 
    505                     if(yChange > 0)
    506                         yChange++;  //需要变化的
    507                     else{
    508                         yChange--;
    509                     }
    510                 }
    511             }
    512         return count; 
    513     }
    514     
    515     @Override
    516     public void run() {
    517         // TODO 自动生成的方法存根
    518         // 判断是否有时间限制
    519         if (maxTime > 0) {
    520             while (true) {
    521                 if (isBlack) {
    522                     blackTime--;
    523                     if(blackTime == 0 )
    524                     {
    525                         JOptionPane.showMessageDialog(this, "黑方超时,游戏结束!!!");
    526                     }
    527                 } else {
    528                     whiteTime--;
    529                     if(whiteTime == 0 )
    530                     {
    531                         JOptionPane.showMessageDialog(this, "白方超时,游戏结束!!!");
    532                     }
    533                 }
    534                 // 时间倒计时
    535                 try {
    536                     Thread.sleep(1000);
    537                 } catch (InterruptedException e) {
    538                     // TODO 自动生成的 catch 块
    539                     e.printStackTrace();
    540                 }
    541                 blackMessage = blackTime / 3600 + ":"
    542                         + (blackTime / 60 - blackTime / 3600 * 60) + ":"
    543                         + (blackTime - blackTime / 60 * 60);
    544                 whiteMessage = whiteTime / 3600 + ":"
    545                         + (whiteTime / 60 - whiteTime / 3600 * 60) + ":"
    546                         + (whiteTime - whiteTime / 60 * 60);
    547                 this.repaint();
    548                 System.out.println(blackTime + "------" + whiteTime);
    549             }
    550         }
    551     }
    552 }
    View Code

    MyFrame.java

      1 package com.ftl.frame;
      2 
      3 import java.awt.Color;
      4 import java.awt.Font;
      5 import java.awt.Graphics;
      6 import java.awt.Toolkit;
      7 import java.awt.event.MouseEvent;
      8 import java.awt.event.MouseListener;
      9 import java.awt.image.BufferedImage;
     10 import java.io.File;
     11 import java.io.IOException;
     12 
     13 import javax.imageio.ImageIO;
     14 import javax.swing.JFrame;
     15 import javax.swing.JOptionPane;
     16 
     17 public class FiveChessFrame extends JFrame implements MouseListener, Runnable {
     18 
     19     // 获取屏幕高
     20     int height = (int) Toolkit.getDefaultToolkit().getScreenSize().getHeight();
     21 
     22     // 获取屏幕宽
     23     int width = (int) Toolkit.getDefaultToolkit().getScreenSize().getWidth();
     24 
     25     // 获取图片
     26     BufferedImage image = null;
     27 
     28     // 保存X坐标
     29     int x = 0;
     30 
     31     // 保存Y坐标
     32     int y = 0;
     33 
     34     // 标识黑白棋子
     35     boolean isBlack = true;
     36 
     37     // 表示当前游戏是否可以就
     38     boolean canPlay = true;
     39 
     40     // 保存显示的信息
     41     String message = "黑方";
     42 
     43     // 游戏设置(线程操作) 最大时间/秒
     44     int maxTime = 0;
     45 
     46     // 倒计时的线程
     47     Thread t = new Thread(this);
     48 
     49     // 保存黑白方的剩余时间
     50     int blackTime = 0;
     51     int whiteTime = 0;
     52 
     53     // 保存双方剩余时间的显示信息
     54 
     55     String blackMessage = "无限制";
     56     String whiteMessage = "无限制";
     57 
     58     // 二维数组保存所有的图标
     59     // 其中数据内容 0 :空 1: 黑 2: 白
     60     int[][] allChess = new int[19][19];
     61 
     62     public FiveChessFrame() {
     63 
     64         // 设置窗体大xiao
     65         this.setSize(500, 500);
     66 
     67         // 设置标题
     68         this.setTitle("Go Fighting FTL");
     69 
     70         // 设置关闭窗口即关闭程序
     71         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     72 
     73         // 设置可以放大窗体
     74         this.setResizable(false);
     75 
     76         // 设置位置
     77         this.setLocation((width - 500) / 2, (height - 500) / 2);
     78 
     79         // 增加鼠标监听
     80         this.addMouseListener(this);
     81 
     82         try {
     83             image = ImageIO.read(new File("F:" + File.separator + "gcg.jpg"));
     84         } catch (IOException e) {
     85             // TODO 自动生成的 catch 块
     86             e.printStackTrace();
     87         }
     88         //现场开始 
     89         t.start();
     90         t.suspend();
     91         
     92         //防止黑屏幕
     93         this.repaint();
     94         
     95         // 设置是否可见
     96         this.setVisible(true);
     97     }
     98 
     99     // 画画窗口
    100     public void paint(Graphics g) {
    101         // 双缓冲技术
    102         // BufferedImage bi = new BufferedImage(500, 500,
    103         // BufferedImage.TYPE_INT_ARGB);
    104         // Graphics g = bi.createGraphics();
    105         //
    106         Font fontOld = g.getFont();
    107         g.drawImage(image, 1, 20, this);
    108         g.setFont(new Font("黑体", Font.BOLD, 20));
    109         g.drawString("游戏信息:" + message, 160, 60);
    110         g.setFont(new Font("宋体", 10, 20));
    111         g.drawString("黑方时间: " + blackMessage, 40, 470);
    112         g.drawString("白方时间: " + whiteMessage, 260, 470);
    113 
    114         // 画出底线
    115         for (int i = 0; i < 19; i++) {
    116             g.drawLine(10, 70 + 20 * i, 370, 70 + 20 * i);
    117             g.drawLine(10 + 20 * i, 70, 10 + 20 * i, 430);
    118         }
    119 
    120         // 标注位
    121         g.fillOval(68, 128, 4, 4);
    122         g.fillOval(308, 128, 4, 4);
    123         g.fillOval(68, 368, 4, 4);
    124         g.fillOval(308, 368, 4, 4);
    125         g.fillOval(308, 248, 4, 4);
    126         g.fillOval(188, 128, 4, 4);
    127         g.fillOval(68, 248, 4, 4);
    128         g.fillOval(188, 368, 4, 4);
    129         g.fillOval(188, 248, 4, 4); // 圆点
    130 
    131         // 绘制棋子
    132         // g.fillOval(x, y, 10, 10);
    133         // x = (x-10)/20 * 20 + 10; y = (y-70)/20 * 20 + 70;
    134 
    135         for (int i = 0; i < 19; i++) {
    136             for (int j = 0; j < 19; j++) {
    137                 // 黑子
    138                 if (allChess[i][j] == 1) {
    139                     int tempX = i * 20 + 10;
    140                     int tempY = j * 20 + 70;
    141                     g.fillOval(tempX - 7, tempY - 7, 14, 14);
    142                 }
    143                 // 白子
    144                 if (allChess[i][j] == 2) {
    145                     int tempX = i * 20 + 10;
    146                     int tempY = j * 20 + 70;
    147                     g.setColor(Color.WHITE);
    148                     g.fillOval(tempX - 7, tempY - 7, 14, 14);
    149                     g.setColor(Color.BLACK);
    150                     g.drawOval(tempX - 7, tempY - 7, 14, 14);
    151                 }
    152             }
    153         }
    154         // g2.drawImage(bi, 0, 0, this);
    155 
    156     }
    157 
    158     @Override
    159     public void mouseClicked(MouseEvent e) {
    160         // TODO 自动生成的方法存根
    161 
    162     }
    163 
    164     @Override
    165     public void mouseEntered(MouseEvent e) {
    166         // TODO 自动生成的方法存根
    167 
    168     }
    169 
    170     @Override
    171     public void mouseExited(MouseEvent e) {
    172         // TODO 自动生成的方法存根
    173 
    174     }
    175 
    176     @Override
    177     public void mousePressed(MouseEvent e) {
    178 
    179         if (canPlay == true) {
    180             // 获取X坐标
    181             x = e.getX();
    182             // 获取Y坐标
    183             y = e.getY();
    184             // 棋盘范围
    185             if (x >= 10 && x <= 370 && y >= 70 && y <= 430) {
    186                 // 重新绘制,重新执行paint方法
    187                 x = (x - 10) / 20;
    188                 y = (y - 70) / 20;
    189 
    190                 if (allChess[x][y] == 0) {
    191 
    192                     // 判读棋子颜色
    193                     if (isBlack == true) {
    194                         allChess[x][y] = 1;
    195                         isBlack = false;
    196                         message = "白方";
    197                     } else {
    198                         allChess[x][y] = 2;
    199                         isBlack = true;
    200                         message = "黑方";
    201                     }
    202 
    203                     // 判断是否连线结束
    204                     boolean winFlag = this.checkWin();
    205                     if (winFlag == true) {
    206                         JOptionPane.showMessageDialog(this, "游戏结束!"
    207                                 + (allChess[x][y] == 1 ? "黑方" : "白方") + "获胜");
    208                         canPlay = false;
    209                     }
    210                 } else {
    211                     JOptionPane.showMessageDialog(this, "当前位置已经有棋子,请重新 落子");
    212                 }
    213                 this.repaint();
    214             }
    215         }
    216         // 开始游戏
    217         if (e.getX() >= 400 && e.getX() <= 470 && e.getY() >= 70
    218                 && e.getY() <= 100) {
    219             int result = JOptionPane.showConfirmDialog(this, "是否重新开始游戏?");
    220             if (result == 0) {
    221                 // 棋盘清空,allChess数组的数据归0 (双层循环也可以)
    222                 // 游戏信息的显示改回开始位置
    223                 // canplay = true, isBlack = true,黑方开始游戏
    224 
    225                 allChess = new int[19][19];
    226                 message = "黑方";
    227                 isBlack = true;
    228                 canPlay = true;
    229                 // 初始化白方和黑房时间
    230                 blackTime = maxTime;
    231                 whiteTime = maxTime;
    232                 if (maxTime > 0) {
    233                     blackMessage = maxTime / 3600 + ":"
    234                             + (maxTime / 60 - maxTime / 3600 * 60) + ":"
    235                             + (maxTime - maxTime / 60 * 60);
    236                     whiteMessage = blackMessage;
    237                     t.resume();
    238                 }
    239                 else {
    240                     blackMessage = "无限制";
    241                     whiteMessage = "无限制";
    242                 }
    243                 this.repaint();
    244             }
    245 
    246             if (result == 1) {
    247                 JOptionPane.showMessageDialog(this, "游戏继续");
    248             }
    249             if (result == 2) {
    250                 JOptionPane.showMessageDialog(this, "请继续游戏!!!");
    251             }
    252         }
    253         // 游戏设置
    254         if (e.getX() >= 400 && e.getX() <= 470 && e.getY() >= 120
    255                 && e.getY() <= 150) {
    256             String input = JOptionPane.showInputDialog(this,
    257                     "请输入游戏的最大时间?(分钟),) 0表示没有时间限制");
    258             try {
    259                 maxTime = Integer.parseInt(input) * 60;
    260                 if (maxTime < 0) {
    261                     JOptionPane.showMessageDialog(this, "时间不能为负,请输入正确时间信息!!!");
    262                 }
    263 
    264                 if (maxTime == 0) {
    265                     int result = JOptionPane.showConfirmDialog(this,
    266                             "设置完成,是否重新开始游戏?");
    267                     if (result == 0) {
    268                         // 棋盘清空,allChess数组的数据归0 (双层循环也可以)
    269                         // 游戏信息的显示改回开始位置
    270                         // canplay = true, isBlack = true,黑方开始游戏
    271 
    272                         allChess = new int[19][19];
    273                         message = "黑方";
    274                         isBlack = true;
    275                         canPlay = true;
    276                         blackTime = maxTime;
    277                         whiteTime = maxTime;
    278                         blackMessage = "无限制";
    279                         whiteMessage = "无限制";
    280                         this.repaint();
    281                     }
    282 
    283                     if (result == 1) {
    284                         JOptionPane.showMessageDialog(this, "游戏继续");
    285                     }
    286                     if (result == 2) {
    287                         JOptionPane.showMessageDialog(this, "请继续游戏!!!");
    288                     }
    289                 }
    290 
    291                 if (maxTime > 0) {
    292                     int result = JOptionPane.showConfirmDialog(this,
    293                             "设置完成,是否重新开始游戏?");
    294                     if (result == 0) {
    295                         // 棋盘清空,allChess数组的数据归0 (双层循环也可以)
    296                         // 游戏信息的显示改回开始位置
    297                         // canplay = true, isBlack = true,黑方开始游戏
    298 
    299                         allChess = new int[19][19];
    300                         message = "黑方";
    301                         isBlack = true;
    302                         canPlay = true;
    303                         blackTime = maxTime;
    304                         whiteTime = maxTime;
    305                         blackMessage = maxTime / 3600 + ":"
    306                                 + (maxTime / 60 - maxTime / 3600 * 60) + ":"
    307                                 + (maxTime - maxTime / 60 * 60);
    308                         whiteMessage = blackMessage;
    309                         t.resume();
    310                         this.repaint();
    311                     }
    312 
    313                     if (result == 1) {
    314                         JOptionPane.showMessageDialog(this, "游戏继续");
    315                     }
    316                     if (result == 2) {
    317                         JOptionPane.showMessageDialog(this, "请继续游戏!!!");
    318                     }
    319                 }
    320 
    321             } catch (Exception e2) {
    322                 JOptionPane.showMessageDialog(this, "请输入正确时间信息!!!");
    323             }
    324 
    325             canPlay = true;
    326         }
    327         // 游戏说明
    328         if (e.getX() >= 400 && e.getX() <= 470 && e.getY() >= 170
    329                 && e.getY() <= 200) {
    330             JOptionPane.showMessageDialog(this, "游戏说明");
    331         }
    332         // 认输
    333         if (e.getX() >= 400 && e.getX() <= 470 && e.getY() >= 270
    334                 && e.getY() <= 300) {
    335             int result = JOptionPane.showConfirmDialog(this, "是否认输?");
    336             if (result == 0) {
    337                 if (isBlack) {
    338                     JOptionPane.showMessageDialog(this, "黑方,已经认输,游戏结束!");
    339                 } else {
    340                     JOptionPane.showMessageDialog(this, "白方,已经认输,游戏结束!");
    341                 }
    342                 canPlay = false;
    343             }
    344             if (result == 1) {
    345                 JOptionPane.showMessageDialog(this, "游戏继续");
    346             }
    347             if (result == 2) {
    348                 JOptionPane.showMessageDialog(this, "请继续游戏!!!");
    349             }
    350         }
    351         // 关于
    352         if (e.getX() >= 400 && e.getX() <= 470 && e.getY() >= 320
    353                 && e.getY() <= 350) {
    354             JOptionPane.showMessageDialog(this, "关于");
    355         }
    356         // 退出
    357         if (e.getX() >= 400 && e.getX() <= 470 && e.getY() >= 370
    358                 && e.getY() <= 400) {
    359             JOptionPane.showMessageDialog(this, "退出");
    360             System.exit(0);
    361         }
    362     }
    363 
    364     @Override
    365     public void mouseReleased(MouseEvent e) {
    366         // TODO 自动生成的方法存根
    367 
    368     }
    369 
    370     public boolean checkWin() {
    371     
    372 //        boolean flag = false;
    373 //        int i = 1;
    374 //        int count = 1;
    375 //        int color = allChess[x][y]; // 判断黑白
    376 //        // 判断横向      是否有5个棋子相连,即 allChess[x][y]的y 是相同的
    377 //        while (color == allChess[x + i][y]) {
    378 //            count++;
    379 //            i++;
    380 //        }
    381 //        i = 1;
    382 //        while (color == allChess[x - i][y]) {
    383 //            count++;
    384 //            i++;
    385 //        }
    386 //        if (count >= 5) {
    387 //            flag = true;
    388 //        }
    389 //        
    390 //        
    391 //        //纵向        
    392 //        int i2 = 1;
    393 //        int count2 = 1;
    394 //        while (color == allChess[x][y + i2]) {
    395 //            count2++;
    396 //            i2++;
    397 //        }
    398 //        i2 = 1;
    399 //        while (color == allChess[x][y - i2]) {
    400 //            count2++;
    401 //            i2++;
    402 //        }
    403 //        if (count2 >= 5) {
    404 //            flag = true;
    405 //        }
    406 //        
    407 //        //斜方向(右上 + 坐下)
    408 //        int i3 = 1;
    409 //        int count3= 1;
    410 //        while (color == allChess[x + i3][y - i3]) {
    411 //            count3++;
    412 //            i3++;
    413 //        }
    414 //        i3 = 1;
    415 //        while (color == allChess[x - i3][y + i3]) {
    416 //            count3++;
    417 //            i3++;
    418 //        }
    419 //        if (count3 >= 5) {
    420 //            flag = true;
    421 //        }
    422 //        
    423 //        //左上 + 右下
    424 //        int i4 = 1;
    425 //        int count4= 1;
    426 //        while (color == allChess[x - i4][y - i4]) {
    427 //            count4++;
    428 //            i4++;
    429 //        }
    430 //        i4 = 1;
    431 //        while (color == allChess[x + i4][y + i4]) {
    432 //            count4++;
    433 //            i4++;
    434 //        }
    435 //        if (count4 >= 5) {
    436 //            flag = true;
    437 //        }
    438 //        
    439 //        
    440 
    441         boolean flag = false;
    442         int count = 1;
    443         int color = allChess[x][y]; // 判断黑白
    444         count = this.checkCount(1, 0, color);
    445         if (count >= 5) {
    446             flag = true;
    447         } else { // 纵向
    448             count = this.checkCount(0, 1, color);
    449             if (count >= 5) {
    450                 flag = true;
    451             }else{//左下右上
    452                 count = this.checkCount(1, -1, color);
    453                 if (count >= 5) {
    454                     flag = true;
    455                 }else{
    456                     count = this.checkCount(1, 1, color);  //右下左上
    457                     if (count >= 5) {
    458                         flag = true;
    459                     } 
    460                 }
    461             }
    462         }
    463         
    464         return flag;
    465     }
    466 
    467     
    468     public int checkCount( int xChange,  int yChange, int color)
    469     {
    470         int count = 1;
    471         int tempX = xChange;
    472         int tempY = yChange;
    473           
    474         while ( x + xChange >= 0 && x + xChange <=18 &&
    475                 y + yChange >= 0 && y + yChange <= 18
    476                 && color == allChess[x + xChange][y + yChange]) {
    477             count++;
    478             if(xChange!= 0 )
    479             {
    480                 xChange++;   //需要变化的
    481             }
    482             if(yChange!= 0 )   
    483             { 
    484                 if(yChange > 0)
    485                     yChange++;  //需要变化的
    486                 else{
    487                     yChange--;
    488                 }
    489             }
    490         }
    491             // i4 = 1
    492             xChange = tempX;
    493             yChange = tempY;
    494             while ( x + xChange >= 0 && x + xChange <=18 &&
    495                     y + yChange >= 0 && y + yChange <= 18
    496                     &&color == allChess[x - xChange][y - yChange])
    497             {
    498                 count++;
    499                 if(xChange != 0 )
    500                 {
    501                     xChange++;   
    502                 }
    503                 if(yChange != 0 )   
    504                 { 
    505                     if(yChange > 0)
    506                         yChange++;  //需要变化的
    507                     else{
    508                         yChange--;
    509                     }
    510                 }
    511             }
    512         return count; 
    513     }
    514     
    515     @Override
    516     public void run() {
    517         // TODO 自动生成的方法存根
    518         // 判断是否有时间限制
    519         if (maxTime > 0) {
    520             while (true) {
    521                 if (isBlack) {
    522                     blackTime--;
    523                     if(blackTime == 0 )
    524                     {
    525                         JOptionPane.showMessageDialog(this, "黑方超时,游戏结束!!!");
    526                     }
    527                 } else {
    528                     whiteTime--;
    529                     if(whiteTime == 0 )
    530                     {
    531                         JOptionPane.showMessageDialog(this, "白方超时,游戏结束!!!");
    532                     }
    533                 }
    534                 // 时间倒计时
    535                 try {
    536                     Thread.sleep(1000);
    537                 } catch (InterruptedException e) {
    538                     // TODO 自动生成的 catch 块
    539                     e.printStackTrace();
    540                 }
    541                 blackMessage = blackTime / 3600 + ":"
    542                         + (blackTime / 60 - blackTime / 3600 * 60) + ":"
    543                         + (blackTime - blackTime / 60 * 60);
    544                 whiteMessage = whiteTime / 3600 + ":"
    545                         + (whiteTime / 60 - whiteTime / 3600 * 60) + ":"
    546                         + (whiteTime - whiteTime / 60 * 60);
    547                 this.repaint();
    548                 System.out.println(blackTime + "------" + whiteTime);
    549             }
    550         }
    551     }
    552 }
    View Code

    Test.java

     1 package com.ftl.test;
     2 
     3 import javax.swing.JOptionPane;
     4 
     5 import com.ftl.frame.FiveChessFrame;
     6 import com.ftl.frame.MyFrame;
     7 
     8 public class Test {
     9 
    10     public static void main(String[] args) {
    11         MyFrame mf = new MyFrame();
    12         
    13         int result = JOptionPane.showConfirmDialog(mf, "确认开始游戏?");
    14 //        if ( result == 0 )
    15 //        {
    16 //            String username = JOptionPane.showInputDialog(mf, "请输入您姓名:");
    17 //            if ( username != null )
    18 //            {
    19 //                JOptionPane.showMessageDialog(mf, "欢迎" + username + ",游戏即将开始! ");
    20 //            } else 
    21 //            {
    22 //                JOptionPane.showMessageDialog(mf, "请输您的姓名:");
    23 //            }
    24 //        }
    25 //        if ( result == 1 )
    26 //        {
    27 //            JOptionPane.showMessageDialog(mf, " 游戏结束! ");
    28 //        }
    29 //        if ( result == 2 )
    30 //        {
    31 //            String username = JOptionPane.showInputDialog(mf, "请输入您姓名:");
    32 //            if ( username != null )
    33 //            {
    34 //                JOptionPane.showMessageDialog(mf, "欢迎" + username + ",游戏即将开始! ");
    35 //            } else 
    36 //            {
    37 //                JOptionPane.showMessageDialog(mf, "请输您的姓名:");
    38 //            }
    39 //        }
    40 //        
    41         
    42         
    43         FiveChessFrame ff = new FiveChessFrame();
    44         
    45     }
    46 
    47 }
    View Code

    游戏截图

    源码下载

    点击下载

  • 相关阅读:
    FastMM、FastCode、FastMove的使用(图文并茂)
    12种JavaScript MVC框架之比较
    十款最佳Node.js MVC框架
    Couchbase 服务器
    C#程序员阅读的书籍
    ORM的实现
    Linux内核策略介绍
    ASP.NET MVC + EF 利用存储过程读取大数据
    面向.Net程序员的dump分析
    动态加载与插件化
  • 原文地址:https://www.cnblogs.com/ftl1012/p/fivechess.html
Copyright © 2011-2022 走看看