Class: JankenContoller

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

Overview

Wrapper class of Pikarin Janken

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.io=(value) ⇒ Object (writeonly)

Sets the attribute io

Parameters:

  • value

    the value to set the attribute io to.



22
23
24
# File 'lib/cureutils/janken_controller.rb', line 22

def io=(value)
  @io = value
end

Class Method Details

.generated_teObject



38
39
40
41
# File 'lib/cureutils/janken_controller.rb', line 38

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

.initializeObject



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

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 = []
  @io = $stdout
end

.input_teObject



43
44
45
46
47
48
# File 'lib/cureutils/janken_controller.rb', line 43

def input_te
  @io.print("1...グー, 2...チョキ, 3...パー : ")
  # TODO: Check input and raise the error.
  player_te = $stdin.gets
  player_te.to_i - 1
end

.jankenObject



28
29
30
31
32
33
34
35
36
# File 'lib/cureutils/janken_controller.rb', line 28

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

.judgeObject



50
51
52
53
54
55
56
57
# File 'lib/cureutils/janken_controller.rb', line 50

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


59
60
61
62
63
64
65
66
# File 'lib/cureutils/janken_controller.rb', line 59

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

.puts(input) ⇒ Object



24
25
26
# File 'lib/cureutils/janken_controller.rb', line 24

def puts(input)
  @buf << input
end