class Boy2Man::Janken
@!attribute [r] history @return [Array] the history of player’s hand
Attributes
history[R]
Public Class Methods
new()
click to toggle source
# File lib/Boy2Man/janken.rb, line 38 def initialize @history = Array.new end
Public Instance Methods
pon(hand)
click to toggle source
@return [String]
# File lib/Boy2Man/janken.rb, line 53 def pon(hand) case hand when *HANDS # 先に手を決めておかないと後出しになる selected = case predict when "グー" then "パー" when "チョキ" then "グー" when "パー" then "チョキ" end @history.push hand selected else end end
reset()
click to toggle source
@return [Array] resets history
# File lib/Boy2Man/janken.rb, line 48 def reset @history.clear end
Private Instance Methods
predict()
click to toggle source
# File lib/Boy2Man/janken.rb, line 73 def predict possible_hands = Array.new if @history.length >= 3 @history.each_with_index do |hand, i| # 最後に出されたと、その前の手から、次の手を予想する if hand == @history.last(2)[0] && @history[i+1] == @history.last possible_hands.push @history[i+2] if @history[i+2] != nil end end end if possible_hands.empty? || (1 < @history.length && @history.length < 3) @history.each_with_index do |hand, i| # 最後に出された手から、次の手を予想する if hand == @history.last possible_hands.push @history[i+1] if @history[i+1] != nil end end end possible_hands.empty? ? %w(グー チョキ パー).sample : possible_hands.sample end