This repository has been archived by the owner on Jan 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathguessing-game.ts
141 lines (131 loc) · 3.2 KB
/
guessing-game.ts
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import * as Alexa from '../src/index'
interface GuessingState {
Name: 'Guessing'
Target: number
Guesses: number
}
type State = { Name: 'NotStarted' } | GuessingState | { Name: 'Finished' }
const State = {
startGuessing: (): GuessingState => ({
Name: 'Guessing',
Target: Math.round(Math.random() * 99 + 1),
Guesses: 0,
}),
nextGuess: (state: GuessingState): GuessingState => ({
Name: 'Guessing',
Target: state.Target,
Guesses: state.Guesses + 1,
}),
}
const startNewGame = (): Alexa.Response<State> => ({
Say: {
Text: "Try and guess the number I'm thinking of. It's between 1 and 100.",
},
NewState: State.startGuessing(),
})
const checkGuess = (
state: GuessingState,
slots: Alexa.Slots,
): Alexa.Response<State> => {
let guess = slots.get('Guess')
if (typeof guess === 'string') {
guess = parseInt(guess, 10)
}
if (guess < state.Target) {
return {
Say: { Text: `Is it ${guess}? Nope, too low! Guess again.` },
NewState: State.nextGuess(state),
}
} else if (guess > state.Target) {
return {
Say: { Text: `Is it ${guess}? Nope, too high! Guess again.` },
NewState: State.nextGuess(state),
}
} else if (guess === state.Target) {
return {
Say: {
Text: `Is it ${guess}? Yes! Congratulations, you guessed it in ${state.Guesses +
1} tries. Would you like to play again?`,
},
NewState: { Name: 'Finished' },
}
} else {
return { Say: { Text: "Sorry, I couldn't tell what number you said." } }
}
}
const help = (): Alexa.Response<State> => ({
Say: { Text: 'Guess a number between 1 and 100, or say "Stop".' },
})
const stop = (): Alexa.Response<State> => ({
Say: { Text: '' },
EndSession: true,
})
/** IntentSchema.json
* {
* "intents": [
* {
* "intent": "GuessNumber",
* "slots": [{ "name": "Guess", "type": "AMAZON.NUMBER" }]
* },
* { "intent": "AMAZON.YesIntent" },
* { "intent": "AMAZON.NoIntent" },
* { "intent": "AMAZON.HelpIntent" },
* { "intent": "AMAZON.StopIntent" }
* ]
* }
*
* Utterances.txt
* GuessNumber I guess {Guess}
* GuessNumber is it {Guess}
* GuessNumber what about {Guess}
* GuessNumber my guess is {Guess}
* GuessNumber if the number is {Guess}
* GuessNumber {Guess}
*/
const routes: Alexa.Routes<State> = {
InitialState: { Name: 'NotStarted' },
Launch: startNewGame,
Standard: {
Yes: state => {
if (state.Name === 'Finished') {
return startNewGame()
} else {
return help()
}
},
No: state => {
if (state.Name === 'Finished') {
return stop()
} else {
return help()
}
},
Help: help,
Stop: stop,
},
Custom: [
[
'GuessNumber',
(state, slots) => {
if (state.Name === 'Guessing') {
return checkGuess(state, slots)
} else {
return checkGuess(State.startGuessing(), slots)
}
},
],
],
}
const unhandled = () =>
Alexa.response(
{
Say: { Text: "Sorry, I didn't get that, try saying a number." },
},
null,
)
export const handler = Alexa.Lambda.pipe([
// To host in AWS lambda
Alexa.Pipe.tracer(),
Alexa.Pipe.router(routes),
unhandled,
])