Skip to content

Commit

Permalink
divide files
Browse files Browse the repository at this point in the history
  • Loading branch information
zakuni committed Oct 10, 2013
1 parent d557fa7 commit 05db55a
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 109 deletions.
111 changes: 3 additions & 108 deletions lib/Boy2Man.rb
@@ -1,110 +1,5 @@
#!/usr/bin/env ruby
# -*- coding: utf-8 -*-
require "Boy2Man/version"

module Boy2Man
HANDS = ["グー", "チョキ", "パー"]

def self.play
stand = Boy2Man.new
loop do
print '> '
hand = gets.chomp
case hand
when "", "bye", "exit"
exit
when *HANDS
puts stand.match(hand)
when "history"
puts stand.history
when "reset"
stand.reset
else
puts stand.select_hand
end
end
end

# @!attribute [r] history
# @return [Array] the history of player's hand
class Boy2Man
attr_reader :history

def initialize
@history = Array.new
end

def history
# retrun deep copy of history, to prevent history to be changed.
Marshal.load(Marshal.dump(@history))
end

# @return [Array] resets history
def reset
@history.clear
end

# @return [String]
def match(hand)
case hand
when *HANDS
# 先に手を決めておかないと後出しになる
selected = select_hand
@history.push hand
case judge(hand, selected)
when hand
selected + "\nYou Win!"
when selected
selected + "\nYou Lose!"
else
selected + "\nDraw!"
end
else
end
end

# @return [String]
def select_hand
case predict
when "グー" then "パー"
when "チョキ" then "グー"
when "パー" then "チョキ"
end
end

private
def predict
@history.empty? ? %w(グー チョキ パー).sample : @history.sample
end

def judge(a, b)
case a
when "グー"
if b == "チョキ"
return a
elsif b == "パー"
return b
else
return nil
end
when "チョキ"
if b == "パー"
return a
elsif b == "グー"
return b
else
return nil
end
when "パー"
if b == "グー"
return a
elsif b == "チョキ"
return b
else
return nil
end
end
end

end
end
require "Boy2Man/cli"
require "Boy2Man/janken"
require "Boy2Man/version"
23 changes: 23 additions & 0 deletions lib/Boy2Man/cli.rb
@@ -0,0 +1,23 @@
module Boy2Man

def self.play
stand = Boy2Man.new
loop do
print '> '
hand = gets.chomp
case hand
when "", "bye", "exit"
exit
when *HANDS
puts stand.match(hand)
when "history"
puts stand.history
when "reset"
stand.reset
else
puts stand.select_hand
end
end
end

end
86 changes: 86 additions & 0 deletions lib/Boy2Man/janken.rb
@@ -0,0 +1,86 @@
module Boy2Man
HANDS = ["グー", "チョキ", "パー"]

# @!attribute [r] history
# @return [Array] the history of player's hand
class Janken
attr_reader :history

def initialize
@history = Array.new
end

def history
# retrun deep copy of history, to prevent history to be changed.
Marshal.load(Marshal.dump(@history))
end

# @return [Array] resets history
def reset
@history.clear
end

# @return [String]
def match(hand)
case hand
when *HANDS
# 先に手を決めておかないと後出しになる
selected = select_hand
@history.push hand
case judge(hand, selected)
when hand
selected + "\nYou Win!"
when selected
selected + "\nYou Lose!"
else
selected + "\nDraw!"
end
else
end
end

# @return [String]
def select_hand
case predict
when "グー" then "パー"
when "チョキ" then "グー"
when "パー" then "チョキ"
end
end

private
def predict
@history.empty? ? %w(グー チョキ パー).sample : @history.sample
end

def judge(a, b)
case a
when "グー"
if b == "チョキ"
return a
elsif b == "パー"
return b
else
return nil
end
when "チョキ"
if b == "パー"
return a
elsif b == "グー"
return b
else
return nil
end
when "パー"
if b == "グー"
return a
elsif b == "チョキ"
return b
else
return nil
end
end
end

end
end
2 changes: 1 addition & 1 deletion test/test_Boy2Man.rb
Expand Up @@ -6,7 +6,7 @@ class TestBoy2Man < MiniTest::Unit::TestCase
include Boy2Man

def setup
@stand = Boy2Man.new
@stand = Janken.new
end

def test_select_hand
Expand Down

0 comments on commit 05db55a

Please sign in to comment.