Thread: Tic Tac Toe

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    61

    Tic Tac Toe

    Okay. Here are my instructions. I am not allowed to use Arrays.

    Instructions:
    You are to create a simulation of a tic-tac-toe game. Each position will be generated randomly based on a seed that is input by the user. The program will print the board, determine if it is a valid board (4 pieces of one and 5 of another, at most one winning pattern), and determine if there is a winner.
    1) Prompt for and read in nonnegative user seed (function) Possible invalid entry: Ran out of data. Non-int data. Not nonnegative. When the user enters an invalid seed, the program should not process the rest of the program. Do NOT use exit() or abort() builtin functions. Use a Boolean function for getting the seed. 2) Generate board (function) 3) Print board (function) 4) Determine if valid (function) Possible errors: too many Xs or Os too many winning patterns If it is invalid, the program should not process the rest of the program. Use a Boolean function to determine if valid. 5) Determine if winner, and if so, whom, and if so, where. (function) 6) Print whether there is a winner, and if so, whom, and if so, where. (function)And here is the code that I have so far:

    Code:
    #include <stdio.h>#include <stdbool.h>
    
    
    bool getInput(int* pInput);
    int genBoard(int input);
    void printBoard(int input, int topRow, int midRow, int bottomRow, int lCol,
                    int midCol, int rCol, int lDiag, int rDiag);
    int ifValid(int input, int topRow, int midRow, int bottomRow, int lCol,
                int midCol, int rCol, int lDiag, int rDiag);
    int winner(int input, int topRow, int midRow, int bottomRow, int lCol,
               int midCol, int rCol, int lDiag, int rDiag);
    void display(int input, int topRow, int midRow, int bottomRow, int lCol,
                 int midCol, int rCol, int lDiag, int rDiag);
    
    
    
    
    int main(void)
    {
       int input;
       int topRow, midRow, bottomRow;
       int lCol, midCol, rCol;
       int lDiag, rDiag;
    
    bool getInput(int* pInput)
    {
       bool success = false;
       int scanRes;
    
    
       printf("\nName: Danielle Evans");
       printf("\nPlease enter a nonnegative integer seed: ");
       scanRes = scanf("%d", input);
    
    
       if (scanRes == 1)
       {
          int input = *pInput;
    
    
          if (input < 0)
          {
              printf("Not nonnegative. ");
          }
          else
             printf("\nRan out of data. ");
       }
       else if (scanRes == 0)
          printf("\nNon-int data. ");
    
    
       if(!success)
          printf("\n\n");
    
    
       return success;
    }
    
    
    int genBoard(int input)
    {
      
    }
    
    void printBoard(int input, int topRow, int midRow, int bottomRow, int lCol,
                    int midCol, int rCol, int lDiag, int rDiag)
    {
    }
    
    
    int ifValid(int input, int topRow, int midRow, int bottomRow, int lCol,
                int midCol, int rCol, int lDiag, int rDiag)
    {
    
    
    }
    
    
    int winner(int input, int topRow, int midRow, int bottomRow, int lCol,
               int midCol, int rCol, int lDiag, int rDiag)
    {
    
    
    }
    
    
    void display(int input, int topRow, int midRow, int bottomRow, int lCol,
                 int midCol, int rCol, int lDiag, int rDiag)
    {
    
    
    }
    
    
    return 0;
    }
    My question is how do I do the genBoard() function and the printBoard() function? Also, is the code that I have so far correct?
    Thanks

  2. #2
    Here we go again...
    Join Date
    Sep 2011
    Location
    San Diego
    Posts
    102
    Instead of this:

    Code:
    int topRow, midRow, bottomRow;    
    int lCol, midCol, rCol;    
    int lDiag, rDiag;
    do this

    Code:
    int game_board[3][3];
    Then use two for loops to fill each spot. Take a look here also Arrays in C and C++ - Cprogramming.com

  3. #3
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Random TicTacToe?! That's new...

    Since it's talking about randomness, you must use the stdlib's functions "srand()" for the seed and "rand()" for the random numbers.
    Say you have nine variables representing each square of the board, you can set it to "rand() % 2". Zero will be the one symbol, and one will be the other. About the printing and checking part, I guess you can figure them out.
    Devoted my life to programming...

  4. #4
    Registered User
    Join Date
    Sep 2011
    Posts
    61
    I am not allowed to use an array.

  5. #5
    Registered User
    Join Date
    Sep 2011
    Posts
    61
    Also, I am only supposed to use the #include stdio.h and #include stdbool.h

  6. #6
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    You can't nest your functions inside main(). Move them out.
    If you understand what you're doing, you're not learning anything.

  7. #7
    Registered User
    Join Date
    Sep 2011
    Posts
    61
    She made us do a structure chart and she said that all 6 functions are inside of main

  8. #8
    Here we go again...
    Join Date
    Sep 2011
    Location
    San Diego
    Posts
    102
    Then you are going to have to print each spot in the correct order, it's not that bad. Since you can't use an array you are going to need a variable for each spot (9 total). I would probably use char's instead of int's and use GReaper's suggestion to fill the spots.

  9. #9
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Quote Originally Posted by Dannibe7 View Post
    Also, I am only supposed to use the #include stdio.h and #include stdbool.h
    Great... Don't worry, it's a good opportunity to make a Pseudo Random Number's Generator.

    EDIT: I forgot, in C you don't have to have a prior declaration. Just call srand and rand and cross your fingers...
    Devoted my life to programming...

  10. #10
    Registered User
    Join Date
    Sep 2011
    Posts
    61
    isnt the int game_board[3][3] an array?

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Dannibe7 View Post
    She made us do a structure chart and she said that all 6 functions are inside of main
    Did your teacher actually tell you to create nested functions... or dis she by chance say they were "called from main"...

    There is a difference.

    C does not support nested functions... you cannot create a function inside a function...

    Now just on the off chance your teacher did tell you to do that...
    You seriously need to get a teacher who knows what he or she is talking about... 'cause yours doesn't.

  12. #12
    Registered User
    Join Date
    Sep 2011
    Posts
    61
    Well I am assuming that it would be called from main then. but my structure chart has all of the functions coming off of main. So I should take all of these out of main?

  13. #13
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Quote Originally Posted by Dannibe7 View Post
    Well I am assuming that it would be called from main then. but my structure chart has all of the functions coming off of main. So I should take all of these out of main?
    Exactly.
    Devoted my life to programming...

  14. #14
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    You do not make a tic-tac-toe game without using arrays; anyone who suggests to do otherwise is either a moron, or just trying to torture you.
    That does not teach you how to program, it teaches you how not to program.

    You don't jump into the sea with large juicy steaks tied to yourself in order to keep sharks away do you?! Don't do the opposite of what makes sense and expect to get something of practical value out of it.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  15. #15
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by GReaper View Post
    EDIT: I forgot, in C you don't have to have a prior declaration. Just call srand and rand and cross your fingers...
    That's a dirty, nasty hack, which I'm sure the teacher wouldn't go for, and just a very bad idea in general. It can actually cause serious problems.

    The declaration makes sure types are correct, but in the absence of a declaration, many compilers assume the function returns an int. This means the compiler can't properly check or convert return types for you. It will generate code assuming an int was returned, but that might not jive with what the called function puts on the stack or in a register for returning. Here's an example:

    main.c
    Code:
    #include <stdio.h>
    
    double foo(void)
    {
        return 123.45;
    }
    
    int main(void)
    {
        int x;
    
        x = foo();
        printf("After calling foo, for which the compiler knows the return type, x = %d\n", x);
        x = bar();
        printf("After calling bar, for which the compiler does NOT know the return type, x = %d\n", x);
    
        return 0;
    }
    func.c
    Code:
    double bar(void)
    {
        return 123.45;
    }
    Compiling (ignoring the warning) and running gives:
    Code:
    $ gcc -Wall main.c func.c
    main.c: In function ‘main’:
    main.c:14: warning: implicit declaration of function ‘bar’
    $ ./a.out
    After calling foo, for which the compiler knows the return type, x = 123
    After calling bar, for which the compiler does NOT know the return type, x = 73
    If the compiler knew that bar returned double, it would correctly truncate the decimal portion and x would be 123 after calling bar too. This can be verified by putting the prototype for bar at the top of main.c.
    Last edited by anduril462; 10-21-2011 at 02:58 PM.

Popular pages Recent additions subscribe to a feed