Created
November 10, 2012 10:02
-
-
Save flada-auxv/4050601 to your computer and use it in GitHub Desktop.
じゃんけんプログラムを作ろうhttps://codeiq.jp/ace/suginoy/q72
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
# -*- coding: utf-8 -*- | |
class Janken | |
HAND = %i(グー チョキ パー) | |
def hand | |
@hand = HAND.shuffle.first | |
end | |
def versus(other) | |
other = other.hand | |
case hand | |
when :グー | |
win(other) if other == :チョキ | |
draw(other) if other == :グー | |
lose(other) if other == :パー | |
when :チョキ | |
win(other) if other == :パー | |
draw(other) if other == :チョキ | |
lose(other) if other == :グー | |
when :パー | |
win(other) if other == :グー | |
draw(other) if other == :パー | |
lose(other) if other == :チョキ | |
end | |
end | |
private | |
def win(other) | |
puts "左の人が勝ちました。 左「#{@hand}」右「#{other}」" | |
end | |
def draw(other) | |
puts "あいこでした。 左「#{@hand}」右「#{other}」" | |
end | |
def lose(other) | |
puts "右の人が勝ちました。 左「#{@hand}」右「#{other}」" | |
end | |
end | |
left = Janken.new | |
right = Janken.new | |
10.times { left.versus right } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment