Answer to Question #240014 in C for Pavan

Question #240014
Tic tac toe game
1
Expert's answer
2021-09-21T02:46:18-0400
#include <stdio.h>
#include <conio.h>


char board[10] = { 'o', '1', '2', '3', '4', '5', '6', '7', '8', '9' };


int getGameStatus();
void displayBoard();


int main()
{
    int player = 1, i, choice;
    char symbol;
    do
    {
        displayBoard();
        player = (player % 2) ? 1 : 2;


        printf("Player %d, enter a number:  ", player);
        scanf("%d", &choice);


        symbol = (player == 1) ? 'X' : 'O';


        if (choice == 1 && board[1] == '1')
            board[1] = symbol;
            
        else if (choice == 2 && board[2] == '2')
            board[2] = symbol;
            
        else if (choice == 3 && board[3] == '3')
            board[3] = symbol;
            
        else if (choice == 4 && board[4] == '4')
            board[4] = symbol;
            
        else if (choice == 5 && board[5] == '5')
            board[5] = symbol;
            
        else if (choice == 6 && board[6] == '6')
            board[6] = symbol;
            
        else if (choice == 7 && board[7] == '7')
            board[7] = symbol;
            
        else if (choice == 8 && board[8] == '8')
            board[8] = symbol;
            
        else if (choice == 9 && board[9] == '9')
            board[9] = symbol;
            
        else
        {
            printf("Invalid move ");


            player--;
            getch();
        }
        i = getGameStatus();


        player++;
    }while (i ==  - 1);
    
    displayBoard();
    
    if (i == 1)
        printf("Player %d win ", --player);
    else
        printf("Game draw");


    getch();


    return 0;
}
int getGameStatus()
{
    if (board[1] == board[2] && board[2] == board[3])
        return 1;
        
    else if (board[4] == board[5] && board[5] == board[6])
        return 1;
        
    else if (board[7] == board[8] && board[8] == board[9])
        return 1;
        
    else if (board[1] == board[4] && board[4] == board[7])
        return 1;
        
    else if (board[2] == board[5] && board[5] == board[8])
        return 1;
        
    else if (board[3] == board[6] && board[6] == board[9])
        return 1;
        
    else if (board[1] == board[5] && board[5] == board[9])
        return 1;
        
    else if (board[3] == board[5] && board[5] == board[7])
        return 1;
        
    else if (board[1] != '1' && board[2] != '2' && board[3] != '3' &&
        board[4] != '4' && board[5] != '5' && board[6] != '6' && board[7] 
        != '7' && board[8] != '8' && board[9] != '9')


        return 0;
    else
        return  - 1;
}
void displayBoard()
{
   
    printf("Player 1 (X)  -  Player 2 (O)\n\n\n");
    printf("     |     |     \n");
    printf("  %c  |  %c  |  %c \n", board[1], board[2], board[3]);


    printf("_____|_____|_____\n");
    printf("     |     |     \n");


    printf("  %c  |  %c  |  %c \n", board[4], board[5], board[6]);


    printf("_____|_____|_____\n");
    printf("     |     |     \n");


    printf("  %c  |  %c  |  %c \n", board[7], board[8], board[9]);


    printf("     |     |     \n\n");
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS