Class: Numeron::Simulator

Inherits:
Object
  • Object
show all
Defined in:
lib/numeron/simulator.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSimulator

Returns a new instance of Simulator.



6
7
8
# File 'lib/numeron/simulator.rb', line 6

def initialize
  @verbose = false
end

Instance Attribute Details

#verboseObject

Returns the value of attribute verbose.



5
6
7
# File 'lib/numeron/simulator.rb', line 5

def verbose
  @verbose
end

Class Method Details

.build_sample_answers(num = 10) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/numeron/simulator.rb', line 64

def self.build_sample_answers(num = 10)
  lists = []
  (0..9).to_a.each do |i|
    (0..9).to_a.each do |j|
      next if i == j
      (0..9).to_a.each do |k|
        next if i == k || j == k
        lists << i.to_s + j.to_s + k.to_s
      end
    end
  end

  [].tap do |result|
    num.times { result << lists.sample }
  end
end

.calc_statistics(result) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
# File 'lib/numeron/simulator.rb', line 81

def self.calc_statistics(result)
  sum = 0
  _result = []
  result.each do |f|
    sum += f[:times]
    _result << f[:times]
  end
  _result.sort!

  { max: _result.max, min: _result.min, median: _result[_result.size / 2], average: (sum.to_f / result.size.to_f) }
end

Instance Method Details

#calc_answer(answer, first_attack, &block) ⇒ Object

答えが見つかる回数を計算する



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/numeron/simulator.rb', line 19

def calc_answer(answer, first_attack, &block)
  calculator = Numeron::Calculator.new
  times = 1
  result = eat_and_bite(answer, first_attack)
  return times if result[:eat] == 3
  calculator.input(first_attack, result[:eat], result[:bite])
  output(times, first_attack, result[:eat], result[:bite], calculator.possibilities.size)

  while times <= 20 # 計算機不具合での無限ループ回避
    times += 1
    input = block.call(calculator)
    result = eat_and_bite(answer, input)
    if result[:eat] == 3
      output(times, input, result[:eat], result[:bite], 0)
      break if result[:eat] == 3
    else
      calculator.input(input, result[:eat], result[:bite])
      output(times, input, result[:eat], result[:bite], calculator.possibilities.size)
    end
  end
  times
end

#eat_and_bite(answer, input) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/numeron/simulator.rb', line 48

def eat_and_bite(answer, input)
  inputs = input.split(//).map(&:to_i)
  answers = answer.split(//).map(&:to_i)

  # eatの計算
  { eat: 0, bite: 0 }.tap do |result|
    inputs.each_with_index{|f, i|
      if answers[i] == f
        result[:eat] = result[:eat] + 1
      elsif answers.include?(f)
        result[:bite] = result[:bite] + 1
      end
    }
  end
end

#output(*args) ⇒ Object



42
43
44
45
46
# File 'lib/numeron/simulator.rb', line 42

def output(*args)
  if @verbose
    puts args.join(',')
  end
end

#run(answers, first_attack, &block) ⇒ Object



10
11
12
13
14
15
16
# File 'lib/numeron/simulator.rb', line 10

def run(answers, first_attack, &block)
  [].tap do |result|
    answers.each {|answer|
      result << {answer: answer, times: calc_answer(answer, first_attack, &block)}
    }
  end
end