![](/img/trans.png)
[英]How to NOT repeat a random.choice when printing a piece of text in python
[英]How to repeat a choice using random
所以我正在嘗試制作一個井字游戲,您可以在其中與計算機對戰。 因為我不知道如何為計算機使用 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()
如果您每次運行都想要不同的值,請將“random.seed()”放在程序的頂部。 有關解釋,請參閱隨機模塊文檔。
您的問題似乎是您不知道如何讓用戶和計算機交替輪流。 一種解決方案是在用戶移動事件處理程序中檢查用戶是否進行了有效的移動,如果是,並且還有移動,則告訴計算機進行移動。 我在下面使用了這種方法並在此過程中簡化了您的代碼:
from turtle import Screen, Turtle
from random import choice
moves = [ \
'top left', 'top mid', 'top right', \
'mid left', 'mid mid', 'mid right', \
'bot left', 'bot mid', 'bot right', \
]
def makex(x, y):
t.goto(x, y)
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(x, y):
t.goto(x, y)
t.right(90)
t.forward(30)
t.left(90)
t.pendown()
t.circle(30)
t.penup()
t.setheading(0)
def user(x, y):
valid_move = False
if moves:
# Top row
if 50 < y < 150:
# Top left
if -150 < x < -50:
if 'top left' in moves:
makex(-100, 100)
moves.remove('top left')
valid_move = True
# Top Mid
elif -50 < x < 50:
if 'top mid' in moves:
makex(0, 100)
moves.remove('top mid')
valid_move = True
# Top Right
elif 50 < x < 150:
if 'top right' in moves:
makex(100, 100)
moves.remove('top right')
valid_move = True
# Mid row
elif -50 < y < 50:
# Mid left
if -150 < x < -50:
if 'mid left' in moves:
makex(-100, 0)
moves.remove('mid left')
valid_move = True
# Mid Mid
elif -50 < x < 50:
if 'mid mid' in moves:
makex(0, 0)
moves.remove('mid mid')
valid_move = True
# Mid Right
elif 50 < x < 150:
if 'mid right' in moves:
makex(100, 0)
moves.remove('mid right')
valid_move = True
# Bot row
elif -150 < y < -50:
# Bot left
if -150 < x < -50:
if 'bot left' in moves:
makex(-100, -100)
moves.remove('bot left')
valid_move = True
# Bot Mid
elif -50 < x < 50:
if 'bot mid' in moves:
makex(0, -100)
moves.remove('bot mid')
valid_move = True
# Bot Right
elif 50 < x < 150:
if 'bot right' in moves:
makex(100, -100)
moves.remove('bot right')
valid_move = True
if valid_move and moves:
computer(choice(moves))
def computer(choice):
screen.onscreenclick(None) # disable user during computer move
if choice == 'top left':
makeo(-100, 100)
moves.remove('top left')
elif choice == 'top mid':
makeo(0, 100)
moves.remove('top mid')
elif choice == 'top right':
makeo(100, 100)
moves.remove('top right')
elif choice == 'mid left':
makeo(-100, 0)
moves.remove('mid left')
elif choice == 'mid mid':
makeo(0, 0)
moves.remove('mid mid')
elif choice == 'mid right':
makeo(100, 0)
moves.remove('mid right')
elif choice == 'bot left':
makeo(-100, -100)
moves.remove('bot left')
elif choice == 'bot mid':
makeo(0, -100)
moves.remove('bot mid')
elif choice == 'bot right':
makeo(100, -100)
moves.remove('bot right')
if moves: # reenable user if it's possible to move
screen.onscreenclick(user)
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.left(90)
t.penup()
t.goto(-50, -150)
t.pendown()
t.forward(300)
t.penup()
t.goto(50, -150)
t.pendown()
t.forward(300)
t.penup()
t.setheading(0)
screen = Screen()
# Define the turtle
t = Turtle()
t.hideturtle()
t.speed('fastest')
board_setup()
computer(choice(moves))
screen.mainloop()
還有更多的代碼簡化可以完成,但在這一點上是不值得的。 您的下一個障礙是添加評分邏輯,以確定任一玩家是否獲勝或是否已達到平局。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.