四、五子棋小游戏设计与实现 使用JavaSwing完成一款五子棋小游戏 功能要求: 1. 设计五子棋游戏棋盘 2. 首页菜单要求具备重新开始、悔棋、退出功能 3. 游戏要求具备人人、人机模式 4. 提供帮助菜单 说明游戏规则 Java
时间: 2024-02-03 22:13:01 浏览: 84
Java+Swing实现的五子棋小游戏
5星 · 资源好评率100%
Swing是Java提供的图形用户界面(GUI)工具包,可以用来实现窗口、按钮、标签等GUI组件。下面是一个简单的五子棋小游戏的设计与实现:
1. 设计五子棋游戏棋盘
我们可以使用JPanel来实现游戏棋盘,可以通过设置背景颜色、绘制线条等方式来美化游戏棋盘。下面是一个简单的实现:
```java
public class ChessBoard extends JPanel {
private int[][] board; // 存储棋盘状态
private int currentPlayer; // 当前落子方
private boolean isGameOver; // 游戏是否结束
public ChessBoard() {
board = new int[15][15];
currentPlayer = 1;
isGameOver = false;
setPreferredSize(new Dimension(500, 500));
addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if (isGameOver) {
return;
}
int row = e.getY() / 33;
int col = e.getX() / 33;
if (row >= 0 && row < 15 && col >= 0 && col < 15 && board[row][col] == 0) {
board[row][col] = currentPlayer;
if (checkWin(row, col)) {
JOptionPane.showMessageDialog(null, "Player " + currentPlayer + " wins!");
isGameOver = true;
} else {
currentPlayer = 3 - currentPlayer;
}
repaint();
}
}
});
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
for (int i = 0; i < 15; i++) {
g.drawLine(0, i * 33, 495, i * 33);
g.drawLine(i * 33, 0, i * 33, 495);
}
for (int i = 0; i < 15; i++) {
for (int j = 0; j < 15; j++) {
if (board[i][j] == 1) {
g.setColor(Color.BLACK);
g.fillOval(j * 33 - 15, i * 33 - 15, 30, 30);
} else if (board[i][j] == 2) {
g.setColor(Color.WHITE);
g.fillOval(j * 33 - 15, i * 33 - 15, 30, 30);
}
}
}
}
private boolean checkWin(int row, int col) {
int count = 1;
int i, j;
i = row - 1;
j = col;
while (i >= 0 && board[i][j] == currentPlayer) {
count++;
i--;
}
i = row + 1;
j = col;
while (i < 15 && board[i][j] == currentPlayer) {
count++;
i++;
}
if (count >= 5) {
return true;
}
count = 1;
i = row;
j = col - 1;
while (j >= 0 && board[i][j] == currentPlayer) {
count++;
j--;
}
i = row;
j = col + 1;
while (j < 15 && board[i][j] == currentPlayer) {
count++;
j++;
}
if (count >= 5) {
return true;
}
count = 1;
i = row - 1;
j = col - 1;
while (i >= 0 && j >= 0 && board[i][j] == currentPlayer) {
count++;
i--;
j--;
}
i = row + 1;
j = col + 1;
while (i < 15 && j < 15 && board[i][j] == currentPlayer) {
count++;
i++;
j++;
}
if (count >= 5) {
return true;
}
count = 1;
i = row - 1;
j = col + 1;
while (i >= 0 && j < 15 && board[i][j] == currentPlayer) {
count++;
i--;
j++;
}
i = row + 1;
j = col - 1;
while (i < 15 && j >= 0 && board[i][j] == currentPlayer) {
count++;
i++;
j--;
}
if (count >= 5) {
return true;
}
return false;
}
}
```
2. 首页菜单要求具备重新开始、悔棋、退出功能
我们可以使用JMenuBar和JMenu来实现菜单栏,通过添加JMenuItem来实现每个菜单项的功能。下面是一个简单的实现:
```java
public class FiveChessGame extends JFrame {
private ChessBoard chessBoard;
public FiveChessGame() {
setTitle("Five Chess Game");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setResizable(false);
chessBoard = new ChessBoard();
setContentPane(chessBoard);
JMenuBar menuBar = new JMenuBar();
JMenu gameMenu = new JMenu("Game");
JMenuItem restartItem = new JMenuItem("Restart");
restartItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
chessBoard = new ChessBoard();
setContentPane(chessBoard);
pack();
setLocationRelativeTo(null);
}
});
gameMenu.add(restartItem);
JMenuItem undoItem = new JMenuItem("Undo");
undoItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
chessBoard.undo();
}
});
gameMenu.add(undoItem);
JMenuItem exitItem = new JMenuItem("Exit");
exitItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
gameMenu.add(exitItem);
menuBar.add(gameMenu);
JMenu helpMenu = new JMenu("Help");
JMenuItem ruleItem = new JMenuItem("Rule");
ruleItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "The rule of five chess game is to connect five pieces vertically, horizontally or diagonally.");
}
});
helpMenu.add(ruleItem);
menuBar.add(helpMenu);
setJMenuBar(menuBar);
pack();
setLocationRelativeTo(null);
setVisible(true);
}
public static void main(String[] args) {
new FiveChessGame();
}
}
```
3. 游戏要求具备人人、人机模式
我们可以通过添加一个JRadioButton来实现人人和人机模式的选择。对于人机模式,我们可以使用简单的随机算法来实现电脑的落子。下面是一个简单的实现:
```java
public class ChessBoard extends JPanel {
private int[][] board; // 存储棋盘状态
private int currentPlayer; // 当前落子方
private boolean isGameOver; // 游戏是否结束
private boolean isAI; // 是否为AI模式
public ChessBoard() {
board = new int[15][15];
currentPlayer = 1;
isGameOver = false;
isAI = false;
setPreferredSize(new Dimension(500, 500));
addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if (isGameOver) {
return;
}
int row = e.getY() / 33;
int col = e.getX() / 33;
if (row >= 0 && row < 15 && col >= 0 && col < 15 && board[row][col] == 0) {
board[row][col] = currentPlayer;
if (checkWin(row, col)) {
JOptionPane.showMessageDialog(null, "Player " + currentPlayer + " wins!");
isGameOver = true;
} else {
currentPlayer = 3 - currentPlayer;
if (isAI) {
aiPlay();
}
}
repaint();
}
}
});
}
public void setAI(boolean isAI) {
this.isAI = isAI;
}
private void aiPlay() {
Random random = new Random();
int row, col;
do {
row = random.nextInt(15);
col = random.nextInt(15);
} while (board[row][col] != 0);
board[row][col] = currentPlayer;
if (checkWin(row, col)) {
JOptionPane.showMessageDialog(null, "Player " + currentPlayer + " wins!");
isGameOver = true;
} else {
currentPlayer = 3 - currentPlayer;
}
repaint();
}
}
```
4. 提供帮助菜单说明游戏规则
我们可以使用JOptionPane来实现游戏规则的提示。下面是一个简单的实现:
```java
public class FiveChessGame extends JFrame {
private ChessBoard chessBoard;
public FiveChessGame() {
setTitle("Five Chess Game");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setResizable(false);
chessBoard = new ChessBoard();
setContentPane(chessBoard);
JMenuBar menuBar = new JMenuBar();
JMenu gameMenu = new JMenu("Game");
JMenuItem restartItem = new JMenuItem("Restart");
restartItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
chessBoard = new ChessBoard();
setContentPane(chessBoard);
pack();
setLocationRelativeTo(null);
}
});
gameMenu.add(restartItem);
JMenuItem undoItem = new JMenuItem("Undo");
undoItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
chessBoard.undo();
}
});
gameMenu.add(undoItem);
JMenuItem exitItem = new JMenuItem("Exit");
exitItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
gameMenu.add(exitItem);
menuBar.add(gameMenu);
JMenu helpMenu = new JMenu("Help");
JMenuItem ruleItem = new JMenuItem("Rule");
ruleItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "The rule of five chess game is to connect five pieces vertically, horizontally or diagonally.");
}
});
helpMenu.add(ruleItem);
menuBar.add(helpMenu);
JMenu modeMenu = new JMenu("Mode");
JRadioButtonMenuItem playerItem = new JRadioButtonMenuItem("Player vs. Player");
playerItem.setSelected(true);
playerItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
chessBoard.setAI(false);
}
});
modeMenu.add(playerItem);
JRadioButtonMenuItem aiItem = new JRadioButtonMenuItem("Player vs. AI");
aiItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
chessBoard.setAI(true);
}
});
modeMenu.add(aiItem);
ButtonGroup group = new ButtonGroup();
group.add(playerItem);
group.add(aiItem);
menuBar.add(modeMenu);
setJMenuBar(menuBar);
pack();
setLocationRelativeTo(null);
setVisible(true);
}
public static void main(String[] args) {
new FiveChessGame();
}
}
```
以上就是一个简单的五子棋小游戏的设计与实现,希望能对您有所帮助!
阅读全文