Skip to content

Instantly share code, notes, and snippets.

@sahildua2305
Last active September 17, 2015 07:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sahildua2305/5686ebbd0051424e1762 to your computer and use it in GitHub Desktop.
Save sahildua2305/5686ebbd0051424e1762 to your computer and use it in GitHub Desktop.
sds
# '-' for empty position
# 'O' for marking O is there
# 'X' for marking X has made a turn there
board = "---------"
# Assuming I already have a dictionary `board_states` for lookup
def nextMove(curr_move):
board[curr_move] = 'X'
# simply lookup the value of next move from the dictionary
index = board_states[board]
board[index] = 'O'
return index
# Optimal strategy
# To reduce the number of states to be stores in the dict, one way is to remove the duplicates.
# Initially let's say we have stores only the unique board states.
# Now for lookup, we will check for 4 possible rotations of the current board state and check whichever state is available in the dict and return the corresponding next move.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment