用java编写五子棋小游戏
时间: 2023-11-27 12:02:38 浏览: 68
使用java编写的五子棋游戏
好的,以下是一个简单的五子棋小游戏的Java代码示例:
```java
import java.util.Scanner;
public class FiveInARow {
private static final int BOARD_SIZE = 15; // 棋盘大小
private static final char EMPTY = ' '; // 空格
private static final char BLACK = 'X'; // 黑子
private static final char WHITE = 'O'; // 白子
private char[][] board; // 棋盘
private char currentPlayer; // 当前玩家
public FiveInARow() {
board = new char[BOARD_SIZE][BOARD_SIZE];
currentPlayer = BLACK; // 黑子先手
initBoard();
}
// 初始化棋盘
private void initBoard() {
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE; j++) {
board[i][j] = EMPTY;
}
}
}
// 显示棋盘
private void displayBoard() {
System.out.print(" ");
for (int i = 0; i < BOARD_SIZE; i++) {
System.out.print((char)('A' + i) + " ");
}
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 makeMove(int row, int col) {
if (row >= 0 && row < BOARD_SIZE && col >= 0 && col < BOARD_SIZE && board[row][col] == EMPTY) {
board[row][col] = currentPlayer;
return true;
}
return false;
}
// 判断胜负
private boolean checkWin(int row, int col) {
int count = 0;
// 横向
for (int i = 0; i < 5; i++) {
if (col - i >= 0 && col - i + 4 < BOARD_SIZE && board[row][col - i] == currentPlayer
&& board[row][col - i + 1] == currentPlayer
&& board[row][col - i + 2] == currentPlayer
&& board[row][col - i + 3] == currentPlayer
&& board[row][col - i + 4] == currentPlayer) {
count++;
}
}
// 竖向
for (int i = 0; i < 5; i++) {
if (row - i >= 0 && row - i + 4 < BOARD_SIZE && board[row - i][col] == currentPlayer
&& board[row - i + 1][col] == currentPlayer
&& board[row - i + 2][col] == currentPlayer
&& board[row - i + 3][col] == currentPlayer
&& board[row - i + 4][col] == currentPlayer) {
count++;
}
}
// 左上到右下
for (int i = 0; i < 5; i++) {
if (row - i >= 0 && row - i + 4 < BOARD_SIZE && col - i >= 0 && col - i + 4 < BOARD_SIZE
&& board[row - i][col - i] == currentPlayer
&& board[row - i + 1][col - i + 1] == currentPlayer
&& board[row - i + 2][col - i + 2] == currentPlayer
&& board[row - i + 3][col - i + 3] == currentPlayer
&& board[row - i + 4][col - i + 4] == currentPlayer) {
count++;
}
}
// 左下到右上
for (int i = 0; i < 5; i++) {
if (row + i < BOARD_SIZE && row + i - 4 >= 0 && col - i >= 0 && col - i + 4 < BOARD_SIZE
&& board[row + i][col - i] == currentPlayer
&& board[row + i - 1][col - i + 1] == currentPlayer
&& board[row + i - 2][col - i + 2] == currentPlayer
&& board[row + i - 3][col - i + 3] == currentPlayer
&& board[row + i - 4][col - i + 4] == currentPlayer) {
count++;
}
}
return count > 0;
}
// 切换玩家
private void switchPlayer() {
if (currentPlayer == BLACK) {
currentPlayer = WHITE;
} else {
currentPlayer = BLACK;
}
}
// 游戏主流程
public void play() {
Scanner scanner = new Scanner(System.in);
int row, col;
while (true) {
displayBoard();
System.out.print(currentPlayer + " Player's turn. Enter your move: ");
String move = scanner.nextLine().toUpperCase();
if (move.equals("QUIT")) {
System.out.println("Game over.");
return;
}
col = move.charAt(0) - 'A';
row = Integer.parseInt(move.substring(1)) - 1;
if (makeMove(row, col)) {
if (checkWin(row, col)) {
displayBoard();
System.out.println(currentPlayer + " Player wins!");
return;
} else if (isBoardFull()) {
displayBoard();
System.out.println("Game ends in a tie!");
return;
} else {
switchPlayer();
}
} else {
System.out.println("Invalid move. Try again.");
}
}
}
// 判断棋盘是否已满
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 static void main(String[] args) {
FiveInARow game = new FiveInARow();
game.play();
}
}
```
这个五子棋小游戏是控制台应用程序,运行后会提示玩家输入落子的位置,例如:A1 表示第一行第一列。
注意:这只是一个简单的五子棋小游戏示例,还有很多优化和改进的空间。
阅读全文