Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Скачиваний:
0
Добавлен:
02.12.2024
Размер:
4.6 Кб
Скачать
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
int GameResult(char field[3][3]);
int ComputerMove(char field[3][3]);
int PrintField(char field[3][3])
{
    if (field == NULL) return 1;
    for (size_t i = 0; i < 3; i++)
    {
        for (size_t j = 0; j < 3; j++)
        {
            if (printf("%c ", field[i][j]) < 0)
            {
                printf("Error\n");
                return 2;
            }
        }
        if (printf("\n") < 0)
        {
            printf("Error\n");
            return 2;
        }
    }
    return 5;
}
int main()
{
    if (printf("Welcome to the Tic Tac toe game!\n")<0)
    {
        printf("Error!\n");
        return 2;
    }
     if (printf("Your goal is to make a line of three ")<0)
    {
        printf("Error!\n");
        return 2;
    }
    if (printf("crosses vertically or horizontally or diagonally!\n")<0)
    {
        printf("Error!\n");
        return 2;
    }
    if (printf("You go first! Enter the number from 0 to 8 to make move!\n")<0)
    {
        printf("Error!\n");
        return 2;
    }
    if (printf("Good luck!\n")<0)
    {
        printf("Error!\n");
        return 2;
    }
    srand(time(NULL));
    char field[3][3]= {{'_','_','_'},{'_','_','_'},{'_','_','_'}};
    if (PrintField(field) != 5)
    {
        if(printf("Error while printing board\n")<0){
            printf("Error\n");
            return 2;
        }
        return 7;
    }
    if (printf("________\n") < 0)
    {
        printf("Error\n");
        return 2;
    }
    while (1)
    {
        int posIndex;
        if (scanf("%d", &posIndex) != 1)
        {
            if(printf("Incorrect index\n")<0){
                printf("Error\n");
                return 2;
            }
            continue;
        }
        if (posIndex<0 || posIndex>8)
        {
            if(printf("Incorrect index\n")<0){
                printf("Error\n");
                return 2;
            }
            continue;
        }
        if (field[posIndex / 3][posIndex % 3] != '_')
        {
            if(printf("Incorrect index\n")<0){
                printf("Error\n");
                return 2;
            }
            continue;
        }
        field[posIndex / 3][posIndex % 3] = 'X';
        if (PrintField(field) != 5)
        {
            if(printf("Error while printing board\n")<0){
                printf("Error\n");
                return 2;
            }
            return 7;
        }
        if (printf("________\n") < 0)
        {
            printf("Error\n");
            return 2;
        }

        int result = GameResult(field);
        switch(result)
        {
            case 10:
                if(printf(" You win\n")<0){
                    printf("Error\n");
                    return 2;
                }
                return 0;
                break;
            case -10:
                if(printf("Computer win\n")<0){
                    printf("Error\n");
                    return 2;
                }
                return 0;
                break;
            case 0:
                if(printf("Draw\n")<0){
                    printf("Error\n");
                    return 2;
                }
                return 0;
                break;
        }

        if (ComputerMove(field) != 3)
        {
            if(printf("Error while making move\n")<0){
                printf("Error\n");
                return 2;
            }
            return 6;
        }
        result = GameResult(field);
        if (PrintField(field) != 5)
        {
            printf("Error while printing board\n");
            return -1;
        }
        if (printf("________\n") < 0)
        {
            printf("Error while printing board\n");
            return -1;
        }
        switch(result)
        {
            case 10:
                if(printf(" You win\n")<0){
                    printf("Error\n");
                    return 2;
                }
                return 0;
                break;
            case -10:
                if(printf("Computer win\n")<0){
                    printf("Error\n");
                    return 2;
                }
                return 0;
                break;
            case 0:
                if(printf("Draw\n")<0){
                    printf("Error\n");
                    return 2;
                }
                return 0;
                break;
        }
    }
}

Соседние файлы в папке Исходный код