用C++编写一个简单的六子棋ai代码
时间: 2024-05-05 22:20:47 浏览: 99
好的,以下是一个简单的六子棋 AI 代码,使用 C 语言编写:
```
#include <stdio.h>
#include <stdlib.h>
#define ROWS 6
#define COLS 6
#define DEPTH 3
#define PLAYER 1
#define COMPUTER 2
int board[ROWS][COLS];
typedef struct {
int row;
int col;
int score;
} Move;
Move getBestMove();
void printBoard();
int evaluate(int player);
int isGameOver();
int minimax(int depth, int player, int alpha, int beta);
int main() {
int turn = PLAYER;
int row, col;
memset(board, 0, sizeof(board));
while (!isGameOver()) {
printBoard();
if (turn == PLAYER) {
printf("Your turn: ");
scanf("%d %d", &row, &col);
if (board[row][col] != 0) {
printf("Invalid move! Try again.\n");
continue;
}
board[row][col] = PLAYER;
turn = COMPUTER;
} else {
Move bestMove = getBestMove();
board[bestMove.row][bestMove.col] = COMPUTER;
turn = PLAYER;
}
}
printBoard();
if (evaluate(PLAYER) > evaluate(COMPUTER)) {
printf("You win!\n");
} else {
printf("Computer wins!\n");
}
return 0;
}
Move getBestMove() {
Move bestMove;
bestMove.score = -1000;
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
if (board[i][j] == 0) {
board[i][j] = COMPUTER;
int score = minimax(DEPTH-1, PLAYER, -1000, 1000);
board[i][j] = 0;
if (score > bestMove.score) {
bestMove.row = i;
bestMove.col = j;
bestMove.score = score;
}
}
}
}
return bestMove;
}
void printBoard() {
printf("\n");
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
if (board[i][j] == PLAYER) {
printf("X ");
} else if (board[i][j] == COMPUTER) {
printf("O ");
} else {
printf(". ");
}
}
printf("\n");
}
}
int evaluate(int player) {
int score = 0;
// Check rows
for (int i = 0; i < ROWS; i++) {
int count = 0;
for (int j = 0; j < COLS; j++) {
if (board[i][j] == player) {
count++;
} else {
count = 0;
}
if (count == 6) {
return 1000;
}
}
}
// Check columns
for (int j = 0; j < COLS; j++) {
int count = 0;
for (int i = 0; i < ROWS; i++) {
if (board[i][j] == player) {
count++;
} else {
count = 0;
}
if (count == 6) {
return 1000;
}
}
}
// Check diagonals
for (int i = 0; i < ROWS-5; i++) {
for (int j = 0; j < COLS-5; j++) {
int count1 = 0;
int count2 = 0;
for (int k = 0; k < 6; k++) {
if (board[i+k][j+k] == player) {
count1++;
} else {
count1 = 0;
}
if (board[i+k][j+5-k] == player) {
count2++;
} else {
count2 = 0;
}
if (count1 == 6 || count2 == 6) {
return 1000;
}
}
}
}
return score;
}
int isGameOver() {
if (evaluate(PLAYER) == 1000 || evaluate(COMPUTER) == 1000) {
return 1;
}
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
if (board[i][j] == 0) {
return 0;
}
}
}
return 1;
}
int minimax(int depth, int player, int alpha, int beta) {
if (depth == 0 || isGameOver()) {
return evaluate(COMPUTER) - evaluate(PLAYER);
}
if (player == COMPUTER) {
int bestScore = -1000;
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
if (board[i][j] == 0) {
board[i][j] = COMPUTER;
int score = minimax(depth-1, PLAYER, alpha, beta);
board[i][j] = 0;
if (score > bestScore) {
bestScore = score;
}
if (bestScore > alpha) {
alpha = bestScore;
}
if (beta <= alpha) {
break;
}
}
}
}
return bestScore;
} else {
int bestScore = 1000;
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
if (board[i][j] == 0) {
board[i][j] = PLAYER;
int score = minimax(depth-1, COMPUTER, alpha, beta);
board[i][j] = 0;
if (score < bestScore) {
bestScore = score;
}
if (bestScore < beta) {
beta = bestScore;
}
if (beta <= alpha) {
break;
}
}
}
}
return bestScore;
}
}
```
以上是一个简单的六子棋 AI 代码,使用了 MiniMax 算法和 Alpha-Beta 剪枝来优化搜索深度,可以让 AI 在有限时间内找到最优解。
阅读全文