我正在制作一个摇滚纸剪刀游戏并遇到decisioncycle()
的问题。我要做的是要求用户在usercycle()
中输入一个选项,让计算机在gamecycle()
中生成一个随机选项,然后确定谁赢得该轮次并跟踪每个结果有输赢数。似乎决定何时随机工作。
import random
class rpsgame:
rps= ["rock", "paper","scissors"]
wincount=0
losecount=0
def usercycle(self):
userchoice = input("rock, paper, scissor.....")
print("SHOOT")
return userchoice
def gamecycle(self):
computerchoice = random.choice(rpsgame.rps)
return computerchoice
def decisioncycle(self):
if rpsgame.usercycle(self) == rpsgame.rps[0] and rpsgame.gamecycle(self) == rpsgame.rps[1]:
print("paper beats rock, you lose!")
rpsgame.losecount +=1
elif rpsgame.usercycle(self) == rpsgame.rps[1] and rpsgame.gamecycle(self) == rpsgame.rps[0]:
print("paper beats rock, you win!")
rpsgame.wincount+=1
elif rpsgame.usercycle(self) == rpsgame.rps[0] and rpsgame.gamecycle(self) == rpsgame.rps[2]:
print("rock beats scissors, you win!")
rpsgame.wincount+=1
elif rpsgame.usercycle(self) == rpsgame.rps[2] and rpsgame.gamecycle(self) == rpsgame.rps[0]:
print("rock beats scissors, you lose!")
rpsgame.losecount+=1
elif rpsgame.usercycle(self) == rpsgame.rps[1] and rpsgame.gamecycle(self) == rpsgame.rps[2]:
print("scissors beats paper, you lose!")
rpsgame.losecount+=1
elif rpsgame.usercycle(self) == rpsgame.rps[2] and rpsgame.gamecycle(self) == rpsgame.rps[1]:
print("scissors beats paper, you win!")
rpsgame.wincount+=1
elif rpsgame.usercycle(self) == rpsgame.gamecycle(self):
print("it's a tie!!!")
print("wins {}, losses {}".format(rpsgame.wincount, rpsgame.losecount))
while True:
rg = rpsgame()
rg.usercycle()
rg.gamecycle()
rg.decisioncycle()
我认为我的问题出在decisioncycle()中。这是我在课堂上的第一次尝试,因为游戏正在使用全局变量,但我在这里读到,这对未来来说是一种不好的做法。
答案 0 :(得分:3)
而不是使用多个周期评估每个组合,您可以使用模运算。
让我们说你做了映射
"rock" => 0
"paper"=>1
"scissors" => 2
您可以将解决方案评估为
(A.number - B.number) % 3
如果此结果为0,则为平局,如果为1,则A赢了,如果2 A丢失
答案 1 :(得分:2)
您要求在每种情况下输入新用户。您可能只想阅读一次,然后每次比较
user_choice = self.usercicle()
game_choice = self.gamecycle()
if(user_choice == self.rps[0] and game_choice == self.rps[1]):
print "Paper beats rock, you lose!"
self.losecount += 1
elif( user_choice...
等等
答案 2 :(得分:1)
我认为你应该为确定获胜者制作一个单独的函数,并使用dict,而不是7-way if 语句:
def who_won(player, computer):
rules = {"rock": "scissors", "paper": "rock", "scissors": "paper"}
if player == computer:
return None
if rules[computer] == player:
return "computer"
return "player"
检查无效输入可能是个好主意,但这应该在输入函数中完成,而不是在此函数中完成。
答案 3 :(得分:1)
我认为这实际上可以简化。有一些错误,他们看起来像是由于对课程的简单不熟悉。看看这里:
class RpsGame:
# create a game loop. Let's try a while loop
# also, let's try using a dict to make comparisons easier
def play(self):
rps = {'rock':'scissors', 'paper':'rock', 'scissors':'paper'}
score = 0
# lets just say player must win 3 or lose 3 to end the game
while -3 < score < 3:
# ask user for their choice just once here, for instance
user = raw_input("Rock, paper or scissors: ").lower()
# and check the input is valid
# get the computer choice with random
# then find the winner and adjust score
# when score reaches -3, computer wins etc.
# comparisons could go like:
if com == rps[user]:
score += 1
elif user == rps[com]:
score -= 1
self.game_over(score)
def game_over(self, score):
if score == -3:
result == "You win"
else:
result == "You lose"
print "%s!!" % result
# We would start by instantiating the game
game = RpsGame()
# And then calling the play method
game.play()
如果我也是你,我会更多关于课程和“自我”的用法。
答案 4 :(得分:0)
那里有很多时髦的东西,但我认为你遇到的问题是你将self
传递给一个没有(真的)作为输入的函数
class TestClass(object):
def some_method(self):
return random.choice(['rock', 'paper', 'scissors'])
def make_choice(self):
return self.some_method(self)
# raises TypeError
方法的类在AUTOMATICALLY中被赋予它所属的实例作为第一个参数。如果你再试一次,那就失败了。
那就是说,我觉得你的课应该是这样的:
class Roshambo(object):
ROCK = 'rock'
PAPER = 'paper'
SCISSORS = 'scissors'
OPTIONS = [ROCK, PAPER, SCISSORS]
def get_user_input(self):
choice = input("r/p/s? ").lower()
if choice.startswith('r'): choice = self.ROCK
elif choice.startswith('p'): choice = self.PAPER
elif choice.startswith('s'): choice = self.SCISSORS
else:
# what do we do now?
pass # for now
return choice
def get_computer_input(self):
return random.choice(self.OPTIONS)
def start(self):
# get input ONCE...
user_choice = get_user_input()
computer_choice = get_computer_input()
if user_choice == ROCK and computer_choice == ROCK:
# tie
# etc....
然后实例化
game = Roshambo()
然后运行
game.start()