java项目五子棋含人机对战AI算法

UI初始化与主方法

private void initUI()
    {
        /** 窗体初始化 */
        this.setTitle("Gobang");
        this.setSize(760, 630);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);   //设置窗体的关闭动作
        this.setLocationRelativeTo(null);       //让窗体位于屏幕中央
        this.setResizable(false);               //让窗体不可改变大小
        this.setLayout(new BorderLayout());     //设置窗体的边界布局

        /** 左侧棋盘初始化 */
        chessboard = new ChessBoard();
        chessboard.setBackground(Color.GRAY);
        this.add(chessboard, BorderLayout.CENTER);      //将棋盘面板添加在边界布局的中央

        /** 右侧菜单初始化 */
        JPanel menu = new JPanel();
        menu.setLayout(new FlowLayout(FlowLayout.LEFT));    //面板设置流式布局
        menu.setPreferredSize(new Dimension(150, 0));   //面板设置大小

        /** 菜单上的控件声明 */
        JButton start = new JButton(START);
        JButton retract_the_false = new JButton(RETRACT);
        JButton give_up = new JButton(GIVEUP);

        /** 菜单上的按钮初始化 */
        start.setPreferredSize(new Dimension(140, 60));
        retract_the_false.setPreferredSize(new Dimension(140, 60));
        give_up.setPreferredSize(new Dimension(140, 60));
        menu.add(start);
        menu.add(retract_the_false);
        menu.add(give_up);

        /** 菜单上的模式选项初始化 */
        JRadioButton pvp = new JRadioButton(PVP);
        JRadioButton pve = new JRadioButton(PVE);
        pvp.setPreferredSize(new Dimension(140, 60));
        pve.setPreferredSize(new Dimension(140, 60));
        pvp.setSelected(true);

        ButtonGroup bg = new ButtonGroup();
        bg.add(pvp);
        bg.add(pve);

        menu.add(pvp);
        menu.add(pve);

        JCheckBox AI_offensive = new JCheckBox(AI);
        menu.add(AI_offensive);

        this.add(menu, BorderLayout.EAST);      //将菜单面板添加在边界布局的右边

        this.setVisible(true);

        GobangListener gl = new GobangListener(this,chessboard,start,retract_the_false,give_up,pvp,pve,AI_offensive);    //事件处理

        start.addActionListener(gl);
        retract_the_false.addActionListener(gl);
        give_up.addActionListener(gl);
        pve.addActionListener(gl);
        pvp.addActionListener(gl);
        AI_offensive.addActionListener(gl);
    }

棋盘控件重绘方法重写

import javax.swing.*;
import java.awt.*;

public class ChessBoard extends JPanel implements GobangConfig
{
    /**
     * 重写绘制棋盘的方法
     */
    @Override
    public void paint(Graphics g) {
        super.paint(g);
        for(int i = 0; i < ROW; i++)
            g.drawLine(X,Y+SIZE*i,X+SIZE*(COLUMN-1),Y+SIZE*i);
        for(int i = 0; i < COLUMN; i++)
            g.drawLine(X+SIZE*i,Y,X+SIZE*i,Y+SIZE*(ROW-1));
        for(int r = 0; r < ROW; r++)
        {
            for(int c = 0; c < COLUMN; c++)
            {
                if(chessArray[r][c] != 0)
                {
                    int x = c*SIZE + X - CHESS_SIZE/2;
                    int y = r*SIZE + Y - CHESS_SIZE/2;

                    if(chessArray[r][c] == 1)
                        g.setColor(Color.BLACK);
                    else
                        g.setColor(Color.WHITE);

                    g.fillOval(x,y,CHESS_SIZE,CHESS_SIZE);
                }
            }
        }
    }
}

常量与全局数据结构接口

import java.awt.*;
import java.util.ArrayList;
import java.util.HashMap;

public interface GobangConfig
{
    /** 棋盘左上角距窗体左上角的距离 */
    public static final int X = 50;
    public static final int Y = 50;

    /** 棋盘的行数和列数 */
    public static final int ROW = 15;
    public static final int COLUMN = 15;

    /** 棋盘的单元格大小 */
    public static final int SIZE = 35;

    /** 棋子大小 */
    public static final int CHESS_SIZE = 35;

    /** 游戏菜单按钮文字 */
    public static final String START = "开始新游戏";
    public static final String RETRACT = "悔棋";
    public static final String GIVEUP = "认输";

    /** 对战模式 */
    public static final String PVE = "人机对战";
    public static final String PVP = "人人对战";

    /** AI先手 */
    public static final String AI = "AI先手";

    /** 黑棋胜利 */
    public static final String BLACK_WIN = "黑棋胜利!";

    /** 白棋认输,黑棋胜利 */
    public static final String GIVEUP_BLACK_WIN = "白棋认输,黑棋胜利!";

    /** 白棋胜利 */
    public static final String WHITE_WIN = "白棋胜利!";

    /** 黑棋认输,白棋胜利 */
    public static final String GIVEUP_WHITE_WIN = "黑棋认输,白棋胜利!";

    /** 记录棋盘上是否有棋子 */
    public static final int[][] chessArray = new int[ROW][COLUMN];


    public static final char[][] AIchessArray = new char[ROW][COLUMN];

    /** 计算棋盘上的权值 */
    public static final int[][] valuematrix = new int[ROW][COLUMN];

    /** 记录棋子的下棋顺序 */
    public static final ArrayList<Point> orderRecoder = new ArrayList<Point>();

    /** 定义存储棋子相连情况和对应权值的Map集合对象。 */
    public static final HashMap<String,Integer> situationmap = new HashMap<String,Integer>();
}

事件监听类

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import static gobang.WeightedMethod.calculateValue;

public class GobangListener extends MouseAdapter implements GobangConfig, ActionListener
{
    /** 窗体控件 */
    private JFrame mainframe;

    /** 棋盘控件 */
    private ChessBoard chessboard;

    /** 棋盘面板画笔*/
    private Graphics g;

    /** 菜单栏的开始、悔棋、认输按钮*/
    private JButton start,retract_the_false,give_up;

    /** 游戏模式选项 */
    private JRadioButton pve, pvp;

    /** 先手选项 */
    private JCheckBox AI_offensive;

    /** 游戏模式标识 true 代表人人对战;false 代表人机对战*/
    private boolean mode = true;

    /** 先手标识 true 代表 AI先手;false 代表 人先手*/
    public static boolean offensive = false;

    /** 黑白棋标志位 true 表示黑棋; false 表示白棋 */
    private boolean flag_chess = true;

    /** 比赛结果 */
    private String result_message;


    /**
     * 构造函数,初始化窗口控件
     * @param chessboard
     * @param start
     * @param retract_the_false
     * @param give_up
     * @param pvp
     * @param pve
     * @param AI_offensive
     */
    public GobangListener(JFrame frame,ChessBoard chessboard,JButton start, JButton retract_the_false,JButton give_up,JRadioButton pvp, JRadioButton pve,JCheckBox AI_offensive)
    {
        this.mainframe = frame;
        this.chessboard = chessboard;
        this.start = start;
        this.retract_the_false = retract_the_false;
        this.give_up = give_up;
        this.pvp = pvp;
        this.pve = pve;
        this.AI_offensive = AI_offensive;
        g = chessboard.getGraphics();
    }

    /**
     * 处理鼠标在棋盘面板的操作
     * @param e
     */
    @Override
    public void mouseClicked(MouseEvent e)
    {
        super.mouseClicked(e);
        int x = e.getX();
        int y = e.getY();

        if(mode == true)
        {
            /** 点击棋盘内部 */
            if(x > X && x <= X + SIZE*(COLUMN-1) && y >= Y && y <= Y &#
  • 1
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值