Created
March 24, 2011 18:23
-
-
Save samrat/885572 to your computer and use it in GitHub Desktop.
tic tac toe
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
grid= [['','',''], | |
['','',''], | |
['','','']] | |
def x(y,x): | |
'''Player X's turn | |
''' | |
if not grid[y][x]: | |
grid[y][x]='x' | |
if check_status(grid)!=None: | |
print "The winner is ", check_status(grid) | |
return grid | |
else: | |
return grid | |
def o(y,x): | |
'''Player O's turn | |
''' | |
if not grid[y][x]: | |
grid[y][x]='o' | |
if check_status(grid)!=None: | |
print "The winner is ", check_status(grid) | |
return grid | |
else: | |
return grid | |
def check_status(grid): | |
#if all the items on a column are same: | |
for x in range(3): | |
l=None | |
win=True | |
for y in range(3): | |
if not l: l=grid[y][x] #l has topmost item | |
elif l!=grid[y][x]: win=False | |
if win!=False and l!='': | |
winner=l | |
return winner | |
#check rows | |
for y in range(3): | |
s=None | |
win=True | |
for x in range(3): | |
if not s: s=grid[y][x] #s now has leftmost item | |
elif s!=grid[y][x]: win=False | |
if win!=False and s!='': | |
winner=s | |
return winner | |
#check diagonals | |
####top left to bottom right | |
y=0 | |
x=0 | |
if grid[y][x]==grid[y+1][x+1]==grid[y+2][x+2] and grid[y][x]!='': | |
return grid[y][ | |
####Top right to bottom left | |
y=2 | |
x=2 | |
if grid[y][x]==grid[y-1][x-1]==grid[y-2][x-2] and grid[y][x]!='': | |
return grid[y][x] | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment