-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add guess number game to examples
- Loading branch information
fletcherist
committed
Jul 8, 2018
1 parent
169a818
commit 3d34c26
Showing
1 changed file
with
45 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
const Alice = require('yandex-dialogs-sdk') | ||
const alice = new Alice() | ||
|
||
const random = (min, max) => Math.floor(Math.random() * (max - min + 1)) + min | ||
|
||
const { button, reply } = Alice | ||
alice.welcome(ctx => { | ||
const replyMsg = reply({ | ||
text: 'Привет! Я загадала число от 1 до 100. Сможешь отгадать его?', | ||
buttons: [button('Давай попробуем!'), button('Не хочу')] | ||
}) | ||
ctx.reply(replyMsg) | ||
}) | ||
|
||
alice.command('ДАВАЙ ПОПРОБУЕМ', ctx => { | ||
ctx.state.guessedNumber = random(1, 100) | ||
ctx.reply('Ну попробуй!') | ||
}) | ||
alice.command('Сыграть ещё раз!', ctx => { | ||
ctx.state.guessedNumber = random(1, 100) | ||
ctx.reply('Ну попробуй!') | ||
}) | ||
alice.command('Не хочу', ctx => ctx.reply('Спасибо за игру!')) | ||
|
||
alice.command(/^\d+$/, ctx => { | ||
const number = Number(ctx.message) | ||
if (number > ctx.state.guessedNumber) { | ||
ctx.reply(`Моё число меньше, чем ${number}`) | ||
} else if (number < ctx.state.guessedNumber) { | ||
ctx.reply(`Моё число больше, чем ${number}`) | ||
} else { | ||
const replyMsg = reply({ | ||
text: `Ты победил! Я загадала число ${ctx.state.guessedNumber}. Сыграешь ещё раз?`, | ||
buttons: [button('Сыграть ещё раз!'), button('Не хочу')] | ||
}) | ||
ctx.reply(replyMsg) | ||
} | ||
}) | ||
|
||
alice.any(ctx => { | ||
ctx.reply('Ты ввёл вообще не число. Попробуй ещё раз!') | ||
}) | ||
|
||
const port = 3000 | ||
alice.listen('/', port, callback => console.log(port)) |