沐之潼 2017-06-22 03:12 采纳率: 66.7%
浏览 855
已采纳

麻烦解释一下各代码的意思,整体思路

package wuziqi1;
import java.awt.Checkbox;
import java.awt.CheckboxGroup;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class wuziqi1 extends JFrame{
CheseGame CG;
wuziqi1 ()
{
this.CG= new CheseGame();
addMouseListener(CG);
add(CG);
}
public static void main(String[] args) {
wuziqi1 fivechese= new wuziqi1 ();
fivechese.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fivechese.setSize(900, 700);
fivechese.setTitle("五子棋");
fivechese.setVisible(true);
fivechese.setResizable(true);
}
}
class CheseGame extends JPanel implements MouseListener,ItemListener
{
int [][]map = new int[20][20];
Color []color = {Color.BLACK,Color.WHITE,Color.GREEN};
int x = -1;
int y = -1;
int flag = 1;
int winner = 3;
JButton btn1 = new JButton("开始");
JButton btn2 = new JButton("再来一局");
CheckboxGroup cbg = new CheckboxGroup();
Checkbox chb1 = new Checkbox("黑棋先开始",cbg,true);
Checkbox chb2 = new Checkbox("白棋先开始",cbg,false);
public void itemStateChanged(ItemEvent ie) {
if(chb1.getState())
{
flag = 1;
}
else
{
flag = 2;
}
}
class GameStart implements ActionListener
{
public void actionPerformed(ActionEvent ae)
{

         if(ae.getSource() == btn1)
         {
             start();
             repaint();
             btn1.setEnabled(false);
             btn2.setEnabled(true);
             chb1.setEnabled(false);
             chb2.setEnabled(false);
         }
         if(ae.getSource() == btn2)
         {
             start();
             repaint();
             chb1.setEnabled(true);
             chb2.setEnabled(true);
         }
    }
}
CheseGame()
{  

    this.setLayout(null);
    chb1.setBounds(650, 50, 100, 30);
    chb2.setBounds(650, 100, 100, 30);
    chb1.addItemListener(this);
    chb2.addItemListener(this);
    btn1.setBounds(650,150,100,30);
    btn1.addActionListener(new GameStart()); 
    btn2.setBounds(650, 200, 100, 30);
    btn2.addActionListener(new GameStart());
    btn2.setEnabled(false);
    this.add(btn1);
    this.add(btn2);
    this.add(chb1);
    this.add(chb2);
}
void start()
{
    for(int i = 0;i < 20;i++)
        for(int j = 0;j < 20;j++)
            map[i][j] = 0;
    winner = 0;
}
Boolean win(int x,int y)
{
    int score = 1;
         for(int i = x;i < x+4;)
         {
             if(i == 19)
                 break;
             if(map[i][y] != map[++i][y])
                break;
             score++;
         }
         for(int i = x;i > x-4;)
         {
             if(i == 0)
                 break;
             if(map[i][y] != map[--i][y])
                break;
             score++;
         } 
        if(score >= 5)
             return true;
        else
             score = 1;
        for(int j = y; j < y+4;)
        {
            if(j == 19)
                break;
            if(map[x][j] != map[x][++j])
                break;
            score++;
        }
        for(int j = y; j > y-4;)
        {
            if(j == 0)
                break;
            if(map[x][j] != map[x][--j])
                break;
            score++;
        } 
        if(score >= 5)
             return true;
        else
             score = 1;
        for(int i = x;i < x+4;)
            for(int j = y;j < y+4;)
            {
                if(i == 19 || y == 19)
                {i = x+4; break;}
                if(map[i][j] != map[++i][++j])
                {
                    i = x+4;
                    break;
                }
                score++;
            }
          for(int i = x;i > x-4;)
            for(int j = y;j > y-4;)
            {
                if(i == 0||j==0)
                {i = x-4; break;}
                if(map[i][j] != map[--i][--j])
                {
                    i = x-4;
                    break;
                }
                score++;
            } 
          if(score >= 5)
             return true;
         else
             score = 1;
        for(int i = x;i > x-4;)
            for(int j = y;j < y+4;)
            {
                if(i==0 || j==19)
                { i = x-4;break;}
                if(map[i][j] != map[--i][++j])
                {
                    i = x-4;
                    break;
                }
                score++;
            }  
        for(int i = x;i < x+4;)
            for(int j = y;j > y-4;)
            {
                if(i==19||j==0)
                { i = x+4;break;}
                if(map[i][j] != map[++i][--j])
                {
                    i = x + 4;
                    break;
                }
                score++; 
            } 
        if(score >= 5)
             return true;
        else
             return false;
}
public void paintComponent(Graphics g)
 {
       super.paintComponent(g);
       setBackground(color[2]);
       for(int i = 1;i <= 20;i++)
       {
           g.setColor(color[0]);
           g.drawLine(30, i*30 ,600 , i*30);
           g.drawLine(i*30,30, i*30, 600);
       }
       for(int i = 0;i < 20;i++)
           for(int j = 0;j < 20;j++)
           {
               if(map[i][j] == 1)
               { 
                   g.setColor(color[0]);
                   g.fillOval((i+1)*30-15, (j+1)*30-15, 30, 30);
               }
               if(map[i][j] == 2)
               {
                   g.setColor(color[1]);
                   g.fillOval((i+1)*30-15, (j+1)*30-15, 30, 30);
               }
           }
       Font font = new Font("",30,50);
       g.setFont(font);
       if(winner == 1)
       {
           g.setColor(color[0]);
           g.drawString("黑棋胜", 650, 300);
       }
       if(winner == 2)
       { 
           g.setColor(color[1]);
           g.drawString("白棋胜", 650, 300);
       }
 }
public void mouseClicked(MouseEvent me) {
}
public void mousePressed(MouseEvent me) {
    if(winner == 0)
    {
        x = me.getX();
        y = me.getY();
        chb1.setEnabled(false);
        chb2.setEnabled(false);
    }
    x = (int)(x/30)-1;
    y = (int)((y-15)/30)-1;
    if(x>=0&&x<=19&&y>=0&&y<=19)
    {
        if(flag == 1&&map[x][y] == 0)
        {
            map[x][y] = 1;
            if(win(x,y))
                winner = 1;
            flag = 2;
        }
        else
            if(flag == 2&&map[x][y] == 0)
        {
            map[x][y] = 2;
             if(win(x,y))
                 winner = 2;
            flag = 1;
        }
    }
    repaint();
}
@Override
public void mouseReleased(MouseEvent me) {
   // throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public void mouseEntered(MouseEvent me) {
   // throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public void mouseExited(MouseEvent me) {
   // throw new UnsupportedOperationException("Not supported yet.");
}

}

  • 写回答

2条回答

  • 尼古拉斯_张三 2017-06-22 03:40
    关注
    package test;
    
    import java.awt.Checkbox;
    import java.awt.CheckboxGroup;
    import java.awt.Color;
    import java.awt.Font;
    import java.awt.Graphics;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.ItemEvent;
    import java.awt.event.ItemListener;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    public class Wuziqi1 extends JFrame {
        private static final long serialVersionUID = 8466850317892978450L;
    
        CheseGame CG;
    
        Wuziqi1() {
            this.CG = new CheseGame();
            // 为窗口增加鼠标监听
            addMouseListener(CG);
            add(CG);
        }
    
        // 执行的主函数. 主要是初始化 窗口用的. 
        public static void main(String[] args) {
            Wuziqi1 fivechese = new Wuziqi1();
            // 设置程序退出方式 . (窗口关闭 即 关闭程序)
            fivechese.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            // 设置窗口尺寸
            fivechese.setSize(900, 700);
            // 设置窗口的标题
            fivechese.setTitle("五子棋");
            // 设置窗口是否课件
            fivechese.setVisible(true);
            // 设置窗口是否可以拖拽调整窗口尺寸
            fivechese.setResizable(true);
        }
    }
    
    class CheseGame extends JPanel implements MouseListener, ItemListener {
        private static final long serialVersionUID = 4244441458513824552L;
    
        // map 用于 存储 棋子信息
        int[][] map = new int[20][20];
        Color[] color = { Color.BLACK, Color.WHITE, Color.GREEN };
        int x = -1;
        int y = -1;
        // 1 黑棋落棋, 2.白棋落棋 
        int flag = 1;
        // 五局三胜制
        int winner = 3;
        JButton btn1 = new JButton("开始");
        JButton btn2 = new JButton("再来一局");
        // 2个单选按钮
        CheckboxGroup cbg = new CheckboxGroup();
        Checkbox chb1 = new Checkbox("黑棋先开始", cbg, true);
        Checkbox chb2 = new Checkbox("白棋先开始", cbg, false);
    
        public void itemStateChanged(ItemEvent ie) {
            if (chb1.getState()) {
                flag = 1;
            } else {
                flag = 2;
            }
        }
    
        // 为 开始 按钮 和 再来一局按钮 设置 监听事件
        class GameStart implements ActionListener {
            public void actionPerformed(ActionEvent ae) {
                if (ae.getSource() == btn1) { // 开始按钮
                    // 执行 开始 的处理逻辑
                    start();
                    // 重绘窗口
                    repaint();
                    // 开始按钮 置灰
                    btn1.setEnabled(false);
                    // 再来一句 可以使用
                    btn2.setEnabled(true);
                    // 单选按钮 置灰
                    chb1.setEnabled(false);
                    chb2.setEnabled(false);
                }
                if (ae.getSource() == btn2) {
                    // 执行 开始 的处理逻辑
                    start();
                    // 重绘窗口
                    repaint();
                    // 单选按钮 可以使用
                    chb1.setEnabled(true);
                    chb2.setEnabled(true);
                }
            }
        }
    
        // 设置窗口布局
        CheseGame() {
            // 设置布局方式  null (标识通过 定位 布局)
            this.setLayout(null);
            // setBounds(x,y,width,height);
            chb1.setBounds(650, 50, 100, 30);
            chb2.setBounds(650, 100, 100, 30);
            chb1.addItemListener(this);
            chb2.addItemListener(this);
            btn1.setBounds(650, 150, 100, 30);
            btn1.addActionListener(new GameStart());
            btn2.setBounds(650, 200, 100, 30);
            btn2.addActionListener(new GameStart());
            btn2.setEnabled(false);
            this.add(btn1);
            this.add(btn2);
            this.add(chb1);
            this.add(chb2);
        }
        // 开始
        void start() {
            // 将 map 清空, 也就是清空棋盘上所有的棋子
            for (int i = 0; i < 20; i++)
                for (int j = 0; j < 20; j++)
                    map[i][j] = 0;
            // 胜负调为0
            winner = 0;
        }
    
        // 检查 黑棋 或 白棋 的胜负问题. 
        Boolean win(int x, int y) {
            int score = 1;
            // map[i][y] != map[++i][y] 类似的这种都是为了检查 棋子 是否是相同的颜色
            // if (i == 19) 棋盘大小20个.对应下标19. 所以 判断19是为了防止下标溢出
    
            // 从放置的棋子开始, 向右检查 有多少个 相连的同色棋子
            for (int i = x; i < x + 4;) {
                if (i == 19)
                    break;
                if (map[i][y] != map[++i][y])
                    break;
                score++;
            }
            // 从放置的棋子开始, 向左检查 有多少个 相连的同色棋子
            for (int i = x; i > x - 4;) {
                if (i == 0)
                    break;
                if (map[i][y] != map[--i][y])
                    break;
                score++;
            }
            // 如果相连的数目大于等于5, 当前落子的一方获胜
            if (score >= 5)
                return true;
            else
                score = 1;
            // 同上, 不过是 检查 纵向 相连的棋子数目
            for (int j = y; j < y + 4;) {
                if (j == 19)
                    break;
                if (map[x][j] != map[x][++j])
                    break;
                score++;
            }
            for (int j = y; j > y - 4;) {
                if (j == 0)
                    break;
                if (map[x][j] != map[x][--j])
                    break;
                score++;
            }
            // 如果相连的数目大于等于5, 当前落子的一方获胜
            if (score >= 5)
                return true;
            else
                score = 1;
            // 同理, 不过现在是 斜着 判断. 所以下标 需要 [++i][++j]
            for (int i = x; i < x + 4;)
                for (int j = y; j < y + 4;) {
                    if (i == 19 || y == 19) {
                        i = x + 4;
                        break;
                    }
                    if (map[i][j] != map[++i][++j]) {
                        i = x + 4;
                        break;
                    }
                    score++;
                }
            for (int i = x; i > x - 4;)
                for (int j = y; j > y - 4;) {
                    if (i == 0 || j == 0) {
                        i = x - 4;
                        break;
                    }
                    if (map[i][j] != map[--i][--j]) {
                        i = x - 4;
                        break;
                    }
                    score++;
                }
            // 如果相连的数目大于等于5, 当前落子的一方获胜
            if (score >= 5)
                return true;
            else
                score = 1;
            // 同理
            for (int i = x; i > x - 4;)
                for (int j = y; j < y + 4;) {
                    if (i == 0 || j == 19) {
                        i = x - 4;
                        break;
                    }
                    if (map[i][j] != map[--i][++j]) {
                        i = x - 4;
                        break;
                    }
                    score++;
                }
            for (int i = x; i < x + 4;)
                for (int j = y; j > y - 4;) {
                    if (i == 19 || j == 0) {
                        i = x + 4;
                        break;
                    }
                    if (map[i][j] != map[++i][--j]) {
                        i = x + 4;
                        break;
                    }
                    score++;
                }
            // 如果相连的数目大于等于5, 当前落子的一方获胜
            if (score >= 5)
                return true;
            else
                return false;
        }
    
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            // 绘制棋盘底色
            setBackground(color[2]);
            // 绘制棋盘格
            for (int i = 1; i <= 20; i++) {
                g.setColor(color[0]);
                g.drawLine(30, i * 30, 600, i * 30);
                g.drawLine(i * 30, 30, i * 30, 600);
            }
            // 绘制棋盘上棋子
            for (int i = 0; i < 20; i++)
                for (int j = 0; j < 20; j++) {
                    if (map[i][j] == 1) {
                        g.setColor(color[0]);
                        g.fillOval((i + 1) * 30 - 15, (j + 1) * 30 - 15, 30, 30);
                    }
                    if (map[i][j] == 2) {
                        g.setColor(color[1]);
                        g.fillOval((i + 1) * 30 - 15, (j + 1) * 30 - 15, 30, 30);
                    }
                }
            Font font = new Font("", 30, 50);
            g.setFont(font);
            // 出现胜负的时候, 绘制获胜方信息
            if (winner == 1) {
                g.setColor(color[0]);
                g.drawString("黑棋胜", 650, 300);
            }
            if (winner == 2) {
                g.setColor(color[1]);
                g.drawString("白棋胜", 650, 300);
            }
        }
    
        public void mouseClicked(MouseEvent me) {
        }
    
        public void mousePressed(MouseEvent me) {
            // 如果还没有获胜方
            if (winner == 0) {
                x = me.getX();
                y = me.getY();
                chb1.setEnabled(false);
                chb2.setEnabled(false);
            }
            // 通过坐标轴 计算 当前点击位置 落子 对应map中的下标
            x = (int) (x / 30) - 1;
            y = (int) ((y - 15) / 30) - 1;
            if (x >= 0 && x <= 19 && y >= 0 && y <= 19) {
                if (flag == 1 && map[x][y] == 0) {
                    // 记录落子信息
                    map[x][y] = 1;
                    // 检查当前落子后的胜负
                    if (win(x, y))
                        winner = 1;
                    flag = 2;
                } else if (flag == 2 && map[x][y] == 0) {
                    // 记录落子信息
                    map[x][y] = 2;
                    // 检查当前落子后的胜负
                    if (win(x, y))
                        winner = 2;
                    flag = 1;
                }
            }
            repaint();
        }
    
        @Override
        public void mouseReleased(MouseEvent me) {
            // throw new UnsupportedOperationException("Not supported yet.");
        }
    
        @Override
        public void mouseEntered(MouseEvent me) {
            // throw new UnsupportedOperationException("Not supported yet.");
        }
    
        @Override
        public void mouseExited(MouseEvent me) {
            // throw new UnsupportedOperationException("Not supported yet.");
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 关于#python#的问题:自动化测试