帮我写一个五子棋的Java代码
时间: 2023-08-20 07:10:33 浏览: 90
好的,以下是一个简单的五子棋Java代码,供您参考:
```java
import java.util.Scanner;
public class GobangGame {
// 棋盘大小
private static final int BOARD_SIZE = 15;
// 空位
private static final int EMPTY = 0;
// 黑子
private static final int BLACK = 1;
// 白子
private static final int WHITE = 2;
// 棋盘
private int[][] board = new int[BOARD_SIZE][BOARD_SIZE];
// 当前下棋方
private int playSide = BLACK;
// 读取用户输入
private Scanner scanner = new Scanner(System.in);
// 游戏开始
public void start() {
// 初始化棋盘
initBoard();
// 开始下棋
while (true) {
// 打印棋盘
printBoard();
// 读取用户输入
System.out.println("请 " + (playSide == BLACK ? "黑方" : "白方") + " 下棋:");
int x = scanner.nextInt();
int y = scanner.nextInt();
// 判断落子是否合法
if (x < 0 || x >= BOARD_SIZE || y < 0 || y >= BOARD_SIZE) {
System.out.println("落子位置不合法,请重新输入!");
continue;
}
if (board[x][y] != EMPTY) {
System.out.println("该位置已经落子,请重新输入!");
continue;
}
// 下棋
board[x][y] = playSide;
// 判断是否获胜
if (isWin(x, y)) {
System.out.println((playSide == BLACK ? "黑方" : "白方") + " 获胜!");
break;
}
// 判断是否平局
if (isDraw()) {
System.out.println("平局!");
break;
}
// 切换下棋方
playSide = (playSide == BLACK ? WHITE : BLACK);
}
}
// 初始化棋盘
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 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++) {
if (board[i][j] == EMPTY) {
System.out.print("+");
} else if (board[i][j] == BLACK) {
System.out.print("●");
} else {
System.out.print("○");
}
}
System.out.println();
}
}
// 判断是否获胜
private boolean isWin(int x, int y) {
// 判断横向是否连成五个
int count = 1;
for (int i = x - 1; i >= 0; i--) {
if (board[i][y] == playSide) {
count++;
} else {
break;
}
}
for (int i = x + 1; i < BOARD_SIZE; i++) {
if (board[i][y] == playSide) {
count++;
} else {
break;
}
}
if (count >= 5) {
return true;
}
// 判断纵向是否连成五个
count = 1;
for (int i = y - 1; i >= 0; i--) {
if (board[x][i] == playSide) {
count++;
} else {
break;
}
}
for (int i = y + 1; i < BOARD_SIZE; i++) {
if (board[x][i] == playSide) {
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 (board[i][j] == playSide) {
count++;
} else {
break;
}
}
for (int i = x + 1, j = y + 1; i < BOARD_SIZE && j < BOARD_SIZE; i++, j++) {
if (board[i][j] == playSide) {
count++;
} else {
break;
}
}
if (count >= 5) {
return true;
}
// 判断右斜线是否连成五个
count = 1;
for (int i = x - 1, j = y + 1; i >= 0 && j < BOARD_SIZE; i--, j++) {
if (board[i][j] == playSide) {
count++;
} else {
break;
}
}
for (int i = x + 1, j = y - 1; i < BOARD_SIZE && j >= 0; i++, j--) {
if (board[i][j] == playSide) {
count++;
} else {
break;
}
}
if (count >= 5) {
return true;
}
return false;
}
// 判断是否平局
private boolean isDraw() {
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;
}
}
```
使用方法:
```java
public class Main {
public static void main(String[] args) {
GobangGame game = new GobangGame();
game.start();
}
}
```
运行后,程序会要求您输入棋子的坐标,先输入横坐标,再输入纵坐标,用空格隔开。例如,输入 `7 7` 表示在第 7 行、第 7 列下棋。
阅读全文