25
22

More than 5 years have passed since last update.

# ズンドコキヨシ with Ruby

Last updated at Posted at 2016-03-11

ひとまず１つめの実装を張っておきます

## 単にループ

``````def zundoko
z, d = zd = ['ズン', 'ドコ']
w1, w2, w3, w4, w5 = 5.times.map { nil }

until [w1, w2, w3, w4, w5] == [z, z, z, z, d]
w1, w2, w3, w4, w5 = w2, w3, w4, w5, zd.sample.tap(&method(:print))
end

print 'キ・ヨ・シ！'
end

zundoko
``````

## Enumerator

なるほど

``````def zundoko
z, d = zd = ['ズン', 'ドコ']

loop.lazy.map { zd.sample }.each_cons(5).with_index do |nominees, i|
i.zero? ? nominees.each(&method(:print)) : print(nominees.last)
break if nominees == [z, z, z, z, d]
end

print 'キ・ヨ・シ！'
end

zundoko
``````

その２

``````
def zundoko
z, d = zd = ['ズン', 'ドコ']

loop.lazy.map { zd.sample }.reduce([]) do |nominees, w|
break print('キ・ヨ・シ！') if nominees == [z, z, z, z, d]
nominees.last(4) + [w.tap(&method(:print))]
end
end

zundoko
``````

## 再帰

``````Z, D = ZD = ['ズン', 'ドコ']

def zundoko(nominees = [])
return print('キ・ヨ・シ！') if nominees == [Z, Z, Z, Z, D]

zundoko(nominees.last(4) + [ZD.sample.tap(&method(:print))])
end

zundoko
``````
25
22
0

Register as a new user and use Qiita more conveniently

1. You get articles that match your needs
2. You can efficiently read back useful information
3. You can use dark theme
What you can do with signing up
25
22