Thread: New Tic Tac Toe program. Only need help with int main function...

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    42

    New Tic Tac Toe program. Only need help with int main function...

    ran once but does not display gameboard and I'm 100% sure it's something to do with my int main. I really struggle with calling functions together..... (it's at very bottom)

    Code:
    #include <stdio.h>
    
    
    
    
    
    
    
    
    
    
    
    
    void info()
    {
     printf("TIC-TAC-TOE\nWritten by MEEEE\n");
     printf("Your Symbol 'x'");
     printf("Computer Symbol 'o'");
    }
    
    
    
    
    /*initialize board*/
    
    
    
    
    void init(char cell[])
    {
     int i;
    for (i=0; i<9; i++) cell[i] = ' ';
    }
    
    
    
    
    /*print gameboard */
    
    
    
    
    void gameBoard(char cell[])
    {
     printf("\n %c | %c | %c \n", cell[0], cell[1], cell[2]);
     printf("---|---|---\n");
     printf(" %c | %c | %c \n", cell[3], cell[4], cell[5]);
     printf("---|---|---\n");
     printf(" %c | %c | %c \n\n", cell[6], cell[7], cell[8]);
    }
    
    
    
    
    /*player moves*/
    
    
    void userMove(char *cell)
    {
    int i = 0;
    
    
    do {
    printf("Your Move (0-8): ");
    scanf("%d", &i);
    }
    // Here should also check if cell already filled
    while ( i < 0 || i > 8 );
    
    
    cell[i]='x';
    }
    
    
    
    
    void computerMove(char *cell)
    {
    
    
    // Here should play smart, for now just choose first empty cell
    int i;
    
    
    for (i=0;i!=9;i++)
    {
    if (cell[i] == ' ')
    {
    cell[i] = 'o';
    return;
    }
    }
    
    
    }
    
    
    
    
    char checkWinner(char* cell)
    {
    //diagonals
    if (cell[0] != ' ')
    if ((cell[0] == cell[4]) && (cell[0] == cell[8]))
    return cell[0];
    if (cell[2] != ' ')
    if ((cell[2] == cell[4]) && (cell[2] == cell[6]))
    return cell[2];
    
    
    // horizontals
    if (cell[0] != ' ')
    if ((cell[0] == cell[1]) && (cell[0] == cell[2]))
    return cell[3];
    if (cell[3] != ' ')
    if ((cell[3] == cell[4]) && (cell[3] == cell[5]))
    return cell[0];
    if (cell[6] != ' ')
    if ((cell[6] == cell[7]) && (cell[6] == cell[8]))
    return cell[6];
    
    
    // verticals
    if (cell[0] != ' ')
    if ((cell[0] == cell[3]) && (cell[0] == cell[6]))
    return cell[0];
    if (cell[1] != ' ')
    if ((cell[1] == cell[4]) && (cell[1] == cell[7]))
    return cell[1];
    if (cell[2] != ' ')
    if ((cell[2] == cell[5]) && (cell[2] == cell[8]))
    return cell[2];
    return ' ';
    }
    
    
    
    
    
    
    bool_gameIsOver(char *cell)
    {
    char winner;
    
    
    winner = checkWinner(cell);
    if (winner=='o')
    printf("Computer win !\n");
    else
    if (winner=='x')
    printf("You win !\n");
    
    
    return winner != ' ';
    }
    
    
    int main()
    {
    int false;
    int bool_continue;
    int _continue;
    
    
    
    
    char    cell[9];
    bool_continue;
    
    
    info();
    init(cell);
    _continue = true;
    while(_continue) {
    computerMove(cell);
    gameBoard(cell);
    if (gameIsOver(cell))
    _continue = false;
    else
    {
    userMove(cell);
    gameBoard(cell);
    if (gameIsOver(cell))
    _continue = false;
    }
    }
    
    
    return 0;
    }

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    Compiler messages:
    |141|warning: return type defaults to 'int' [-Wreturn-type]|
    |In function 'main':|
    |168|warning: statement with no effect [-Wunused-value]|
    |173|error: 'true' undeclared (first use in this function)|
    |173|note: each undeclared identifier is reported only once for each function it appears in|
    |177|warning: implicit declaration of function 'gameIsOver' [-Wimplicit-function-declaration]|
    And this variable is never given a value:
    Line 160: int false;

    EDIT: Are you sure it compiled?! Curious...
    Last edited by GReaper; 10-04-2011 at 03:44 PM.
    Devoted my life to programming...

  3. #3
    Here we go again...
    Join Date
    Sep 2011
    Location
    San Diego
    Posts
    102
    That code will not compile. You need to write a few lines then compile and test, you are doing too much at one time.

    Let's give you a little more direction. I've removed a lot of your code to make it easier to understand. You're main right now should look something like this:

    Code:
    int main()
    {
        char cell[9];
    
        info();
        init(cell);
        gameBoard(cell);
        userMove(cell);
        gameBoard(cell);
    
        return 0;
    }
    And only those 4 functions should be in your code at this time. Can you at least get the user to input a number and it display the 'x'?

  4. #4
    Registered User
    Join Date
    Oct 2011
    Posts
    42

    well that is what I was originally going to put but you have totally disregarded....

    the end game function....now how exactly will I implement it into the int main. I really need help because I am so stummped... suggetions really can't help me here... I have all the functions...ok.....but now I need help constructing the int main...which I really have no clue to begin with...someone help me.

  5. #5
    Here we go again...
    Join Date
    Sep 2011
    Location
    San Diego
    Posts
    102
    You're missing the point. You won't need to end the game if you can even play it.

    Here is a little more help, it's your code just modified a bit.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
     
    void info()
    {
        printf("TIC-TAC-TOE\nWritten by MEEEE\n");
        printf("Your Symbol 'x'");
        printf("Computer Symbol 'o'");
    }
     
    /*initialize board*/
    void init(char cell[])
    {
        int i;
        for (i=0; i<9; i++) 
            cell[i] = ' ';
    }
     
    /*print gameboard */
    void gameBoard(char cell[])
    {
        printf("\n %c | %c | %c \n", cell[0], cell[1], cell[2]);
        printf("---|---|---\n");
        printf(" %c | %c | %c \n", cell[3], cell[4], cell[5]);
        printf("---|---|---\n");
        printf(" %c | %c | %c \n\n", cell[6], cell[7], cell[8]);
    }
     
    /*player moves*/
    void userMove(char cell[])
    {
        int i = 0;
        int cont = 1;
    
        do{
            printf("Your Move (0-8): ");
            scanf("%d", &i);
    
            if((cell[i] != ' ') || (i < 0 || i > 8))
            {
                printf("Invalid Input\n");    //invalid input
            }
            else
            {
                cell[i] = 'x';
                cont--;        //set while loop to false, break
            }
    
    
        }
        while(cont);
    
    }
    
    /*computer moves*/
    void computerMove(char cell[])
    {
     
        int i = 0;
        int cont = 1;
    
        do
        {
            if(cell[i] == ' ')
            {
                cell[i] = 'o';
                cont--;
            }
            else
            {
                i++;
            }
    
        }while(cont);
    
    }
     
    int main()
    {
        char cell[9];
        int cont = 1;
        int ccode = 0;
    
        info();
        init(cell);
        gameBoard(cell);
        while(cont)
        {
    userMove(cell); gameBoard(cell); //check if user wins //if user wins, print user won //and cont-- to exit loop computerMove(cell); gameBoard(cell); //check if computer wins, print computer won //and cont-- to exit loop //if nobody won get another user input
    } return 0; }
    Now you have to make your code work for the checkWinner and printWinner.

  6. #6
    Registered User
    Join Date
    Oct 2011
    Posts
    42

    ok so now I have included checkWinner, but I am calling the function wrong... how do

    I call the function so it performs checkwinner?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    void info()
    {
        printf("TIC-TAC-TOE\nWritten by MEEEE\n");
        printf("Your Symbol 'x'");
        printf("Computer Symbol 'o'");
    }
    
    
    /*initialize board*/
    void init(char cell[])
    {
        int i;
        for (i=0; i<9; i++)
            cell[i] = ' ';
    }
    
    
    /*print gameboard */
    void gameBoard(char cell[])
    {
        printf("\n %c | %c | %c \n", cell[0], cell[1], cell[2]);
        printf("---|---|---\n");
        printf(" %c | %c | %c \n", cell[3], cell[4], cell[5]);
        printf("---|---|---\n");
        printf(" %c | %c | %c \n\n", cell[6], cell[7], cell[8]);
    }
    
    
    /*player moves*/
    void userMove(char cell[])
    {
        int i = 0;
        int cont = 1;
    
    
        do{
            printf("Your Move (0-8): ");
            scanf("%d", &i);
    
    
            if((cell[i] != ' ') || (i < 0 || i > 8))
            {
                printf("Invalid Input\n");    //invalid input
            }
            else
            {
                cell[i] = 'x';
                cont--;        //set while loop to false, break
            }
    
    
    
    
        }
        while(cont);
    
    
    }
    
    
    
    
    
    
    
    
    
    
    /*computer moves*/
    void computerMove(char cell[])
    {
    
    
        int i = 0;
        int cont = 1;
    
    
        do
        {
            if(cell[i] == ' ')
            {
                cell[i] = 'o';
                cont--;
            }
            else
            {
                i++;
            }
    
    
        }while(cont);
    
    
    }
    
    
    char checkWinner(char cell[])
    {
    //diagonals
    if (cell[0] != ' ')
    if ((cell[0] == cell[4]) && (cell[0] == cell[8]))
    return cell[0];
    if (cell[2] != ' ')
    if ((cell[2] == cell[4]) && (cell[2] == cell[6]))
    return cell[2];
    
    
    // horizontals
    if (cell[0] != ' ')
    if ((cell[0] == cell[1]) && (cell[0] == cell[2]))
    return cell[3];
    if (cell[3] != ' ')
    if ((cell[3] == cell[4]) && (cell[3] == cell[5]))
    return cell[0];
    if (cell[6] != ' ')
    if ((cell[6] == cell[7]) && (cell[6] == cell[8]))
    return cell[6];
    
    
    // verticals
    if (cell[0] != ' ')
    if ((cell[0] == cell[3]) && (cell[0] == cell[6]))
    return cell[0];
    if (cell[1] != ' ')
    if ((cell[1] == cell[4]) && (cell[1] == cell[7]))
    return cell[1];
    if (cell[2] != ' ')
    if ((cell[2] == cell[5]) && (cell[2] == cell[8]))
    return cell[2];
    return 'o';
    return 'x';
    
    
    char winner;
    
    
    winner = checkWinner(cell);
    if (winner=='o')
    printf("Computer win !\n");
    else
    if (winner=='x')
    printf("You win !\n");
    return winner != ' ';
    }
    
    
    
    
    
    
    int main()
    {
        char cell[9];
        int cont = 1;
        int ccode = 0;
    
    
        info();
        init(cell);
        gameBoard(cell);
        while(cont)
        {
    userMove(cell);
    gameBoard(cell);
    
    
    
    
    //check if user wins
    //if user wins, print user won
    //and cont-- to exit loop
    computerMove(cell);
    gameBoard(cell);
    
    
    checkWinner(cell);
    
    
    //check if computer wins, print computer won
    //and cont-- to exit loop
    //if nobody won get another user input
          }
    
    
        return 0;
    }

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Before you do anything else, you should learn how to indent. No one wants to read that crap, including you.


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    Oct 2011
    Posts
    42
    Well you guys are my last hope, my teacher won't help me and he teaches with powerpoints and never shows us how exactly we are supposed to apply what we learned........................

  9. #9
    Here we go again...
    Join Date
    Sep 2011
    Location
    San Diego
    Posts
    102
    This part has some issues...

    Use curly brackets for every if statement. Right now this is giving you what you think it should. I can't even indent it for you because it won't make sense.

    Code:
    char checkWinner(char cell[])
    
    {
    //diagonals
    if (cell[0] != ' ')
    if ((cell[0] == cell[4]) && (cell[0] == cell[8]))
    return cell[0];
    if (cell[2] != ' ')
    if ((cell[2] == cell[4]) && (cell[2] == cell[6]))
    return cell[2];
     
     
    // horizontals
    if (cell[0] != ' ')
    if ((cell[0] == cell[1]) && (cell[0] == cell[2]))
    return cell[3];
    if (cell[3] != ' ')
    if ((cell[3] == cell[4]) && (cell[3] == cell[5]))
    return cell[0];
    if (cell[6] != ' ')
    if ((cell[6] == cell[7]) && (cell[6] == cell[8]))
    return cell[6];
     
     
    // verticals
    if (cell[0] != ' ')
    if ((cell[0] == cell[3]) && (cell[0] == cell[6]))
    return cell[0];
    if (cell[1] != ' ')
    if ((cell[1] == cell[4]) && (cell[1] == cell[7]))
    return cell[1];
    if (cell[2] != ' ')
    if ((cell[2] == cell[5]) && (cell[2] == cell[8]))
    return cell[2];
    return 'o';
    return 'x';
     
     
    char winner;
     
     
    winner = checkWinner(cell);
    if (winner=='o')
    printf("Computer win !\n");
    else
    if (winner=='x')
    printf("You win !\n");
    return winner != ' ';
    }

  10. #10
    Registered User
    Join Date
    Oct 2011
    Posts
    42

    you mean something like this?

    Code:
    if (cell[0] != ' ')
    
    
        {
            if ((cell[0] == cell[1]) && (cell[0] == cell[2]))
            return cell[3];
        }
    
    
    if (cell[3] != ' ')
    
    
        {if ((cell[3] == cell[4]) && (cell[3] == cell[5]))
        return cell[0];
        }
    
    
    if (cell[6] != ' ')
        {
            if ((cell[6] == cell[7]) && (cell[6] == cell[8]))
        return cell[6];
        }

  11. #11
    Registered User
    Join Date
    Oct 2011
    Posts
    42

    Ok ok, It compiles the same exact way even without the curly brackets...try

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    void info()
    {
        printf("TIC-TAC-TOE\nWritten by MEEEE\n");
        printf("Your Symbol 'x'");
        printf("Computer Symbol 'o'");
    }
    
    
    /*initialize board*/
    void init(char cell[])
    {
        int i;
        for (i=0; i<9; i++)
            cell[i] = ' ';
    }
    
    
    /*print gameboard */
    void gameBoard(char cell[])
    {
        printf("\n %c | %c | %c \n", cell[0], cell[1], cell[2]);
        printf("---|---|---\n");
        printf(" %c | %c | %c \n", cell[3], cell[4], cell[5]);
        printf("---|---|---\n");
        printf(" %c | %c | %c \n\n", cell[6], cell[7], cell[8]);
    }
    
    
    /*player moves*/
    void userMove(char cell[])
    {
        int i = 0;
        int cont = 1;
    
    
        do{
            printf("Your Move (0-8): ");
            scanf("%d", &i);
    
    
            if((cell[i] != ' ') || (i < 0 || i > 8))
            {
                printf("Invalid Input\n");    //invalid input
            }
            else
            {
                cell[i] = 'x';
                cont--;        //set while loop to false, break
            }
    
    
    
    
        }
        while(cont);
    
    
    }
    
    
    
    
    
    
    
    
    
    
    /*computer moves*/
    void computerMove(char cell[])
    {
    
    
        int i = 0;
        int cont = 1;
    
    
        do
        {
            if(cell[i] == ' ')
            {
                cell[i] = 'o';
                cont--;
            }
            else
            {
                i++;
            }
    
    
        }while(cont);
    
    
    }
    
    
    char checkWinner(char cell[])
    {
    
    
    
    
    
    
    //diagonals
    if (cell[0] != ' ')
        {
        if ((cell[0] == cell[4]) && (cell[0] == cell[8]))
        return cell[0];
        }
    
    
    if (cell[2] != ' ')
       {
        if ((cell[2] == cell[4]) && (cell[2] == cell[6]))
        return cell[2];
       }
    
    
    
    
    
    
    
    
    
    
    // horizontals
    if (cell[0] != ' ')
        {
        if ((cell[0] == cell[1]) && (cell[0] == cell[2]))
        return cell[3];
        }
    
    
    if (cell[3] != ' ')
        {
        if ((cell[3] == cell[4]) && (cell[3] == cell[5]))
        return cell[0];
        }
    
    
    if (cell[6] != ' ')
        {
            if ((cell[6] == cell[7]) && (cell[6] == cell[8]))
        return cell[6];
        }
    
    
    
    
    
    
    
    
    
    
    // verticals
    if (cell[0] != ' ')
       {
        if ((cell[0] == cell[3]) && (cell[0] == cell[6]))
        return cell[0];
       }
    if (cell[1] != ' ')
       {
        if ((cell[1] == cell[4]) && (cell[1] == cell[7]))
        return cell[1];
       }
    if (cell[2] != ' ')
        {
        if ((cell[2] == cell[5]) && (cell[2] == cell[8]))
        return cell[2];
        }
    return 'o';
    return 'x';
    
    
    char winner;
    
    
    winner = checkWinner(cell);
    if (winner=='o')
    printf("Computer win !\n");
    else
    if (winner=='x')
    printf("You win !\n");
    return winner != ' ';
    }
    
    
    
    
    
    
    int main()
    {
        char cell[9];
        int cont = 1;
        int ccode = 0;
    
    
        info();
        init(cell);
        gameBoard(cell);
        while(cont)
        {
    userMove(cell);
    gameBoard(cell);
    
    
    
    
    //check if user wins
    //if user wins, print user won
    //and cont-- to exit loop
    computerMove(cell);
    gameBoard(cell);
    
    
    checkWinner(cell);
    
    
    //check if computer wins, print computer won
    //and cont-- to exit loop
    //if nobody won get another user input
          }
    
    
        return 0;
    }

  12. #12
    Here we go again...
    Join Date
    Sep 2011
    Location
    San Diego
    Posts
    102
    yes, now dont return a char. return an int telling if someone won. Try returning a 0 is no one has one yet, return a 1 if the user won or return a 2 if the computer won.

  13. #13
    Here we go again...
    Join Date
    Sep 2011
    Location
    San Diego
    Posts
    102
    get rid of that return 'x'; return 'o'; That doesn't make any sense.

  14. #14
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Fix your code. Stop pasting in five extra blank lines for no reason. Fix your indentation. Stop programming on your phone or whatever it is you are doing.


    Quzah.
    Hope is the first step on the road to disappointment.

  15. #15
    Registered User
    Join Date
    Oct 2011
    Posts
    42
    if i get rid of the return x and return o, the program stops working after a number is entered

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 06-01-2011, 03:08 AM
  2. Replies: 9
    Last Post: 05-08-2011, 12:14 PM
  3. function declaration in Main function
    By filipn in forum C Programming
    Replies: 2
    Last Post: 11-11-2009, 01:31 PM
  4. having some trouble calling a function in the main program
    By CMakesMeSad :( in forum C Programming
    Replies: 19
    Last Post: 06-26-2009, 09:33 PM
  5. Replies: 2
    Last Post: 03-30-2009, 02:06 AM

Tags for this Thread