Created
July 22, 2014 03:04
-
-
Save rohit-jamuar/ce01a0ef730fa379af0f to your computer and use it in GitHub Desktop.
Tick-Tack-Toe Verifier
This file contains 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
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