Untitled

X O Game By Hendy
mail@pastecode.io avatar
unknown
c_cpp
2 months ago
3.7 kB
2
Indexable
Never
    #include <stdio.h>
    #include <stdlib.h>
    void printBoard(char board[3][3]);
    int getPosition(int *x, int *y, int position, char board[3][3]);
    char checkForWinner(char board[3][3]);
    int startNewGame();
    #define X_Winner 55
    #define O_Winner 66
    #define Draw 22
    int main()
    {
        int x = startNewGame();
        while (x == Draw)
        {
            printf("Game is draw, starting new game\n");
            x = startNewGame();
        }
        if (x == X_Winner)
        {
            printf("Player 1 wins\n");
        }
        else if (x == O_Winner)
        {
            printf("Player 2 wins\n");
        }
        return 0;
    }
    char checkForWinner(char board[3][3])
    {
        int i;
        for (i = 0; i < 3; i++)
        {
            if (board[i][0] == board[i][1] && board[i][1] == board[i][2])
                return board[i][0];
            if (board[0][i] == board[1][i] && board[1][i] == board[2][i])
                return board[0][i];
        }
        if (board[0][0] == board[1][1] && board[1][1] == board[2][2])
            return board[0][0];
        if (board[0][2] == board[1][1] && board[1][1] == board[2][0])
            return board[0][2];

        return 255;
    }
    void printBoard(char board[3][3])
    {
        int i, j;
        printf("The current board is:\n");
        for (i = 0; i < 3; i++)
        {
            for (j = 0; j < 3; j++)
                printf("%c ", board[i][j]);
            printf("\n");
        }
    }
    int getPosition(int *x, int *y, int position, char board[3][3])
    {
        for (int i = 0; i < 3; i++)
        {
            for (int j = 0; j < 3; j++)
                if (board[i][j] == position + '0')
                {
                    *x = i;
                    *y = j;
                    return 1;
                }
        }
        *x = -1;
        *y = -1;
        return 0;
    }
    int startNewGame()
    {
        int i, j, counter;
        char board[3][3];
        for (i = 0; i < 3; i++)
        {
            for (j = 0; j < 3; j++)
                board[i][j] = i * 3 + j + 1 + '0';
        }
        printf("Welcome to tic-tac-toe\n");
        printf("Player 1: X\nPlayer 2: O\n");
        for (counter = 0; counter < 9; counter++)
        {
            if (counter % 2 == 0)
            {

                printf("Player 1, enter the position where you want to place X: ");
                scanf("%d", &i);
                if (getPosition(&i, &j, i, board))
                {
                    board[i][j] = 'X';
                }
                else
                {
                    printf("Invalid move, try again\n");
                    counter--;
                }
            }
            else
            {
                printf("Player 2, enter the position where you want to place O: ");
                scanf("%d", &i);
                if (getPosition(&i, &j, i, board))
                {
                    board[i][j] = 'O';
                }
                else
                {
                    printf("Invalid move, try again\n");
                    counter--;
                }
            }
            printBoard(board);
            printf("----------------\n");
            if (checkForWinner(board) != 255)
            {
                if (checkForWinner(board) == 'X')
                {
                    return X_Winner;
                }
                else if (checkForWinner(board) == 'O')
                {

                    return O_Winner;
                }
            }
        }
        return Draw;
    }
Leave a Comment