Checking for a Winner - Tic Tac Toe [SOLVED]

Started by
11 comments, last by Sandman 18 years, 10 months ago
Quote:Original post by villiageidiot
Success!

Thanks for the help you all...I cleaned up the code a little bit using for loops to check wins across the board and vertically, and two if statements to check the diagonals and ... it works!! I'm so excited...the hours finally paid off.


Have you tested a release build yet? I have a suspicion it may do strange things.

You're still using arrays as though they are 1 based, and while you may be able to get away with it enough for it to appear to work (since you're doing so consistently) you are in fact writing to areas of memory which don't strictly belong to you, which can cause weird bugs and crashes.

Debug builds have buffers at the end of arrays to catch overwriting of this nature - run it in a debugger and you'll probably get all sorts of runtime errors cropping up talking about damage after memory blocks. Release builds aren't so forgiving though, and you can get some really strange bugs with this sort of error.
Advertisement
Quote:Original post by Sandman
Have you tested a release build yet? I have a suspicion it may do strange things.

You're still using arrays as though they are 1 based, and while you may be able to get away with it enough for it to appear to work (since you're doing so consistently) you are in fact writing to areas of memory which don't strictly belong to you, which can cause weird bugs and crashes.

Debug builds have buffers at the end of arrays to catch overwriting of this nature - run it in a debugger and you'll probably get all sorts of runtime errors cropping up talking about damage after memory blocks. Release builds aren't so forgiving though, and you can get some really strange bugs with this sort of error.


Everything seems to work fine, but no I haven't tested a release build yet. I see what you are saying though...so in other words if I am going to continue using arrays as 1 based, change my array to char board[4][4]? Or use the arrays as they are originally intended with a base of 0 and leave the board as char board[3][3]. Would this fix the problem?
Quote:Original post by villiageidiot
so in other words if I am going to continue using arrays as 1 based, change my array to char board[4][4]? Or use the arrays as they are originally intended with a base of 0 and leave the board as char board[3][3]. Would this fix the problem?


Either will work, although the latter solution is definitely preferred. Better to get into the habit of addressing the first element properly than to cheat by allocating a bigger array than you need.

This topic is closed to new replies.

Advertisement