
[英]Rock, paper, scissors computer decision based on player decision python
[英]Having trouble with computer selecting output for rock, paper, scissors based on player choice
试图弄清楚如何使计算机根据玩家的决定做出选择。 对不起,草率的代码,但这是我第一学期尝试学习python的方法。 谢谢
这就是我要玩家选择的方式
#Making a game of rock, paper, scissors
# Player and computer score starting at 0
computer_wins = 0
player_wins = 0
import random
from random import randint
#getting the result from the player
def the_game(Rchoice, Pchoice, Schoice):
choice = input("Pick between Rock (r), Paper(p), Scissors(s): ")
if choice == 'r':
Rchoice = 'r'
elif choice == 'p':
Pchoice = 'p'
elif choice == 's':
Schoice = 's'
else:
print("That is not a valid option, please try again.")
the_game()
return choice
# Get the result from the computer
def comp_choice():
c_choice = random.randint(1,3)
if c_choice == 1:
c_choice = 'r'
elif c_choice == 2:
c_choice = 'p'
elif c_choice == 3:
c_choice = 's'
return c_choice
这是我希望计算机做出高级决策的地方。
#Making the computer choose based on players decisions
def biased_choice(the_game):
if Rchoice == Pchoice or Schoice:
return comp_choice()
elif Pchoice == Rchoice or Schoice:
return comp_choice()
elif Schoice == Rchoice or Pchoice:
return comp_choice()
else:
if (Rchoice > Pchoice) and (Rchoice > Schoice):
return bias_choice == 'p'
elif (Pchoice > Rchoce) and (Pchoice > Schoice):
return bias_choice == 's'
elif (Schoice > Rchoice) and (Schoice > Pchoice):
return bias_choice == 'r'
这是我一起比赛的地方
while True:
player_choice = the_game()
computer_choice = biased_choice()
if player_choice == 'r':
if computer_choice == 'r':
print("You both chose rock. You tied. Try again. \n")
elif computer_choice == 'p':
print("You chose rock and lost to paper. \n")
computer_wins += 1
elif computer_choice == 's':
print("You chose rock and won! \n")
player_wins += 1
elif player_choice == 'p':
if computer_choice == 'p':
print("You both chose paper. You tied. Try again. \n")
elif computer_choice == 's':
print("You chose paper and lost to scissors. \n")
computer_wins += 1
elif computer_choice == 'p':
print("You chose paper and won!!! \n")
player_wins += 1
elif player_choice == 's':
if computer_choice == 's':
print("You both chose scissors. You tied. Try again. \n")
elif computer_choice == 'r':
print("You chose scissors and lost to rock.\n")
computer_wins += 1
elif computer_choice == 'p':
print("You chose scissors and won! \n")
player_wins += 1
print("Player wins: " + str(player_wins))
print("Computer wins: " + str(computer_wins))
player_choice = input("Do you want to play again? (y/n) \n")
if player_choice == 'y':
pass
elif player_choice == 'n':
break
else:
print("Invalid choice! Thanks for playing!")
break
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.