C_C++

(C언어) 컴퓨터와 대결하는 틱택토 tic-tac-toe 게임 소스

고니자니 2023. 5. 27. 08:03
반응형

다음 코드는 C언어로 만든  컴퓨터와 대결하는 틱택토 게임입니다.

컴퓨터는 computer_move() 함수를 이용해서 무작위로 위치를 선택합니다.

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

char board[3][3]; // Tic-Tac-Toe board

// Function to initialize the board
void initialize_board() {
    int i, j;
    for (i = 0; i < 3; i++) {
        for (j = 0; j < 3; j++) {
            board[i][j] = ' ';
        }
    }
}

// Function to print the board
void print_board() {
    int i, j;
    printf("\n");
    for (i = 0; i < 3; i++) {
        for (j = 0; j < 3; j++) {
            printf(" %c ", board[i][j]);
            if (j != 2) {
                printf("|");
            }
        }
        printf("\n");
        if (i != 2) {
            printf("---+---+---\n");
        }
    }
    printf("\n");
}

// Function to check if the move is valid
int is_valid_move(int row, int col) {
    if (row < 0 || row >= 3 || col < 0 || col >= 3) {
        return 0;
    }
    if (board[row][col] != ' ') {
        return 0;
    }
    return 1;
}

// Function to check if there is a win
int check_win(char symbol) {
    int i;

    // Check rows
    for (i = 0; i < 3; i++) {
        if (board[i][0] == symbol && board[i][1] == symbol && board[i][2] == symbol) {
            return 1;
        }
    }

    // Check columns
    for (i = 0; i < 3; i++) {
        if (board[0][i] == symbol && board[1][i] == symbol && board[2][i] == symbol) {
            return 1;
        }
    }

    // Check diagonals
    if (board[0][0] == symbol && board[1][1] == symbol && board[2][2] == symbol) {
        return 1;
    }
    if (board[0][2] == symbol && board[1][1] == symbol && board[2][0] == symbol) {
        return 1;
    }

    return 0;
}

// Function to check if the game is a draw
int is_draw() {
    int i, j;
    for (i = 0; i < 3; i++) {
        for (j = 0; j < 3; j++) {
            if (board[i][j] == ' ') {
                return 0;
            }
        }
    }
    return 1;
}

// Function for computer's move
void computer_move() {
    int row, col;

    // Randomly select a valid move
    do {
        row = rand() % 3;
        col = rand() % 3;
    } while (!is_valid_move(row, col));

    board[row][col] = 'O';
}

// Function to play the game against the computer
void play_game() {
    int row, col;
    int game_finished = 0;

    initialize_board();

    printf("Tic-Tac-Toe Game against Computer\n");

    while (!game_finished) {
        print_board();

        // Player's move
        printf("Player, enter the row and column (e.g., 1 1): ");
        scanf("%d %d", &row, &col);

        if (!is_valid_move(row - 1, col - 1)) {
            printf("Invalid move! Try again.\n");
            continue;
        }

        board[row - 1][col - 1] = 'X';

        if (check_win('X')) {
            print_board();
            printf("Player wins!\n");
            game_finished = 1;
        }
        else if (is_draw()) {
            print_board();
            printf("It's a draw!\n");
            game_finished = 1;
        }
        else {
            // Computer's move
            computer_move();

            if (check_win('O')) {
                print_board();
                printf("Computer wins!\n");
                game_finished = 1;
            }
            else if (is_draw()) {
                print_board();
                printf("It's a draw!\n");
                game_finished = 1;
            }
        }
    }
}

int main() {
    srand(time(0)); // Seed the random number generator
    play_game();
    return 0;
}

틱택토 게임

 

tic-tac-toe.exe
0.01MB
tic-tac-toe.zip
0.01MB

728x90
반응형