Skip to content

Instantly share code, notes, and snippets.

@teenst
Created March 8, 2015 05:21
Show Gist options
  • Save teenst/d94a8645a0f914d5e90c to your computer and use it in GitHub Desktop.
Save teenst/d94a8645a0f914d5e90c to your computer and use it in GitHub Desktop.
数当
#!/usr/bin/env ruby
# citation from まつもとら, "ネットを支えるオープンソース", 角川学芸出版, 2014
## 若干の変更あり
def genprob
a = ''
b = '123456789'
4.times do
i = rand(b.length)
a += b[i..i]
b[i..i] = ''
end
a
end
def judge(ans, str)
hit = blow = 0
4.times do |i|
4.times do |j|
next unless ans[i] == str[j]
if i == j
hit += 1
else
blow += 1
end
end
end
[hit, blow]
end
def kazuate
ans = genprob
10.times do
print 'your guess? '
str = gets
hit, blow = judge(ans, str)
if hit == 4
puts 'you win!'
return
end
puts "hit = #{hit}, blow = #{blow}"
end
puts "you lose! answer = #{ans}."
end
kazuate
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment