Welcome to Code Forum!

Join a community that supports you and your coding journey from day one. We strive to be a friendly, supportive community that empowers everyone to be better developers. By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    GIF shows where to locate </> in the thread and or post editor toolbar.
    To learn more about how to use our BBCode feature, review our "How to post your code into threads" here.

    Thank you, Code Forum.

Need help to fix bug in tic tac toe game

saad90

New Coder
I can't find the bug . I watched the video on 'bro code' youtube channel. Someone help in finding the bug.
e
C:
#include<stdio.h>
#include <stdlib.h>
#include <time.h>
#include <ctype.h>
char board[3][3];
const char PLAYER='X';
const char COMPUTER='O';
void resetBoard();
void printBoard();
void playerMove();
void computerMove();
int checkfreespace();
char checkwinner();
void printwinner(char);

int main(){
    char winner=' ';
    char response;
    do
    { winner=' ';
    response=' ';
        resetBoard();
    while (winner==' '&& checkfreespace()!=0)
    {
        printBoard();
        playerMove();
        winner=checkwinner();
        if(winner!=' '||checkfreespace()==0){break;}
        computerMove();
        winner=checkwinner();
        if(winner!=' '||checkfreespace()==0){break;}
        
        
    }
    
    printBoard();
    printwinner(winner);
    printf("\nWould you like to play again?:(Y/N)");
    scanf(" %c",&response);
    response=toupper(response);

        
    } while (response=='Y');
    
    printf("Thanks for playing");
    return 0;
}
void resetBoard(){
    for(int i=0;i<3;i++){
        for (int j = 0; j< 3; j++)
        {
            board[i][j]=' ';
        }
        
    }
};
void printBoard(){
    printf(" %c | %c | %c",board[0][0],board[0][1],board[0][2]);
    printf("\n---|---|---\n");
    printf(" %c | %c | %c",board[1][0],board[1][1],board[1][2]);
    printf("\n---|---|---\n");
    printf(" %c | %c | %c",board[2][0],board[2][1],board[2] [2]);
    printf("\n");
};
void playerMove(){
    int x;
    int y;
    do{printf("Enter row(1-3):");
    scanf("%d",&x);
    x--;
    printf("Enter column(1-3):");
    scanf("%d",&y);
    y--;
    if(board[x][y]!=' '){
        printf("Already occupied\n");
    }else{board[x][y]=PLAYER;break;};}while(board[x][y]!=' ');
    
};
void computerMove(){
    srand(time(0));
    int x;
    int y;
    if(checkfreespace()!=0){
    do{x=rand()%3;y=rand()%3;}while(board[x][y]!=' ');board[x][y]=COMPUTER
    ;}else{printwinner(' ');}
};
int checkfreespace(){
    int freespaces=9;
    for (int i = 0; i < 3; i++)
    {
       for (int j = 0; j < 3; j++)
       {
        if(board[i][j]!=' '){freespaces--;}

       };
      
    
};return freespaces;}
char checkwinner(){
    //check row
    for(int i=0;i<3;i++){
        if(board [i][0]==board[i][1]&&board [i][0]==board[i][2]){return board[i][0];};
    };
    //check column
    for(int i=0;i<3;i++){
        if(board [0][i]==board[1][i]&&board [0][i]==board[2][i]){return board[0][i];};
    };
    //check diagonal
    if(board[0][0]==board[1][1]&&board[0][0]==board[2][2]){return board[0][0];}
    if(board[0][2]==board[1][1]&&board[0][2]==board[2][0]){return board[0][2];}
    return ' ';
};
void printwinner(char winner){
    if(winner==PLAYER){printf("You win");
    }else if(winner==COMPUTER){printf("You lose");}
    else{printf("It's a tie");}
};
 
Hello this is Gulshan Negi
Well, you need to add semicolon ; at the end of this line.

Code:
do{x=rand()%3;y=rand()%3;}while(board[x][y]!=' ');board[x][y]=COMPUTER

Correct line

Code:
do{x=rand()%3;y=rand()%3;}while(board[x][y]!=' ');board[x][y]=COMPUTER;

I hope it will work.
Thanks
 

Buy us a coffee!

Buy me a coffee.
Back
Top Bottom