RPSLS:程序如何根据哪个玩家获胜来打印获胜逻辑?



所以在尝试学习Python,我正在建立一个RPSLS类游戏。我已经完成了大部分工作(尽管效率可以提高),但希望根据导致胜利的行动来改变结果。

例如参与人1选择Spock,参与人2选择Rock。我想让程序输出Spock vaporizes rock

我已经测试了一个2D数组,并尝试玩索引,但唉。任何提示或什么甚至搜索将不胜感激。

我的程序是这样的:

winningPairs = [("scissors","paper"),
("scissors","lizard"),
("spock","scissors"),
("spock","rock"),
("lizard","spock"),
("lizard","paper"),
("rock","lizard"),
("rock","scissors"),
("paper","rock"),
("paper","spock")]
actionPairs = [ "  - Scissors cuts paper",
"  - Scissors decapitates lizard",
"  - Spock smashes scissors",
"  - Spock vaporizes rock",
"  - Lizard poisons Spock",
"  - Lizard eats paper",
"  - Rock crushes lizard",
"  - Rock crushes scissors",
"  - Paper covers rock",
"  - Paper disproves Spock"]
# ask players for their name
print()
namePlayerOne = input("Player 1, enter your name: ")
namePlayerTwo = input("Player 2, enter your name: ")
print()
# set two variables for keeping score
playerOneScore = 0
playerTwoScore = 0
# ask if players want to see instructions for winning
instructions = input("Would you like to see instructions for winning (y/n)?")
if instructions == "y":
for action in actionPairs:
print(action)
print()
while True:
# asking players for their choice
playerOneOption = input(f"{namePlayerOne} select your option (Rock, Paper, Scissors, Lizard, Spock): ").lower()
playerTwoOption = input(f"{namePlayerTwo} select your option (Rock, Paper, Scissors, Lizard, Spock): ").lower() 
# if both players select the same option, it's always a draw
if playerOneOption == playerTwoOption: 
results = "Draw"
# check if order of input is same as element in winningPairs, if it is Player one wins (who selects first)
elif (playerOneOption,playerTwoOption) in winningPairs: 
results = f"{namePlayerOne} wins" # print win
playerOneScore += 1 # add 1 score to player

# if elif is not true, player two wins
else:
results = f"{namePlayerTwo} wins" # print win
playerTwoScore += 1 # add 1 score to player
print("-"*20)
print(f"{namePlayerOne} chose {playerOneOption}n{namePlayerTwo} chose {playerTwoOption}") # print each player's pick
print(results) # print results
print()
print(f"{namePlayerOne} score: {playerOneScore}n{namePlayerTwo} score: {playerTwoScore}") # print player scores
print("-"*20)
'''for result in actionPairs:
if (playerOneOption,playerTwoOption) in winningPairs:
print(result[winningPairs])'''
playAgain = input("Play again? (y/n): ") # ask if players want to play again
if playAgain.lower() != "y":
break

这是目前为止的输出,当玩家不希望看到说明:

Player 1, enter your name: name1
Player 2, enter your name: name2
Would you like to see instructions for winning (y/n)?n
name1 select your option (Rock, Paper, Scissors, Lizard, Spock): Rock
name2 select your option (Rock, Paper, Scissors, Lizard, Spock): Spock
--------------------
name1 chose rock    
name2 chose spock   
name2 wins
name1 score: 0      
name2 score: 1      
--------------------
Play again? (y/n): n

在name2获胜之前,我希望它说Spock vaporizes rock, name2 wins,如果获胜条件改变,那么玩家1是赢家,它应该改变它以支持该玩家。

改动:-

(1) actionPairs initialised as dictionary
(2) see the edit made portion.
(3) change in the results

代码:

winningPairs = [("scissors","paper"),
("scissors","lizard"),
("spock","scissors"),
("spock","rock"),
("lizard","spock"),
("lizard","paper"),
("rock","lizard"),
("rock","scissors"),
("paper","rock"),
("paper","spock")]
actionPairs = { 0:"Scissors cuts paper",
1:"Scissors decapitates lizard",
2:"Spock smashes scissors",
3:"Spock vaporizes rock",
4:"Lizard poisons Spock",
5:"Lizard eats paper",
6:"Rock crushes lizard",
7:"Rock crushes scissors",
8:"Paper covers rock",
9:"Paper disproves Spock"}
# ask players for their name
print()
namePlayerOne = input("Player 1, enter your name: ")
namePlayerTwo = input("Player 2, enter your name: ")
print()
# set two variables for keeping score
playerOneScore = 0
playerTwoScore = 0
# ask if players want to see instructions for winning
instructions = input("Would you like to see instructions for winning (y/n)?")
if instructions == "y":
for action in actionPairs:
print(action)
print()
while True:
# asking players for their choice
playerOneOption = input(f"{namePlayerOne} select your option (Rock, Paper, Scissors, Lizard, Spock): ").lower()
playerTwoOption = input(f"{namePlayerTwo} select your option (Rock, Paper, Scissors, Lizard, Spock): ").lower() 
#edit made..
for i in range(len(winningPairs)):
if winningPairs[i]==(playerOneOption,playerTwoOption) or winningPairs[i]==(playerTwoOption,playerOneOption):
temp=i


# if both players select the same option, it's always a draw
if playerOneOption == playerTwoOption: 
results = "Draw"

# check if order of input is same as element in winningPairs, if it is Player one wins (who selects first)
elif (playerOneOption,playerTwoOption) in winningPairs: 
results=actionPairs.get(temp,"")+", "
results += f"{namePlayerOne} wins" # print win
playerOneScore += 1 # add 1 score to player

# if elif is not true, player two wins
else:
results=actionPairs.get(temp,"")+", "
results += f"{namePlayerTwo} wins" # print win
playerTwoScore += 1 # add 1 score to player
print("-"*20)
print(f"{namePlayerOne} chose {playerOneOption}n{namePlayerTwo} chose {playerTwoOption}") # print each player's pick
print(results) # print results
print()
print(f"{namePlayerOne} score: {playerOneScore}n{namePlayerTwo} score: {playerTwoScore}") # print player scores
print("-"*20)
'''for result in actionPairs:
if (playerOneOption,playerTwoOption) in winningPairs:
print(result[winningPairs])'''
playAgain = input("Play again? (y/n): ") # ask if players want to play again
if playAgain.lower() != "y":
break

输出: -

Player 1, enter your name: Yash
Player 2, enter your name: Rohan
Would you like to see instructions for winning (y/n)?n
Yash select your option (Rock, Paper, Scissors, Lizard, Spock): rock
Rohan select your option (Rock, Paper, Scissors, Lizard, Spock): paper
--------------------
Yash chose rock
Rohan chose paper
Paper covers rock, Rohan wins
Yash score: 0
Rohan score: 1
--------------------
Play again? (y/n): y
Yash select your option (Rock, Paper, Scissors, Lizard, Spock): paper
Rohan select your option (Rock, Paper, Scissors, Lizard, Spock): rock
--------------------
Yash chose paper
Rohan chose rock
Paper covers rock, Yash wins
Yash score: 1
Rohan score: 1
--------------------
Play again? (y/n): y
Yash select your option (Rock, Paper, Scissors, Lizard, Spock): paper
Rohan select your option (Rock, Paper, Scissors, Lizard, Spock): paper
--------------------
Yash chose paper
Rohan chose paper
Draw
Yash score: 1
Rohan score: 1
--------------------
Play again? (y/n): n

最新更新