java双人对战五子棋实现悔棋、保存、认输游戏功能
时间: 2024-01-18 21:04:30 浏览: 50
实现双人对战五子棋的功能需求:
1. 棋盘显示:绘制一个棋盘,用于展示棋局。
2. 落子功能:玩家可以在棋盘上落子,每次只能落一颗棋子。需要判断当前位置是否有已经落子,如果有,则不能再次落子。
3. 判断胜负:每次玩家落子后,需要判断当前局面是否出现了五子连珠,如果出现,则该玩家胜利。
4. 悔棋功能:玩家可以选择悔棋,每次只能悔一步棋,需要将上一步的棋子从棋盘上移除。
5. 保存游戏:玩家可以选择保存当前游戏状态,下次进入游戏可以继续进行。
6. 认输游戏:玩家可以选择认输,对手则获得胜利。
实现思路:
1. 棋盘显示:通过Java Swing 绘制一个棋盘,可以使用JPanel或JFrame实现。
2. 落子功能:使用鼠标监听器监听玩家落子的位置,每次落子时需要判断当前位置是否已经落子,如果已经落子则不能再次落子。
3. 判断胜负:每次玩家落子后,需要判断当前局面是否出现了五子连珠,如果出现,则该玩家胜利。
4. 悔棋功能:使用栈来存储每一步落子的信息,当玩家选择悔棋时,从栈中弹出上一步的落子信息,并将该位置从棋盘上移除。
5. 保存游戏:使用文件流将当前游戏状态保存到本地文件中,下次进入游戏时读取该文件,恢复游戏状态。
6. 认输游戏:玩家可以选择认输,对手则获得胜利。
具体实现细节可以参考下面的代码示例:
相关问题
java代码双人对战五子棋实现悔棋、保存、认输游戏功能
以下是一个简单的 Java 代码实现,实现了双人对战五子棋的悔棋、保存、认输游戏功能。
```java
import java.util.Scanner;
public class FiveInRow {
private static final int BOARD_SIZE = 15;
private static final char PLAYER1 = 'X';
private static final char PLAYER2 = 'O';
private static final char EMPTY = '.';
private static final int MAX_UNDO = 5;
private char[][] board = new char[BOARD_SIZE][BOARD_SIZE];
private boolean player1Turn = true;
private int undoCount = 0;
public FiveInRow() {
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE; j++) {
board[i][j] = EMPTY;
}
}
}
public void play() {
Scanner scanner = new Scanner(System.in);
boolean gameOver = false;
while (!gameOver) {
drawBoard();
char player = player1Turn ? PLAYER1 : PLAYER2;
System.out.println("Player " + player + "'s turn.");
System.out.print("Enter row (1-" + BOARD_SIZE + "): ");
int row = scanner.nextInt() - 1;
System.out.print("Enter column (1-" + BOARD_SIZE + "): ");
int col = scanner.nextInt() - 1;
if (isValidMove(row, col)) {
makeMove(row, col, player);
if (isWinningMove(row, col)) {
gameOver = true;
System.out.println("Player " + player + " wins!");
} else if (isBoardFull()) {
gameOver = true;
System.out.println("Game over! The board is full.");
} else {
player1Turn = !player1Turn;
}
} else {
System.out.println("Invalid move. Please try again.");
}
}
}
private void drawBoard() {
System.out.print(" ");
for (int i = 0; i < BOARD_SIZE; i++) {
System.out.print((i + 1) + " ");
}
System.out.println();
for (int i = 0; i < BOARD_SIZE; i++) {
System.out.print((i + 1) + " ");
for (int j = 0; j < BOARD_SIZE; j++) {
System.out.print(board[i][j] + " ");
}
System.out.println();
}
}
private boolean isValidMove(int row, int col) {
if (row < 0 || row >= BOARD_SIZE || col < 0 || col >= BOARD_SIZE) {
return false;
}
return board[row][col] == EMPTY;
}
private void makeMove(int row, int col, char player) {
board[row][col] = player;
undoCount++;
}
private void undoMove(int row, int col) {
board[row][col] = EMPTY;
undoCount--;
}
private boolean isWinningMove(int row, int col) {
// Check horizontal
int count = 0;
for (int i = Math.max(0, col - 4); i <= Math.min(BOARD_SIZE - 1, col + 4); i++) {
if (board[row][i] == board[row][col]) {
count++;
} else {
count = 0;
}
if (count == 5) {
return true;
}
}
// Check vertical
count = 0;
for (int i = Math.max(0, row - 4); i <= Math.min(BOARD_SIZE - 1, row + 4); i++) {
if (board[i][col] == board[row][col]) {
count++;
} else {
count = 0;
}
if (count == 5) {
return true;
}
}
// Check diagonal
count = 0;
for (int i = -4; i <= 4; i++) {
int r = row + i;
int c = col + i;
if (r >= 0 && r < BOARD_SIZE && c >= 0 && c < BOARD_SIZE) {
if (board[r][c] == board[row][col]) {
count++;
} else {
count = 0;
}
if (count == 5) {
return true;
}
}
}
// Check anti-diagonal
count = 0;
for (int i = -4; i <= 4; i++) {
int r = row + i;
int c = col - i;
if (r >= 0 && r < BOARD_SIZE && c >= 0 && c < BOARD_SIZE) {
if (board[r][c] == board[row][col]) {
count++;
} else {
count = 0;
}
if (count == 5) {
return true;
}
}
}
return false;
}
private boolean isBoardFull() {
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE; j++) {
if (board[i][j] == EMPTY) {
return false;
}
}
}
return true;
}
public void undo() {
if (undoCount < MAX_UNDO) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter row (1-" + BOARD_SIZE + ") to undo: ");
int row = scanner.nextInt() - 1;
System.out.print("Enter column (1-" + BOARD_SIZE + ") to undo: ");
int col = scanner.nextInt() - 1;
if (board[row][col] != EMPTY) {
undoMove(row, col);
player1Turn = !player1Turn;
System.out.println("Undo successful.");
} else {
System.out.println("There is no piece to undo at that location.");
}
} else {
System.out.println("You have reached the maximum number of undos.");
}
}
public void save() {
// TODO: Implement save function
System.out.println("Save function not implemented.");
}
public void forfeit() {
char player = player1Turn ? PLAYER1 : PLAYER2;
System.out.println("Player " + player + " forfeits!");
player1Turn = !player1Turn;
}
public static void main(String[] args) {
FiveInRow game = new FiveInRow();
Scanner scanner = new Scanner(System.in);
boolean quit = false;
while (!quit) {
System.out.println("--------------------");
System.out.println("1. New game");
System.out.println("2. Undo move");
System.out.println("3. Save game");
System.out.println("4. Forfeit game");
System.out.println("5. Quit game");
System.out.print("Enter choice (1-5): ");
int choice = scanner.nextInt();
switch (choice) {
case 1:
game = new FiveInRow();
game.play();
break;
case 2:
game.undo();
break;
case 3:
game.save();
break;
case 4:
game.forfeit();
break;
case 5:
quit = true;
System.out.println("Thanks for playing!");
break;
default:
System.out.println("Invalid choice. Please try again.");
break;
}
}
}
}
```
该代码使用了一个 `FiveInRow` 类来实现游戏逻辑。`play` 方法用于循环进行游戏,每次循环中先绘制棋盘,然后根据当前玩家的回合提示输入行和列,再检查输入的位置是否合法,如果合法则落子并检查是否获胜或平局,否则提示重新输入。`drawBoard` 方法用于绘制棋盘,`isValidMove` 方法用于检查输入的位置是否合法,`makeMove` 方法用于落子,`isWinningMove` 方法用于检查是否获胜,`isBoardFull` 方法用于检查棋盘是否已满。游戏过程中还提供了 `undo`、`save` 和 `forfeit` 方法实现悔棋、保存和认输游戏功能。
在 `main` 方法中,使用一个循环来显示菜单,根据用户的选择调用相应的方法。
java实现五子棋游戏可开始,认输,悔棋
好的,以下是Java实现五子棋游戏的示例代码,包括可开始、认输、悔棋等功能:
```java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Gobang extends JFrame implements ActionListener, MouseListener {
private static final long serialVersionUID = 1L;
private JPanel mainPanel;
private int[][] chessboard;
private int currentPlayer;
private JLabel statusLabel;
private JButton restartButton;
private JButton backButton;
private int currentX;
private int currentY;
private int winner;
public Gobang() {
super("五子棋游戏");
mainPanel = new JPanel(new GridLayout(15, 15));
chessboard = new int[15][15];
currentPlayer = 1;
winner = 0;
for (int i = 0; i < 15; i++) {
for (int j = 0; j < 15; j++) {
JPanel panel = new JPanel();
panel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
mainPanel.add(panel);
panel.addMouseListener(this);
}
}
statusLabel = new JLabel("当前玩家: " + currentPlayer);
restartButton = new JButton("重新开始");
backButton = new JButton("悔棋");
JPanel bottomPanel = new JPanel(new GridLayout(1, 3));
bottomPanel.add(statusLabel);
bottomPanel.add(restartButton);
bottomPanel.add(backButton);
restartButton.addActionListener(this);
backButton.addActionListener(this);
add(mainPanel, BorderLayout.CENTER);
add(bottomPanel, BorderLayout.SOUTH);
pack();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == restartButton) {
restart();
} else if (e.getSource() == backButton) {
if (currentPlayer != winner) {
undo();
}
}
}
public void mouseClicked(MouseEvent e) {
if (winner != 0) {
return;
}
JPanel panel = (JPanel) e.getSource();
int index = mainPanel.getComponentZOrder(panel);
int x = index % 15;
int y = index / 15;
if (chessboard[x][y] != 0) {
return;
}
currentX = x;
currentY = y;
chessboard[x][y] = currentPlayer;
panel.setBackground(currentPlayer == 1 ? Color.BLACK : Color.WHITE);
if (checkWin(x, y)) {
winner = currentPlayer;
statusLabel.setText("玩家 " + winner + " 获胜");
} else {
currentPlayer = currentPlayer == 1 ? 2 : 1;
statusLabel.setText("当前玩家: " + currentPlayer);
}
}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
private boolean checkWin(int x, int y) {
int count = 1;
// 横向
for (int i = x - 1; i >= 0; i--) {
if (chessboard[i][y] == currentPlayer) {
count++;
} else {
break;
}
}
for (int i = x + 1; i < 15; i++) {
if (chessboard[i][y] == currentPlayer) {
count++;
} else {
break;
}
}
if (count >= 5) {
return true;
}
count = 1;
// 纵向
for (int i = y - 1; i >= 0; i--) {
if (chessboard[x][i] == currentPlayer) {
count++;
} else {
break;
}
}
for (int i = y + 1; i < 15; i++) {
if (chessboard[x][i] == currentPlayer) {
count++;
} else {
break;
}
}
if (count >= 5) {
return true;
}
count = 1;
// 斜向
for (int i = x - 1, j = y - 1; i >= 0 && j >= 0; i--, j--) {
if (chessboard[i][j] == currentPlayer) {
count++;
} else {
break;
}
}
for (int i = x + 1, j = y + 1; i < 15 && j < 15; i++, j++) {
if (chessboard[i][j] == currentPlayer) {
count++;
} else {
break;
}
}
if (count >= 5) {
return true;
}
count = 1;
// 反斜向
for (int i = x - 1, j = y + 1; i >= 0 && j < 15; i--, j++) {
if (chessboard[i][j] == currentPlayer) {
count++;
} else {
break;
}
}
for (int i = x + 1, j = y - 1; i < 15 && j >= 0; i++, j--) {
if (chessboard[i][j] == currentPlayer) {
count++;
} else {
break;
}
}
if (count >= 5) {
return true;
}
return false;
}
private void undo() {
chessboard[currentX][currentY] = 0;
Component component = mainPanel.getComponent(currentY * 15 + currentX);
component.setBackground(null);
currentPlayer = currentPlayer == 1 ? 2 : 1;
statusLabel.setText("当前玩家: " + currentPlayer);
}
private void restart() {
for (int i = 0; i < 15; i++) {
for (int j = 0; j < 15; j++) {
chessboard[i][j] = 0;
Component component = mainPanel.getComponent(j * 15 + i);
component.setBackground(null);
}
}
currentPlayer = 1;
winner = 0;
statusLabel.setText("当前玩家: " + currentPlayer);
}
public static void main(String[] args) {
new Gobang();
}
}
```
这个代码会创建一个15x15的棋盘,玩家点击棋盘上的格子可以下棋。当有一方获胜时,会显示胜利信息;玩家可以点击“重新开始”按钮重新开始游戏,也可以点击“悔棋”按钮悔棋。