Java五子棋开发思路【蓝杰项目】

2021/10/2 20:44:38

本文主要是介绍Java五子棋开发思路【蓝杰项目】,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

Java五子棋开发思路【蓝杰项目】

    • 绘制窗体
    • 画棋盘
    • 画棋子
    • 计算棋子位置
    • 判断是否连成线
    • 人机对战(权值算法)

绘制窗体

针对一个项目来说,人机交互是必须的,那么先来绘制一个窗体吧
用到的类是JFrame,上代码

this.setSize(800, 800);
this.setTitle("五子棋游戏");
//设置居中显示
this.setLocationRelativeTo(null);
//退出进程
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);

这时候就有问题了,不是说好JFrame对象吗,this又是啥?

public class GameUI extends JFrame implements Config

哦,原来继承了JFrame这个类,this其实就是下文的jf

JFrame jf = new JFrame();
jf.setSize(800, 800);
jf.setTitle("五子棋游戏");
//设置居中显示
jf.setLocationRelativeTo(null);
//退出进程
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setVisible(true);

画棋盘

        for(int i=0;i<LINE;i++) {
            g.drawLine(X0, Y0+i*SIZE, X0+(LINE-1)*SIZE, Y0+i*SIZE);
            g.drawLine(X0+i*SIZE, Y0, X0+i*SIZE, Y0+(LINE-1)*SIZE);
        }

你现在随便调整一下窗体 咦 奇怪 怎么没了
这就需要重写一个paint函数 每次都重绘框体 自然棋子也保存了

        //重写绘制组件的方法
    public void paint(Graphics g) {
        //1.绘制组件
        super.paint(g);

        System.out.println("paint");

        //2.绘制棋盘/棋子
        //硬编码
        for(int i=0;i<LINE;i++) {
            g.drawLine(X0, Y0+i*SIZE, X0+(LINE-1)*SIZE, Y0+i*SIZE);
            g.drawLine(X0+i*SIZE, Y0, X0+i*SIZE, Y0+(LINE-1)*SIZE);
        }

        for (int i=0;i<chess.length;i++){
            Chess chess2 = chess[i];
            if(chess2!=null){
                g.setColor(chess2.getColor());
                g.fillOval(chess2.getX(),chess2.getY(),CHESS,CHESS);
            }
        }
    }

画棋子

画完窗口画棋子 画画画
图形内容显示在哪个组件上,画笔就从该组件上获取
从窗体上获取画笔对象,获取画笔对象
在这介绍一下画笔工具Graphics类的使用

Graphics g = jf.getGraphics();

画笔有了 那画在哪里了 就靠鼠标点击了
来吧监听器登场 用于获取鼠标点击位置

public interface MouseListener extends EventListener {

    /**
     * Invoked when the mouse button has been clicked (pressed
     * and released) on a component.
     */
    public void mouseClicked(MouseEvent e);

    /**
     * Invoked when a mouse button has been pressed on a component.
     */
    public void mousePressed(MouseEvent e);

    /**
     * Invoked when a mouse button has been released on a component.
     */
    public void mouseReleased(MouseEvent e);

    /**
     * Invoked when the mouse enters a component.
     */
    public void mouseEntered(MouseEvent e);

    /**
     * Invoked when the mouse exits a component.
     */
    public void mouseExited(MouseEvent e);
}

什么鬼 看不懂!!!
别急 慢慢解释 这是java提供的监听器的接口interface
所以我们需要干嘛了 创建一个类 将这些接口写好方法
我创建的就是GameMouse类

        GameMouse mouse = new GameMouse();
        this.addMouseListener(mouse);

监听器写好了,我们需要监听器去干什么了
自然是要把棋子绘制出来 绘制棋子的代码

        g.setColor(Color.WHITE);
        g.fillOval(x, y, CHESS, CHESS);

CHESS是什么,这是我定义的常量,代表棋子大小,这样方便与之后的代码修改,避免了硬编码

计算棋子位置

现在棋子是点哪画哪 那咋办了 就需要算法了
靠近那个棋盘放在哪

            int x = e.getX();
            if ((x - X0) % SIZE > SIZE / 2)
                x = ((x - X0) / SIZE + 1) * SIZE + X0 - CHESS / 2;
            else
                x = (x - X0) / SIZE * SIZE + X0 - CHESS / 2;
            int y = e.getY();
            if ((y - Y0) % SIZE > SIZE / 2)
                y = ((y - Y0) / SIZE + 1) * SIZE + Y0 - CHESS / 2;
            else
                y = (y - Y0) / SIZE * SIZE + Y0 - CHESS / 2;

PS:SIZE是棋盘两条线直接的距离

判断是否连成线

只有在下完棋子之后 判断这个棋子周边是否连成线就可以了
算法思想就是计算左边几个棋子右边几个棋子 加一起是5个吗
一共是四个方向哈

public void JudgeRow(int x,int y){
        int count = 0;
        for (int i = (x - X0 + CHESS / 2) / SIZE + 1; i < LINE; i++) {
            if (chessBoard[i][(y - Y0 + CHESS / 2) / SIZE] == chessBoard[(x - X0 + CHESS / 2) / SIZE][(y - Y0 + CHESS / 2) / SIZE]) {
                count++;
            } else break;
        }
        for (int i = (x - X0 + CHESS / 2) / SIZE; i >= 0; i--) {
            if (chessBoard[i][(y - Y0 + CHESS / 2) / SIZE] == chessBoard[(x - X0 + CHESS / 2) / SIZE][(y - Y0 + CHESS / 2) / SIZE]) {
                count++;
            } else break;
        }
        if (count == 5) {
            this.startFlag=0;
            if (chessBoard[(x - X0 + CHESS / 2) / SIZE][(y - Y0 + CHESS / 2) / SIZE] == 1)
                JOptionPane.showMessageDialog(null, "黑棋胜利");
            else
                JOptionPane.showMessageDialog(null, "白棋胜利");
        }
    }

五子棋就写完了 但是很孤独啊 没有人陪你玩



这篇关于Java五子棋开发思路【蓝杰项目】的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程