Created
November 28, 2012 11:49
-
-
Save hanachin/4160690 to your computer and use it in GitHub Desktop.
挑戦者求む!【Ruby】じゃんけんプログラムを作ろう by フリーランス suginoy suginoy│CodeIQ 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
#encoding: utf-8 | |
class Janken | |
def self.hands | |
%w(グー チョキ パー) | |
end | |
def self.win | |
[%w(グー チョキ), %w(チョキ パー), %w(パー グー)] | |
end | |
def self.lose | |
win.map(&:reverse) | |
end | |
def self.draw | |
hands.zip(hands) | |
end | |
def hand | |
Janken.hands[rand Janken.hands.size] | |
end | |
def versus(right) | |
left_hand, right_hand = hand, right.hand | |
case [left_hand, right_hand] | |
when *Janken.draw | |
"あいこでした。「#{left_hand}」" | |
when *Janken.win | |
"左の人が勝ちました。右「#{right_hand}」左「#{left_hand}」" | |
when *Janken.lose | |
"右の人が勝ちました。右「#{right_hand}」左「#{left_hand}」" | |
else | |
raise "どっちが勝ったかわかりません。右「#{right_hand}」、左「#{left_hand}」" | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment