/*
############ Project Name : TIC TAC TOE

############ CODED BY

############  > Faysal Ishtiaq Rabby
############  ~ Student ID : 120909
############  Khulna University, Bangladesh.

*/

#include "stdafx.h"
#include<stdlib.h>

int main()

    /* Declaring Variables */
    char gamestart;

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

    int gameloop, option_position,input,flag=0,player_number,rand_int,i;

    /* Variable Declaration Finished*/

    printf("Press \"ENTER\" to start.");

    scanf("%c",&gamestart);

    while(gamestart == '\n')
    {
        /* Re-defining Variables..... */
        for(i=0; i<9; i++)
            pos_input[i]= (char) i+49;

        flag = 0;

        printf("\n\n\n\n Choose number of players \n\n maximum player : 2 \n\n minimum player : 1 \n\n to exit, input 0 \n\n Input: ");
        scanf("%d",&player_number);

        if(player_number == 0)
            exit(1);

        else if(player_number == 2)
        {       
            /* prints game name */

            printf("***************TIC TAC TOE***************\n\n\n\n");

            /*prints game status */

            printf("\n\n\n\n*************GAME STATUS***************\n\n");

            for(option_position=0; option_position<9; option_position++)
            {
                /* prints new line after printing 3 position in a row */

                if(option_position==0||option_position==3|| option_position==6)   
                    printf("\n\n");                                                   

                /* prints position value */

                printf("   %c",pos_input[option_position]);                     
            }

            for(gameloop=0; gameloop<9; gameloop++)
            {
                /* Takes Input from Player 1 for the 1st, 3rd, 5th, 7th and 9th time, the loop is running*/

                if(gameloop==0||gameloop==2||gameloop==4||gameloop==6||gameloop==8)
                {
                    printf("\n\n\nPlayer 1 ( enter position value. your input is shown as \'O\' ) :");

                    /*Taking Input from Player 1*/

                    scanf("%d",&input); 

                    /* Assigning new value to desired position */

                    pos_input[input-1]='O';  


                    printf("\n\n\n\n*************GAME STATUS***************\n\n");

                    for(option_position=0; option_position<9; option_position++)
                    {
                        /* prints new line after printing 3 position in a row */

                        if(option_position==0||option_position==3|| option_position==6)   
                            printf("\n\n");                                                   

                        /* prints position value */

                        printf("   %c",pos_input[option_position]);                     
                    }

                    /*******************************************************************************************
                    ################################## DECIDING RESULT #########################################
                    ********************************************************************************************
                    ######################## Checks for three same input in a line #############################
                    ################### If there, Player 1 is the WINNER !!!!!!!!!!!!! #########################
                    *******************************************************************************************/

                    if(pos_input[0]=='O'&&pos_input[1]=='O'&&pos_input[2]=='O')
                    {
                        printf("\n\nPlayer 1 wins!!!!!");
                        flag++;
                        break;
                    }

                    else if(pos_input[0]=='O'&&pos_input[3]=='O'&&pos_input[6]=='O')
                    {
                        printf("\n\nPlayer 1 wins!!!!!");
                        flag++;
                        break;
                    }

                    else if(pos_input[0]=='O'&&pos_input[4]=='O'&&pos_input[8]=='O')
                    {
                        printf("\n\nPlayer 1 wins!!!!!");
                        flag++;
                        break;
                    }

                    else if(pos_input[1]=='O'&&pos_input[4]=='O'&&pos_input[7]=='O')
                    {
                        printf("\n\nPlayer 1 wins!!!!!");
                        flag++;
                        break;
                    }

                    else if(pos_input[2]=='O'&&pos_input[5]=='O'&&pos_input[8]=='O')
                    {
                        printf("\n\nPlayer 1 wins!!!!!");
                        flag++;
                        break;
                    }

                    else if(pos_input[2]=='O'&&pos_input[4]=='O'&&pos_input[6]=='O')
                    {
                        printf("\n\nPlayer 1 wins!!!!!");
                        flag++;
                        break;
                    }

                    else if(pos_input[3]=='O'&&pos_input[4]=='O'&&pos_input[5]=='O')
                    {
                        printf("\n\nPlayer 1 wins!!!!!");
                        flag++;
                        break;
                    }

                    else if(pos_input[6]=='O'&&pos_input[7]=='O'&&pos_input[8]=='O')
                    {
                        printf("\n\nPlayer 1 wins!!!!!");
                        flag++;
                        break;
                    }
                }

                /* Takes Input from Player 2 for the 2nd, 4th, 6th and 8th time, the loop is running*/

                else
                {
                    printf("\n\n\nPlayer 2 ( enter position value. your input is shown as \'X\' ) :");

                    /*Taking Input from Player 1*/

                    scanf("%d",&input);    

                    /* Assigning new value to desired position */

                    pos_input[input-1]='X';  


                    printf("\n\n\n\n*************GAME STATUS***************\n\n");

                    for(option_position=0; option_position<9; option_position++)
                    {
                        /* prints new line after printing 3 position in a row */

                        if(option_position==0||option_position==3|| option_position==6)   
                            printf("\n\n");                                                   

                        /* prints position value */

                        printf("   %c",pos_input[option_position]);                     
                    }

                    /*******************************************************************************************
                    ################################## DECIDING RESULT #########################################
                    ********************************************************************************************
                    ######################## Checks for three same input in a line #############################
                    ################### If there, Player 2 is the WINNER !!!!!!!!!!!!! #########################
                    *******************************************************************************************/

                    if(pos_input[0]=='X'&&pos_input[1]=='X'&&pos_input[2]=='X')
                    {
                        printf("\n\nPlayer 2 wins!!!!!");
                        flag++;
                        break;
                    }

                    else if(pos_input[0]=='X'&&pos_input[3]=='X'&&pos_input[6]=='X')
                    {
                        printf("\n\nPlayer 2 wins!!!!!");
                        flag++;
                        break;
                    }

                    else if(pos_input[0]=='X'&&pos_input[4]=='X'&&pos_input[8]=='X')
                    {
                        printf("\n\nPlayer 2 wins!!!!!");
                        flag++;
                        break;
                    }

                    else if(pos_input[1]=='X'&&pos_input[4]=='X'&&pos_input[7]=='X')
                    {
                        printf("\n\nPlayer 2 wins!!!!!");
                        flag++;
                        break;
                    }

                    else if(pos_input[2]=='X'&&pos_input[5]=='X'&&pos_input[8]=='X')
                    {
                        printf("\n\nPlayer 2 wins!!!!!");
                        flag++;
                        break;
                    }

                    else if(pos_input[2]=='X'&&pos_input[4]=='X'&&pos_input[6]=='X')
                    {
                        printf("\n\nPlayer 2 wins!!!!!");
                        flag++;
                        break;
                    }

                    else if(pos_input[3]=='X'&&pos_input[4]=='X'&&pos_input[5]=='X')
                    {
                        printf("\n\nPlayer 2 wins!!!!!");
                        flag++;
                        break;
                    }

                    else if(pos_input[6]=='X'&&pos_input[7]=='X'&&pos_input[8]=='X')
                    {
                        printf("\n\nPlayer 2 wins!!!!!");
                        flag++;
                        break;
                    }

                }
            }
        }

        else if(player_number == 1)
        {
            gameloop = 0;

            /* prints game name */

            printf("\n\n***************TIC TAC TOE***************\n\n\n\n");

            /* prints game status */
            printf("\n\n\n\n*************GAME STATUS***************\n\n");

            for(option_position=0; option_position<9; option_position++)
            {
                /* prints new line after printing 3 position in a row */

                if(option_position==0||option_position==3|| option_position==6)   
                    printf("\n\n");                                                   

                /* prints position value */

                printf("   %c",pos_input[option_position]);                     
            }

            for(gameloop=0; gameloop<9; gameloop++)
            {
                if(gameloop==0||gameloop==2||gameloop==4||gameloop==6||gameloop==8)             
                {
                    printf("\n\n\nPlayer 1 ( enter position value. your input is shown as \'O\' ) :");

                    /*Taking Input from Player 1*/

                    scanf("%d",&input); 

                    /* Assigning new value to desired position */

                    pos_input[input-1]='O';

                    printf("\n\n\n\n*************GAME STATUS***************\n\n");

                    for(option_position=0; option_position<9; option_position++)
                    {
                        /* prints new line after printing 3 position in a row */

                        if(option_position==0||option_position==3|| option_position==6)   
                            printf("\n\n");                                                   

                        /* prints position value */

                        printf("   %c",pos_input[option_position]);                     
                    }

                    /*******************************************************************************************
                    ################################## DECIDING RESULT #########################################
                    ********************************************************************************************
                    ######################## Checks for three same input in a line #############################
                    ################### If there, Player 1 is the WINNER !!!!!!!!!!!!! #########################
                    *******************************************************************************************/

                    if(pos_input[0]=='O'&&pos_input[1]=='O'&&pos_input[2]=='O')
                    {
                        printf("\n\nPlayer 1 wins!!!!!");
                        flag++;
                        break;
                    }

                    else if(pos_input[0]=='O'&&pos_input[3]=='O'&&pos_input[6]=='O')
                    {
                        printf("\n\nPlayer 1 wins!!!!!");
                        flag++;
                        break;
                    }

                    else if(pos_input[0]=='O'&&pos_input[4]=='O'&&pos_input[8]=='O')
                    {
                        printf("\n\nPlayer 1 wins!!!!!");
                        flag++;
                        break;
                    }

                    else if(pos_input[1]=='O'&&pos_input[4]=='O'&&pos_input[7]=='O')
                    {
                        printf("\n\nPlayer 1 wins!!!!!");
                        flag++;
                        break;
                    }

                    else if(pos_input[2]=='O'&&pos_input[5]=='O'&&pos_input[8]=='O')
                    {
                        printf("\n\nPlayer 1 wins!!!!!");
                        flag++;
                        break;
                    }

                    else if(pos_input[2]=='O'&&pos_input[4]=='O'&&pos_input[6]=='O')
                    {
                        printf("\n\nPlayer 1 wins!!!!!");
                        flag++;
                        break;
                    }

                    else if(pos_input[3]=='O'&&pos_input[4]=='O'&&pos_input[5]=='O')
                    {
                        printf("\n\nPlayer 1 wins!!!!!");
                        flag++;
                        break;
                    }

                    else if(pos_input[6]=='O'&&pos_input[7]=='O'&&pos_input[8]=='O')
                    {
                        printf("\n\nPlayer 1 wins!!!!!");
                        flag++;
                        break;
                    }
                }

                else
                {
                    printf("\n\n\nComputer ( enter position value. your input is shown as \'X\' ) :");

                    /*********************************************************************************
                    **********************************************************************************
                    ########################### ARTIFICIAL INTELLIGENCE ##############################
                    **********************************************************************************
                    **********************************************************************************/


                    if((pos_input[0] =='X' && pos_input[1] =='X') && pos_input[2] != 'O')
                    {
                        pos_input[2]='X';
                    }

                    else if((pos_input[1] =='X' && pos_input[2] =='X') && pos_input[0] != 'O')
                    {
                        pos_input[0]='X';
                    }

                    else if((pos_input[0] =='X' && pos_input[2] =='X') && pos_input[1] != 'O')
                    {
                        pos_input[1]='X';
                    }

                    else if((pos_input[3] =='X' && pos_input[4] =='X') && pos_input[5] != 'O')
                    {
                        pos_input[5]='X';
                    }

                    else if((pos_input[4] =='X' && pos_input[5] =='X') && pos_input[3] != 'O')
                    {
                        pos_input[3]='X';
                    }

                    else if((pos_input[3] =='X' && pos_input[5] =='X') && pos_input[4] != 'O')
                    {
                        pos_input[4]='X';
                    }

                    else if((pos_input[6] =='X' && pos_input[7] =='X') && pos_input[8] != 'O')
                    {
                        pos_input[8]='X';
                    }

                    else if((pos_input[6] =='X' && pos_input[8] =='X') && pos_input[7] != 'O')
                    {
                        pos_input[2]='X';
                    }

                    else if((pos_input[7] =='X' && pos_input[8] =='X') && pos_input[6] != 'O')
                    {
                        pos_input[6]='X';
                    }

                    else if((pos_input[0] =='X' && pos_input[3] =='X') && pos_input[6] != 'O')
                    {
                        pos_input[6]='X';
                    }

                    else if((pos_input[0] =='X' && pos_input[6] =='X') && pos_input[3] != 'O')
                    {
                        pos_input[3]='X';
                    }

                    else if((pos_input[3] =='X' && pos_input[6] =='X') && pos_input[0] != 'O')
                    {
                        pos_input[0]='X';
                    }

                    else if((pos_input[1] =='X' && pos_input[4] =='X') && pos_input[7] != 'O')
                    {
                        pos_input[7]='X';
                    }

                    else if((pos_input[1] =='X' && pos_input[7] =='X') && pos_input[4] != 'O')
                    {
                        pos_input[4]='X';
                    }

                    else if((pos_input[4] =='X' && pos_input[7] =='X') && pos_input[1] != 'O')
                    {
                        pos_input[1]='X';
                    }

                    else if((pos_input[2] =='X' && pos_input[5] =='X') && pos_input[8] != 'O')
                    {
                        pos_input[8]='X';
                    }

                    else if((pos_input[2] =='X' && pos_input[8] =='X') && pos_input[5] != 'O')
                    {
                        pos_input[5]='X';
                    }

                    else if((pos_input[5] =='X' && pos_input[8] =='X') && pos_input[2] != 'O')
                    {
                        pos_input[2]='X';
                    }

                    else if((pos_input[0] =='X' && pos_input[4] =='X') && pos_input[8] != 'O')
                    {
                        pos_input[8]='X';
                    }

                    else if((pos_input[0] =='X' && pos_input[8] =='X') && pos_input[4] != 'O')
                    {
                        pos_input[4]='X';
                    }

                    else if((pos_input[4] =='X' && pos_input[8] =='X') && pos_input[0] != 'O')
                    {
                        pos_input[0]='X';
                    }

                    else if((pos_input[2] =='X' && pos_input[4] =='X') && pos_input[6] != 'O')
                    {
                        pos_input[6]='X';
                    }

                    else if((pos_input[2] =='X' && pos_input[6] =='X') && pos_input[4] != 'O')
                    {
                        pos_input[4]='X';
                    }

                    else if((pos_input[4] =='X' && pos_input[6] =='X') && pos_input[2] != 'O')
                    {
                        pos_input[2]='X';
                    }

                    else if((pos_input[0]== 'O' && pos_input[1] == 'O') && pos_input[2] != 'X')
                    {
                        pos_input[2] = 'X';
                    }

                    else if((pos_input[1] =='O' && pos_input[2] =='O') && pos_input[0] != 'X')
                    {
                        pos_input[0] ='X';
                    }

                    else if((pos_input[0] =='O' && pos_input[2] =='O') && pos_input[1] != 'X')
                    {
                        pos_input[1]= 'X';
                    }

                    else if((pos_input[0] =='O' && pos_input[4] =='O') && pos_input[8] != 'X')
                    {
                        pos_input[8]='X';
                    }

                    else if((pos_input[0] =='O' && pos_input[8] =='O') && pos_input[4] != 'X')
                    {
                        pos_input[4]='X';
                    }

                    else if((pos_input[4] =='O' && pos_input[8] =='O') && pos_input[0] != 'X')
                    {
                        pos_input[0]='X';
                    }

                    else if((pos_input[0] =='O' && pos_input[3] =='O') && pos_input[6] != 'X')
                    {
                        pos_input[6]='X';
                    }

                    else if((pos_input[0] =='O' && pos_input[6] =='O') && pos_input[3] != 'X')
                    {
                        pos_input[3]='X';
                    }

                    else if((pos_input[3] =='O' && pos_input[6] =='O') && pos_input[0] != 'X')
                    {
                        pos_input[0]='X';
                    }

                    else if((pos_input[3] =='O' && pos_input[4] =='O') && pos_input[5] != 'X')
                    {
                        pos_input[5]='X';
                    }

                    else if((pos_input[3] =='O' && pos_input[5] =='O') && pos_input[4] != 'X')
                    {
                        pos_input[4]='X';
                    }

                    else if((pos_input[4] =='O' && pos_input[5] =='O') && pos_input[3] != 'X')
                    {
                        pos_input[3]='X';
                    }

                    else if((pos_input[6] =='O' && pos_input[7] =='O') && pos_input[8] != 'X')
                    {
                        pos_input[8]='X';
                    }

                    else if((pos_input[6] =='O' && pos_input[8] =='O') && pos_input[7] != 'X')
                    {
                        pos_input[7]='X';
                    }

                    else if((pos_input[7] =='O' && pos_input[8] =='O') && pos_input[6] != 'X')
                    {
                        pos_input[6]='X';
                    }

                    else if((pos_input[0] =='O' && pos_input[4] =='O') && pos_input[8] != 'X')
                    {
                        pos_input[8]='X';
                    }

                    else if((pos_input[1] =='O' && pos_input[4] =='O') && pos_input[7] != 'X')
                    {
                        pos_input[7]='X';
                    }

                    else if((pos_input[1] =='O' && pos_input[7] =='O') && pos_input[4] != 'X')
                    {
                        pos_input[4]='X';
                    }

                    else if((pos_input[7] =='O' && pos_input[4] =='O') && pos_input[1] != 'X')
                    {
                        pos_input[1]='X';
                    }

                    else if((pos_input[2] =='O' && pos_input[5] =='O') && pos_input[8] != 'X')
                    {
                        pos_input[8]='X';
                    }

                    else if((pos_input[2] =='O' && pos_input[8] =='O') && pos_input[5] != 'X')
                    {
                        pos_input[5]='X';
                    }

                    else if((pos_input[5] =='O' && pos_input[8] =='O') && pos_input[5] != 'X')
                    {
                        pos_input[5]='X';
                    }

                    else if((pos_input[2] =='O' && pos_input[4] =='O') && pos_input[6] != 'X')
                    {
                        pos_input[6]='X';
                    }

                    else if((pos_input[2] =='O' && pos_input[6] =='O') && pos_input[4] != 'X')
                    {
                        pos_input[4]='X';
                    }

                    else if((pos_input[6] =='O' && pos_input[4] =='O') && pos_input[2] != 'X')
                    {
                        pos_input[2]='X';
                    }

                    else if(pos_input[5] == 'O' && pos_input[2] != 'O' && pos_input[2] != 'X')
                    {
                        pos_input[2]='X';
                    }

                    else if(pos_input[3] == 'O' && pos_input[0] != 'O' && pos_input[0] != 'X')
                    {
                        pos_input[0]='X';
                    }


                    else
                    {
                        if(pos_input[4] == 'O')
                        {
A:
                            /* Generates integers between 0 to 8 , including them. */

                            rand_int = rand()%9;

                            /* Verifies, whether acceptable or not. */

                            if(rand_int != 1 && rand_int != 3 && rand_int != 5 && rand_int != 7 
                                && pos_input[rand_int] != 'X' && pos_input[rand_int] != 'O')

                                pos_input[rand_int] = 'X';

                            else
                                goto A;
                        }

                        else
                        {
                            if(pos_input[4] != 'X')
                                pos_input[4] = 'X';
                            else
                            {
B:
                                /* Generates integers between 0 to 8 , including 0 & 8. */

                                rand_int = rand()%9;     

                                /* Verifies, whether acceptable or not. */

                                if((rand_int == 1 || rand_int == 3 || rand_int == 5 || rand_int == 7)
                                    && pos_input[rand_int] != 'X' && pos_input[rand_int] != 'O')

                                    pos_input[rand_int] = 'X';

                                else
                                    goto B;
                            }
                        }
                    }

                    /*******************************************************************************
                    ********************************************************************************
                    ###################### ARTIFICIAL INTELLIGENCE PART FINISHED ###################
                    ********************************************************************************
                    *******************************************************************************/

                    printf("\n\n\n\n*************GAME STATUS***************\n\n");

                    for(option_position=0; option_position<9; option_position++)
                    {
                        /* prints new line after printing 3 position in a row */

                        if(option_position==0||option_position==3|| option_position==6)   
                            printf("\n\n");                                                   

                        /* prints position value */

                        printf("   %c",pos_input[option_position]);                     
                    }

                    /*******************************************************************************************
                    ################################## DECIDING RESULT #########################################
                    ********************************************************************************************
                    ######################## Checks for three same input in a line #############################
                    ################### If there, Computer is the WINNER !!!!!!!!!!!!! #########################
                    *******************************************************************************************/

                    if(pos_input[0]=='X'&&pos_input[1]=='X'&&pos_input[2]=='X')
                    {
                        printf("\n\nComputer wins!!!!!");
                        flag++;
                        break;
                    }

                    else if(pos_input[0]=='X'&&pos_input[3]=='X'&&pos_input[6]=='X')
                    {
                        printf("\n\nComputer wins!!!!!");
                        flag++;
                        break;
                    }

                    else if(pos_input[0]=='X'&&pos_input[4]=='X'&&pos_input[8]=='X')
                    {
                        printf("\n\nComputer wins!!!!!");
                        flag++;
                        break;
                    }

                    else if(pos_input[1]=='X'&&pos_input[4]=='X'&&pos_input[7]=='X')
                    {
                        printf("\n\nComputer wins!!!!!");
                        flag++;
                        break;
                    }

                    else if(pos_input[2]=='X'&&pos_input[5]=='X'&&pos_input[8]=='X')
                    {
                        printf("\n\nComputer wins!!!!!");
                        flag++;
                        break;
                    }

                    else if(pos_input[2]=='X'&&pos_input[4]=='X'&&pos_input[6]=='X')
                    {
                        printf("\n\nComputer wins!!!!!");
                        flag++;
                        break;
                    }

                    else if(pos_input[3]=='X'&&pos_input[4]=='X'&&pos_input[5]=='X')
                    {
                        printf("\n\nComputer wins!!!!!");
                        flag++;
                        break;
                    }

                    else if(pos_input[6]=='X'&&pos_input[7]=='X'&&pos_input[8]=='X')
                    {
                        printf("\n\nComputer wins!!!!!");
                        flag++;
                        break;
                    }   
                }
            }
        }

        else
            printf("\n\n\nOnly 1 or 2 player available. \n");

        if((player_number == 1 || player_number ==2) && flag== 0)
            printf("\n\nDRAW!!!!!!!!!!!!!!!");

        printf("\n\n\nThanks for playing......\n\n\n Game re-starting.......\n\n\n Game re-started.");

        }

    return 0;
}

...is there a question here? or is this a code snippet? if it is a code snippet, I propose uploading this to the appropriate section. That can be found here, however; I honestly feel that this code is WAY to extensive. You need to look into compacting it somehow.

Thanks for the information. This was uploaded as a code snippet. But now I want your help on compacting it.

I'll help you,
start by defining the entire state of the game.

We'll call this
int board[9] = { 0 };
A 3x3 space in two dimensions.
0 - Unknown state
1 - Player 1 claims
2 - Player 2 claims

Next, you want to allow input.
The simplest inputs are [position, player]
There are two players.

void set(int pos, int player) { board[pos] = player; }
Where player is [0-2] and pos [0-8]

Check for a cat's game!

bool catsGame(  )
{ // return false if any tile is unset
  for(int i=0;i<9;++i) if( ! board[i] ) return false;
  return true; // To be fair, you could detect a cat's game many moves ahead
} // I didn't take that higher complexity approach here.

Finally, you want to detect victory conditions.

int victory(  )
{
  // check rows [0, 3, 6] are left sides
  for( int i=0;i<7;i+=3; )
  {
    if( !board[i] ) continue; // Unset.
    if( board[i] == board[i+1] && board[i] == board[i+2] ) return board[i];
  }
  // next, check columns [0, 1, 2]
  for( int i=0;i<2;++i )
  {
    if( !board[i] ) continue;
    if( board[i] == board[i+3] && board[i] == board[i+6] ) return board[i];
  }
  // finally, evaluate diagonals [4] is center
  if( board[4] )
  {
    if( board[0] == board[4] && board[0] == board[8] ) return board[4];
    if( board[2] == board[4] && board[2] == board[6] ) return board[4];
  }
  // No winner detected.
  return 0;
}

You can translate position to space this way,

#define WIDTH 3
#define HEIGHT 3

Assume you have an arbitrary coordinate type [x,y]
You can map this type to your grid with,
board[ n ] = y * WIDTH + x

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.