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?
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?
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]
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.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; }
The third part of this condition seems trivially true. Not that it would affect the outcome of the code.theBoard[ranCho-1]=='X' && theBoard[ranCho-1]=='O' && ranCho != 0
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
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.
>> 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?
I quote myself:
This means that the loop will NEVER repeat, and thus ALWAYS accept the first random value, whether it's valid or not.The first and the second part, however, are mutually exclusive.
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
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.