Python 石头剪刀-从数学上获得冠军
我刚刚用python完成了我的石头剪刀游戏。现在,用户必须在三个按钮之间进行选择,字符串Python 石头剪刀-从数学上获得冠军,python,algorithm,Python,Algorithm,我刚刚用python完成了我的石头剪刀游戏。现在,用户必须在三个按钮之间进行选择,字符串user1被设置为“石头”、“布”或“剪刀”。 当我评估游戏时,代码只运行一些if语句,例如检查 if computer == "Paper" 但是如果我想用我自己的物品来扩展游戏,那就需要很多“如果/否则”。Sow我想我应该给每个项目一个唯一的数字,这样我就可以使用这个数字,而不必乱弄字符串 我的老师给了我一个提示,让我把这两个整数分开,以决定谁赢 但是在我考虑了一段时间之后,我没有找到任何解决办法来解决
user1
被设置为“石头”、“布”或“剪刀”。
当我评估游戏时,代码只运行一些if语句,例如检查
if computer == "Paper"
但是如果我想用我自己的物品来扩展游戏,那就需要很多“如果/否则”。Sow我想我应该给每个项目一个唯一的数字,这样我就可以使用这个数字,而不必乱弄字符串
我的老师给了我一个提示,让我把这两个整数分开,以决定谁赢
但是在我考虑了一段时间之后,我没有找到任何解决办法来解决这个问题。这只是关于这背后的逻辑,我不想要任何代码
我怎样才能用两个整数作为项目,仅仅通过一点数学就找到赢家呢?
谢谢大家! 假设我们正在使用普通的石头剪刀,正如您在对问题的评论中所说的那样。让我们分配:
Rock: 0
Paper: 1
Scissors: 2
这个答案假设你知道模运算符(%
),所以如果不知道,请先查一下它的意思。我在这个答案中使用了它,所以当我们将1添加到剪刀(即2)中时,得到的是0,而不是3,因为石头是0,数字3没有项目
通过将数字分配给选择,我们希望选择在对方选择之后获胜,在对方选择之前失败,在双方平等的情况下平局。例如,2紧跟在1之后,所以剪刀胜过纸。我们将使用模运算符确保数字保持在1和3之间(包括1和3)
所以,如果你想确定玩家1是否获胜,你应该检查他们的移动是否比玩家2的移动大1。要判断他们是否平手,看他们是否有相同的动作。如果这两个都不是真的,那么玩家2一定赢了。下面是一个带有一些测试的示例实现:
>>> 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的次数多于它),并与任何其他项目保持平局,尽管这将是一个相当无聊的游戏
希望这个答案有帮助!!祝你好运 解决这一问题的一个更简单的方法是为赢和输条件制作两个字典,然后将用户输入指定为键,将计算机输入指定为字典中的值
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!')
你的问题到底是什么?问题编辑@Rob。我只想通过使用一点整数来获得取决于两个整数的获胜者math@DirtyDev你还没有解释如何选择获胜者。你说你想添加你自己的项目,这些项目会有数字,但我们不知道什么数字会超过什么数字,如果没有这些信息,我们就不可能帮助你编写算法,因为我们甚至不知道算法应该做什么。@ChristopherShroba okay sry。假设这是一把普通的石头剪刀
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"