基于玩家决策python的剪刀,石头,剪刀纸计算机决策

问题描述 投票:-1回答:1

所以我正在重试,没有结果。我认为这次看起来更好,但是偏向选择仍然需要一些修复。它仍然只是给我随机的选择。同样有时候,当我给'p'作为选项时,除了询问我是否要再次玩之外,它什么也没做。任何帮助表示赞赏!

    #Making a game of rock, paper, scissors

找到所有输入/结果

# Player and computer score starting at 0
#I also print the number of times that the player picks rock, paper , or scissors and print it
computer_wins = 0
player_wins = 0
player_rock = 0
player_paper = 0
player_scissor = 0
import random
from random import randint

#getting the result from the player

玩家选择的地方

    def the_game():
        choice = input("Pick between Rock (r), Paper(p), Scissors(s):  ").lower()
        global player_rock
        global player_paper
        global player_scissor
        if choice == 'r':
            Rchoice = 'r'
            player_rock +=1
        elif choice == 'p':
            Pchoice = 'p'
            player_paper +=1
        elif choice == 's':
            Schoice = 's'
            player_scissor += 1
        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():
    Rchoice =  player_rock
    Pchoice =  player_paper
    Schoice =  player_scissor
    if Rchoice > (Pchoice and Schoice):
        bias_choice = 'p'

    elif Pchoice > (Rchoice and Schoice):
        bias_choice = 's'

    elif Schoice > (Rchoice and Pchoice):
        bias_choice = 'r'
    else:
        bias_choice = comp_choice()

    return bias_choice 

在哪里玩游戏。有时候,当我输入“ p”时,它会完全跳过游戏,只是问我是否要再次玩。有什么帮助

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

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


    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' or 'Y':
            pass
        elif player_choice == 'n':
            break
        else:
            print("Invalid choice! Thanks for playing!")
            break
python if-statement boolean-logic
1个回答
0
投票

嗨,我尝试用自己的代码运行剪刀石头布,我得到了这个结果,希望对您有所帮助;如果有人读过它,认为效率不高,有更好的书写方法,请通知我code for rock paper scissors

© www.soinside.com 2019 - 2024. All rights reserved.