Computer Science Canada Tic Tac Toe |
Author: | metachief [ Wed Sep 16, 2009 9:25 pm ] | ||
Post subject: | Tic Tac Toe | ||
What can be done more efficiently?
|
Author: | Zren [ Wed Sep 16, 2009 10:27 pm ] | ||||
Post subject: | Re: Tic Tac Toe | ||||
Other than converting the CheckWin if statements into one long unreadable line. Though that would make it faster to type, and less readability. It's nicely done for a simple game of tic tac toe. Though I'm all about code shortening so we could make this:
Into this:
| can be replaced with or by the way. |
Author: | DemonWasp [ Wed Sep 16, 2009 11:56 pm ] |
Post subject: | RE:Tic Tac Toe |
In InitBoard: - Be aware that lower ( board ) and upper ( board ) will return only the length of the first dimension. While this code will work for square arrays (which is all you need), it won't work for anything non-square. In CheckForWin: - You don't need to check for both players, you just need to check that the parts of the array have the same value. If you compare (1,1) to (2,2) and find them to be equal and that (2,2) = (3,3), then you know that someone has won. You can then just use the value of any one of those squares to see who won. - You could also use for loops over the array to shorten the amount of code you'd have to write. In DrawWord: - With frequently-used resources like fonts, it's probably best to create them once, cache them, then free them only when you're really done with them. Right now, every time you DrawWord, Turing has to load the requested font before it can draw. Not bad though. |
Author: | A.J [ Thu Sep 17, 2009 7:44 am ] |
Post subject: | RE:Tic Tac Toe |
Good job, metachief Well, I would suggest that you store the tic tac toe board in a one dimensional array instead of a two dimensional array. So, for example, the position (1, 1). would become 1, and the position (1, 2) would become 2, etc... (something like this: ____________ | 1 | 2 | 3 | | 4 | 5 | 6 | | 7 | 8 | 9 | ____________ Using a simple hashing function to hash coordinates on the board to a single integer would be pretty easy (for example, in this case, given x and y, (x, y) on the board is y + 3*(x-1)). And, this can make it easier to work with. |