Well, look at the cell #'s
You're either checking n+1, n+3, or n+2
Name:
Anonymous2010-02-21 15:03
If that's the total size of your board, a brute force like that is really best your bet. It's fast, and it's not difficult to understand. Anything else would be unnecessary bloat.
Name:
Anonymous2010-02-21 15:17
You could rewrite into a 2D array, and then it's a case of two fors. But I doubt it's worth it.
for (rows)
while (col++ == col);
if (col == cols) you win;
for (cols)
while (row++ == row);
if (row == rows) you win;
Similar things for diagonal
Name:
52010-02-21 16:00
in fact, OP, your worst case is 13 comparisons in this occasion:
1 0 0
0 1 0
0 0 1
Name:
Anonymous2010-02-21 16:03
Store game state as bit pattern, use lookup tables. Exploit symmetry to trade speed vs. memory use.
Name:
Anonymous2010-02-21 16:03
>>7
and by that I meant
1 1 0
1 1 0
0 0 1
and that's 15 comparisons
Name:
Anonymous2010-02-21 16:04
Why not represent the whole board as a 16-bit integer and just AND it with predetermined values to test for whatever you want? It would certainly be an ENTERPRISE C SOLUTION.
Name:
Anonymous2010-02-21 16:09
>>10
Because that would be a total of 8 comparisons in the worst case instead of 6 up there. ENTERPRISE++
Name:
Anonymous2010-02-21 16:18
>>5
I still prefer >>1-san's version. His example is the easiest to read and maintain. If there's an error with the game logic -- for example, if one the array indices was incorrect -- I'd have to do far more work to figure out the mistake in your code than in his code. Meanwhile, I highly doubt your ENTERPRISE optimisation is going to turnkey the performance metric to realtime.
This is a case of premature optimisation. Just leave it as is, OP, until you can demonstrate that it's a bottleneck to your program. Which it definitely isn't.
Name:
Anonymous2010-02-21 16:22
>>12
If I was looking for readability I think I'd use 2 fors like >>6 did, because it'd easier to transit from "1" to "-1" when checking (it would probably be better with 1 and 2 anyway)
Name:
Anonymous2010-02-21 16:34
I've seen a very elegant solution using two 8-bit bytes, one for each player (one of the squares was implicit or something, can't remember how that was done), and a rather clever mapping of the tiles to bits allowed simple integer arithmetic to determine if the player had won. It was around 6-8 instructions but you'd never think you'd be able to come up with something as beautiful as that once you understood how it works.
Name:
Anonymous2010-02-21 17:59
>>10
That's 7 bits wasted for a standard board. Unacceptable.
Name:
Anonymous2010-02-21 18:22
/* fix this shit because OP is an idiot */
for(int i = 0; i < 9; ++i) board[i] = ((1 - board[i] ^ 1) + 1) / 2
>>16 >>18
You don't have to fix that, just comment + assume it. I said e.g. -1 and 1 for the players and 0 for blank.
>>n
Thanks, gave me some ideas.
The original code (not mine btw) was simply too inefficient for REAL PROGRAMMERS. Just think about checking for a win of one player, then checking for a win of the other player, then checking for a draw, and all that every single round. Holy Sussman! This universe holds no REAL SOLUTIONS for me, goodbye. *dissolves into mist while giving God a one-finger salute*