Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

код

.txt
Скачиваний:
1
Добавлен:
02.12.2024
Размер:
2.88 Кб
Скачать
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
int gameResult(char board[3][3]);
int makeMove(char board[3][3]);
int printBoard(char board[3][3])
{
    if (board == NULL){
    return 1;
    }
    for (size_t i = 0; i < 3; i++){
        for (size_t j = 0; j < 3; j++){
            if (printf("%c ", board[i][j]) < 0){
            return 2;
            }
        }
        if (printf("\n") < 0){
            return 2;
        }
    }
    return 100;
}
int main()
{
    srand(time(NULL));
    char board[3][3]={
    {'_','_','_'},
    {'_','_','_'},
    {'_','_','_'}
    };
    if (printBoard(board) != 100){
        if(printf("Error while printing board\n")<0) return 2;
        return -1;
    }
    if (printf("________\n") < 0){
        if(printf("Error while printing board\n")<0) return 2;
        return -1;
    }
    while (1){
        int posIndex;
        if (scanf("%d", &posIndex) != 1){
            if(printf("Incorrect index\n")<0) return 2;
            continue;
        }
        if (posIndex<0 || posIndex>8){
            if(printf("Incorrect index\n")<0) return 2;
            continue;
        }
        if (board[posIndex / 3][posIndex % 3] != '_'){
            if(printf("Incorrect index\n")<0) return 2;
            continue;
        }
        board[posIndex / 3][posIndex % 3] = 'X';
        if (printBoard(board) != 100){
            if(printf("Error while printing board\n")<0) return 2;
            return -1;
        }
        if (printf("________\n") < 0) {
            printf("Error while printing board\n");
            return -1;
        }

        int result = gameResult(board);
        switch(result)
        {
            case 10:
                printf(" You win\n");
                return 1;
                break;
            case -10:
                printf("Computer win\n");
                return 1;
                break;
            case 0:
                printf("Draw\n");
                return 1;
                break;
        }

        if (makeMove(board) != 3){
            printf("Error while making move\n");
            return -1;
        }
        result = gameResult(board);
        if (printBoard(board) != 100){
            printf("Error while printing board\n");
            return -1;
        }
        if (printf("________\n") < 0) {
            printf("Error while printing board\n");
            return -1;
        }
        switch(result)
        {
            case 10:
                printf(" You win\n");
                return 1;
                break;
            case -10:
                printf("Computer win\n");
                return 1;
                break;
            case 0:
                printf("Draw\n");
                return 1;
                break;
        }
    }
}