Search code examples
ctic-tac-toe

Need some assistance with a variable in a Tic-Tac-Toe Game (Beginner Coder)


I am having some issues with my Tic-Tac-Toe game I am creating in C.

I have a variable called int cats (a cats game is a tie game) that is used to help in calculating and displaying text for a tie game.

I have tried pretty much all week to figure out how to get the game to tie. Even if you guys could point me in the right direction, that would be Sweet!

-Frendin

(P.S. The empty if statement at the beginning is for a player vs. computer game. If I can figure out how to get the game to tie, I can code that part no problem! Thanks again guys!)

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

void printTheBoard(char boardPieces[3][3]);
int playerOne(char boardPieces[3][3], int cats);
int playerTwo(char boardPieces[3][3], int cats);
void checkBoard(char boardPieces[3][3], int cats);

int main(void)
{
    char boardPieces[3][3] = {'\0'};
    int cats = 0;  //A tie game in Tic-Tac-Toe is called a Cats Game.
    int choice;

    printf("Would you like to play against the computer or another player?\n");
    printf("Press 1 to play against another player.\n");
    printf("Press 2 to play against the computer.\n");
    scanf("%d", &choice);

    if(choice == 1)
    {
        for(int i = 0; i < 5; ++i)
        {
            printTheBoard(boardPieces);
            playerOne(boardPieces, cats);
            checkBoard(boardPieces, cats);

            printTheBoard(boardPieces);
            playerTwo(boardPieces, cats);
            checkBoard(boardPieces, cats);
        }   
    }

    if(choice == 2)
    {

    }

    return 0;
}

void printTheBoard(char boardPieces[3][3])
{
    printf("\n %c | %c | %c\n", boardPieces[0][0], boardPieces[0][1], boardPieces[0][2]);
    printf("---|---|---\n");
    printf(" %c | %c | %c\n", boardPieces[1][0], boardPieces[1][1], boardPieces[1][2]);
    printf("---|---|---\n");
    printf(" %c | %c | %c\n\n", boardPieces[2][0], boardPieces[2][1], boardPieces[2][2]);
}

int playerOne(char boardPieces[3][3], int cats)
{
    int choice;

    printf("It is your turn. You are player O. Please enter in the space you would like to  take. The first space is 1, continuing left to right, top to bottom\n");
    scanf("%d", &choice);

    if(choice == 1)
    {
        boardPieces[0][0] = 'O';
        ++cats;
        return cats;
    }
    else if(choice == 2)
    {
        boardPieces[0][1] = 'O';
        ++cats;
        return cats;
    }
    else if(choice == 3)
    {
        boardPieces[0][2] = 'O';
        ++cats;
        return cats;
    }
    else if(choice == 4)
    {
        boardPieces[1][0] = 'O';
        ++cats;
        return cats;
    }
    else if(choice == 5)
    {
        boardPieces[1][1] = 'O';
        ++cats;
        return cats;
    }
    else if(choice == 6)
    {
        boardPieces[1][2] = 'O';
        ++cats;
        return cats;
    }
    else if(choice == 7)
    {
        boardPieces[2][0] = 'O';
        ++cats;
        return cats;
    }
    else if(choice == 8)
    {
        boardPieces[2][1] = 'O';
        ++cats;
        return cats;
    }
    else if(choice == 9)
    {
        boardPieces[2][2] = 'O';
        ++cats;
        return cats;
    }
}

int playerTwo(char boardPieces[3][3], int cats)
{
    int choice;

    printf("It is your turn. You are player X. Please enter in the space you would like to  take. The first space is 1, continuing left to right, top to bottom\n");
    scanf("%d", &choice);

    if(choice == 1)
    {
        boardPieces[0][0] = 'X';
        ++cats;
        return cats;
    }
    else if(choice == 2)
    {
        boardPieces[0][1] = 'X';
        ++cats;
        return cats;
    }
    else if(choice == 3)
    {
        boardPieces[0][2] = 'X';
        ++cats;
        return cats;
    }
    else if(choice == 4)
    {
        boardPieces[1][0] = 'X';
        ++cats;
        return cats;
    }
    else if(choice == 5)
    {
        boardPieces[1][1] = 'X';
        ++cats;
        return cats;
    }
    else if(choice == 6)
    {
        boardPieces[1][2] = 'X';
        ++cats;
        return cats;
    }
    else if(choice == 7)
    {
        boardPieces[2][0] = 'X';
        ++cats;
        return cats;
    }
    else if(choice == 8)
    {
        boardPieces[2][1] = 'X';
        ++cats;
        return cats;
    }
    else if(choice == 9)
    {
        boardPieces[2][2] = 'X';
        ++cats;
        return cats;
    }
}

void checkBoard(char boardPieces[3][3], int cats)
{
    if(boardPieces[0][0] == 'O')
    {
        if(boardPieces[0][1] == 'O')
        {
            if(boardPieces[0][2] == 'O')
            {
                printf("Player O has won the game!\n");
            }
        }
        else if(boardPieces[1][1] == 'O')
        {
            if(boardPieces[2][2] == 'O')
            {
                printf("Player O has won the game!\n");
            }
        }
        else if(boardPieces[1][0] == 'O')
        {
            if(boardPieces[2][0] == 'O')
            {
                printf("Player O has won the game!\n");
            }
        }
    }
    else if(boardPieces[0][2] == 'O')
    {
        if(boardPieces[1][1] == 'O')
        {
            if(boardPieces[2][0] == 'O')
            {
                printf("Player O has won the game!\n");
            }
        }
        else if(boardPieces[1][2] == 'O')
        {
            if(boardPieces[2][2] == 'O')
            {
                printf("Player O has won the game!\n");
            }
        }
    }
    else if(boardPieces[1][1] == 'O')
    {
        if(boardPieces[0][1] == 'O')
        {
            if(boardPieces[2][1] == 'O')
            {
                printf("Player O has won the game!\n");
            }
        }
        else if(boardPieces[1][0] == 'O')
        {
            if(boardPieces[1][2] == 'O')
            {
                printf("Player O has won the game!\n");
            }
        }
    }
    else if(boardPieces[2][0] == 'O')
    {
        if(boardPieces[2][1] == 'O')
        {
            if(boardPieces[2][2] == 'O')
            {
                printf("Player O has won the game!\n");
            }
        }
    }




    if(boardPieces[0][0] == 'X')
    {
        if(boardPieces[0][1] == 'X')
        {
            if(boardPieces[0][2] == 'X')
            {
                printf("Player X has won the game!\n");
            }
        }
        else if(boardPieces[1][1] == 'X')
        {
            if(boardPieces[2][2] == 'X')
            {
                printf("Player X has won the game!\n");
            }
        }
        else if(boardPieces[1][0] == 'X')
        {
            if(boardPieces[2][0] == 'X')
            {
                printf("Player X has won the game!\n");
            }
        }
    }
    else if(boardPieces[0][2] == 'X')
    {
        if(boardPieces[1][1] == 'X')
        {
            if(boardPieces[2][0] == 'X')
            {
                printf("Player X has won the game!\n");
            }
        }
        else if(boardPieces[1][2] == 'X')
        {
            if(boardPieces[2][2] == 'X')
            {
                printf("Player X has won the game!\n");
            }
        }
    }
    else if(boardPieces[1][1] == 'X')
    {
        if(boardPieces[0][1] == 'X')
        {
            if(boardPieces[2][1] == 'X')
            {
                printf("Player X has won the game!\n");
            }
        }
        else if(boardPieces[1][0] == 'X')
        {
            if(boardPieces[1][2] == 'X')
            {
                printf("Player X has won the game!\n");
            }
        }
    }
    else if(boardPieces[2][0] == 'X')
    {
        if(boardPieces[2][1] == 'X')
        {
            if(boardPieces[2][2] == 'X')
            {
                printf("Player O has won the game!\n");
            }
        }
    }



    else if(cats == 9)
    {
        printf("There was a tie game!\n");
    }
}

Solution

  • In the functions playerOne() and playerTwo() you increment the variable cats declared as function argument. You should store the value in the cats variable you declared in start of main, e.g.:

    for(int i = 0; i < 5; ++i)
    {
        printTheBoard(boardPieces);
        cats = playerOne(boardPieces, cats);
        checkBoard(boardPieces, cats);
    
        printTheBoard(boardPieces);
        cats = playerTwo(boardPieces, cats);
        checkBoard(boardPieces, cats);
    }
    

    A more detailed explanation follows. Consider the following code:

    void func(int var)
    {
        var = 2;
    }
    
    int main(void) 
    {
        int var;
        var = 1;
        func(var);
    }
    

    There are two different variables with the name var here. One is declared in the beginning of main, and its scope or lifetime is within the main function.

    The other one is declared as an argument to func, and its scope is local to the func function. This means that the variable and its value cease to exist when the func function returns.

    Now, if we want to use the value of func's local variable after the function returns, we can:

    • Return the value modified by func:

      int func(int var)
      {
          var++;
          return var;
      }
      ...
      new_var = func(old_var);
      
    • Have func modify a variable in-place via a pointer instead:

      void func(int *var)
      {
          (*var)++;
      }
      ...
      func(&var);
      

    Also, the else if condition to print the tie game message in the end of checkBoard() will never be run in your current code:

    ...
    else if(boardPieces[2][0] == 'X')
    {
        if(boardPieces[2][1] == 'X')
        {
            if(boardPieces[2][2] == 'X')
            {
                printf("Player O has won the game!\n");
            }
        }
    }
    
    else if(cats == 9)
    {
        printf("There was a tie game!\n");
    }
    

    Consider returning from the checkBoard() function after printing the result of the game. Maybe consider returning a value indicating if the game has ended, so you can end the program when this happens.