Thread: Tic Tac Toe

  1. #31
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Quote Originally Posted by Dannibe7 View Post
    I dont know how to test it. I am using secure shell and when I do ./a.out it does not let me test it yet.
    Well, you're not calling any of your functions from main() yet so you're probably just seeing your program exit right away. main() doesn't have a lot to do in your current code listing:
    Code:
    int main(void)
    {
       int input;
       int topRow, midRow, bottomRow;
       int lCol, midCol, rCol;
       int lDiag, rDiag;
              
       // Where is this stuff?! You're not doing anything!
    
       return 0;
    }
    If you understand what you're doing, you're not learning anything.

  2. #32
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,907
    It wont magically test itself. You have to write your program to use your functions, then you test and see if they behave according to the assignment. You don't call any of your functions from main, so when you do ./a.out, your program doesn't actually do anything. You need to add code to main to call your functions so you can test if they work. Add a call to your getInput function, then compile and run your program. Does it do the right thing or produce the right error message when you enter "foo" or -123 or 42?

  3. #33
    Registered User
    Join Date
    Sep 2011
    Posts
    61
    So far this seems better.
    Code:
    #include <stdio.h>#include <stdbool.h>
    
    
    bool getInput(int* pInput);
    int genBoard(int input);
    void printBoard(int topL, int topC, int topR, int cenL,
                    int cenC, int cenR, int botL, int botC, int botR);
    bool ifValid(int topL, int topC, int topR, int cenL, int cenC,
                 int cenR, int botL, int botC, int botR);
    int winner(int topL, int topC, int topR, int cenL, int cenC,
                 int cenR, int botL, int botC, int botR);
    void display(int topL, int topC, int topR, int cenL, int cenC,
                 int cenR, int botL, int botC, int botR);
    
    
    int main(void)
    {
       int input;
       int topL, topC, topR;
       int cenL,cenC, cenR;
       int botL, botC, botR;
    
    
       if (getInput(&input))
          display(topL, topC, topR, cenL, cenC, cenR, botL, botC, botR);
    
    
       return 0;
    }
    
    
    bool getInput(int* pInput)
    {
       bool success = false;
       int scanRes;
    
    
       printf("\nName: Danielle Evans");
       printf("\nPlease enter a nonnegative integer seed: ");
       scanRes = scanf("%d", pInput);
    
    
       if (scanRes == 1)
       {
          int input = *pInput;
    
    
          if (input < 0)
          {
              printf("Not nonnegative. ");
          }
          else
          {
             if(scanRes == 0)
                printf("\nRan out of data. ");
          }
        }
       else if (scanRes == 0)
          printf("\nNon-int data. ");
    
    
       if(!success)
          printf("\n\n");
    
    
       return success;
    }
    
    
    
    
    int genBoard(int input)
    {
       int topL = rand() % 2;
       int topC = rand() % 2;
       int topR = rand() % 2;
       int cenL = rand() % 2;
       int cenC = rand() % 2;
       int cenR = rand() % 2;
       int botL = rand() % 2;
       int botC = rand() % 2;
       int botR = rand() % 2;
    
    
       int x, o;
       x = 0;
       o = 1;
    
    
    }
    
    
    void printBoard(int topL, int topC, int topR, int cenL,
                    int cenC, int cenR, int botL, int botC, int botR)
    {
       printf(" %c | %c | %c ", topL, topC ,topR);
       printf(" %c | %c | %c ", cenL, cenC, cenR);
       printf(" %c | %c | %c ", botL, botC ,botR);
    }
    
    
    bool ifValid(int topL, int topC, int topR, int cenL,
                 int cenC, int cenR, int botL, int botC, int botR)
    {
       int total= topL + topC + topR + cenL + cenC+ cenR + botL + botC + botR;
       if(total !=4 && total !=5)
          return false;
    }
    
    
    int winner(int topL, int topC, int topR, int cenL, int cenC,
                 int cenR, int botL, int botC, int botR)
    
    
    {
    
    
    }
    
    
    void display(int input, int topRow, int midRow, int bottomRow, int lCol,
                 int midCol, int rCol, int lDiag, int rDiag)
    {
    
    
    }
    I'm not sure how the user is supposed to get the "Ran out of Data" reply though.

  4. #34
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,907
    Quote Originally Posted by Dannibe7 View Post
    I'm not sure how the user is supposed to get the "Ran out of Data" reply though.
    Me neither. You'll have to check with your teacher or TA.

    Code:
          if (input < 0)
          {
              printf("Not nonnegative. ");
          }
          else
          {
             if(scanRes == 0)
                printf("\nRan out of data. ");
          }
    That can never be true. The only way to get to that if statement is if scanRes is 1. Actually, as a temporary solution to get you moving, just do the following:
    Code:
    if (input < 0)
    {
        printf("Not nonnegative.");
    }
    else
    {
        success = true;  // they entered a number and it's nonnegative, sounds like a winner to me
    }
    Regarding your genBoard function, you need to ditch the x and o bit entirely. Also, each "square" on your board needs to be passed in as a pointer to genBoard, so that the changes can be "passed back" to your main function. Here's what the function signature should look like:
    Code:
    int genBoard(int *topL, int *topC, ..., int *botR)
    When you call it from main, you need to pass the address of the topR, etc variables so that genBoard can change them and have the changes be available in main.

    printBoard has it's own problems. Each square will have a value of 0 or 1. Those numbers correspond to the ASCII characters null and start of header. Those characters don't actually show up on the screen. You need to check each square. If it's a 0, print an 'O', if it's a 1, print an 'X'. You also need to print a new line between each row of your game board.

  5. #35
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by Dannibe7 View Post
    Well our next project is to take this project and to turn it into arrays.
    Well okay it seems that your instructor really wants to drill into you why we have arrays. I can somewhat understand that, but I still don't like it.
    Sure we learn by making mistakes, doing things the wrong way. But making you do it the wrong way in order to learn from that does not seem like the best approach. Especially for anyone in the class already understands right away why not using arrays would be silly.

    Good luck! I'll be happy to help you turn in into arrays when you're ready to start doing that project.
    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"

  6. #36
    Registered User
    Join Date
    Sep 2011
    Posts
    61
    So would my printboard be something like this?
    Code:
    if ( *pTopL == 0)
          {
             printf("\nX");
          }
          else printf("\nO");
    and then repeated for all 9 spaces?

    Thanks

  7. #37
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,907
    Quote Originally Posted by Dannibe7 View Post
    So would my printboard be something like this?
    Code:
    if ( *pTopL == 0)
          {
             printf("\nX");
          }
          else printf("\nO");
    and then repeated for all 9 spaces?

    Thanks
    Almost, but you don't want that \n before each character. Only between each row. I would write another function that returns an 'X' or an 'O' based on whether a square is 0 or 1, and call that from my main print function:
    Code:
    char outputChar(int square)
    {
        if (square)
            return 'X';
        else
            return 'O';
    }
    
    void printBoard(int topL, int topC, int topR, ..., int botR)  // no need for pointers here
    {
        printf(" %c | %c | %c \n", outputChar(topL), outputChar(topC), ...);
        // same for middle and bottom rows
    }

  8. #38
    Registered User
    Join Date
    Sep 2011
    Posts
    61
    our program can only be the seven functions that I already have set up. is there a way to do all that all inside the print function?

  9. #39
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,907
    What a ridiculous set of constraints. You can tell your professor I think he/she is a complete moron.

    You could use the conditional operator ( ?: ), but that's probably outlawed too.
    Code:
    printf(" %c | %c | %c \n", topL ? 'X' : 'O', topC ? 'X' : 'O', ...);
    Otherwise, you have to do something similar to what you suggested with an if/else for each square.
    Last edited by anduril462; 10-22-2011 at 08:10 PM.

  10. #40
    Registered User
    Join Date
    Sep 2011
    Posts
    61
    Okay so this is what I have now, I don't know where to go from here and I am sure that there are mistakes. I just don't know where. This is so confusing.


    Code:
    #include <stdio.h>
    #include <stdbool.h>
    
    
    bool getInput(int* pInput);
    int genBoard(int* pTopL, int* pTopC, int* pTopR, int* pCenL,
                 int* pCenC, int* pCenR, int* pBotL, int* pBotC, int* pBotR);
    void printBoard(int* pTopL, int* pTopC, int* pTopR, int* pCenL,
                    int* pCenC, int* pCenR, int* pBotL, int* pBotC, int* pBotR);
    bool ifValid(int topL, int topC, int topR, int cenL, int cenC,
                 int cenR, int botL, int botC, int botR);
    int winner(int topL, int topC, int topR, int cenL, int cenC,
               int cenR, int botL, int botC, int botR);
    void display(int topL, int topC, int topR, int cenL, int cenC,
                 int cenR, int botL, int botC, int botR);
    
    
    int main(void)
    {
       int input;
       int topL, topC, topR;
       int cenL,cenC, cenR;
       int botL, botC, botR;
    
    
       if (getInput(&input))
          display(topL, topC, topR, cenL, cenC, cenR, botL, botC, botR);
       return 0;
    }
    
    bool getInput(int* pInput)
    {
       bool success = false;
       int scanRes;
    
    
       printf("\nName: Danielle Evans");
       printf("\nPlease enter a nonnegative integer seed: ");
       scanRes = scanf("%d", pInput);
    
    
       if (scanRes == 1)
       {
          int input = *pInput;
    
    
          if (input < 0)
          {
              printf("Not nonnegative. ");
          }
          else
          {
             success = true;
          }
        }
       else if (scanRes == 0)
          printf("\nNon-int data. ");
    
    
       if(!success)
          printf("\n\n");
    
    
       return success;
    }
    
    int genBoard(int* pTopL, int* pTopC, int* pTopR, int* pCenL,
                 int* pCenC, int* pCenR, int* pBotL, int* pBotC, int* pBotR)
    {
       *pTopL = rand() % 2;
       *pTopC = rand() % 2;
       *pTopR = rand() % 2;
       *pCenL = rand() % 2;
       *pCenC = rand() % 2;
       *pCenR = rand() % 2;
       *pBotL = rand() % 2;
       *pBotC = rand() % 2;
       *pBotR = rand() % 2;
    }
    
    void printBoard(int* pTopL, int* pTopC, int* pTopR, int* pCenL,
                    int* pCenC, int* pCenR, int* pBotL, int* pBotC, int* pBotR)
    {
       if ( *pTopL == 0)
          {
             printf("X");
          }
          else printf("O");
    
    
       if ( *pTopC == 0)
          {
             printf("X");
          }
          else printf("O");
    
    
       if ( *pTopR == 0)
          {
             printf("X");
          }
          else printf("O");
    
    
       if ( *pCenL == 0)
          {
             printf("X");
          }
          else printf("O");
    
    
       if ( *pCenC == 0)
          {
             printf("X");
          }
          else printf("O");
    if ( *pCenR == 0)
          {
             printf("X");
          }
          else printf("O");
    
    
       if ( *pBotL == 0)
          {
             printf("X");
          }
          else printf("O");
    
    
       if ( *pBotC == 0)
          {
             printf("X");
          }
          else printf("O");
    
    
       if ( *pBotR == 0)
          {
             printf("X");
          }
          else printf("O");
    
    
       printf(" %c | %c | %c \n", *pTopL, *pTopC, *pTopR);
       printf(" %c | %c | %c \n", *pCenL, *pCenC, *pCenR);
       printf(" %c | %c | %c \n", *pBotL, *pBotC, *pBotR);
    }
    
    
    bool ifValid(int topL, int topC, int topR, int cenL,
                 int cenC, int cenR, int botL, int botC, int botR)
    {
       int total= topL + topC + topR + cenL + cenC+ cenR + botL + botC + botR;
    
    
       if(total !=4 && total !=5)
          return false;
    }
    int winner(int topL, int topC, int topR, int cenL, int cenC,
               int cenR, int botL, int botC, int botR)
    
    
    {
    
    
    }
    
    
    void display(int input, int topRow, int midRow, int bottomRow, int lCol,
                 int midCol, int rCol, int lDiag, int rDiag)
    {
    
    
    }

  11. #41
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Teacher = Moron ... yep.

    One compromise would be to make the square variables chars and actually put X or O in them when randomizing.

    Code:
    square1 = (rand() % 2) ? 'X' : 'O';
    // etc.
    Then all you have to do is print them as character variables like this...

    Code:
    printf(" %c | %c | %c ", square1, square2, square3
    // etc.
    After this screwball assinment the OP will just love loops and arrays...
    Last edited by CommonTater; 10-22-2011 at 08:37 PM.

  12. #42
    Registered User
    Join Date
    Sep 2011
    Posts
    61
    So to determine a winner would I just have to do a lot of if statements?

  13. #43
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Dannibe7 View Post
    So to determine a winner would I just have to do a lot of if statements?
    A LOT of if() statements.

  14. #44
    Registered User
    Join Date
    Sep 2011
    Posts
    61
    Okay so how can I set the topL topC and topR to the topRow?
    That would be the winning pattern of topRow. I guess this would be going into the int winner() function

  15. #45
    Registered User
    Join Date
    Oct 2011
    Posts
    28
    Interesting.

Popular pages Recent additions subscribe to a feed