利用Java.swing包
话不多说直接上代码!
该程序使用JButton数组来表示棋盘,在按钮中显示黑棋、白棋或空棋盘图标。每当玩家点击一个按钮,程序会检查该位置是否可以放置棋子,然后判断是否有五子连线或者是否平局,最后切换到下一个玩家。如果游戏结束,程序会弹出一个对话框提示。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class FiveInARow extends JFrame implements ActionListener {
private JButton[][] board; // 棋盘按钮数组
private ImageIcon black; // 黑棋图标
private ImageIcon white; // 白棋图标
private ImageIcon empty; // 空棋盘图标
private int currentPlayer; // 当前玩家(1代表黑棋,2代表白棋)
private boolean gameOver; // 游戏是否结束
public FiveInARow() {
// 初始化棋盘按钮数组
board = new JButton[15][15];
for (int i = 0; i < 15; i++) {
for (int j = 0; j < 15; j++) {
board[i][j] = new JButton();
board[i][j].setIcon(empty);
board[i][j].setPreferredSize(new Dimension(40, 40));
board[i][j].addActionListener(this);
}
}
// 初始化图标和玩家
black = new ImageIcon(getClass().getResource("black.png"));
white = new ImageIcon(getClass().getResource("white.png"));
empty = new ImageIcon(getClass().getResource("empty.png"));
currentPlayer = 1;
gameOver = false;
// 添加到布局中
JPanel panel = new JPanel(new GridLayout(15, 15));
for (int i = 0; i < 15; i++) {
for (int j = 0; j < 15; j++) {
panel.add(board[i][j]);
}
}
add(panel);
// 设置窗口属性
setTitle("五子棋");
setSize(600, 600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
}
// 处理按钮点击事件
public void actionPerformed(ActionEvent e) {
if (gameOver) {
return;
}
// 获取点击的按钮
JButton button = (JButton)e.getSource();
// 获取按钮在数组中的位置
int row = -1, col = -1;
for (int i = 0; i < 15; i++) {
for (int j = 0; j < 15; j++) {
if (button == board[i][j]) {
row = i;
col = j;
break;
}
}
}
// 如果该按钮已经有棋子或者按钮不在棋盘内,不做任何处理
if (row == -1 || col == -1 || board[row][col].getIcon() != empty) {
return;
}
// 在该位置放置当前玩家的棋子
board[row][col].setIcon(currentPlayer == 1 ? black : white);
// 检查是否有五子连线
if (checkWin(row, col)) {
JOptionPane.showMessageDialog(this, "玩家 " + currentPlayer + " 获胜!");
gameOver = true;
return;
}
// 判断是否平局
boolean isTie = true;
for (int i = 0; i < 15; i++) {
for (int j = 0; j < 15; j++) {
if (board[i][j].getIcon() == empty) {
isTie = false;
break;
}
}
if (!isTie) {
break;
}
}
if (isTie) {
JOptionPane.showMessageDialog(this, "平局!");
gameOver = true;
return;
}
// 切换到下一个玩家
currentPlayer = currentPlayer == 1 ? 2 : 1;
}
// 检查是否有五子连线
private boolean checkWin(int row, int col) {
int count = 1;
// 检查横向连线
for (int i = col - 1; i >= 0; i--) {
if (board[row][i].getIcon() == board[row][col].getIcon()) {
count++;
} else {
break;
}
}
for (int i = col + 1; i < 15; i++) {
if (board[row][i].getIcon() == board[row][col].getIcon()) {
count++;
} else {
break;
}
}
if (count >= 5) {
return true;
}
// 检查纵向连线
count = 1;
for (int i = row - 1; i >= 0; i--) {
if (board[i][col].getIcon() == board[row][col].getIcon()) {
count++;
} else {
break;
}
}
for (int i = row + 1; i < 15; i++) {
if (board[i][col].getIcon() == board[row][col].getIcon()) {
count++;
} else {
break;
}
}
if (count >= 5) {
return true;
}
// 检查左上到右下连线
count = 1;
for (int i = row - 1, j = col - 1; i >= 0 && j >= 0; i--, j--) {
if (board[i][j].getIcon() == board[row][col].getIcon()) {
count++;
} else {
break;
}
}
for (int i = row + 1, j = col + 1; i < 15 && j < 15; i++, j++) {
if (board[i][j].getIcon() == board[row][col].getIcon()) {
count++;
} else {
break;
}
}
if (count >= 5) {
return true;
}
// 检查左下到右上连线
count = 1;
for (int i = row + 1, j = col - 1; i < 15 && j >= 0; i++, j--) {
if (board[i][j].getIcon() == board[row][col].getIcon()) {
count++;
} else {
break;
}
}
for (int i = row - 1, j = col + 1; i >= 0 && j < 15; i--, j++) {
if (board[i][j].getIcon() == board[row][col].getIcon()) {
count++;
} else {
break;
}
}
if (count >= 5) {
return true;
}
// 无连线
return false;
}
public static void main(String[] args) {
new FiveInARow();
}
}