Home > Back-end >  How do i create a rock paper scissors with a random chosen AI answer?
How do i create a rock paper scissors with a random chosen AI answer?

Time:03-23

So on Codesters(#Python) I was trying to make a ai powered Rock paper scissors where the user types in a answer(Rock paper or scissors) and the AI or the sprite responds to that by randomly choosing a option (Rock paper or scissors) then responds to the user on how to actual games works (hoping you know that) so like if i do scissors then the computer randomly chooses an option (let's do rock for now) then the sprite goes like: "I beat you!". Does anyone know how to do it? It's a graded project for my class (***I have some code linked to the end of without it ***)

# Title: None
# License: 
# Source: https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTXxwMFEbS7PcwUvwpebZqEJEHfeAo6y8QU_-jaUdkSFEn5NOeWYoDKUDJMo7-Tx0MWfiQ:https://www.sprite.com/content/dam/nagbrands/us/sprite/en/products/thirst-for-yours/products/sprite/desktop/base_featurecan.jpg&usqp=CAU
Jimbo= codesters.Sprite("98eb3cb95b0c486cacb5267accc2090b")
Jimbo.say("Welcome User to Rock Paper Scissors!")
stage.wait(2)
user_name = Jimbo.ask("What's your name?")

Jimbo.say(user_name)
stage.wait(2)
Jimbo.say("Great To meet you!")
stage.wait(2)
Jimbo.say("To beat this game, you must face world champions of rock paper scissors.")
stage.wait(2)


stage.set_background("park")
Jimbo.say("we should get started.")
Jimbo.move_forward(550)
score = 0
score_display = codesters.Display(score, -200, 200)
stage.remove_sprite(Jimbo)

Mack = codesters.Sprite("person1")
Mack.say("Welcome to your first game!")
stage.wait(1)
Mack.say("Rock")
stage.wait(1)
Mack.say("Paper")
stage.wait(1)
Mack.say("Scissors")
stage.wait(1)
Mack.say("SHOOT")
stage.wait(1)

yay=Mack.ask("What will You Pick?")
yay=yay.lower()
if yay=="rock":
   score = 1
   score_display.update(score)
   Mack.say("I CHOOSE SCISSORS")
   stage.wait(1)
   Mack.say("HOW THE HECK DID YOU BEAT ME, I'm SO GOOD AT THIS AND I LOST!?!?!?")
   stage.wait(1)
   Mack.say("GGs")

else:
   Mack.say("HAHAHAHAHA, I won, now you must beat the next person to advance to the quarterfinals.")
   stage.wait(2)
   Mack.say("you need atleast 1 point to advance")

CodePudding user response:

You can select a random option from a list using the random library

import random
aimove = random.choice(["Rock", "Paper", "Scissors"])

CodePudding user response:

You can use the random module from the Python Standard Library for the computer player to "randomly" choose between Rock, Paper and Scissors:

random.choice(['Rock', 'Pape', 'Scissors'])

I suggest you to read this module documentation, but specially the section Functions for sequences.

I made this game while I was doing the course An Introduction to Interactive Python Programming at Coursera in 2013. You can check this code at my GitHub.

Note: "randomly" with quotes because it's actually a pseudo-randomization.

  • Related