【问题标题】:How to repeat a choice using random如何使用随机重复选择
【发布时间】: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


【解决方案1】:

您的问题似乎是您不知道如何使用户和计算机交替轮流。一种解决方案是在用户移动事件处理程序中检查用户是否进行了有效的移动,如果是,并且还有移动,则告诉计算机进行移动。我在下面使用了这种方法并在此过程中简化了您的代码:

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()

还有更多可以简化的代码,但目前不值得。您的下一个障碍是添加计分逻辑,以确定任一玩家是否获胜或是否打平。

【讨论】:

    【解决方案2】:

    如果您每次运行时都需要不同的值,请将“random.seed()”放在程序的顶部。有关说明,请参阅随机模块文档。

    【讨论】:

      猜你喜欢
      • 2019-05-05
      • 2017-10-25
      • 1970-01-01
      • 1970-01-01
      • 2013-09-16
      • 1970-01-01
      • 2020-02-19
      • 1970-01-01
      • 2019-10-29
      相关资源
      最近更新 更多