人机对决五子棋Java代码
时间: 2023-07-09 08:36:23 浏览: 154
以下是一个简单的五子棋Java代码,实现了人机对弈的功能:
```java
import java.util.Scanner;
public class Gobang {
private static final int BOARD_SIZE = 15; // 棋盘大小
private static final char[] PIECES = {' ', 'X', 'O'}; // 棋子类型:空、玩家、电脑
private static final int PLAYER = 1; // 玩家标记
private static final int COMPUTER = 2; // 电脑标记
private int[][] board = new int[BOARD_SIZE][BOARD_SIZE]; // 棋盘
private int currentPlayer = PLAYER; // 当前玩家
public void start() {
Scanner scanner = new Scanner(System.in);
printBoard();
while (true) {
if (currentPlayer == PLAYER) {
System.out.print("请输入落子位置(格式:行 列,如:7 8):");
int row = scanner.nextInt();
int col = scanner.nextInt();
if (isValidMove(row, col)) {
move(row, col);
currentPlayer = COMPUTER;
} else {
System.out.println("无效的落子位置,请重新输入!");
}
} else {
int[] computerMove = findBestMove();
move(computerMove[0], computerMove[1]);
currentPlayer = PLAYER;
}
printBoard();
if (isGameOver()) {
break;
}
}
scanner.close();
}
// 打印棋盘
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++) {
System.out.print(PIECES[board[i][j]] + " ");
}
System.out.println(i);
}
System.out.print(" ");
for (int i = 0; i < BOARD_SIZE; i++) {
System.out.print(i + " ");
}
System.out.println();
}
// 判断落子位置是否合法
private boolean isValidMove(int row, int col) {
return row >= 0 && row < BOARD_SIZE && col >= 0 && col < BOARD_SIZE && board[row][col] == 0;
}
// 落子
private void move(int row, int col) {
board[row][col] = currentPlayer;
}
// 判断游戏是否结束
private boolean isGameOver() {
return isWin(currentPlayer) || isDraw();
}
// 判断是否平局
private boolean isDraw() {
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE; j++) {
if (board[i][j] == 0) {
return false; // 存在空位,不是平局
}
}
}
return true; // 没有空位,是平局
}
// 查找最佳落子位置
private int[] findBestMove() {
int[] bestMove = new int[2];
int bestScore = Integer.MIN_VALUE;
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE; j++) {
if (board[i][j] == 0) {
int score = minimax(i, j, 0, Integer.MIN_VALUE, Integer.MAX_VALUE, COMPUTER);
if (score > bestScore) {
bestScore = score;
bestMove[0] = i;
bestMove[1] = j;
}
}
}
}
return bestMove;
}
// 极大极小搜索算法
private int minimax(int row, int col, int depth, int alpha, int beta, int player) {
move(row, col);
if (isWin(player)) {
int score = (COMPUTER == player ? 1 : -1) * (100 - depth);
board[row][col] = 0;
return score;
}
if (isDraw()) {
board[row][col] = 0;
return 0;
}
if (player == COMPUTER) {
int bestScore = Integer.MIN_VALUE;
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE; j++) {
if (board[i][j] == 0) {
int score = minimax(i, j, depth + 1, alpha, beta, PLAYER);
bestScore = Math.max(bestScore, score);
alpha = Math.max(alpha, bestScore);
if (beta <= alpha) {
break;
}
}
}
}
board[row][col] = 0;
return bestScore;
} else {
int bestScore = Integer.MAX_VALUE;
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE; j++) {
if (board[i][j] == 0) {
int score = minimax(i, j, depth + 1, alpha, beta, COMPUTER);
bestScore = Math.min(bestScore, score);
beta = Math.min(beta, bestScore);
if (beta <= alpha) {
break;
}
}
}
}
board[row][col] = 0;
return bestScore;
}
}
// 判断是否胜利
private boolean isWin(int player) {
// 判断行
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j <= BOARD_SIZE - 5; j++) {
if (board[i][j] == player && board[i][j+1] == player && board[i][j+2] == player && board[i][j+3] == player && board[i][j+4] == player) {
return true;
}
}
}
// 判断列
for (int i = 0; i <= BOARD_SIZE - 5; i++) {
for (int j = 0; j < BOARD_SIZE; j++) {
if (board[i][j] == player && board[i+1][j] == player && board[i+2][j] == player && board[i+3][j] == player && board[i+4][j] == player) {
return true;
}
}
}
// 判断正对角线
for (int i = 0; i <= BOARD_SIZE - 5; i++) {
for (int j = 0; j <= BOARD_SIZE - 5; j++) {
if (board[i][j] == player && 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;
}
}
}
// 判断反对角线
for (int i = 0; i <= BOARD_SIZE - 5; i++) {
for (int j = 4; j < BOARD_SIZE; j++) {
if (board[i][j] == player && 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 static void main(String[] args) {
Gobang game = new Gobang();
game.start();
}
}
```
在这个例子中,电脑采用了极大极小搜索算法,可以在有限时间内搜索到最优解。但是,这个算法只适用于较小的棋盘,对于更大的棋盘可能会出现性能问题。
阅读全文