So I am trying to make a tic tac toe game where you can play against the computer. Because I do not know how to use AI for the computer so it can make a decision, I chose to have it use a random choice. Because it needs to make multiple choices for multiple turns, I want to figure out how to have the 'choice' variable loop, so it makes a different random choice each time the game loops. Here is the code.
import turtle
import random
# Define the screen
screen = turtle.Screen()
screen.setup(width=300, height=300)
# Define the turtle
t = turtle.Turtle()
t.speed(0)
error = "Move already made, you lost a turn"
moves = ["top left", "top mid", "top right", "mid left", "mid mid", "mid right", "bot left", "bot mid", "bot right"]
def top_left():
t.penup()
t.goto(-100, 100)
t.pendown()
makex()
def top_mid():
t.penup()
t.goto(0, 100)
t.pendown()
makex()
def top_right():
t.penup()
t.goto(100, 100)
t.pendown()
makex()
def mid_left():
t.penup()
t.goto(-100, 0)
t.pendown()
makex()
def mid_mid():
t.penup()
t.goto(0, 0)
t.pendown()
makex()
def mid_right():
t.penup()
t.goto(100, 0)
t.pendown()
makex()
def bot_left():
t.penup()
t.goto(-100, -100)
t.pendown()
makex()
def bot_mid():
t.penup()
t.goto(0, -100)
t.pendown()
makex()
def bot_right():
t.penup()
t.goto(100, -100)
t.pendown()
makex()
def pc_top_left():
t.penup()
t.goto(-100, 100)
t.pendown()
makeo()
def pc_top_mid():
t.penup()
t.goto(0, 100)
t.pendown()
makeo()
def pc_top_right():
t.penup()
t.goto(100, 100)
t.pendown()
makeo()
def pc_mid_left():
t.penup()
t.goto(-100, 0)
t.pendown()
makeo()
def pc_mid_mid():
t.penup()
t.goto(0, 0)
t.pendown()
makeo()
def pc_mid_right():
t.penup()
t.goto(100, 0)
t.pendown()
makeo()
def pc_bot_left():
t.penup()
t.goto(-100, -100)
t.pendown()
makex()
def pc_bot_mid():
t.penup()
t.goto(0, -100)
t.pendown()
makex()
def pc_bot_right():
t.penup()
t.goto(100, -100)
t.pendown()
makex()
def makex():
t.pendown()
t.left(45)
t.forward(30)
t.right(180)
t.pendown()
t.forward(60)
t.penup()
t.right(180)
t.forward(30)
t.right(90)
t.forward(30)
t.right(180)
t.pendown()
t.forward(60)
t.penup()
t.setheading(0)
def makeo():
t.penup()
t.right(90)
t.forward(30)
t.left(90)
t.pendown()
t.circle(30)
t.penup()
t.setheading(0)
def user(x, y):
print(x, y)
# Top row
if y <= 150 and y >= 50:
# Top left
if x >= -150 and x <= -50:
if "top left" in moves:
print('top left')
top_left()
moves.remove("top left")
else:
print(error)
# Top Mid
elif x >= -50 and x <= 50:
if "top mid" in moves:
print('top mid')
top_mid()
moves.remove("top mid")
else:
print(error)
# Top Right
elif x >= 50 and x <= 150:
if "top right" in moves:
print('top right')
top_right()
moves.remove('top right')
else:
print(error)
# Mid row
elif y <= 50 and y >= -50:
# Mid left
if x >= -150 and x <= -50:
if "mid left" in moves:
print('mid left')
mid_left()
moves.remove('mid left')
else:
print(error)
# Mid Mid
elif x >= -50 and x <= 50:
if 'mid mid' in moves:
print('mid mid')
mid_mid()
moves.remove('mid mid')
else:
print(error)
# Mid Right
elif x >= 50 and x <= 150:
if 'mid right' in moves:
print('mid right')
mid_right()
moves.remove('mid right')
else:
print(error)
# Bot row
elif y <= -50 and y >= -150:
# Bot left
if x >= -150 and x <= -50:
if 'bot left' in moves:
print('bot left')
bot_left()
moves.remove('bot left')
else:
print(error)
# Bot Mid
elif x >= -50 and x <= 50:
if 'bot mid' in moves:
print('bot mid')
bot_mid()
moves.remove('bot mid')
else:
print(error)
# Bot Right
elif x >= 50 and x <= 150:
if 'bot right' in moves:
print('bot right')
bot_right()
moves.remove('bot right')
else:
print(error)
print(moves)
def computer(choice):
if choice == 'top left':
pc_top_left()
moves.remove('top left')
elif choice == 'top mid':
pc_top_mid()
moves.remove('top mid')
elif choice == 'top right':
pc_top_right()
moves.remove('top right')
elif choice == 'mid left':
pc_mid_left()
moves.remove('mid left')
elif choice == 'mid mid':
pc_mid_mid()
moves.remove('mid mid')
elif choice == 'mid right':
pc_mid_right()
moves.remove('mid right')
elif choice == 'bot left':
pc_bot_left()
moves.remove('bot left')
elif choice == 'bot mid':
pc_bot_mid()
moves.remove('bot mid')
elif choice == 'bot right':
pc_bot_right()
moves.remove('bot right')
def board_setup():
t.penup()
t.goto(-150, 50)
t.pendown()
t.forward(300)
t.penup()
t.goto(-150, -50)
t.pendown()
t.forward(300)
t.penup()
t.left(90)
t.goto(-50, -150)
t.pendown()
t.forward(300)
t.penup()
t.goto(50, -150)
t.pendown()
t.forward(300)
t.setheading(0)
# Game loop
board_setup()
screen.listen()
screen.onscreenclick(user)
choice = random.choice(moves)
computer(choice)
screen.mainloop()
Aucun commentaire:
Enregistrer un commentaire