Thread: Tell me what i did wrong!

  1. #16
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You haven't changed the code at all since you posted it? Can you post the simplest inputs you give that will make the error happen every time?

  2. #17
    Registered User Terran's Avatar
    Join Date
    May 2008
    Location
    Nashua, NH
    Posts
    100
    of course i've changed it. The debugger narrowed it down to these two functions when the comp makes a move (as far as i can tell.)

    I've changed int theBoard[3][3] to char theBoard[9]

    Code:
            if(theBoard[4]=='X'){
                int ranCho;
                do {
                    ranCho=((rand() % (9)) + 1);
                } while(theBoard[ranCho-1]=='X' && theBoard[ranCho-1]=='O' && ranCho != 0);
                return ranCho;
            }
            if(theBoard[4]=='O'){
                int ranCho;
                do {
                    ranCho=((rand() % (9)) + 1);
                } while(theBoard[ranCho-1]=='X' && theBoard[ranCho-1]=='O' && ranCho != 0);
                return ranCho;
            }
    These are at the bottom of the move logic to ad a little randomness if the game isn't about to be won. But it seems to be making illegal moves.

  3. #18
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    theBoard[ranCho-1]=='X' && theBoard[ranCho-1]=='O' && ranCho != 0
    The third part of this condition seems trivially true. Not that it would affect the outcome of the code.

    The first and the second part, however, are mutually exclusive.

    Also, ugly code duplication. Merge the conditions of the two ifs, the code in them is the same.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #19
    Registered User Terran's Avatar
    Join Date
    May 2008
    Location
    Nashua, NH
    Posts
    100
    Well i did the code duplication to try and see if i could force the correct outcome, but i can't. So right now i'm piping the computers move choice through the function that checks to see if moves are legal. and that's working.

  5. #20
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> of course i've changed it.
    I asked you to post the pertinent code, you said the whole code was posted above. But if the code has changed from what was posted above, then the above code isn't as relevant, right?

    It's hard enough to read a bunch of code and figure out your error, but if the code is different than what is posted it makes it almost impossible.

    Thank you for posting the updated code for spot 5 (I assume that's what that is), but what about the spot 6 code that fails?

  6. #21
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    I quote myself:
    The first and the second part, however, are mutually exclusive.
    This means that the loop will NEVER repeat, and thus ALWAYS accept the first random value, whether it's valid or not.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #22
    Registered User Terran's Avatar
    Join Date
    May 2008
    Location
    Nashua, NH
    Posts
    100
    Quote Originally Posted by CornedBee View Post
    I quote myself:
    The first and the second part, however, are mutually exclusive.
    This means that the loop will NEVER repeat, and thus ALWAYS accept the first random value, whether it's valid or not.

    But i'm, confused shouldn't it loop until ALL the conditions are met?

  8. #23
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    It loops while the statement is true. So it loops while theBoard[ranCho-1] is 'X' and it is 'O'. Since it cannot be both, that statement will never be true. Since that statement is never true, the loop will always only run once.

  9. #24
    Registered User Terran's Avatar
    Join Date
    May 2008
    Location
    Nashua, NH
    Posts
    100
    ahh gotcha, duh.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 07-15-2004, 03:30 PM
  2. Debugging-Looking in the wrong places
    By JaWiB in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-03-2003, 10:50 PM
  3. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 12:40 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 12:01 PM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM