java编写一个简单可进行游玩的五子棋
时间: 2023-08-02 19:05:09 浏览: 44
好的,以下是一个简单的 Java 五子棋游戏实现:
```java
import java.util.Scanner;
public class Gomoku {
// 棋盘大小
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 static int[][] board = new int[BOARD_SIZE][BOARD_SIZE];
// 当前落子的玩家,初始为黑方
private static int currPlayer = BLACK;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (true) {
// 打印棋盘
printBoard();
// 落子
System.out.println("当前是 " + (currPlayer == BLACK ? "黑方" : "白方") + " 落子,请输入坐标(如:3,4):");
String input = scanner.nextLine();
String[] inputs = input.split(",");
if (inputs.length != 2) {
System.out.println("无效的输入,请重新输入!");
continue;
}
int x = Integer.parseInt(inputs[0]);
int y = Integer.parseInt(inputs[1]);
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] = currPlayer;
// 判断胜负
if (checkWin(x, y)) {
printBoard();
System.out.println((currPlayer == BLACK ? "黑方" : "白方") + " 获胜!");
break;
}
// 切换玩家
currPlayer = currPlayer == BLACK ? WHITE : BLACK;
}
scanner.close();
}
// 打印棋盘
private static 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] == EMPTY ? "+" : board[i][j] == BLACK ? "●" : "○"));
}
System.out.println();
}
}
// 判断胜负
private static boolean checkWin(int x, int y) {
int count = 1;
// 横向
for (int i = x - 1; i >= 0; i--) {
if (board[i][y] == currPlayer) {
count++;
} else {
break;
}
}
for (int i = x + 1; i < BOARD_SIZE; i++) {
if (board[i][y] == currPlayer) {
count++;
} else {
break;
}
}
if (count >= 5) {
return true;
}
// 纵向
count = 1;
for (int i = y - 1; i >= 0; i--) {
if (board[x][i] == currPlayer) {
count++;
} else {
break;
}
}
for (int i = y + 1; i < BOARD_SIZE; i++) {
if (board[x][i] == currPlayer) {
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] == currPlayer) {
count++;
} else {
break;
}
}
for (int i = x + 1, j = y + 1; i < BOARD_SIZE && j < BOARD_SIZE; i++, j++) {
if (board[i][j] == currPlayer) {
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] == currPlayer) {
count++;
} else {
break;
}
}
for (int i = x + 1, j = y - 1; i < BOARD_SIZE && j >= 0; i++, j--) {
if (board[i][j] == currPlayer) {
count++;
} else {
break;
}
}
if (count >= 5) {
return true;
}
return false;
}
}
```
在这个实现中,我们通过一个二维数组来表示棋盘,其中 `EMPTY` 表示空格,`BLACK` 表示黑子,`WHITE` 表示白子。通过 `printBoard()` 方法来打印棋盘,通过 `checkWin(int x, int y)` 方法来判断胜负。在落子时,我们通过 `Scanner` 类来读取用户输入,判断输入是否合法,并且切换玩家。当有一方胜出时,游戏结束并且打印胜者。