Created
February 7, 2019 10:36
-
-
Save quanon/4715d5a8085ded76ddac88ad57ce886c to your computer and use it in GitHub Desktop.
mod 演算を使ったじゃんけんスクリプト
This file contains hidden or 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
HANDS = { | |
0 => 'グー', | |
1 => 'チョキ', | |
2 => 'パー' | |
}.freeze | |
DRAW = 0 | |
LOSE = 1 | |
WIN = 2 | |
legend = HANDS.map { |i, name| "#{i}: #{name}" }.join(', ') | |
puts('何を出しますか?数字で選択してください。') | |
puts(legend) | |
loop do | |
you = gets.chomp.to_i | |
unless HANDS.keys.include?(you) | |
puts("#{you} は無効です。必ず次の中から選択してください。") | |
puts(legend) | |
next | |
end | |
cp = HANDS.keys.sample | |
puts("you: #{HANDS[you]} VS cp: #{HANDS[cp]}") | |
case (you - cp) % HANDS.length | |
when DRAW | |
puts('引き分けました。もう一度、数字を選択してください。') | |
puts(legend) | |
when LOSE | |
puts('You lose...') | |
break | |
when WIN | |
puts('You win!') | |
break | |
end | |
end⏎ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment