Sha256: 398cdc3531d7faa334446fa6f6243d044add5c01d965b253d881a332e0174ed6

Contents?: true

Size: 1.5 KB

Versions: 8

Compression:

Stored size: 1.5 KB

Contents

# frozen_string_literal: true

require 'cureutils/logic/base_logic'

#
# Class of Pikarin Janken
#
class JankenLogic < BaseLogic
  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

  attr_writer :io

  def puts(input)
    @buf << input
  end

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

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

  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

  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

  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
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
cureutils-1.7.0 lib/cureutils/logic/janken_logic.rb
cureutils-1.6.0 lib/cureutils/logic/janken_logic.rb
cureutils-1.5.0 lib/cureutils/logic/janken_logic.rb
cureutils-1.4.0 lib/cureutils/logic/janken_logic.rb
cureutils-1.3.1 lib/cureutils/logic/janken_logic.rb
cureutils-1.3.0 lib/cureutils/logic/janken_logic.rb
cureutils-1.2.0 lib/cureutils/logic/janken_logic.rb
cureutils-1.1.1 lib/cureutils/logic/janken_logic.rb