-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
guessinggame.js
72 lines (64 loc) · 2.27 KB
/
guessinggame.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
var chatskills = require('./lib/chatskills');
var readlineSync = require('readline-sync');
// Allow this module to be reloaded by hotswap when changed
module.change_code = 1;
chatskills.verbose = true;
// Define an alexa-app
var app = chatskills.app('guessinggame'); //new alexa.app('guessinggame');
app.launch(function(req,res) {
var number = Math.floor(Math.random()*99)+1;
res.session('number',number);
res.session('guesses',0);
var prompt = "Guess a number between 1 and 100!";
res.say(prompt).reprompt(prompt).shouldEndSession(false);
});
app.intent('run', {
"slots": {},
"utterances": ["{to|} {run|start|go|launch}"]
}, function(req, res) {
var number = Math.floor(Math.random()*99)+1;
res.session('number',number);
res.session('guesses',0);
var prompt = "Guess a number between 1 and 100!";
res.say(prompt).reprompt(prompt).shouldEndSession(false);
}
);
app.intent('guess',{
"slots":{"guess":"NUMBER"}
,"utterances":["{1-100|guess}"]
},
function(req,res) {
var guesses = (+req.session('guesses'))+1;
var guess = req.slot('guess');
var number = +req.session('number');
if (!guess) {
res.say("Sorry, I didn't hear a number. The number was "+number);
}
else if (guess==number) {
res.say("Congratulations, you guessed the number in " + guesses + (guesses==1?" try":" tries"));
}
else {
if (guess > number) {
res.say("Guess lower");
}
else if (guess < number) {
res.say("Guess higher");
}
res.reprompt("Sorry, I didn't hear a number. Try again.");
res.session('guesses',guesses);
res.shouldEndSession(false);
}
}
);
module.exports = app;
// Start running our skill. You can also just create a "start" intent and ask, "chatskills, ask guessinggame start".
chatskills.launch(app);
// Console client.
var text = ' ';
while (text.length > 0 && text != 'quit') {
text = readlineSync.question('> ');
// Respond to input.
chatskills.respond(text, function(response) {
console.log(response);
});
}