用java写一个五子棋程序
时间: 2023-11-26 10:02:48 浏览: 77
好的,以下是一个简单的五子棋程序的Java代码:
```java
import java.util.Scanner;
public class FiveChessGame {
private static final int BOARD_SIZE = 15; // 棋盘大小
private static final char BLACK_CHESS = '●'; // 黑棋
private static final char WHITE_CHESS = '○'; // 白棋
private char[][] board; // 棋盘
private boolean blackTurn; // 轮到黑棋下了吗
public FiveChessGame() {
board = new char[BOARD_SIZE][BOARD_SIZE];
blackTurn = true;
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE; j++) {
board[i][j] = '╋'; // 初始化棋盘
}
}
}
public void start() {
System.out.println("欢迎来玩五子棋!");
Scanner scanner = new Scanner(System.in);
while (true) {
printBoard();
if (isGameOver()) {
break;
}
char chess = blackTurn ? BLACK_CHESS : WHITE_CHESS;
System.out.print("请" + (blackTurn ? "黑" : "白") + "方下棋(格式:x y):");
int x = scanner.nextInt();
int y = scanner.nextInt();
if (!isValidMove(x, y)) {
System.out.println("无效的落子,请重新输入!");
continue;
}
board[x][y] = chess;
blackTurn = !blackTurn;
}
printBoard();
System.out.println("游戏结束!");
}
private void printBoard() {
System.out.print(" ");
for (int i = 0; i < BOARD_SIZE; i++) {
System.out.print(i + " ");
}
System.out.println();
for (int i = 0; i < BOARD_SIZE; i++) {
System.out.print(i + " ");
for (int j = 0; j < BOARD_SIZE; j++) {
System.out.print(board[i][j] + " ");
}
System.out.println();
}
}
private boolean isGameOver() {
if (hasWon(BLACK_CHESS)) {
System.out.println("黑方胜利!");
return true;
} else if (hasWon(WHITE_CHESS)) {
System.out.println("白方胜利!");
return true;
} else if (isFull()) {
System.out.println("平局!");
return true;
} else {
return false;
}
}
private boolean hasWon(char chess) {
// 检查每一行
for (int i = 0; i < BOARD_SIZE; i++) {
int count = 0;
for (int j = 0; j < BOARD_SIZE; j++) {
if (board[i][j] == chess) {
count++;
if (count == 5) {
return true;
}
} else {
count = 0;
}
}
}
// 检查每一列
for (int i = 0; i < BOARD_SIZE; i++) {
int count = 0;
for (int j = 0; j < BOARD_SIZE; j++) {
if (board[j][i] == chess) {
count++;
if (count == 5) {
return true;
}
} else {
count = 0;
}
}
}
// 检查正对角线
for (int i = 0; i < BOARD_SIZE - 4; i++) {
for (int j = 0; j < BOARD_SIZE - 4; j++) {
int count = 0;
for (int k = 0; k < 5; k++) {
if (board[i+k][j+k] == chess) {
count++;
if (count == 5) {
return true;
}
} else {
break;
}
}
}
}
// 检查反对角线
for (int i = 0; i < BOARD_SIZE - 4; i++) {
for (int j = 4; j < BOARD_SIZE; j++) {
int count = 0;
for (int k = 0; k < 5; k++) {
if (board[i+k][j-k] == chess) {
count++;
if (count == 5) {
return true;
}
} else {
break;
}
}
}
}
return false;
}
private boolean isFull() {
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE; j++) {
if (board[i][j] == '╋') {
return false;
}
}
}
return true;
}
private boolean isValidMove(int x, int y) {
return x >= 0 && x < BOARD_SIZE && y >= 0 && y < BOARD_SIZE && board[x][y] == '╋';
}
}
```
你可以在 `main` 方法中创建一个 `FiveChessGame` 对象并调用 `start` 方法来开始游戏。例如:
```java
public static void main(String[] args) {
FiveChessGame game = new FiveChessGame();
game.start();
}
```
在游戏中,你需要输入一个坐标来下棋。例如,如果你想在第3行第4列下黑棋,可以输入 `2 3`。如果输入的坐标无效(例如超出了棋盘范围或已经有棋子了),程序会提示你重新输入。
阅读全文