![]() Topic awaiting preservation: Optimizing Tic-Tac-Toe logic (Page 1 of 1) |
|
---|---|
Paranoid (IV) Inmate From: schillmania.com |
![]() 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 |
Paranoid (IV) Inmate From: USA |
![]() 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 |
![]() Here is a tic tac toe in c++... |
Maniac (V) Mad Scientist From: 100101010011 <-- right about here |
![]() 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 |
![]() 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 |
![]() 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: |
![]() I may be coming out of left field here, but with the whole bitwise testing... |
Paranoid (IV) Inmate From: USA |
![]() quote:
|
Paranoid (IV) Inmate From: schillmania.com |
![]() 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 |
![]() 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 |
![]() 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. |