Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
476 views
in Technique[技术] by (71.8m points)

python - 无法根据玩家的选择在计算机上选择岩石,纸张,剪刀的输出(Having trouble with computer selecting output for rock, paper, scissors based on player choice)

Trying to figure out how to make the computer make a choice based on the player decision.

(试图弄清楚如何使计算机根据玩家的决定做出选择。)

Sorry for sloppy code but this is my first semester trying to learn python.

(对不起,草率的代码,但这是我第一学期尝试学习python的方法。)

Thanks

(谢谢)

This is how I have to player choosing

(这就是我要玩家选择的方式)

#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

This is where I want the computer to make advanced decisions.

(这是我希望计算机做出高级决策的地方。)

#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'

This where I have the game coming together

(这是我一起比赛的地方)

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. 
")
        elif computer_choice == 'p':
            print("You chose rock and lost to paper. 
")
            computer_wins += 1
        elif computer_choice == 's':
            print("You chose rock and won! 
")
            player_wins += 1

    elif player_choice == 'p':
        if computer_choice == 'p':
            print("You both chose paper. You tied. Try again. 
")
        elif computer_choice == 's':
            print("You chose paper and lost to scissors. 
")
            computer_wins += 1
        elif computer_choice == 'p':
            print("You chose paper and won!!! 
")
            player_wins += 1

    elif player_choice == 's':
        if computer_choice == 's':
            print("You both chose scissors. You tied. Try again. 
")
        elif computer_choice == 'r':
            print("You chose scissors and lost to rock.
")
            computer_wins += 1
        elif computer_choice == 'p':
            print("You chose scissors and won! 
")
            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) 
")
        if player_choice == 'y':
            pass
        elif player_choice == 'n':
            break
        else:
            print("Invalid choice! Thanks for playing!")
            break
  ask by chasememore translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...