mardi 3 décembre 2019

Rock, paper, scissors computer decision based on player decision python

So I'm trying again with no results. I think it looks better this time but the biased choice still needs some fixes. It is still just giving me random options. Also sometimes when i give 'p' as an option it doesn't do anything except ask if I want to play again. Any help is appreciated!

    #Making a game of rock, paper, scissors

All the inputs/results are located

# 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

Where the player makes a choice

    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

Computer making a random choice

    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

Computer making a biased choice based on the play inputs (where I am having trouble).

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 

Where the game is play. Sometimes when I put in 'p' it will skip the game entirely and just ask if I want to play again. Anything helps

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

Aucun commentaire:

Enregistrer un commentaire