0% found this document useful (0 votes)
7 views3 pages

Python Turtle Tic Tac Toe Game

Uploaded by

kushan4589
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views3 pages

Python Turtle Tic Tac Toe Game

Uploaded by

kushan4589
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

screen = turtle.

Screen()
[Link](800,800)
[Link]("Tic Tac Toe - [Link]")
[Link](-5,-5,5,5)
[Link]('yellow')
[Link](0,0)
[Link]()

def draw_board():
[Link]('green')
[Link](10)
[Link]()
[Link](-3,-1)
[Link](0)
[Link]()
[Link](6)
[Link]()
[Link](-3,1)
[Link](0)
[Link]()
[Link](6)
[Link]()
[Link](-1,-3)
[Link](90)
[Link]()
[Link](6)
[Link]()
[Link](1,-3)
[Link](90)
[Link]()
[Link](6)

def draw_circle(x,y):
[Link]()
[Link](x,y-0.75)
[Link](0)
[Link]('red')
[Link]()
[Link](0.75, steps=100)

def draw_x(x,y):
[Link]('blue')
[Link]()
[Link](x-0.75,y-0.75)
[Link]()
[Link](x+0.75,y+0.75)
[Link]()
[Link](x-0.75,y+0.75)
[Link]()
[Link](x+0.75,y-0.75)

def draw_piece(i,j,p):
if p==0: return
x,y = 2*(j-1), -2*(i-1)
if p==1:
draw_x(x,y)
else:
draw_circle(x,y)

def draw(b):
draw_board()
for i in range(3):
for j in range(3):
draw_piece(i,j,b[i][j])
[Link]()

# return 1 if player 1 wins, 2 if player 2 wins, 3 if tie, 0 if game is not over


def gameover(b):
if b[0][0]>0 and b[0][0] == b[0][1] and b[0][1] == b[0][2]: return b[0][0]
if b[1][0]>0 and b[1][0] == b[1][1] and b[1][1] == b[1][2]: return b[1][0]
if b[2][0]>0 and b[2][0] == b[2][1] and b[2][1] == b[2][2]: return b[2][0]
if b[0][0]>0 and b[0][0] == b[1][0] and b[1][0] == b[2][0]: return b[0][0]
if b[0][1]>0 and b[0][1] == b[1][1] and b[1][1] == b[2][1]: return b[0][1]
if b[0][2]>0 and b[0][2] == b[1][2] and b[1][2] == b[2][2]: return b[0][2]
if b[0][0]>0 and b[0][0] == b[1][1] and b[1][1] == b[2][2]: return b[0][0]
if b[2][0]>0 and b[2][0] == b[1][1] and b[1][1] == b[0][2]: return b[2][0]
p=0
for i in range(3):
for j in range(3):
p += (1 if b[i][j] > 0 else 0)
if p==9: return 3
else: return 0

def play(x,y):
global turn
i = 3-int(y+5)//2
j = int(x+5)//2 - 1
if i>2 or j>2 or i<0 or j<0 or b[i][j]!=0: return
if turn == 'x': b[i][j], turn = 1, 'o'
else: b[i][j], turn = 2, 'x'
draw(b)
r = gameover(b)
if r==1:
[Link]("Game over!","X won!")
elif r==2:
[Link]("Game over!","O won!")
elif r==3:
[Link]("Game over!", "Tie!")

b = [ [ 0,0,0 ], [ 0,0,0 ], [ 0,0,0 ] ]


draw(b)
turn = 'x'
[Link](play)
[Link]()

You might also like