Skip to content

Instantly share code, notes, and snippets.

@yatemmma
Created August 30, 2012 17:00
Show Gist options
  • Save yatemmma/3533294 to your computer and use it in GitHub Desktop.
Save yatemmma/3533294 to your computer and use it in GitHub Desktop.
じゃんけんプログラム
class Input < String
def exit?
self =~ /^(exit|quit|q)$/
end
def gu?
self =~ /^(gu|g)$/
end
def choki?
self =~ /^(choki|c)$/
end
def pa?
self =~ /^(pa|p)$/
end
def right?
self.gu? || self.choki? || self.pa?
end
end
# 拳クラス
class Ken
GU = 0
CHOKI = 1
PA = 2
attr :v
def initialize(input = nil)
# 引数無しの場合はランダム
if input.nil?
@v = rand 3
else
if input.gu?
@v = GU
elsif input.choki?
@v = CHOKI
elsif input.pa?
@v = PA
end
end
end
def to_s
case @v
when GU
"g"
when CHOKI
"c"
when PA
"p"
end
end
def fight(other)
# 大小比較する。差が2以上の場合は小さい方に3を足して比較
self_value = self.v
other_value = other.v
if (other.v - self.v).abs > 1
if other.v > self.v
self_value = self.v + 3
else
other_value = other.v + 3
end
end
other_value <=> self_value
end
end
$win = 0
$even = 0
$lose = 0
def exit_game
puts
puts "#{$win} wins, #{$lose} losses, #{$even} evens."
end
# enjoy!
puts "input g or c or p or exit."
while text = gets
input = Input.new(text)
if input.exit?
exit_game
break
end
next unless input.right?
yours = Ken.new input.chomp
mine = Ken.new
print " #{yours.to_s} vs #{mine.to_s} "
case yours.fight(mine)
when 1
$win += 1
puts "you win!"
when 0
$even += 1
puts "even"
when -1
$lose += 1
puts "you lose..."
end
end
@yatemmma
Copy link
Author

=~ はマッチした文字位置を返す。マッチしなかったらnull
self =~ /gu|g/ は"efg"にもマッチするから /^(gu|g)$/

@yatemmma
Copy link
Author

【仕様】
ユーザからの入力が下記のいずれかに該当する場合は、対応する処理を行う。
該当しない場合は何も行わず、次の入力を待つ。

exit アプリケーションを終了
g グー でジャンケンして結果を表示
c チョキ 〃
p パー 〃

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment