编辑代码

#include <stdio.h>
#include <stdlib.h>

#define PLAYER 'X'
#define COMPUTER 'O'
#define EMPTY ' '

char board[3][3];

void initializeBoard() {
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            board[i][j] = EMPTY;
        }
    }
}

void printBoard() {
    printf("\n");
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            printf(" %c ", board[i][j]);
            if (j < 2) printf("|");
        }
        printf("\n");
        if (i < 2) printf("---|---|---\n");
    }
    printf("\n");
}

int isMovesLeft() {
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            if (board[i][j] == EMPTY) {
                return 1;
            }
        }
    }
    return 0;
}

int evaluate() {
    // Checking for Rows for X or O victory
    for (int row = 0; row < 3; row++) {
        if (board[row][0] == board[row][1] && board[row][1] == board[row][2]) {
            if (board[row][0] == COMPUTER)
                return +10;
            else if (board[row][0] == PLAYER)
                return -10;
        }
    }

    // Checking for Columns for X or O victory
    for (int col = 0; col < 3; col++) {
        if (board[0][col] == board[1][col] && board[1][col] == board[2][col]) {
            if (board[0][col] == COMPUTER)
                return +10;
            else if (board[0][col] == PLAYER)
                return -10;
        }
    }

    // Checking for Diagonals for X or O victory
    if (board[0][0] == board[1][1] && board[1][1] == board[2][2]) {
        if (board[0][0] == COMPUTER)
            return +10;
        else if (board[0][0] == PLAYER)
            return -10;
    }

    if (board[0][2] == board[1][1] && board[1][1] == board[2][0]) {
        if (board[0][2] == COMPUTER)
            return +10;
        else if (board[0][2] == PLAYER)
            return -10;
    }

    // Else if none of them have won then return 0
    return 0;
}

int minimax(int depth, int isMax) {
    int score = evaluate();

    if (score == 10)
        return score - depth;
    if (score == -10)
        return score + depth;
    if (isMovesLeft() == 0)
        return 0;

    if (isMax) {
        int best = -1000;

        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                if (board[i][j] == EMPTY) {
                    board[i][j] = COMPUTER;
                    best = best > minimax(depth + 1, !isMax) ? best : minimax(depth + 1, !isMax);
                    board[i][j] = EMPTY;
                }
            }
        }
        return best;
    } else {
        int best = 1000;

        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                if (board[i][j] == EMPTY) {
                    board[i][j] = PLAYER;
                    best = best < minimax(depth + 1, !isMax) ? best : minimax(depth + 1, !isMax);
                    board[i][j] = EMPTY;
                }
            }
        }
        return best;
    }
}

void findBestMove(int *bestMoveRow, int *bestMoveCol) {
    int bestVal = -1000;
    *bestMoveRow = -1;
    *bestMoveCol = -1;

    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            if (board[i][j] == EMPTY) {
                board[i][j] = COMPUTER;
                int moveVal = minimax(0, 0);
                board[i][j] = EMPTY;
                if (moveVal > bestVal) {
                    *bestMoveRow = i;
                    *bestMoveCol = j;
                    bestVal = moveVal;
                }
            }
        }
    }
}

int main() {
    int x, y;
    initializeBoard();
    printBoard();

    while (1) {
        printf("Enter your move (row and column): ");
        scanf("%d %d", &x, &y);
        if (x < 0 || x > 2 || y < 0 || y > 2 || board[x][y] != EMPTY) {
            printf("Invalid move! Try again.\n");
            continue;
        }
        board[x][y] = PLAYER;
        printBoard();

        if (evaluate() == -10) {
            printf("You win!\n");
            break;
        }

        if (!isMovesLeft()) {
            printf("It's a tie!\n");
            break;
        }

        int bestMoveRow, bestMoveCol;
        findBestMove(&bestMoveRow, &bestMoveCol);
        board[bestMoveRow][bestMoveCol] = COMPUTER;
        printBoard();

        if (evaluate() == 10) {
            printf("Computer wins!\n");
            break;
        }

        if (!isMovesLeft()) {
            printf("It's a tie!\n");
            break;
        }
    }

    return 0;
}