1、一些对象和静态常量的定义
//定义三个BufferedImage,分别代表棋盘图、黑子图、白子图
private BufferedImage table;
private BufferedImage black;
private BufferedImage white;
//定义一个BufferedImage,代表当鼠标移动时将要下子的选择框
private BufferedImage selected;
//定义棋盘的宽高,这里的定义尺寸和给定的board.jpg图片的尺寸一致因为棋盘背景是通过图片加载的
private final int TABLE_WIDTH = 535;
private final int TABLE_HEIGHT = 536;
//定义棋盘中,每行和每列可下子的数目,这个数目跟给定的board.jpg中的数目是一致的,都为15
private final int BOARD_SIZE = 15;
//定义每个棋子所占棋盘总宽度的大小比率;每个棋子所占宽度 535/15=35
private final int RATE = TABLE_WIDTH/BOARD_SIZE;
//定义棋盘有效区域与背景图坐标之间的偏移值,x坐标右移5个像素,y坐标下移6个像素
private final int X_OFFSET = 5;
private final int Y_OFFSET = 6;
2、定义一个二维数组用于记录棋子
该数组的索引与该棋子在棋盘上的坐标需要有一个对应关系: 例如: 索引[2][3]处的棋子,对一个的真实绘制坐标应该是: xpos = 2*RATE+X_OFFSET=75; ypos = 3*RATE+Y_OFFSET=111;
private int[][] board = new int[BOARD_SIZE][BOARD_SIZE];//如果存储0,代表没有棋子,如果存储1,代表黑棋,如果存储2,代表白棋
3、定义五子棋游戏窗口
private JFrame f = new JFrame("五子棋游戏-Ace2NoU制作");
4、定义五子棋游戏棋盘对应的Canvas组件
private class ChessBoard extends JPanel{
//重写paint方法,实现绘画
@Override
public void paint(Graphics g) {
//绘制五子棋棋盘
g.drawImage(table,0,0,null);
//绘制选中点的红框
if (selectX>0 && selectY>0){
g.drawImage(selected,selectX*RATE+X_OFFSET,selectY*RATE+Y_OFFSET,null);
}
//遍历数组,绘制棋子
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE; j++) {
//绘制黑棋
if (board[i][j]==1){
g.drawImage(black,i*RATE+X_OFFSET,j*RATE+Y_OFFSET,null);
}
//绘制白棋
if (board[i][j]==2){
g.drawImage(white,i*RATE+X_OFFSET,j*RATE+Y_OFFSET,null);
}
}
}
}
}
private ChessBoard chessBoard = new ChessBoard();
5、定义变量,记录当前选中的坐标点对应的boad数组中对应的棋子索引
private int selectX = -1;
private int selectY = -1;
6、定义一个变量,记录当前用户选择下的是白棋还是黑棋还是清除,清除:0,黑棋:1,白棋:2;默认黑棋先走
private int chessCategory = 1;
7、定义Panel,放置点击按钮
Panel p = new Panel();
private Button whiteBtn = new Button("白棋");
private Button blackBtn = new Button("黑棋");
private Button clearBtn = new Button("删除");
8、按钮点击颜色更新
public void updateBtnColor(Color whiteBtnColor,Color blackBtnColor,Color clearBtnColor){
whiteBtn.setBackground(whiteBtnColor);
blackBtn.setBackground(blackBtnColor);
clearBtn.setBackground(clearBtnColor);
}
9、胜负判定方法
public boolean isWon(int x, int y, int color){
int count = 1;
int posX = 0;
int posY = 0;
/*先判断水平方向上的胜负
将水平方向以传入的点x上的y轴作为分割线分为两部分
先向左遍历,判断到的相同的连续的点 count++ */
for(posX = x-1; posX > 0;posX--){
if (board[posX][y] == color) {
count++;
if (count >= 5)
return true;
}else break;
}
for (posX = x +1; posX < BOARD_SIZE; posX++){
if(board[posX][y] == color){
count++;
if(count>=5)
return true;
} else
break;
}
count = 1;
for(posY = y-1;posY>0;posY--){
if(board[x][posY] == color){
count++;
if(count>=5)
return true;
} else
break;
}
for(posY = y+1; posY<BOARD_SIZE; posY++){
if(board[x][posY] == color){
count++;
if (count>=5)
return true;
} else
break;
}
count=1;
for(posX = x-1, posY = y-1; posX>0&&posY>0; posX--,posY--){
if (board[posX][posY] == color){
count++;
if(count>=5){
//count = 1;
return true;
}
} else
break;
}
for (posX = x+1, posY = y+1;posX<BOARD_SIZE&&posY<BOARD_SIZE; posX++, posY++){
if (board[posX][posY] == color){
count++;
if(count>=5){
//count = 1;
return true;
}
}else
break;
}
count=1;
for (posX = x+1, posY = y-1; posX<BOARD_SIZE&&posY>0; posX++,posY--){
if (board[posX][posY] == color){
count++;
if(count>=5){
//count = 1;
return true;
}
}else
break;
}
for (posX = x-1, posY = y+1; posX>0&&posY<BOARD_SIZE; posX--,posY++){
if (board[posX][posY] == color){
count++;
if(count>=5){
//count = 1;
return true;
}
}else
break;
}
return false;
}
10、主要功能函数
public void init() throws Exception{
//初始化按钮的颜色
updateBtnColor(Color.LIGHT_GRAY,Color.GREEN,Color.LIGHT_GRAY);
whiteBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
chessCategory = 2;
updateBtnColor(Color.GREEN,Color.LIGHT_GRAY,Color.LIGHT_GRAY);
}
});
blackBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
chessCategory=1;
updateBtnColor(Color.LIGHT_GRAY,Color.GREEN,Color.LIGHT_GRAY);
}
});
clearBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
chessCategory=0;
updateBtnColor(Color.LIGHT_GRAY,Color.LIGHT_GRAY,Color.GREEN);
}
});
p.add(whiteBtn);
p.add(blackBtn);
p.add(clearBtn);
//把Panel放入到frame底部
f.add(p,BorderLayout.SOUTH);
//初始化黑棋,白棋,棋盘,选中框
table = ImageIO.read(new File("board.jpg"));
black = ImageIO.read(new File("black.gif"));
white = ImageIO.read(new File("white.gif"));
selected = ImageIO.read(new File("selected.gif"));
//初始化board数组,默认情况下,所有位置处都没有棋子
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE; j++) {
board[i][j]=0;
}
}
//设置chessBoard的最佳大小
chessBoard.setPreferredSize(new Dimension(TABLE_WIDTH,TABLE_HEIGHT));
//给chessBoard注册鼠标监听器
chessBoard.addMouseListener(new MouseAdapter() {
//鼠标单击会触发
@Override
public void mouseClicked(MouseEvent e) {
//将用户鼠标的坐标,转换成棋子的坐标
int xPos = (e.getX()-X_OFFSET)/RATE;
int yPos = (e.getY()-Y_OFFSET)/RATE;
if(board[xPos][yPos]==0 && chessCategory!=0)
board[xPos][yPos] = chessCategory;
else if(chessCategory == 0)
board[xPos][yPos] = chessCategory;
//重绘chessBoard
chessBoard.repaint();
if(chessCategory!=0){
if(isWon(xPos,yPos,chessCategory)) {
if (chessCategory == 1)
JOptionPane.showMessageDialog(null, "黑棋胜利", "结束",
JOptionPane.WARNING_MESSAGE,new ImageIcon("win.png"));
else
JOptionPane.showMessageDialog(null, "白棋胜利", "结束",
JOptionPane.WARNING_MESSAGE, new ImageIcon("win.png"));
for (int i = 0; i < BOARD_SIZE; i++)
for (int j = 0; j < BOARD_SIZE; j++)
board[i][j] = 0;
chessBoard.repaint();
}
}
}
//当鼠标退出棋盘区域后,复位选中坐标,重绘chessBoard,要保证红色选中框显示正确
@Override
public void mouseExited(MouseEvent e) {
selectX=-1;
selectY=-1;
chessBoard.repaint();
}
});
//给chessBoard注册鼠标移动监听器
chessBoard.addMouseMotionListener(new MouseMotionAdapter() {
//当鼠标移动时,修正selectX和selectY,重绘chessBoard,要保证红色选中框显示正确
@Override
public void mouseMoved(MouseEvent e) {
//将鼠标的坐标,转换成棋子的索引
selectX = (e.getX()-X_OFFSET)/RATE;
selectY = (e.getY()-Y_OFFSET)/RATE;
chessBoard.repaint();
}
});
//把chessBoard添加到Frame中
f.add(chessBoard);
f.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
//设置frame最佳大小并可见
f.pack();
f.setVisible(true);
}
11、主函数
public static void main(String[] args) throws Exception{
new Gobang().init();
}