java代码双人对战五子棋实现悔棋、保存、认输游戏功能
时间: 2023-07-10 19:04:28 浏览: 123
以下是一个简单的 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` 方法中,使用一个循环来显示菜单,根据用户的选择调用相应的方法。
阅读全文