具体代码如下所示
黑子和白子可以自定义,将下方图片路径修改为你的路径即可
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;
public class GomokuGame extends JFrame {
private static final int BOARD_SIZE = 15;
private static final int CELL_SIZE = 40;
private static final int BOARD_OFFSET = 20;
private enum Player { BLACK, WHITE }
private Player currentPlayer;
private JButton[][] board;
private ImageIcon blackIcon;
private ImageIcon whiteIcon;
public GomokuGame() {
setTitle("老杨大战老云,谁将获得版权费用?");
setSize(BOARD_SIZE * CELL_SIZE + BOARD_OFFSET, BOARD_SIZE * CELL_SIZE + BOARD_OFFSET);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
currentPlayer = Player.BLACK;
blackIcon = new ImageIcon("在这里输入你的图片路径(黑子)");
blackIcon.setImage(blackIcon.getImage().getScaledInstance(CELL_SIZE, CELL_SIZE, Image.SCALE_DEFAULT));
whiteIcon = new ImageIcon("在这里输入你的图片路径(白子)");
whiteIcon.setImage(whiteIcon.getImage().getScaledInstance(CELL_SIZE, CELL_SIZE, Image.SCALE_DEFAULT));
initializeBoard();
}
private void initializeBoard() {
setLayout(new GridLayout(BOARD_SIZE, BOARD_SIZE));
board = new JButton[BOARD_SIZE][BOARD_SIZE];
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE; j++) {
JButton cell = new JButton();
cell.setPreferredSize(new Dimension(CELL_SIZE, CELL_SIZE));
cell.setBackground(new Color(139, 69, 19));
cell.setBorder(BorderFactory.createLineBorder(Color.BLACK));
cell.addMouseListener(new CellClickListener(i, j));
board[i][j] = cell;
add(cell);
}
}
}
private class CellClickListener extends MouseAdapter {
private int row, col;
public CellClickListener(int row, int col) {
this.row = row;
this.col = col;
}
@Override
public void mouseClicked(MouseEvent e) {
JButton cell = (JButton) e.getComponent();
if (cell.getBackground().equals(new Color(139, 69, 19))) {
if (currentPlayer == Player.BLACK) {
cell.setIcon(blackIcon);
checkWin(row, col, blackIcon);
currentPlayer = Player.WHITE;
} else {
cell.setIcon(whiteIcon);
checkWin(row, col, whiteIcon);
currentPlayer = Player.BLACK;
}
cell.setBackground(null);
}
}
}
private boolean checkDirection(int row, int col, int rowIncrement, int colIncrement, ImageIcon icon) {
int count = 0;
for (int i = -4; i <= 4; i++) {
int newRow = row + i * rowIncrement;
int newCol = col + i * colIncrement;
if (newRow >= 0 && newRow < BOARD_SIZE && newCol >= 0 && newCol < BOARD_SIZE) {
if (board[newRow][newCol].getIcon() == icon) {
count++;
if (count == 5) {
return true;
}
} else {
count = 0;
}
} else {
count = 0;
}
}
return false;
}
private void checkWin(int row, int col, ImageIcon icon) {
if (checkDirection(row, col, 1, 0, icon) ||
checkDirection(row, col, 0, 1, icon) ||
checkDirection(row, col, 1, 1, icon) ||
checkDirection(row, col, 1, -1, icon)) {
JOptionPane.showMessageDialog(GomokuGame.this, icon == blackIcon ? "老杨获胜" : "老云获胜");
resetBoard();
}
}
private void resetBoard() {
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE; j++) {
board[i][j].setIcon(null);
board[i][j].setBackground(new Color(139, 69, 19));
}
}
currentPlayer = Player.BLACK;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
new GomokuGame().setVisible(true);
});
}
}