Class: CureJankenManager

Inherits:
Object
  • Object
show all
Defined in:
lib/cureutils/cure_janken_manager.rb

Overview

Class of Pikarin Janken

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCureJankenManager



6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/cureutils/cure_janken_manager.rb', line 6

def initialize
  # 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 = []
  @out = $stdout
end

Instance Attribute Details

#io=(value) ⇒ Object (writeonly)

Sets the attribute io



21
22
23
# File 'lib/cureutils/cure_janken_manager.rb', line 21

def io=(value)
  @io = value
end

Instance Method Details

#generated_teObject



40
41
42
43
# File 'lib/cureutils/cure_janken_manager.rb', line 40

def generated_te
  @buf.last =~ /(#{@te_idx.join('|')})/
  @te_hash[Regexp.last_match(1)].to_i
end

#input_teObject



45
46
47
48
49
50
# File 'lib/cureutils/cure_janken_manager.rb', line 45

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

#jankenObject



31
32
33
34
35
36
37
38
# File 'lib/cureutils/cure_janken_manager.rb', line 31

def janken
  Cure.peace.io = self
  Cure.peace.janken
  @buf[0..1].each do |msg|
    @out.puts msg
  end
  judge
end

#judgeObject



52
53
54
55
56
57
58
59
# File 'lib/cureutils/cure_janken_manager.rb', line 52

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


61
62
63
64
65
66
67
68
# File 'lib/cureutils/cure_janken_manager.rb', line 61

def print_results(player_te, cure_te, result)
  @out.puts
  @out.puts 'あなた: ' + player_te
  @out.puts 'キュアピース: ' + cure_te
  @out.puts
  @out.puts '[結果]'
  @out.puts result
end

#puts(input) ⇒ Object



27
28
29
# File 'lib/cureutils/cure_janken_manager.rb', line 27

def puts(input)
  @buf << input
end

#source_output(output = $stdout) ⇒ Object



23
24
25
# File 'lib/cureutils/cure_janken_manager.rb', line 23

def source_output(output = $stdout)
  @out = output
end