Thread: tic tac toe

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

    tic tac toe

    Here is my tic tac toe simulation game, is there any way to rewrite this code so that it isn't so large without using arrays? Any help is greatly appreciated.

    proj3.c
    Last edited by SaraDawkins; 03-04-2012 at 01:58 PM.

  2. #2
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    Yes, you should do two things to improve your code:

    - Dont use magic numbers, instead, use constant #defines for the win status
    - Use arrays for the board. Typically, you'll want a 3x3 array of 1-byte values.
    Code:
    char board[3][3];
    /* or */
    int i;
    char **board = malloc(3 * sizeof(char *));
    for (i = 0; i < 3; i++)
        board[i] = malloc(3 * sizeof(char));

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    28
    Thank you for the suggestions, however I have been specifically ordered not to use arrays. I am curious however as to what you mean by, "don't use magic numbers, use constant #defines for the win status." Can you please give me an example of what this means?

  4. #4
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    In your function findWin, all of the values for pXwin and pOwin are called "magic numbers". That means that they're random numbers with no meaning outside your code. To fix that, use #defines or enums:

    Code:
    enum win_codes {
        TOP_ROW, 
        MIDDLE_ROW, 
        BOTTOM_ROW,
    /* ..... */
        LEFT_DIAGONAL
    };
    
    /* ... */
    
    if (topR == 1 && topC == 1 && topL == 1) {
        *pXwin = TOP_ROW;
    }
    Or, you could just print the results immediately and combine findWin and printWin.

    --------

    > I have been specifically ordered not to use arrays
    Wtf? What kind of assignment is that? Can you use structs? Like this:

    Code:
    struct row {
        char y1, y2, y3;
    };
    
    struct board {
        struct row x1, x2, x3;
    };
    
    /* ... */
    
    struct board board; 
    board.x1.y1 = 'x';
    board.x2.y3 = 'o';
    /* ... etc */

  5. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    28
    Thanks for the clarification. Yeah, tell me about it... I'm not allowed to use structs either, it is frustrating.
    Last edited by SaraDawkins; 03-04-2012 at 02:34 PM.

  6. #6
    Registered User
    Join Date
    Oct 2011
    Posts
    28
    How would I use #defines instead of enums in that situation?

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    I suppose the answer they're looking for is the one using bits.

    For example, you can represent all the 'X' positions with
    int xboard = 0;
    ...
    xboard |= (1 << moveposition);

    where moveposition is from 0 to 8

    Checking for winning lines is really simple.
    Eg.
    if ( (xboard & 0x7) == 0x7 ) // first row
    You can test the whole board with 8 such statements.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    Oct 2011
    Posts
    28
    Quote Originally Posted by Salem View Post
    I suppose the answer they're looking for is the one using bits.

    For example, you can represent all the 'X' positions with
    int xboard = 0;
    ...
    xboard |= (1 << moveposition);

    where moveposition is from 0 to 8

    Checking for winning lines is really simple.
    Eg.
    if ( (xboard & 0x7) == 0x7 ) // first row
    You can test the whole board with 8 such statements.

    I'm a little confused as I've never seen this before, can you please go into a little more detail for me? I would greatly appreciate it. (I am new to coding)

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    I could, but it would be pretty much handing you the answer on a plate.

    And to bounce back an answer in less than 3 minutes shows some lack of initiative.

    > (I am new to coding)
    You've been here for 5 months, you should have come across bitwise operators by now.

    Now that you know you can represent a board in only 9 bits, spend some time drawing bit patterns out on paper until you figure it out.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    Registered User
    Join Date
    Oct 2011
    Posts
    28
    I wish I knew what this meant. Sorry if I've offended you somehow.

  11. #11
    Registered User
    Join Date
    Dec 2011
    Posts
    795

  12. #12
    Registered User
    Join Date
    Mar 2012
    Posts
    5
    Why are you assigning the spaces values of 0 and 1 and then changing to X and O?

    Just do this:

    *pTopL = (rand() % 2) ? 'X' : 'O';

    This will gen a rand num (0 or 1) and if it is 0 assign X, or if it is 1, assign O.

    There should be 4 X's or 5 x's.
    The ASCII value of X is 88 and the ASCII value of O is 79.

    Instead of testing for whether or not they all equal 4 or 5, just test

    if ((topL + topC + topR + midL + midC + midR + botL + botC + botR)
    > 756 )

    and

    if ((topL + topC + topR + midL + midC + midR + botL + botC + botR)
    < 747 )


    This saves space because you don't have to reassign the value and THEN test. You just assign once and test based on the ASCII values. Which would also means all of your Ints need to be Char variables.



    And dear lord all those Else If's. Just make a couple if else if's and do X or O is winner for each.
    Last edited by Ellondu; 03-18-2012 at 01:07 AM.

  13. #13
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by Salem View Post
    Now that you know you can represent a board in only 9 bits, spend some time drawing bit patterns out on paper until you figure it out.
    How? In tic-tac-toe, each cell can have three states: empty, circle and cross.
    So, I'd say that at least two 9 bit patterns will be needed.

  14. #14
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > How? In tic-tac-toe, each cell can have three states: empty, circle and cross.
    Now why do you suppose I named one of the variables xboard?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  15. #15
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by Salem View Post
    > How? In tic-tac-toe, each cell can have three states: empty, circle and cross.
    Now why do you suppose I named one of the variables xboard?
    Right.. I did not notice that.

Popular pages Recent additions subscribe to a feed