java五子棋人机博弈
时间: 2023-12-31 20:24:36 浏览: 72
根据提供的引用内容,以下是一个简单的Java五子棋人机博弈的示例代码:
```java
import java.util.Scanner;
public class GomokuGame {
private static final int SIZE = 15;
private static final char EMPTY = '-';
private static final char PLAYER = 'X';
private static final char COMPUTER = 'O';
private char[][] board;
public GomokuGame() {
board = new char[SIZE][SIZE];
initializeBoard();
}
private void initializeBoard() {
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
board[i][j] = EMPTY;
}
}
}
private void printBoard() {
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
System.out.print(board[i][j] + " ");
}
System.out.println();
}
}
private boolean isBoardFull() {
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
if (board[i][j] == EMPTY) {
return false;
}
}
}
return true;
}
private boolean isWinningMove(int row, int col, char player) {
// 检查行
int count = 0;
for (int i = Math.max(0, col - 4); i <= Math.min(SIZE - 1, col + 4); i++) {
if (board[row][i] == player) {
count++;
if (count == 5) {
return true;
}
} else {
count = 0;
}
}
// 检查列
count = 0;
for (int i = Math.max(0, row - 4); i <= Math.min(SIZE - 1, row + 4); i++) {
if (board[i][col] == player) {
count++;
if (count == 5) {
return true;
}
} else {
count = 0;
}
}
// 检查主对角线
count = 0;
int startRow = Math.max(0, row - 4);
int startCol = Math.max(0, col - 4);
while (startRow <= Math.min(SIZE - 1, row + 4) && startCol <= Math.min(SIZE - 1, col + 4)) {
if (board[startRow][startCol] == player) {
count++;
if (count == 5) {
return true;
}
} else {
count = 0;
}
startRow++;
startCol++;
}
// 检查副对角线
count = 0;
startRow = Math.min(SIZE - 1, row + 4);
startCol = Math.max(0, col - 4);
while (startRow >= Math.max(0, row - 4) && startCol <= Math.min(SIZE - 1, col + 4)) {
if (board[startRow][startCol] == player) {
count++;
if (count == 5) {
return true; }
} else {
count = 0;
}
startRow--;
startCol++;
}
return false;
}
private boolean isValidMove(int row, int col) {
return row >= 0 && row < SIZE && col >= 0 && col < SIZE && board[row][col] == EMPTY;
}
private void makeMove(int row, int col, char player) {
board[row][col] = player;
}
private void playGame() {
Scanner scanner = new Scanner(System.in);
boolean isPlayerTurn = true;
while (true) {
System.out.println("当前棋盘:");
printBoard();
if (isPlayerTurn) {
System.out.println("玩家,请输入您的下棋位置(行 列,以空格分隔):");
int row = scanner.nextInt();
int col = scanner.nextInt();
if (isValidMove(row, col)) {
makeMove(row, col, PLAYER);
if (isWinningMove(row, col, PLAYER)) {
System.out.println("玩家获胜!");
break;
}
isPlayerTurn = false;
} else {
System.out.println("无效的位置,请重新输入。");
}
} else {
System.out.println("电脑正在思考下棋位置...");
// 在这里实现电脑的下棋逻辑
// ...
int row = 0; // 电脑下棋的行
int col = 0; // 电脑下棋的列
if (isValidMove(row, col)) {
makeMove(row, col, COMPUTER);
if (isWinningMove(row, col, COMPUTER)) {
System.out.println("电脑获胜!");
break;
}
isPlayerTurn = true;
}
}
if (isBoardFull()) {
System.out.println("平局!");
break;
}
}
scanner.close();
}
public static void main(String[] args) {
GomokuGame game = new GomokuGame();
game.playGame();
}
}
```
阅读全文