五子棋(一)

上手练习一下五子棋,倡导在做之前先做出一个计划,才不会稀里糊涂。

具体操作步骤如下:

  • 创建一个界面类,在里面创建界面和添加按钮对象,监听器等等。

  • 建立一个监听类,监听器类用来声明接口,接口包括动作监听器,鼠标动作监听器,还会声明一个用来防止数值的接口(之后会创建)。

  • 建立一个接口,用来获取数值(因为JAVA只能继承一个父类,所以接口会比较自由一些,接口是框架的重要组成部分)。

  • 再创建一个类,用来重绘和绘制棋盘和棋子(分开写会比较清晰)

 界面类:

public class GoBang extends JFrame implements GoBangconfig {

    GBListener gL=new GBListener();//创建监听器对象

    public void initUI() {
        //创建界面的方法
        //JFrame jf=new JFrame();
      Shape jf=new Shape();//由于Shape类继承了界面,所以可以用Shape类来创建对象。
        jf.setTitle("五子棋游戏");
        jf.setSize(1000,1000);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //设置窗体中
        jf.setLocationRelativeTo(null);
        jf.setLayout(null);
        jf.getContentPane().setBackground(Color.darkGray);//设置窗体颜色
        JButton jButton1=new JButton("开始游戏");
        jButton1.setBounds(800,200,90,30);
        jf.add(jButton1);
        jButton1.addActionListener(gL);
        JButton jButton2=new JButton("认输");
        jButton2.setBounds(800,280,90,30);

        jf.add(jButton2);
        jButton2.addActionListener(gL);
        JButton jButton3=new JButton("悔棋");
        jButton3.setBounds(800,360,90,30);
        jf.add(jButton3);
        jButton3.addActionListener(gL);
        jf.setVisible(true);
}

界面类也可以添加弹窗来进行提示,但注意了这步操作是需要刷新操作的.

构建弹窗操作:

JOptionPane.showMessageDialog(null,"");

 监听器类:

public class GBListener implements MouseListener,GoBangconfig, ActionListener {
Graphics g;
    JFrame jf;
    int x1,y1,x2,y2;
    int count=1;//颜色
    public int [][] isAvail=new int [16][16];//在这里定义一个二维数组为棋子的落子情况
    int begin;//开始游戏
    int flag=1;
    int xx,yy;//行和列
    int Setx[]=new int[row*row];
    int Sety[]=new int[column*column];
    int index;
public void actionPerformed(ActionEvent e){
       String gbStr=e.getActionCommand();
       //开始游戏,source
        switch (gbStr){
            case"开始游戏":
            begin=2;//开始游戏
 jf.repaint();//记住一定要刷新
break;
            case"悔棋":
                System.out.println("index"+index);
 jf.repaint();
break;
             case"认输":
 jf.repaint();
break;
 System.out.println(gbStr+"被点击了");
}
 public void mouseClicked(MouseEvent e){

    }


    public void mousePressed(MouseEvent e) {

        x1 = e.getX();
        y1 = e.getY();
        if (begin == 2) {
        //计算棋子的交值点
        if ((x1 - x) % size < size / 2) {
            xx = (x1 - x) / size;
        } else if ((x1 - x) % size > size / 2) {
            xx = (x1 - x) / size + 1;
        }
        if ((y1 - y) % size < size / 2) {
            yy = (y1 - y) / size;
        } else if ((y1 - y) % size > size / 2) {
            yy = (y1 - y) / size + 1;
        }
        if (xx>=0&&xx<=(row-1)&&yy>=0&&yy<=(column-1)){
        //当该位置没有棋子的时候,可以下棋
        if (isAvail[xx][yy] == 0) {
                if (count == 1){
                    //count=1为黑棋
                    isAvail[xx][yy] = 1;
                    g.setColor(Color.BLACK);
                    count++;
                } else if (count == 2) {
                    //count=1时为白棋
                    g.setColor(Color.WHITE);
                    isAvail[xx][yy] = 2;
                    count--;
                }
                g.fillOval(xx * size + x - size / 2, yy * size + y - size / 2, size, size);
                Setx[index] = xx;
                Sety[index] = yy;
                index++;
            }
public void mouseReleased(MouseEvent e){}

    public void mouseEntered(MouseEvent e){}


    public void mouseExited(MouseEvent e){}
}

注意:重写接口,记住要重写接口内的方法。

接口类:

public interface GoBangconfig {
    int x=50,y=75,size=40,row=15,column=15;
}

shape类:用来重绘和绘制棋盘

public class Shape extends JFrame implements GoBangconfig {
    Shape shape;
   int count;
  // Shape jf;
    int countx,county,x1,y1;
    public int [][] isAvail;
    public void paint(Graphics g){
        super.paint(g);
        //首先重绘棋盘
        g.setColor(Color.pink);//设置画笔颜色

        for (int i=0;i<row;i++){
            g.drawLine(x,y+size*i,x+size*(column-1),y+size*i);
        }
        for (int j=0;j<column;j++){
            g.drawLine(x+size*j,y,x+size*j,y+size*(row-1));
        }
        //在这里实现重绘棋子的功能,遍历
        for (int i=0;i<isAvail.length;i++){
            for (int j=0;j< isAvail.length;j++){
                int arr=isAvail[i][j];
                if (arr!=0) {
                    if (arr == 1) {
                        g.setColor(Color.BLACK);
                        g.fillOval(i * size + x - size / 2, j * size + y - size / 2, size, size);

                    } else if (arr == 2) {
                        g.setColor(Color.WHITE);
                        g.fillOval(i * size + x - size / 2, j * size + y - size / 2, size, size);
                    }
                }
            }
        }
    }
}

在界面类里,是要传递数值的,不然无法实现,包括重绘(重绘需要将监听器里的数组传递给界面类的数组),传递监听器。

  Graphics g=jf.getGraphics();
        //调用画笔
        gL.g=g;
        gL.jf=jf;
         jf.addMouseListener(gL);
       jf.isAvail=gL.isAvail;//将监听器的数组传递给界面
    }

在这篇文章需要实现的有绘制棋盘,在监听类重写接口的方法,鼠标按下获取坐标,矫正坐标的位置,棋子的形状为实心圆,重绘了棋子。

对了千万不要忘了创建一个主类,不然实现不了!!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的五子棋代码示例,使用 Python 语言实现: ```python # 定义棋盘大小 BOARD_SIZE = 15 # 定义棋盘 board = [[0] * BOARD_SIZE for i in range(BOARD_SIZE)] # 定义当前玩家(1 或 2) current_player = 1 # 判断是否胜利 def check_win(row, col, player): # 水平方向 count = 0 for i in range(col - 4, col + 5): if i >= 0 and i < BOARD_SIZE and board[row][i] == player: count += 1 if count == 5: return True else: count = 0 # 垂直方向 count = 0 for i in range(row - 4, row + 5): if i >= 0 and i < BOARD_SIZE and board[i][col] == player: count += 1 if count == 5: return True else: count = 0 # 左上到右下方向 count = 0 for i in range(-4, 5): r = row + i c = col + i if r >= 0 and r < BOARD_SIZE and c >= 0 and c < BOARD_SIZE and board[r][c] == player: count += 1 if count == 5: return True else: count = 0 # 右上到左下方向 count = 0 for i in range(-4, 5): r = row + i c = col - i if r >= 0 and r < BOARD_SIZE and c >= 0 and c < BOARD_SIZE and board[r][c] == player: count += 1 if count == 5: return True else: count = 0 return False # 主循环 while True: # 打印当前棋盘 for row in range(BOARD_SIZE): print(' '.join(str(board[row][col]) for col in range(BOARD_SIZE))) print() # 获取玩家落子位置 while True: try: row = int(input('请输入行号:')) col = int(input('请输入列号:')) if row < 0 or row >= BOARD_SIZE or col < 0 or col >= BOARD_SIZE or board[row][col] != 0: raise ValueError break except ValueError: print('无效的输入,请重新输入!') # 落子 board[row][col] = current_player # 判断胜利 if check_win(row, col, current_player): print('玩家 %d 胜利!' % current_player) break # 切换玩家 current_player = 3 - current_player ``` 该代码实现了一个简单的五子棋游戏,玩家可以输入行号和列号来落子,程序会判断是否胜利,并在胜利时输出胜利信息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值