【发布时间】:2020-06-21 20:54:03
【问题描述】:
所以我正在尝试制作一个可以与计算机对战的井字游戏。因为我不知道如何将 AI 用于计算机以便它可以做出决定,所以我选择让它使用随机选择。因为它需要为多个回合做出多个选择,所以我想弄清楚如何让“选择”变量循环,所以每次游戏循环时都会做出不同的随机选择。这是代码。
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()
【问题讨论】:
-
我不确定我是否理解问题所在,你能澄清一下吗?
标签: python random turtle-graphics