How to make a Rock Paper Scissor Alexa Skill game in Python??
Hello Guys!! Today I will tell you how to make a Rock Paper Scissor Alexa skill in Alexa-Hosted(Python).
It's very interesting to make this game in python because we don't have to write much code as compared to Node.js. But the switch condition will be the same which you used in Node.js. We will use slots to make this Alexa skill game.
To make this skill you should have an Amazon Developer Account. If you have simply Sign-in and without wasting your time start your work to make this interesting skill game. You can also learn how to make a fact skill in python.
Let’s Start…..
Navigate to Alexa Developer Console and click on that blue button → Create Skill.
Give the skill name Rock Paper Scissor and select Alexa-Hosted(Python). Also, remember that select your country I selected India. You select your own country. And then skill on the blue button → Create Skill. It will take a minute to load and then you will be navigated to the frontend where we do small but important tasks.
Now we will initialize Intent, Utterances, and slots. You will see HelloWorldIntent immediately delete it and add a new Intent → GetActionIntent. Now in sample utterances add a slot variable name in curly braces → {action}. Save the model but don't build the model we will do later after adding the slots.
After adding Intent and Utterances, Go to Slot Type and add slot name → actiontype. Now add some slots values →
paper
scissor
rock
After adding slot values Save the model and Build the model. It will a minute to build and after that, you will be navigated to the Code tab where we have to do some coding…
Add the random function →
import random
Also, below the random function add a variable name → Actions and make a list like this line of code.
Actions=[
‘paper’,
‘rock’,
‘scissor’
]
Now come to class name → LaunchRequestHandler where we have to give the Launch request text which means the introduction line about the skill. There you will see →speak_output. Change the text with this line of code.
speak_output = “Welcome to rock paper scissor. What do you choose?”
Then slowly come down there you will see another class name → HelloWorldIntentHandler. Change the whole code of that class with this line of code.
Build better voice apps. Get more articles & interviews from voice technology experts at voicetechpodcast.com
class GetActionIntentHandler(AbstractRequestHandler):
“””Handler for Hello World Intent.”””
def can_handle(self, handler_input):
# type: (HandlerInput) -> bool
return ask_utils.is_intent_name(“GetActionIntent”)(handler_input)def handle(self, handler_input):
# type: (HandlerInput) -> Response
slots = handler_input.request_envelope.request.intent.slotsuserAction = slots.[‘action’].value
speakOutput = ‘ ’;
repromptOutput =’ What is your next move?’
alexaaction = random.choice(Actions)
combo = userAction+alexaAction
def switcher(combo):
switcher = {
‘rockrock’:
speakOutput+=”you played rock and i played rock, it is a tie! “
‘rockpaper’:
speakOutput+=”you played rock and i played paper, I win! “
‘rockscissor’:
speakOutput+=”you played rock and i played scissor, you win! congratulations “
‘paperrock’:
speakOutput+=”you played paper and i played rock, you win! congratulations “
‘paperpaper’:
speakOutput+=”you played paper and i played paper, it is a tie! “
‘paperscissor’:
speakOutput+=”you played paper and i played scissor, I win! “
‘scissorrock’:
speakOutput+=”you played scissor and i played rock, I win! “
‘scissorpaper’:
speakOutput+=”you played scissor and i played paper, you win! congratulations “
‘scissorscissor’:
speakOutput+=”you played scissor and i played scissor, it is a tie! “
}return (
handler_input.response_builder
.speak(speakOutput)
.ask(repromptOutput)
.response
)
Now come to the bottom of the code there you will see HelloWorldIntentHandler→
sb.add_request_handler(HelloWorldIntentHandler())
change this line of code with GetActionIntentHandler →
sb.add_request_handler(GetActionIntentHandler())
Some more modifications you have to do in this code. See for HelpIntentHandler, you just change the speak_output text with this →
speak_output = “You can say hello to me! How can I help?”
After doing all this stuff, simply Save and Deploy. After getting a successful deployment message. Then move to the Test tab there we will test our skill.
Change the Skill testing enable from Off to Development. And then open your skill by typing all the text in lowercase →
open rock paper scissor
then choose any of the slot values and enjoy your game!!
Hurray!! You have successfully made your Rock Paper Scissor Alexa skill. Make this skill Live, just go Distribution add some necessary details like description, keywords or skill icon. For → Privacy Policy and Terms of Condition. Just simply edit it and add your contact details.
Try more skills and make them live.