我刚刚在python中完成了我的石头剪刀游戏。现在,用户必须在三个按钮之间进行选择,并将字符串user1
设置为“Rock”,“Paper”或“Scissors”。
当我评估游戏时,代码只运行一些if语句,例如检查,
if computer == "Paper"
但是,如果我想用我自己的项目稍微扩展游戏,那将是很多“if / else”。母猪我以为我会给每个项目一个唯一的数字,这样我就可以使用这个数字,而不必乱用字符串。
我的老师给了我一个暗示将两个整数划分以获得谁获胜的提示。
但是在我考虑了一段时间之后,我没有找到任何有关如何做到这一点的解决方案。这只是背后的逻辑,我不想要任何代码。
如何使用两个整数作为项目,我只能通过一点点数学找到胜利者?
祝你有个愉快的一天 DirtyDev!
答案 0 :(得分:6)
让我们说我们正在实施正常的Rock Paper Scissors,正如您在评论中所说的那样。我们分配:
Rock: 0
Paper: 1
Scissors: 2
这个答案假设你知道模运算符(%
),所以如果没有,请先查看它以了解它的含义。我在这个答案中使用它,这样当我们向剪刀(即2)添加1时,你会得到0而不是3,因为摇滚是0并且没有3号项目。
通过将这些数字分配给选择,我们想要一个选择,如果它恰好在另一个人的选择之后就会赢,如果它在它之前就会输,如果它们相等就会结合。例如,2在1之后,因此剪刀击败纸张。我们将使用模运算符来确保我们的数字保持在1到3之间(包括1和3)。
因此,如果您想确定玩家1是否获胜,您将检查他们的移动是否比玩家2的移动大1。要判断他们是否打成平手,看看他们是否有同样的举动。如果这些都不是真的,那么第二名球员必须获胜。这是一个带有一些测试的示例实现:
>>> def winner(p1, p2):
... if (p1+1) % 3 == p2:
... return "Player 2 won because their move is one greater than player 1"
... elif p1 == p2:
... return "It's a draw because both players played the same move"
... else:
... return "Player 1 wins because we know that it's not a draw and that player 2 didn't win"
...
>>>
>>>
>>> rock = 0
>>> paper = 1
>>> scissors = 2
>>> winner(rock, paper)
'Player 2 won because their move is one greater than player 1'
>>> winner(paper, scissors)
'Player 2 won because their move is one greater than player 1'
>>> winner(scissors, rock)
'Player 2 won because their move is one greater than player 1'
>>> winner(rock, scissors)
"Player 1 wins because we know that it's not a draw and that player 2 didn't win"
>>> winner(paper, paper)
"It's a draw because both players played the same move"
现在在这个游戏中,数学规则是一个项目击败项目的数字比它少1(模3)。如果你添加更多项目,你需要提出一个数学规则来管理游戏的运作方式。一个例子(那不是很有趣)将保持一个项目比项目1少于它的规则(因此比项目1更多地输掉它),并且与任何其他项目相关联虽然这将是一个相当无聊的游戏。
希望答案有帮助!!祝你好运!
答案 1 :(得分:0)
解决此问题的一种简便方法是制作两个用于输赢条件的词典,然后将用户输入指定为键,将计算机输入指定为字典中的值。
rps_win={'rock':'scissor','paper':'rock','scissor':'paper'}
rps_lose={'rock':'paper','paper':'scissor','scissor':'rock'}
然后检查该用户计算机对是否来自于胜利或失败字典,并相应地添加分数。
if rps_win[b]==a:
player=player+1//incrementing player points
print('win point!')
elif rps_lose[b]==a:
comp=comp+1//incrementing computer points
print('lose point!')
else:
print('tie!')
答案 2 :(得分:0)
An easy way [for beginners] codehs rocks, paper and scissors
Long because its for beginners if u are advanced use the above code..
"""
Introduction - Imports, Instructions, Declaring Variables, Lists and Computer Choice
"""
# Imports
import time
time.sleep(2)
print "Hello !"
# Delays Response to 2 secs
time.sleep(2)
# Instructions
print "Are you ready to play Rock, Paper and Scissors with the Computer ? "
time.sleep(4)
# Asking if user is ready or not !! (Does Nothing whatever the User Answers)
yes_no = input("Are You Ready to play (Y/N) : ")
time.sleep(2)
print "Ready or Not, here we go !! "
time.sleep(2)
# Declaring Variables
win_user = 0
win_comp = 0
tie = 0
# Imports Random and Sys
import random
import sys
# Choices in a list
choices = ["Rock", "Paper", "Scissors"]
# Computer Choice - between choices [list]
comp_choice = random.choice(choices)
"""
User Input and Computer Input
"""
# Asks User for Rocks, Papers or Scissors
user_input = input("Rock, Paper or Scissors ? : ")
# User_input is now set to User_choice
user_choice = user_input
time.sleep(2)
# Prints what user entered
print "User entered : " + user_choice
time.sleep(2)
# Prints what computer entered
print "Computer entered : " + comp_choice
# Basic Gameplay
if user_choice == "Rock" and comp_choice == "Paper":
win_comp += 1
if user_choice == "Paper" and comp_choice == "Rock":
win_user += 1
if user_choice == "Scissors" and comp_choice == "Paper":
win_user += 1
if user_choice == "Paper" and comp_choice == "Scissors":
win_comp +=1
if user_choice == "Rock" and comp_choice == "Scissors":
win_user +=1
if user_choice == "Scissors" and comp_choice == "Rock":
win_comp +=1
if user_choice == "Rock" and comp_choice == "Rock":
tie +=1
if user_choice == "Paper" and comp_choice == "Paper":
tie +=1
if user_choice == "Scissors" and comp_choice == "Scissors":
tie +=1
"""
Ties, User Wins and Computer Wins and Conclusion
"""
time.sleep(2)
# Checks the amounts of ties, user wins and computer wins !!!
print "User Wins : " + str(win_user)
time.sleep(2)
print "Computer Wins : " + str(win_comp)
time.sleep(2)
print "Ties : " + str(tie)
time.sleep(2)
print "Thank You For Playing !!"
"""
Exit and Repeats
"""
time.sleep(2)
# Asks if User wants to exit or Not !!
ask_user = input("Do you want to exit (Y/N) : ")
# Exit is a variable which does nothing thus it exits the game
exit = 0
# If Y, then it will exit
if ask_user == "Y":
# Does nothing, except adding a 1 to exit
exit += 1
# Elif ask_user == "N", repeats everything again using the WHile loop
elif ask_user == "N":
# WHILE LOOP
while ask_user == "N":
win_user = 0
win_comp = 0
tie = 0
import time
import random
import sys
choices = ["Rock", "Paper", "Scissors"]
comp_choice = random.choice(choices)
time.sleep(2)
user_input = input("Rock, Papers or Scissors ? : ")
user_choice = user_input
time.sleep(2)
print "User entered : " + user_choice
time.sleep(2)
print "Computer entered : " + comp_choice
if user_choice == "Rock" and comp_choice == "Paper":
win_comp += 1
if user_choice == "Paper" and comp_choice == "Rock":
win_user += 1
if user_choice == "Scissors" and comp_choice == "Paper":
win_user += 1
if user_choice == "Paper" and comp_choice == "Scissors":
win_comp +=1
if user_choice == "Rock" and comp_choice == "Scissors":
win_user +=1
if user_choice == "Scissors" and comp_choice == "Rock":
win_comp +=1
if user_choice == "Rock" and comp_choice == "Rock":
tie +=1
if user_choice == "Paper" and comp_choice == "Paper":
tie +=1
if user_choice == "Scissors" and comp_choice == "Scissors":
tie +=1
time.sleep(2)
print "User Wins : " + str(win_user)
time.sleep(2)
print "Computer Wins : " + str(win_comp)
time.sleep(2)
print "Ties : " + str(tie)
time.sleep(2)
print "Thank You For Playing !!"
time.sleep(2)
ask_user = input("Do you want to exit (Y/N) : ")
if ask_user == "Y" or "Yes" or "y" or "yes":
break
else:
print "Wrong Input, Try again by restarting the program"