Java 黑白棋 源代码280


简介

黑白棋,也称为奥赛罗棋或反转棋,是一种两人对弈的策略棋盘游戏。它的目标是将棋盘上尽可能多的棋子翻转成自己的颜色,并在游戏结束时拥有更多颜色的棋子。

Java 黑白棋代码

以下是用 Java 编写的黑白棋游戏的源代码:
```java
import ;
public class Othello {
// 定义棋盘大小
private static final int BOARD_SIZE = 8;
// 定义棋盘
private static char[][] board = new char[BOARD_SIZE][BOARD_SIZE];
// 定义玩家符号
private static final char PLAYER1_SYMBOL = 'X';
private static final char PLAYER2_SYMBOL = 'O';
// 定义方向
private static final int[][] DIRECTIONS = {{-1, -1}, {-1, 0}, {-1, 1}, {0, -1}, {0, 1}, {1, -1}, {1, 0}, {1, 1}};
public static void main(String[] args) {
// 初始化棋盘
initializeBoard();
// 交替轮流
boolean player1Turn = true;
while (true) {
// 获取玩家输入
int row = getRowInput();
int col = getColInput();
// 检查输入是否合法
if (!isValidMove(player1Turn, row, col)) {
("Invalid move. Please try again.");
continue;
}
// 下棋
placePiece(player1Turn, row, col);
// 翻转棋子
flipPieces(player1Turn, row, col);
// 交换玩家
player1Turn = !player1Turn;
// 检查游戏是否结束
if (isGameOver()) {
break;
}
}
// 打印游戏结果
printBoard();
(getWinner());
}
// 初始化棋盘
private static void initializeBoard() {
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE; j++) {
board[i][j] = ' ';
}
}
// 设置初始棋子
board[3][3] = PLAYER1_SYMBOL;
board[3][4] = PLAYER2_SYMBOL;
board[4][3] = PLAYER2_SYMBOL;
board[4][4] = PLAYER1_SYMBOL;
}
// 获取玩家输入的行号
private static int getRowInput() {
Scanner scanner = new Scanner();
("Enter row number (1-8): ");
return () - 1;
}
// 获取玩家输入的列号
private static int getColInput() {
Scanner scanner = new Scanner();
("Enter column number (1-8): ");
return () - 1;
}
// 检查输入是否合法
private static boolean isValidMove(boolean player1Turn, int row, int col) {
// 检查边界
if (row < 0 || row >= BOARD_SIZE || col < 0 || col >= BOARD_SIZE) {
return false;
}
// 检查该位置是否已经下过棋
if (board[row][col] != ' ') {
return false;
}
// 检查是否有相邻的异色棋子
boolean hasAdjacentOpponentPiece = false;
for (int[] direction : DIRECTIONS) {
int i = row + direction[0];
int j = col + direction[1];
if (i >= 0 && i < BOARD_SIZE && j >= 0 && j < BOARD_SIZE && board[i][j] == (player1Turn ? PLAYER2_SYMBOL : PLAYER1_SYMBOL)) {
hasAdjacentOpponentPiece = true;
break;
}
}
return hasAdjacentOpponentPiece;
}
// 下棋
private static void placePiece(boolean player1Turn, int row, int col) {
board[row][col] = player1Turn ? PLAYER1_SYMBOL : PLAYER2_SYMBOL;
}
// 翻转棋子
private static void flipPieces(boolean player1Turn, int row, int col) {
for (int[] direction : DIRECTIONS) {
int i = row + direction[0];
int j = col + direction[1];
while (i >= 0 && i < BOARD_SIZE && j >= 0 && j < BOARD_SIZE && board[i][j] == (player1Turn ? PLAYER2_SYMBOL : PLAYER1_SYMBOL)) {
board[i][j] = player1Turn ? PLAYER1_SYMBOL : PLAYER2_SYMBOL;
i += direction[0];
j += direction[1];
}
}
}
// 检查游戏是否结束
private static boolean isGameOver() {
// 检查是否有空位
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 static void printBoard() {
for (char[] row : board) {
for (char cell : row) {
(cell + " ");
}
();
}
}
// 获取获胜者
private static String getWinner() {
int player1Count = 0;
int player2Count = 0;
for (char[] row : board) {
for (char cell : row) {
if (cell == PLAYER1_SYMBOL) {
player1Count++;
} else if (cell == PLAYER2_SYMBOL) {
player2Count++;
}
}
}
if (player1Count > player2Count) {
return "Player 1 wins!";
} else if (player2Count > player1Count) {
return "Player 2 wins!";
} else {
return "Draw!";
}
}
}
```

2024-12-10


上一篇:Java 中定义字节数组

下一篇:Java 中使用 MySQL 查询数据库的全面指南