Topic awaiting preservation: Optimizing Tic-Tac-Toe logic (Page 1 of 1) |
|
---|---|
Paranoid (IV) Inmate From: schillmania.com |
posted 10-20-2004 02:16
A friend of mine started a Comp.Sci program (I think) this year, and in one of his classes they have to recreate Tic-Tac-Toe in python. I'm going to assume of course everyone knows how the game is played (three in a row on a 3x3 grid.) code: var map = [ After I did this of course, I figured some of the in-house code gurus might be able to show me up with some crazy two-liner that does the same thing ..So I'd be interested in hearing other peoples' thoughts. |
Paranoid (IV) Inmate From: USA |
posted 10-20-2004 05:26
I think the best way to implement this would be to represent the board with a bitstring of length 9, with all squares taken by the player to (let's assume X) to be 1, and all other spaces 0. Then, each solution will also be a bitstring of length 9, with 1's where the proper spaces are to complete a row. And if (board & solution == solution) then that solution fufills the requirements, and the game is won. Note the bitwise AND, not a logical AND. |
Maniac (V) Inmate From: Eagleshieldsbay, Sweden |
posted 10-20-2004 15:33
Here is a tic tac toe in c++... C++ Help |
Maniac (V) Mad Scientist From: 100101010011 <-- right about here |
posted 10-20-2004 19:47
The only problem with the bitshifting method is there are 3 states for each square, blank, O, and X. But that was the first thing I thought about too . |
Paranoid (IV) Inmate From: France |
posted 10-20-2004 20:24
bitdamaged: If you look closely at the source provided by Scott, you'll see that he starts from a fulfilled map, thus each cell can be encoded on a single bit. code: for( var i=0; !(result=win(isWin[i])) && i<isWin.length; i++ ); And of course, if you don't fear the obfuscation, you can also remove the win() function and place its body directly the for loop, which gives: code: for( var i=0; !(result=((s=map[isWin[i][0]]+map[isWin[i][1]]+map[isWin[i][2]])=='XXX'||s=='OOO')) && i<isWin.length; i++ ); And again you could go further and include the declaration of the isWin array in the for itself, which gives : |
Maniac (V) Mad Scientist From: 100101010011 <-- right about here |
posted 10-20-2004 21:16
True but in a real tic-tac-toe situation/program you wouldn't start with a fulfilled grid. You'd test after each move. |
Bipolar (III) Inmate From: |
posted 10-20-2004 22:21
I may be coming out of left field here, but with the whole bitwise testing... |
Paranoid (IV) Inmate From: USA |
posted 10-21-2004 01:19
quote:
|
Paranoid (IV) Inmate From: schillmania.com |
posted 10-21-2004 02:38
quote: Agreed on the latter point. The algorithm is what I was wondering about; ie. finding a pattern that can be exploited to efficiently determine the win cases. |
Paranoid (IV) Mad Scientist with Finglongers From: Germany |
posted 10-21-2004 10:28
you could probably not store some of the win cases by representing them as bit shifted variants of other win cases. |
Paranoid (IV) Inmate From: France |
posted 10-21-2004 10:56
Actually the solution evoked by mobrul sounds really good. Using one bit-field variable per player to store the positions they hold. Mix that with the binary AND win case check method proposed by Iron Wallaby to test if the bit-field of the current player matches a the bit-field of a win case and I think we've got a good candidate here. |