Skip to content

Commit 23fef60

Browse files
committed
shiritori: switch interact to promise
Change-Id: Id75656c2053edda5a3d209ad3fa238d6fab6bdb3
1 parent 75a124b commit 23fef60

File tree

3 files changed

+49
-73
lines changed

3 files changed

+49
-73
lines changed

functions/shiritori/index.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -98,29 +98,29 @@ exports.kanas = (word) => new Set((() => {
9898
})())
9999

100100
// しりとりのゲームループ。
101-
exports.interact = (dict, word, previousInputs, callbacks) => {
102-
const next = exports.kanas(word)
103-
if ((next.size === 0) || !exports.check(word, previousInputs)) {
104-
return callbacks.lose()
101+
exports.interact = (dict, word, chain) => new Promise((resolve, reject) => {
102+
const next = exports.kanas(word);
103+
if ((next.size === 0) || !exports.check(word, chain)) {
104+
return reject({loose: true})
105105
}
106106
const key = next.values().next().value
107107
dict(key).then((words) => {
108108
const unused = []
109109
if (words) {
110110
for (const k of Object.keys(words)) {
111-
if (!previousInputs.includes(words[k])) {
111+
if (!chain.includes(words[k])) {
112112
unused.push(k)
113113
}
114114
}
115115
}
116116
if (unused.length === 0) {
117-
return callbacks.win()
117+
return reject({win: true})
118118
}
119119
const w = unused[Math.floor(Math.random() * unused.length)]
120120
const wk = words[w]
121121
if (wk[wk.length - 1] === 'ん') {
122-
return callbacks.win(w, wk)
122+
return reject({win: true, 'word': w, 'kana': wk})
123123
}
124-
return callbacks.next(w, wk)
124+
return resolve({'word': w, 'kana': wk})
125125
})
126-
}
126+
})

functions/shiritori/main.js

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -27,30 +27,30 @@ const rl = readline.createInterface({
2727
output: process.stdout
2828
})
2929

30-
const previousInputs = ['しりとり']
31-
console.log(previousInputs[0])
30+
const chain = ['しりとり']
31+
console.log(chain[0])
3232
rl.prompt()
3333
shiritori.loaded.then(() => {
34-
rl.on('line', (input) => {
35-
shiritori.interact(kana => Promise.resolve(
36-
corpus[kana]), input, previousInputs, {
37-
win (word, kana) {
38-
if (word) {
39-
console.log(`${word} [${kana}]`)
40-
} else {
41-
console.log('すごい!')
42-
}
34+
rl.on('line', input => {
35+
shiritori
36+
.interact(
37+
kana => Promise.resolve(corpus[kana]),
38+
input,
39+
chain,
40+
)
41+
.then(result => {
42+
console.log(`${result.word} [${result.kana}]`)
43+
chain.unshift(input)
44+
chain.unshift(result.word)
45+
rl.prompt()
46+
})
47+
.catch(reason => {
48+
if (reason.win) {
49+
console.log('すごい!')
4350
process.exit(0)
44-
},
45-
lose () {
51+
} else {
4652
console.log('ざんねん。')
4753
process.exit(-1)
48-
},
49-
next (word, kana) {
50-
previousInputs.unshift(input)
51-
previousInputs.unshift(kana)
52-
console.log(`${word} [${kana}]`)
53-
rl.prompt()
5454
}
5555
})
5656
})

functions/shiritori/test.js

Lines changed: 21 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -79,57 +79,33 @@ const dict = k => Promise.resolve({
7979
}
8080
}[k])
8181

82-
test.cb('internal.next', t => {
83-
t.plan(2)
84-
shiritori.interact(dict, 'べんと', [], {
85-
next (word, kana) {
86-
t.is('とんかつ', kana)
87-
t.is('とんかつ', word)
88-
t.end()
89-
}
90-
})
82+
test('internal.next', async t => {
83+
const result = await shiritori.interact(dict, 'べんと', [],)
84+
t.is('とんかつ', result.kana)
85+
t.is('とんかつ', result.word)
9186
})
9287

93-
test.cb('interact.lose', t => {
94-
t.plan(1)
95-
shiritori.interact(dict, 'つと', ['とつ', 'つと'], {
96-
lose (result) {
97-
t.is(undefined, result)
98-
t.end()
99-
}
100-
})
88+
test('interact.lose', async t => {
89+
const result = await t.throws(shiritori.interact(dict, 'つと', ['とつ', 'つと']))
90+
t.is(true, result.loose)
10191
})
10292

103-
test.cb('internal.win without result', t => {
104-
t.plan(1)
105-
shiritori.interact(dict, 'つと', ['とんかつ', 'べんと'], {
106-
win (result) {
107-
t.is(undefined, result)
108-
t.end()
109-
}
110-
})
93+
test('internal.win without result', async t => {
94+
const result = await t.throws(shiritori.interact(dict, 'つと', ['とんかつ', 'べんと']))
95+
t.is(true, result.win)
11196
})
11297

113-
test.cb('internal.win with result', t => {
114-
t.plan(2)
115-
shiritori.interact(dict, 'とんかつ', ['べんと'], {
116-
win (word, kana) {
117-
t.is('つけまん', kana)
118-
t.is('つけまん', word)
119-
t.end()
120-
}
121-
})
98+
test('internal.win with result', async t => {
99+
const result = await t.throws(shiritori.interact(dict, 'とんかつ', ['べんと']))
100+
t.is(true, result.win)
101+
t.is('つけまん', result.kana)
102+
t.is('つけまん', result.word)
122103
})
123104

124-
test.cb('interact.win kanji', t => {
125-
t.plan(2)
126-
shiritori.loaded.then(() => {
127-
shiritori.interact(dict, '銀座', ['鰻'], {
128-
win (word, kana) {
129-
t.is('ざぶとん', kana)
130-
t.is('座布団', word)
131-
t.end()
132-
}
133-
})
134-
})
105+
test('interact.win kanji', async t => {
106+
await shiritori.loaded
107+
const result = await t.throws(shiritori.interact(dict, '銀座', ['鰻']))
108+
t.is(true, result.win)
109+
t.is('ざぶとん', result.kana)
110+
t.is('座布団', result.word)
135111
})

0 commit comments

Comments
 (0)