Skip to content

Instantly share code, notes, and snippets.

@rohit-jamuar
Created July 22, 2014 03:04
Show Gist options
  • Save rohit-jamuar/ce01a0ef730fa379af0f to your computer and use it in GitHub Desktop.
Save rohit-jamuar/ce01a0ef730fa379af0f to your computer and use it in GitHub Desktop.
Tick-Tack-Toe Verifier
def tick_tack_toe_verifier(game_result):
'''
Input : A nXn grid ('game_result')
Output : Checks the status of game in 'game_result'. If there is a winner
it return 'X' or 'O', else in case of a draw, it returns 'D'.
'''
for r in range(len(game_result)):
x, o = 0, 0
for c in range(len(game_result)):
if game_result[r][c] == 'X':
x += 1
if game_result[r][c] == 'O':
o += 1
if x == len(game_result):
return 'X'
elif o == len(game_result):
return 'O'
for c in range(len(game_result)):
x, o = 0, 0
for r in range(len(game_result)):
if game_result[r][c] == 'X':
x += 1
if game_result[r][c] == 'O':
o += 1
if x == len(game_result):
return 'X'
elif o == len(game_result):
return 'O'
x, o = 0, 0
for r in range(len(game_result)):
for c in range(len(game_result)):
if r == c:
if game_result[r][c] == 'X':
x += 1
if game_result[r][c] == 'O':
o += 1
break
if x == len(game_result):
return 'X'
elif o == len(game_result):
return 'O'
x, o = 0, 0
for r in range(len(game_result)):
for c in range(len(game_result)-1,-1,-1):
if r+c == len(game_result)-1:
if game_result[r][c] == 'X':
x += 1
if game_result[r][c] == 'O':
o += 1
break
if x == len(game_result):
return 'X'
elif o == len(game_result):
return 'O'
return 'D'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment