Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Logical Error?

$
0
0
I am facing a small problem.The computer is making two moves for every single move made by the user.
//TIC TAC TOE

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<time.h>

#define clrscr() system("CLS");

void rules();
char toss();
void play_U();
void play_C();

int flag_winU=0;
int flag_winC=0;
char move[3][3];
int move_value[3][3];

int main(void)
{
    int counter=0,x,y;
    char fill_values=0;
    
    printf("TIC TAC TOE");
    printf("\n-----------");
    
    rules();
    
    clrscr();
    
    //filling up the tic tac toe board with numbers from 0 to 9 indicating the square number
    for(x=0;x<=2;x++)
    {
        for(y=0;y<=2;y++)
        {
            move[x][y]='0'+fill_values++;
            move_value[x][y]=0;
        }
    }
    
    //Gameplay
    while(1)
    {
         clrscr();
         
         //Display the current status
         for(x=0;x<=2;x++)
         {
             printf("\n");
             for(y=0;y<=2;y++)
             {
                 printf("%c",move[x][y]);
                 printf("|");
             }
             printf("\n------");
         }
         
         play_U();
         counter++;

         if(counter==9 || flag_winU==1 || flag_winC==1)
         {
             clrscr();
             for(x=0;x<=2;x++)
             {
                 printf("\n");
                 for(y=0;y<=2;y++)
                 {
                     printf("%c",move[x][y]);
                     printf("|");
                 }
                 printf("\n------");
             }
             //Display results
             if(flag_winU==1)
                 printf("\nCongrats user,you have won");
    
             else if(flag_winC==1)
                 printf("\nComputer Wins");
    
             else
                 printf("\nDraw");
                 
             getch();
             return 0;
        
         }
         
         play_C();
         counter++;  
    }
}

//Function:Rules of the game
void rules()
{
    printf("\n\nRules");
    printf("\n\n1.Only a single move can be made at a time");
    printf("\n\n2.Choose the square number(eg.1,2,3,etc) \nwhere you would like to place your X or O");
    printf("\n\n3.Whoever places three X's or O's \nhorizontally,vertically or diagonally first wins");
    printf("\n\n4.If both fail to place three X's or O's \nhorizontally,vertically or diagonally,game ends in a draw");
    printf("\n\n5.The game will be played on a 3X3 board");

    //Displaying the board
    printf("\n 0 | 1 | 2");
    printf("\n ---------");
    printf("\n 3 | 4 | 5");
    printf("\n ---------");
    printf("\n 6 | 7 | 8");
    printf("\n ---------");
     
    printf("\nPress any key to continue...");
    getch();
    return; 
}

//Function:User Move
void play_U()
{
     char move_u;
     int x,y;
     int diag_sum1=0,diag_sum2=0;     
     
     printf("\nPlace X in square number :");
     scanf("%c",&move_u);
             
     //Replace the square number with X
     for(x=0;x<=2;x++)
     {
         for(y=0;y<=2;y++)
         {
             if(move[x][y]==move_u)
             {
                 move[x][y]='X';
                 move_value[x][y]=10;
             }
         }
     }
     
     //Check if user has won horizontally
     for(x=0;x<=2;x++)
     {
         int sum=0;
         for(y=0;y<=2;y++)
         {
              sum+=move_value[x][y];
         }
         if(sum==30)
         {
             flag_winU=1;
             return;           
         }
     }
     
     //Check if the user has won vertically
     for(y=0;y<=2;y++)
     {
         int sum=0;
         for(x=0;x<=2;x++)
         {
              sum+=move_value[x][y];
         }
         if(sum==30)
         {
             flag_winU=1;
             return;           
         }
     }
     
     //Check if the user has won diagonally
     for(x=0;x<=2;x++)
     {
         for(y=0;y<=2;y++)
         {
             if(x==y)
                 diag_sum1+=move_value[x][y];
             if((x+y)==2)
                 diag_sum2+=move_value[x][y];
         }
     }
     
     if(diag_sum1 == 30 || diag_sum2 == 30)
     {
         flag_winU=1;
         return;           
     }
     
}

//Function:Computer move
void play_C()
{
     int x,y;
     int diag_sum1=0,diag_sum2=0;
     int move_x,move_y;
     
     srand(time(NULL));
     move_x=rand()%3;
     move_y=rand()%3;
     
     //Computer's move
     while(1)
     {         
         if(move[move_x][move_y]!='X' && move[move_x][move_y]!='O')
         {
             move[move_x][move_y]='O';
             move_value[move_x][move_y]=100;
             break;
         }
                     
         else
         {
             srand(time(NULL));
             move_x=rand()%3;
             move_y=rand()%3;
         }
     }
     
     //Check if the computer won horizontally
     for(x=0;x<=2;x++)
     {
         int sum=0;
         for(y=0;y<=2;y++)
         {
              sum+=move_value[x][y];
         }
         if(sum==300)
         {
             flag_winC=1;
             return;           
         }
     }
     
     //Check if the computer has won vertically
     for(y=0;y<=2;y++)
     {
         int sum=0;
         for(x=0;x<=2;x++)
         {
              sum+=move_value[x][y];
         }
         if(sum==300)
         {
             flag_winC=1;
             return;           
         }
     }
     
     //Check if the computer has won diagonally
     for(x=0;x<=2;x++)
     {
         for(y=0;y<=2;y++)
         {
             if(x==y)
                 diag_sum1+=move_value[x][y];
             if((x+y)==2)
                 diag_sum2+=move_value[x][y];
         }
     }
     
     if(diag_sum1 == 300 || diag_sum2 == 300)
     {
         flag_winC=1;
         return;           
     }         
}


I am using DEV C++
Please help
Thanks and Regards

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>