Thread: Tic Tac Toe

  1. #46
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    So, you can's use arrays, loops and addional functions, but can you use macros? This can be compressed:
    Code:
    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);
    }
    Into this:
    Code:
    #define PRINT_BOARD_HELPER(var)  if ( *var== 0)\
          {\
             printf("X");\
          }\
          else printf("O");
    void printBoard(int* pTopL, int* pTopC, int* pTopR, int* pCenL,
                    int* pCenC, int* pCenR, int* pBotL, int* pBotC, int* pBotR)
    {
     
       PRINT_BOARD_HELPER(pTopL)
       PRINT_BOARD_HELPER(pTopC)
       PRINT_BOARD_HELPER(pTopR)
       PRINT_BOARD_HELPER(pCenL)
       PRINT_BOARD_HELPER(pCenC)
       PRINT_BOARD_HELPER(pCenR)
       PRINT_BOARD_HELPER(pBotL)
       PRINT_BOARD_HELPER(pBotC)
       PRINT_BOARD_HELPER(pBotR)
    
       printf(" %c | %c | %c \n", *pTopL, *pTopC, *pTopR);
       printf(" %c | %c | %c \n", *pCenL, *pCenC, *pCenR);
       printf(" %c | %c | %c \n", *pBotL, *pBotC, *pBotR);
    }
    Better yes?


    From a broader perspective, you should avoid repeat code. The first remedy is a loop. If that's not applicable, try a function. As a last resort consider a macro.
    Last edited by King Mir; 10-23-2011 at 02:22 PM.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  2. #47
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by Dannibe7 View Post
    So to determine a winner would I just have to do a lot of if statements?
    Quote Originally Posted by CommonTater View Post
    A LOT of if() statements.
    Well there are only 8 ways to win.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  3. #48
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Why are you using pointers on functions that never change their arguments? Also, what about empty squares? You are assuming that these boards have a full nine moves plotted?


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

  4. #49
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,736
    Quote Originally Posted by quzah View Post
    Why are you using pointers on functions that never change their arguments? Also, what about empty squares? You are assuming that these boards have a full nine moves plotted?


    Quzah.
    Yeah, it says so in his first post:
    1) Generate the board randomly
    2) Evaluate if it's valid
    3) Find the winner

    Although, I don't understand why would someone want to do such a thing...
    Devoted my life to programming...

  5. #50
    Registered User
    Join Date
    Sep 2011
    Posts
    61
    no players or moves
    Last edited by Dannibe7; 10-23-2011 at 04:46 PM.

  6. #51
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well that explains one problem. But since you have randomly generated boards, how do you know who won first?
    Code:
    xox
    oox
    xox
    Also, you still don't need to be using pointers for a function that isn't trying to change any values.


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

  7. #52
    Registered User
    Join Date
    Sep 2011
    Posts
    61
    well if there are two sets of winning x's or o's I have to make make it say "too many winning patterns." Also if there are too many x's or o's (over5) then i have to make it say "too many x's or o's."

  8. #53
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well be sure you account for this:
    Code:
    *xx
    xoo
    xoo
    x's final valid move is * which gives him 2 wins.


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

  9. #54
    Registered User
    Join Date
    Sep 2011
    Posts
    61
    So the only pointer I need would be in the get input function? I should remove all the other pointers?

  10. #55
    Registered User
    Join Date
    Sep 2011
    Posts
    61
    I think I am giving up.

  11. #56
    Registered User
    Join Date
    Sep 2011
    Posts
    61
    So for the winning function. I have been trying to say that if topL, topC, and topR are all X, then print Congratulations, X! You are the winner. The winning pattern is top row. But I can't figure out how to do that.

    Any suggestions?

  12. #57
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You've just explained how.
    Code:
    if topL == X and topC == X and topR == X
        print yay you win X across the top row

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

  13. #58
    Registered User
    Join Date
    Sep 2011
    Posts
    61
    that is what I have been trying. I just get error messages
    is this what I should be tyring?
    Code:
    if(topL == X && topC ==X && topR ==X)
       printf("this stuff")

  14. #59
    Registered User
    Join Date
    Sep 2011
    Posts
    61
    why is my print board function not printing? Can anyone tell?

  15. #60
    Registered User
    Join Date
    Sep 2011
    Posts
    61
    I have got my code to this point. I cant get it to print the board. Can anyone help?
    Thanks
    Code:
    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);
    
    
       if(topL == 1)
          printf("X");
       if(topL == 0)
          printf("O");
    
    
       if(topC == 1)
          printf("X");
       if(topC == 0)
          printf("O");
    
    
       if(topR == 1)
          printf("X");
       if(topR == 0)
          printf("O");
    
    
       if(cenL == 1)
          printf("X");
       if(cenL == 0)
          printf("O");
    
    
       if(cenC == 1)
          printf("X");
       if(cenC == 0)
          printf("O");
     if(cenR == 1)
          printf("X");
       if(cenR == 0)
          printf("O");
    
    
       if(botL == 1)
          printf("X");
       if(botL == 0)
          printf("O");
    
    
       if(botC == 1)
          printf("X");
       if(botC == 0)
          printf("O");
    
    
       if(botR == 1)
          printf("X");
       if(botR == 0)
          printf("O");
    
    
    }
    
    
    void printBoard(int topL, int topC, int topR, int cenL, int cenC,
               int cenR, int botL, int botC, int botR)
    {
       printf(" %c | %c | %c \n", topL, topC, topR);
       printf(" %c | %c | %c \n", cenL, cenC, cenR);
       printf(" %c | %c | %c \n", 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)
    {
       int win;
       int topRow, midRow, botRow;
       int lCol, midCol, rCol;
       int rDiag, lDiag;
    
    
    
    
       if(topL == topC == topR)
          {
             win == topRow;
          }
             printf("\nThe winning pattern is the top row.");
       if(cenL == cenC == cenR)
          {
             win == midRow;
          }
       if(botL == botC == botR)
          {
             win == botRow;
          }
       if(topL == cenL == botL)
          {
             win == lCol;
          }
       if(topC == cenC == botC)
          {
             win == midCol;
     }
       if(topL == cenC == botR)
          {
             win == rDiag;
          }
       if(botL == cenC == topR)
          {
             win == lDiag;
          }
       return 1;
    }
    
    
    void display(int input, int topRow, int midRow, int bottomRow, int lCol,
                 int midCol, int rCol, int lDiag, int rDiag)
    {
       if(topRow == 'X')
          printf("Congratulations, X! You are the winner.");
       if(topRow == 'O')
          printf("Congratulations, O! You are the winner.");
    }

Popular pages Recent additions subscribe to a feed