【java】五子棋小游戏

[java]  view plain copy print ?
  1. /* 
  2.  
  3.    java简单的五子棋小游戏 
  4.  
  5. */  
  6.   
  7. import java.awt.*;  
  8. import java.awt.event.*;  
  9. import java.applet.Applet;  
  10. import java.awt.Color;  
  11.   
  12. public class GoBang extends Applet implements ActionListener, MouseListener,  
  13.         MouseMotionListener, ItemListener {  
  14.     int color = 0;// 旗子的颜色标识 0:白子 1:黑子  
  15.   
  16.     boolean isStart = false;// 游戏开始标志  
  17.   
  18.     int bodyArray[][] = new int[16][16]; // 设置棋盘棋子状态 0 无子 1 白子 2 黑子  
  19.   
  20.     Button b1 = new Button("游戏开始");  
  21.   
  22.     Button b2 = new Button("重置游戏");  
  23.   
  24.     Label lblWin = new Label(" ");  
  25.   
  26.     Checkbox ckbHB[] = new Checkbox[2];  
  27.   
  28.     CheckboxGroup ckgHB = new CheckboxGroup();  
  29.   
  30.     public void init() {  
  31.         setLayout(null);  
  32.   
  33.         addMouseListener(this);  
  34.         add(b1);  
  35.         b1.setBounds(330508030);  
  36.         b1.addActionListener(this);  
  37.         add(b2);  
  38.         b2.setBounds(330908030);  
  39.         b2.addActionListener(this);  
  40.         ckbHB[0] = new Checkbox("白子先", ckgHB, false);  
  41.         ckbHB[0].setBounds(320206030);  
  42.         ckbHB[1] = new Checkbox("黑子先", ckgHB, false);  
  43.         ckbHB[1].setBounds(380206030);  
  44.         add(ckbHB[0]);  
  45.         add(ckbHB[1]);  
  46.         ckbHB[0].addItemListener(this);  
  47.         ckbHB[1].addItemListener(this);  
  48.         add(lblWin);  
  49.         lblWin.setBounds(3301308030);  
  50.   
  51.         gameInit();  
  52.         this.resize(new Dimension(450350));  
  53.     }  
  54.   
  55.     public void itemStateChanged(ItemEvent e) {  
  56.         if (ckbHB[0].getState()) // 选择黑子先还是白子先  
  57.         {  
  58.             color = 0;  
  59.         } else {  
  60.             color = 1;  
  61.         }  
  62.     }  
  63.   
  64.     public void actionPerformed(ActionEvent e) {  
  65.         if (e.getSource() == b1) {  
  66.             gameStart();  
  67.         } else {  
  68.             reStart();  
  69.         }  
  70.     }  
  71.   
  72.     public void mousePressed(MouseEvent e) {  
  73.     }  
  74.   
  75.     public void mouseClicked(MouseEvent e) {  
  76.         int x1, y1;  
  77.         x1 = e.getX();  
  78.         y1 = e.getY();  
  79.         if (e.getX() < 20 || e.getX() > 300 || e.getY() < 20 || e.getY() > 300) {  
  80.             return;  
  81.         }  
  82.   
  83.         if (x1 % 20 > 10) {  
  84.             x1 += 20;  
  85.         }  
  86.   
  87.         if (y1 % 20 > 10) {  
  88.             y1 += 20;  
  89.         }  
  90.   
  91.         x1 = x1 / 20 * 20;  
  92.         y1 = y1 / 20 * 20;  
  93.         setDown(x1, y1);  
  94.   
  95.     }  
  96.   
  97.     public void mouseEntered(MouseEvent e) {  
  98.     }  
  99.   
  100.     public void mouseExited(MouseEvent e) {  
  101.     }  
  102.   
  103.     public void mouseReleased(MouseEvent e) {  
  104.     }  
  105.   
  106.     public void mouseDragged(MouseEvent e) {  
  107.     }  
  108.   
  109.     public void mouseMoved(MouseEvent e) {  
  110.     }  
  111.   
  112.     public void paint(Graphics g) {  
  113.         g.setColor(Color.lightGray);  
  114.         g.fill3DRect(1010300300true);  
  115.         g.setColor(Color.black);  
  116.         for (int i = 1; i < 16; i++) {  
  117.             g.drawLine(2020 * i, 30020 * i);  
  118.             g.drawLine(20 * i, 2020 * i, 300);  
  119.         }  
  120.     }  
  121.   
  122.     public void setDown(int x, int y) // 落子  
  123.     {  
  124.         if (!isStart) // 判断游戏未开始  
  125.         {  
  126.             return;  
  127.         }  
  128.   
  129.         if (bodyArray[x / 20][y / 20] != 0) {  
  130.             return;  
  131.         }  
  132.         Graphics g = getGraphics();  
  133.   
  134.         if (color == 1)// 判断黑子还是白子  
  135.         {  
  136.             g.setColor(Color.black);  
  137.             color = 0;  
  138.         } else {  
  139.             g.setColor(Color.white);  
  140.             color = 1;  
  141.         }  
  142.   
  143.         g.fillOval(x - 10, y - 102020);  
  144.   
  145.         bodyArray[x / 20][y / 20] = color + 1;  
  146.   
  147.         if (gameWin1(x / 20, y / 20)) // 判断输赢  
  148.         {  
  149.             lblWin.setText(startColor(color) + "赢了!");  
  150.             isStart = false;  
  151.         }  
  152.   
  153.         if (gameWin2(x / 20, y / 20)) // 判断输赢  
  154.         {  
  155.             lblWin.setText(startColor(color) + "赢了!");  
  156.             isStart = false;  
  157.         }  
  158.   
  159.         if (gameWin3(x / 20, y / 20)) // 判断输赢  
  160.         {  
  161.             lblWin.setText(startColor(color) + "赢了!");  
  162.             isStart = false;  
  163.         }  
  164.   
  165.         if (gameWin4(x / 20, y / 20)) // 判断输赢  
  166.         {  
  167.             lblWin.setText(startColor(color) + "赢了!");  
  168.             isStart = false;  
  169.         }  
  170.     }  
  171.   
  172.     public String startColor(int x) {  
  173.         if (x == 0) {  
  174.             return "黑子";  
  175.         } else {  
  176.             return "白子";  
  177.         }  
  178.     }  
  179.   
  180.     public void gameStart() // 游戏开始  
  181.     {  
  182.         isStart = true;  
  183.         enableGame(false);  
  184.         b2.setEnabled(true);  
  185.     }  
  186.   
  187.     public void gameInit() // 游戏开始初始化  
  188.     {  
  189.         isStart = false;  
  190.         enableGame(true);  
  191.         b2.setEnabled(false);  
  192.         ckbHB[0].setState(true);  
  193.   
  194.         for (int i = 0; i < 16; i++) {  
  195.             for (int j = 0; j < 16; j++) {  
  196.                 bodyArray[i][j] = 0;  
  197.             }  
  198.         }  
  199.         lblWin.setText("");  
  200.     }  
  201.   
  202.     public void reStart() // 游戏重新开始  
  203.     {  
  204.         repaint();  
  205.         gameInit();  
  206.     }  
  207.   
  208.     public void enableGame(boolean e) // 设置组件状态  
  209.     {  
  210.         b1.setEnabled(e);  
  211.         b2.setEnabled(e);  
  212.         ckbHB[0].setEnabled(e);  
  213.         ckbHB[1].setEnabled(e);  
  214.     }  
  215.   
  216.     public boolean gameWin1(int x, int y) // 判断输赢 横  
  217.     {  
  218.         int x1, y1, t = 1;  
  219.         x1 = x;  
  220.         y1 = y;  
  221.   
  222.         for (int i = 1; i < 5; i++) {  
  223.             if (x1 > 15) {  
  224.                 break;  
  225.             }  
  226.             if (bodyArray[x1 + i][y1] == bodyArray[x][y]) {  
  227.                 t += 1;  
  228.             } else {  
  229.                 break;  
  230.             }  
  231.   
  232.         }  
  233.   
  234.         for (int i = 1; i < 5; i++) {  
  235.             if (x1 < 1) {  
  236.                 break;  
  237.             }  
  238.   
  239.             if (bodyArray[x1 - i][y1] == bodyArray[x][y]) {  
  240.                 t += 1;  
  241.             } else {  
  242.                 break;  
  243.             }  
  244.         }  
  245.   
  246.         if (t > 4) {  
  247.             return true;  
  248.         } else {  
  249.             return false;  
  250.         }  
  251.     }  
  252.   
  253.     public boolean gameWin2(int x, int y) // 判断输赢 竖  
  254.     {  
  255.         int x1, y1, t = 1;  
  256.         x1 = x;  
  257.         y1 = y;  
  258.   
  259.         for (int i = 1; i < 5; i++) {  
  260.             if (x1 > 15) {  
  261.                 break;  
  262.             }  
  263.             if (bodyArray[x1][y1 + i] == bodyArray[x][y]) {  
  264.                 t += 1;  
  265.             } else {  
  266.                 break;  
  267.             }  
  268.   
  269.         }  
  270.   
  271.         for (int i = 1; i < 5; i++) {  
  272.             if (x1 < 1) {  
  273.                 break;  
  274.             }  
  275.   
  276.             if (bodyArray[x1][y1 - i] == bodyArray[x][y]) {  
  277.                 t += 1;  
  278.             } else {  
  279.                 break;  
  280.             }  
  281.         }  
  282.   
  283.         if (t > 4) {  
  284.             return true;  
  285.         } else {  
  286.             return false;  
  287.         }  
  288.     }  
  289.   
  290.     public boolean gameWin3(int x, int y) // 判断输赢 左斜  
  291.     {  
  292.         int x1, y1, t = 1;  
  293.         x1 = x;  
  294.         y1 = y;  
  295.   
  296.         for (int i = 1; i < 5; i++) {  
  297.             if (x1 > 15) {  
  298.                 break;  
  299.             }  
  300.             if (bodyArray[x1 + i][y1 - i] == bodyArray[x][y]) {  
  301.                 t += 1;  
  302.             } else {  
  303.                 break;  
  304.             }  
  305.   
  306.         }  
  307.   
  308.         for (int i = 1; i < 5; i++) {  
  309.             if (x1 < 1) {  
  310.                 break;  
  311.             }  
  312.   
  313.             if (bodyArray[x1 - i][y1 + i] == bodyArray[x][y]) {  
  314.                 t += 1;  
  315.             } else {  
  316.                 break;  
  317.             }  
  318.         }  
  319.   
  320.         if (t > 4) {  
  321.             return true;  
  322.         } else {  
  323.             return false;  
  324.         }  
  325.     }  
  326.   
  327.     public boolean gameWin4(int x, int y) // 判断输赢 左斜  
  328.     {  
  329.         int x1, y1, t = 1;  
  330.         x1 = x;  
  331.         y1 = y;  
  332.   
  333.         for (int i = 1; i < 5; i++) {  
  334.             if (x1 > 15) {  
  335.                 break;  
  336.             }  
  337.             if (bodyArray[x1 + i][y1 + i] == bodyArray[x][y]) {  
  338.                 t += 1;  
  339.             } else {  
  340.                 break;  
  341.             }  
  342.   
  343.         }  
  344.   
  345.         for (int i = 1; i < 5; i++) {  
  346.             if (x1 < 1) {  
  347.                 break;  
  348.             }  
  349.   
  350.             if (bodyArray[x1 - i][y1 - i] == bodyArray[x][y]) {  
  351.                 t += 1;  
  352.             } else {  
  353.                 break;  
  354.             }  
  355.         }  
  356.   
  357.         if (t > 4) {  
  358.             return true;  
  359.         } else {  
  360.             return false;  
  361.         }  
  362.     }  
  363. }  
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值