Tic Tac Toe Game Code in Python
Tic Tac Toe Game Code in Python
sc = tur.Screen()
sc.setup(800,800)
sc.title("Pythontpoint")
sc.setworldcoordinates(-5,-5,5,5)
sc.bgcolor('black')
sc.tracer(0,0)
tur.hideturtle()
def draw_board():
tur.pencolor('blue')
tur.pensize(10)
tur.up()
tur.goto(-3,-1)
tur.seth(0)
tur.down()
tur.fd(6)
tur.up()
tur.goto(-3,1)
tur.seth(0)
tur.down()
tur.fd(6)
tur.up()
tur.goto(-1,-3)
tur.seth(90)
tur.down()
tur.fd(6)
tur.up()
tur.goto(1,-3)
tur.seth(90)
tur.down()
tur.fd(6)
def draw_circle(x,y):
tur.up()
tur.goto(x,y-0.75)
tur.seth(0)
tur.color('red')
tur.down()
tur.circle(0.75, steps=100)
def draw_x(x,y):
tur.color('blue')
tur.up()
tur.goto(x-0.75,y-0.75)
tur.down()
tur.goto(x+0.75,y+0.75)
tur.up()
tur.goto(x-0.75,y+0.75)
tur.down()
tur.goto(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])
sc.update()
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:
sc.textinput("Game over!","X's won!")
elif r==2:
sc.textinput("Game over!","O's won!")
elif r==3:
sc.textinput("Game over!", "Tie!")