class JankenLogic

Class of Pikarin Janken

Attributes

io[W]

Public Class Methods

new() click to toggle source
Calls superclass method BaseLogic::new
# File lib/cureutils/logic/janken_logic.rb, line 9
def initialize
  super
  # Set the sleep time 0
  Rubicure::Girl.sleep_sec = 0
  # 0: win, 1: lose, 2: aiko
  @result_table = [[2, 0, 1, 1],
                   [1, 2, 0, 1],
                   [0, 1, 2, 1],
                   [0, 0, 0, 2]]
  @result_idx = %w[あなたのかち あなたのまけ あいこ]
  @te_idx = %w[グー チョキ パー グッチョッパー]
  @te_hash = Hash[[@te_idx, (0..3).map(&:to_s)].transpose]
  @buf = []
end

Public Instance Methods

generated_te() click to toggle source
# File lib/cureutils/logic/janken_logic.rb, line 39
def generated_te
  @buf.last =~ /(#{@te_idx.join('|')})/
  @te_hash[Regexp.last_match(1)].to_i
end
input_te() click to toggle source
# File lib/cureutils/logic/janken_logic.rb, line 44
def input_te
  @out.print('1...グー, 2...チョキ, 3...パー : ')
  # TODO: Check input and raise the error.
  player_te = $stdin.gets
  player_te.to_i - 1
end
janken() click to toggle source
# File lib/cureutils/logic/janken_logic.rb, line 30
def janken
  Cure.peace.io = self
  Cure.peace.janken
  @buf[0..1].each do |msg|
    @out.puts msg
  end
  judge
end
judge() click to toggle source
# File lib/cureutils/logic/janken_logic.rb, line 51
def judge
  cure_te = generated_te
  player_te = input_te
  result_num = @result_table[player_te][cure_te]
  result = @result_idx[result_num]
  print_results(@te_idx[player_te], @te_idx[cure_te], result)
  result_num
end
print_results(player_te, cure_te, result) click to toggle source
puts(input) click to toggle source
# File lib/cureutils/logic/janken_logic.rb, line 26
def puts(input)
  @buf << input
end