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 + SIZE*(ROW-1))
{
/** 计算最近的交叉点的行数和列数 */
int r = (y - Y + SIZE/2)/SIZE;
int c = (x - X + SIZE/2)/SIZE;
/** 判断该位置是否已经有棋子 */
if(chessArray[r][c] == 0)
{
/** 计算交叉点坐标值 */
x = c*SIZE + X - CHE