Created
March 10, 2012 06:53
-
-
Save Fluxx/2010621 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 connected(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(connected(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