Skip to content

Instantly share code, notes, and snippets.

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

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

Select an option

Save Fluxx/2010617 to your computer and use it in GitHub Desktop.
import json
import sys
from pprint import pprint as pp
# io = open('board.json')
io = sys.stdin
board = json.loads(io.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