1. 首页 > 游戏攻略

五子棋ai算法代码c 五子棋游戏算法代码

作者:admin 更新时间:2026-01-05
摘要:下面是一个简单的五子棋AI算法的C语言实现,这个AI使用的是极简单的启发式搜索,即只考虑当前棋局的最优走法,这个AI没有使用复杂的搜索算法,如Minimax或Alpha-Beta剪枝,因此它的水平有限。 #include <stdio.h> #inclu...,五子棋ai算法代码c 五子棋游戏算法代码

 

下面一个简单的五子棋AI算法的C语言实现,这个AI运用的是极简单的启发式搜索,即只思考当前棋局的最优走法,这个AI没有运用复杂的搜索算法,如Minimax或Alpha-Beta剪枝,因此它的水平有限。

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define BOARD_SIZE 15
int board[BOARD_SIZE][BOARD_SIZE];
int turn = 1; // 1 for player, -1 for AI
// Function to initialize the board
void initializeBoard() {
    for (int i = 0; i < BOARD_SIZE; i++) {
        for (int j = 0; j < BOARD_SIZE; j++) {
            board[i][j] = 0;
        }
    }
}
// Function to print the board
void printBoard() {
    for (int i = 0; i < BOARD_SIZE; i++) {
        for (int j = 0; j < BOARD_SIZE; j++) {
            if (board[i][j] == 1) printf("X ");
            else if (board[i][j] == -1) printf("O ");
            else printf(". ");
        }
        printf("\n");
    }
}
// Function to check if a position is valid
int isValid(int x, int y) {
    return (x >= 0 && x < BOARD_SIZE && y >= 0 && y < BOARD_SIZE && board[x][y] == 0);
}
// Function to make a move
int makeMove(int x, int y) {
    if (isValid(x, y)) {
        board[x][y] = turn;
        return 1;
    }
    return 0;
}
// Function to undo a move
void undoMove(int x, int y) {
    board[x][y] = 0;
}
// Function to check for a win
int checkWin() {
    // Check horizontally, vertically, diagonally
    for (int i = 0; i < BOARD_SIZE; i++) {
        for (int j = 0; j < BOARD_SIZE; j++) {
            if (board[i][j] != 0) {
                // Check horizontally
                int count = 1;
                for (int k = 1; k < 5 && j + k < BOARD_SIZE; k++) {
                    if (board[i][j + k] == board[i][j]) count++;
                    else break;
                }
                if (count == 5) return board[i][j];
                // Check vertically
                count = 1;
                for (int k = 1; k < 5 && i + k < BOARD_SIZE; k++) {
                    if (board[i + k][j] == board[i][j]) count++;
                    else break;
                }
                if (count == 5) return board[i][j];
                // Check diagonally (top-left to bottom-right)
                count = 1;
                for (int k = 1; k < 5 && i + k < BOARD_SIZE && j + k < BOARD_SIZE; k++) {
                    if (board[i + k][j + k] == board[i][j]) count++;
                    else break;
                }
                if (count == 5) return board[i][j];
                // Check diagonally (top-right to bottom-left)
                count = 1;
                for (int k = 1; k < 5 && i + k < BOARD_SIZE && j - k >= 0; k++) {
                    if (board[i + k][j - k] == board[i][j]) count++;
                    else break;
                }
                if (count == 5) return board[i][j];
            }
        }
    }
    return 0;
}
// Simple AI move
void aiMove() {
    int bestScore = -2;
    int bestX = -1, bestY = -1;
    // Try all possible moves
    for (int i = 0; i < BOARD_SIZE; i++) {
        for (int j = 0; j < BOARD_SIZE; j++) {
            if (isValid(i, j)) {
                // Make the move
                board[i][j] = -1;
                // Calculate the score
                int score = evaluate();
                // Undo the move
                board[i][j] = 0;
                // Check if this is the best move
                if (score > bestScore) {
                    bestScore = score;
                    bestX = i;
                    bestY = j;
                }
            }
        }
    }
    // Make the best move
    board[bestX][bestY] = -1;
    printf("AI moves to (%d, %d)\n", bestX, bestY);
}
// Evaluate the board
int evaluate() {
    int score = 0;
    // Check for four in a row
    for (int i = 0; i < BOARD_SIZE; i++) {
        for (int j = 0; j < BOARD_SIZE; j++) {
            if (board[i][j] == -1) {
                // Check horizontally
                int count = 1;
                for (int k = 1; k < 5 && j + k < BOARD_SIZE; k++) {
                    if (board[i][j + k] == -1) count++;
                    else break;
                }
                if (count == 4) score += 1000;
                // Check vertically
                count = 1;
                for (int k = 1; k < 5 && i + k < BOARD_SIZE; k++) {
                    if (board[i + k][j] == -1) count++;
                    else break;
                }
                if (count == 4) score += 1000;
                // Check diagonally (top-left to bottom-right)
                count = 1;
                for (int k = 1; k < 5 && i + k < BOARD_SIZE && j + k < BOARD_SIZE; k++) {
                    if (board[i + k][j + k] == -1) count++;
                    else break;
                }
                if (count == 4) score += 1000;
                // Check diagonally (top-right to bottom-left)
                count = 1;
                for (int k = 1; k < 5 && i + k < BOARD_SIZE && j - k >= 0; k++) {
                    if (board[i + k][j - k] == -1) count++;
                    else break;
                }
                if (count == 4) score += 1000;
            }
        }
    }
    return score;
}
int main() {
    initializeBoard();
    printBoard();
    while (1) {
        if (turn == 1) {
            // Player's turn
            int x, y;
            printf("Player's move (row col): ");
            scanf("%d %d", &x, &y);
            if (makeMove(x, y)) {
                printBoard();
                if (checkWin()) {
                    printf("Player wins!\n");
                    break;
                }
                turn = -1;
            } else {
                printf("Invalid move. Try again.\n");
            }
        } else {
            // AI's turn
            aiMove();
            printBoard();
            if (checkWin()) {
                printf("AI wins!\n");
                break;
            }
            turn = 1;
        }
    }
    return 0;
}

这段代码实现了壹个简单的五子棋游戏,其中AI运用了壹个特别基础的启发式搜索算法来决定下一步棋,AI的搜索深度特别浅,因此它的水平不高,这个AI的evaluate函数只思考了连四的情况,而且给予很高的分值,在实际应用中,你也许需要更复杂的评估函数和搜索算法来进步AI的水平。