Skip to content

Instantly share code, notes, and snippets.

@Fluxx
Created March 10, 2012 06:51
Show Gist options
  • Select an option

  • Save Fluxx/2010619 to your computer and use it in GitHub Desktop.

Select an option

Save Fluxx/2010619 to your computer and use it in GitHub Desktop.
import json
import sys
board = json.loads(sys.stdin.read())
add_one = lambda x, y: x + y
subtract_one = lambda x, y: x - y
no_op = lambda x, y: x
transforms = (
(no_op, add_one), # horizontal
(add_one, no_op), # vertical
(add_one, subtract_one), # ul/lr
(subtract_one, subtract_one) # ll/ur
)
def transform(char, row, col, row_transform, col_transform):
try:
return all(
board[row_transform(row, i)][col_transform(col, i)] == char
for i in range(4)
)
except IndexError:
return False
for row in range(len(board)):
for col in range(len(board[0])):
for char in ('X', 'O'):
if any(transform(char, row, col, *t) for t in transforms):
print 'Winner: %s' % char
sys.exit(0)
print 'No winner.'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment