Class: Numeron::Solver

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSolver

Returns a new instance of Solver.



6
7
8
9
10
# File 'lib/numeron/solver.rb', line 6

def initialize
  @calc = Numeron::Calculator.new
  @card_size = 3
  @used_items = {double: false, shuffle: false, change: false, target: false, high_and_low: false, slash: false}
end

Instance Attribute Details

#calcObject

Returns the value of attribute calc.



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

def calc
  @calc
end

#card_sizeObject

Returns the value of attribute card_size.



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

def card_size
  @card_size
end

#used_itemsObject

Returns the value of attribute used_items.



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

def used_items
  @used_items
end

Instance Method Details

#changeObject



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/numeron/solver.rb', line 107

def change
  position = nil
  is_high = nil
  while 1
    print "Input change position: "
    f = STDIN.gets.chomp.to_i
    if f >= 0 && f <= 2
      position = f
      break
    else
      puts "Input error. Please input 0 to 2 number."
    end
  end

  while 1
    print "Input high or low: "
    f = STDIN.gets.chomp.downcase
    if f == 'high' || f == 'low'
      is_high = f == 'high'
      break
    else
      puts "Input error. Please input high or low."
    end
  end

  @calc.change(position, is_high)
end

#doubleObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/numeron/solver.rb', line 51

def double
  position = nil
  number = nil

  while 1
    print "Input open number: "
    f = STDIN.gets.chomp.to_i
    if f >= 0 && f <= 9
      number = f
      break
    else
      puts "Input error. Please input 0 to 9 number."
    end
  end

  while 1
    print "Input open position [0, 1, 2]: "
    f = STDIN.gets.chomp.to_i
    if f >= 0 && f <= 2
      position = f
      break
    else
      puts "Input error. Please input 0 or 1 or 2."
    end
  end

  @calc.double(number, position)
end

#helpObject



30
31
32
33
34
35
36
# File 'lib/numeron/solver.rb', line 30

def help
  puts "----- help messege -----"
  puts "item ... using item, shortcut 0 or i"
  puts "question ... call 3 digits numbers, shortcut 1 or q"
  puts "think ... thinking answer, shortcut 2 or t"
  puts "finish ... exit this console, shortcut 3 or f"
end

#high_and_lowObject



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/numeron/solver.rb', line 135

def high_and_low
  result = []
  3.times do |i|
    while 1
      print "position " + i.to_s + " is high? [yes|no]: "
      input = STDIN.gets.chomp.downcase
      if ['yes', 'y', 'no', 'n'].include?(input)
        result[i] = (input == 'yes' || input == 'y')
        break
      else
        puts "Input error. Please input yes or no."
      end
    end
  end
  @calc.high_and_low(result)
end

#itemObject

アイテムの使用



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/numeron/solver.rb', line 39

def item
  @used_items.each do |f|
    next if f[1] == true
    print "Using " + f[0].to_s + " ? [yes]: "
    input = STDIN.gets.chomp.downcase
    next if input != 'yes' && input != 'y'
    send(f[0])
    @used_items[f[0]] = true
    break
  end
end

#questionObject



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/numeron/solver.rb', line 166

def question
  attack_number = nil
  eat = 0
  bite = 0
  while 1
    print "Attack number: "
    attack_number = STDIN.gets.chomp
    if @card_size == attack_number.split(//).size
      break
    else
      puts 'Required ' + @card_size.to_s + ' digits.'
    end
  end

  while 1
    while 1
      print "Eat number: "
      eat = STDIN.gets.chomp.to_i
      if @card_size > eat
        break
      else
        puts 'Required less than ' + @card_size.to_s + ' digits.'
      end
    end

    while 1
      print "Bite number: "
      bite = STDIN.gets.chomp.to_i
      if @card_size >= bite
        break
      else
        print 'Required ' + @card_size.to_s + ' digits or less'
      end
    end
    if eat + bite <= @card_size
      break
    else
      puts "Error, Eat + Bite > " + @card_size.to_s
    end
  end

  @calc.input(attack_number, eat, bite)
end

#runObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/numeron/solver.rb', line 12

def run
  while 1
    print "action (item|question|think|finish|help): "
    input = STDIN.gets.chomp.downcase
    if %w(0 i item).include?(input)
      item
    elsif %w(1 q question).include?(input)
      question
    elsif %w(2 t think).include?(input)
      think
    elsif %w(3 f finish).include?(input)
      break
    else
      help
    end
  end
end

#shuffleObject



162
163
164
# File 'lib/numeron/solver.rb', line 162

def shuffle
  @calc.shuffle
end

#slashObject



152
153
154
155
156
157
158
159
160
# File 'lib/numeron/solver.rb', line 152

def slash
  while 1
    print "Input slash number: "
    f =STDIN.gets.chomp.to_i
    break if f >= 2 && f <= 9
    puts "Invalid slash number."
  end
  @calc.slash(f)
end

#targetObject



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/numeron/solver.rb', line 80

def target
  position = nil
  number = nil

  while 1
    print "Input open number: "
    f = STDIN.gets.chomp.to_i
    if f >= 0 && f <= 9
      number = f
      break
    else
      puts "Input error. Please input 0 to 9 number."
    end
  end

  while 1
    puts "Input open position."
    puts "If the enemy does not open card position, don't put anything"
    print "Input [0, 1, 2, or empty]: "
    f = STDIN.gets.chomp.to_i
    position = f if f >= 0 && f <= 2
    break
  end

  @calc.target(number, position)
end

#thinkObject



210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/numeron/solver.rb', line 210

def think
  puts "... thinking"
  puts "possibilities: " + @calc.possibilities.size.to_s
  analyzer = Numeron::Analyzer.new(@calc)
  if @calc.possibilities.size > 2
    result = {recommend: 0}
    if @calc.possibilities.size > 64
      result = analyzer.run_average_mode
    elsif @calc.possibilities.size > 21
      result = analyzer.run_possibilities
    else
      cases = [
        {eat: 0, bite: 0},
        {eat: 0, bite: 1},
        {eat: 0, bite: 2},
        {eat: 1, bite: 0},
        {eat: 1, bite: 1},
        {eat: 2, bite: 0}
      ]
      result = analyzer.run_average_mode(cases)
    end
    if result[:recommend].size > 0
      puts "Analyzer Answer: " + result[:recommend].sample.to_s
    else
      puts "Calculator Error."
    end
  end
  puts "Possibilitiy list random: " + @calc.possibilities.sample.to_s
end