Class: JankenLogic

Inherits:
BaseLogic show all
Defined in:
lib/cureutils/logic/janken_logic.rb

Overview

Class of Pikarin Janken

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from BaseLogic

#source_err, #source_input, #source_output

Constructor Details

#initializeJankenLogic

Returns a new instance of JankenLogic.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# 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

Instance Attribute Details

#io=(value) ⇒ Object (writeonly)

Sets the attribute io

Parameters:

  • value

    the value to set the attribute io to.



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

def io=(value)
  @io = value
end

Instance Method Details

#generated_teObject



39
40
41
42
# 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_teObject



44
45
46
47
48
49
# 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

#jankenObject



30
31
32
33
34
35
36
37
# 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

#judgeObject



51
52
53
54
55
56
57
58
# 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


60
61
62
63
64
65
66
67
# File 'lib/cureutils/logic/janken_logic.rb', line 60

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



26
27
28
# File 'lib/cureutils/logic/janken_logic.rb', line 26

def puts(input)
  @buf << input
end