Class: Numeron::Calculator
- Inherits:
-
Object
- Object
- Numeron::Calculator
- Defined in:
- lib/numeron/calculator.rb
Instance Attribute Summary collapse
-
#answer_size ⇒ Object
解の桁数(未実装).
-
#histories ⇒ Object
コール履歴.
-
#mays ⇒ Object
各桁における解の可能性として考えられる数値.
-
#possibilities ⇒ Object
解として可能性があるデータリスト.
-
#slash_number ⇒ Object
Slash Number.
Instance Method Summary collapse
-
#change(position, is_high) ⇒ Object
チェンジ チェンジする桁数の指定とその数値がhigh or lowどちらなのかを宣言する.
-
#double(number, position) ⇒ Object
ダブル 相手がダブルを使用し、カードの数値を公開した場合.
-
#high_and_low(result = []) ⇒ Object
ハイ&ロー.
-
#initialize(answer_size = 3) ⇒ Calculator
constructor
A new instance of Calculator.
-
#input(attack, eat, bite) ⇒ Boolean
コールした数値とその結果を設定.
-
#shuffle ⇒ Object
シャッフル.
-
#slash(number) ⇒ Object
スラッシュ.
-
#target(number, position = nil) ⇒ Object
ターゲット.
Constructor Details
#initialize(answer_size = 3) ⇒ Calculator
Returns a new instance of Calculator.
16 17 18 19 20 21 22 |
# File 'lib/numeron/calculator.rb', line 16 def initialize(answer_size = 3) @histories = [] @possibilities = nil @mays = [(0..9).to_a, (0..9).to_a, (0..9).to_a] @answer_size = answer_size @slash_number = nil end |
Instance Attribute Details
#answer_size ⇒ Object
解の桁数(未実装)
12 13 14 |
# File 'lib/numeron/calculator.rb', line 12 def answer_size @answer_size end |
#histories ⇒ Object
コール履歴
6 7 8 |
# File 'lib/numeron/calculator.rb', line 6 def histories @histories end |
#mays ⇒ Object
各桁における解の可能性として考えられる数値
10 11 12 |
# File 'lib/numeron/calculator.rb', line 10 def mays @mays end |
#possibilities ⇒ Object
解として可能性があるデータリスト
8 9 10 |
# File 'lib/numeron/calculator.rb', line 8 def possibilities @possibilities end |
#slash_number ⇒ Object
Slash Number
14 15 16 |
# File 'lib/numeron/calculator.rb', line 14 def slash_number @slash_number end |
Instance Method Details
#change(position, is_high) ⇒ Object
チェンジ チェンジする桁数の指定とその数値がhigh or lowどちらなのかを宣言する
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/numeron/calculator.rb', line 116 def change(position, is_high) raise ArgumentError, 'Invalid argument. position is 0 to 3' if position < 0 && position > 2 _possibilities = is_high ? [5, 6, 7, 8, 9] : [0, 1, 2, 3, 4] @mays.each_with_index do |may, i| _possibilities -= may if may.size == 1 if i != position tmp = may.select {|f| is_high ? f > 4 : f <= 4 } next if tmp.size != 1 @mays[i] -= tmp end end # 3種類に絞りこまれていたら、残りの種類であることが確定 x = (@mays[0] + @mays[1] + @mays[2]).uniq _possibilities -= x if x.size == 3 @mays[position] = _possibilities # 可能性の再計算 @possibilities = recalculate end |
#double(number, position) ⇒ Object
ダブル 相手がダブルを使用し、カードの数値を公開した場合
139 140 141 |
# File 'lib/numeron/calculator.rb', line 139 def double(number, position) target(number, position) end |
#high_and_low(result = []) ⇒ Object
ハイ&ロー
145 146 147 148 149 150 151 152 |
# File 'lib/numeron/calculator.rb', line 145 def high_and_low(result = []) raise ArgumentError, 'Invalid argument.' if result.size != 3 result.each_with_index do |f, i| @mays[i] = @mays[i] & (f ? [5, 6, 7, 8, 9] : [0, 1, 2, 3, 4]) end update_possibilities(recalculate) end |
#input(attack, eat, bite) ⇒ Boolean
コールした数値とその結果を設定
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/numeron/calculator.rb', line 29 def input(attack, eat, bite) attack = attack.split(//).map(&:to_i) eat = eat.to_i bite = bite.to_i if attack.size != @answer_size || eat > @answer_size || bite > @answer_size && eat + bite > @answer_size raise ArgumentError, 'Invalid argument. ' end @histories << {attack: attack, eat: eat, bite: bite} if eat == 0 && bite == 0 zero_eat_zero_bite(attack) elsif eat == 0 && bite == 3 zero_eat_three_bite(attack) elsif eat == 0 && bite == 2 zero_eat_two_bite(attack) elsif eat == 0 && bite == 1 zero_eat_one_bite(attack) elsif eat == 1 && bite == 2 one_eat_two_bite(attack) elsif eat == 1 && bite == 1 one_eat_one_bite(attack) elsif eat == 1 && bite == 0 one_eat_zero_bite(attack) elsif eat == 2 && bite == 0 two_eat_zero_bite(attack) elsif eat == 3 && bite == 0 @possibilities = [] else return false end return true end |
#shuffle ⇒ Object
シャッフル
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/numeron/calculator.rb', line 66 def shuffle # 使用確定数字を計算(候補数が1つの場合) fixed = @mays.select{|f| f.size == 1}.flatten @mays[0] = @mays[0] | @mays[1] | @mays[2] @mays[1] = @mays[0] | @mays[1] | @mays[2] @mays[2] = @mays[0] | @mays[1] | @mays[2] list = [] @mays[0].each do |i| @mays[1].each do |j| next if i == j @mays[2].each do |k| next if i == k || j == k if fixed.size == 0 list << validation(i, j, k) else fixed.each do |f| # ちょっと計算量がおおいか。。。 list << validation(f, j, k) list << validation(i, f, k) list << validation(i, j, f) end end end end end @possibilities = list.compact.uniq @histories.each do |history| eat_and_bite = history[:eat] + history[:bite] if eat_and_bite == 1 one_eat_or_one_bite(history[:attack]) elsif eat_and_bite == 2 two_eat_or_two_bite(history[:attack]) end end calc_slash end |
#slash(number) ⇒ Object
スラッシュ
107 108 109 110 |
# File 'lib/numeron/calculator.rb', line 107 def slash(number) @slash_number = number calc_slash end |
#target(number, position = nil) ⇒ Object
ターゲット
157 158 159 160 161 162 163 164 165 166 |
# File 'lib/numeron/calculator.rb', line 157 def target(number, position = nil) raise ArgumentError, 'Invalid argumet. number is 0 to 9' if number < 0 && number > 9 raise ArgumentError, 'Invalid argument. position is nil or 0 to 3' if !position.nil? && position < 0 && position > 2 if position.nil? 3.times {|i| @mays[i] = @mays[i] - [number]} else @mays[position] = [number] end update_possibilities(recalculate) end |