用 java 实现五子棋
时间: 2023-12-04 21:03:52 浏览: 78
好的,以下是 Java 实现五子棋的代码示例:
```
import java.util.Scanner;
public class FiveChess {
private static final int ROWS = 15; // 棋盘行数
private static final int COLS = 15; // 棋盘列数
private static final int EMPTY = 0; // 空格子
private static final int BLACK = 1; // 黑棋
private static final int WHITE = 2; // 白棋
private int[][] board; // 棋盘数组
private int currentPlayer; // 当前落子方
public FiveChess() {
board = new int[ROWS][COLS];
currentPlayer = BLACK; // 黑棋先行
}
// 落子
public void play(int row, int col) {
if (board[row][col] != EMPTY) {
throw new RuntimeException("该位置已有棋子!");
}
board[row][col] = currentPlayer;
currentPlayer = (currentPlayer == BLACK) ? WHITE : BLACK; // 切换落子方
}
// 判断游戏是否结束
public boolean isGameOver() {
return isWin(BLACK) || isWin(WHITE) || isDraw();
}
// 判断是否平局
public boolean isDraw() {
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
if (board[i][j] == EMPTY) {
return false;
}
}
}
return true;
}
// 判断是否获胜
public boolean isWin(int player) {
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
if (board[i][j] == player) {
// 判断横向是否连续五子
if (j + 4 < COLS &&
board[i][j + 1] == player &&
board[i][j + 2] == player &&
board[i][j + 3] == player &&
board[i][j + 4] == player) {
return true;
}
// 判断纵向是否连续五子
if (i + 4 < ROWS &&
board[i + 1][j] == player &&
board[i + 2][j] == player &&
board[i + 3][j] == player &&
board[i + 4][j] == player) {
return true;
}
// 判断左斜是否连续五子
if (i + 4 < ROWS && j + 4 < COLS &&
board[i + 1][j + 1] == player &&
board[i + 2][j + 2] == player &&
board[i + 3][j + 3] == player &&
board[i + 4][j + 4] == player) {
return true;
}
// 判断右斜是否连续五子
if (i + 4 < ROWS && j - 4 >= 0 &&
board[i + 1][j - 1] == player &&
board[i + 2][j - 2] == player &&
board[i + 3][j - 3] == player &&
board[i + 4][j - 4] == player) {
return true;
}
}
}
}
return false;
}
// 打印棋盘
public void printBoard() {
System.out.print(" ");
for (int j = 0; j < COLS; j++) {
System.out.print(j + " ");
}
System.out.println();
for (int i = 0; i < ROWS; i++) {
System.out.print(i + " ");
for (int j = 0; j < COLS; 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(i);
}
System.out.print(" ");
for (int j = 0; j < COLS; j++) {
System.out.print(j + " ");
}
}
public static void main(String[] args) {
FiveChess game = new FiveChess();
Scanner scanner = new Scanner(System.in);
while (!game.isGameOver()) {
game.printBoard();
System.out.print("请 " + ((game.currentPlayer == BLACK) ? "黑方" : "白方") + " 落子(例如:3,4):");
String input = scanner.nextLine();
String[] parts = input.split(",");
int row = Integer.parseInt(parts[0]);
int col = Integer.parseInt(parts[1]);
try {
game.play(row, col);
} catch (RuntimeException e) {
System.out.println(e.getMessage());
}
}
game.printBoard();
if (game.isDraw()) {
System.out.println("平局!");
} else {
System.out.println(((game.currentPlayer == BLACK) ? "白方" : "黑方") + " 获胜!");
}
}
}
```
使用上述代码可以在控制台上实现五子棋游戏。
阅读全文