Class: Berlin::Fake::State
- Inherits:
-
Object
- Object
- Berlin::Fake::State
- Defined in:
- lib/ai/fake.rb
Instance Attribute Summary collapse
-
#state ⇒ Object
Returns the value of attribute state.
Instance Method Summary collapse
- #apply_moves(moves) ⇒ Object
- #as_json ⇒ Object
-
#initialize(from_json) ⇒ State
constructor
A new instance of State.
- #inspect ⇒ Object
- #remove_soldiers(move) ⇒ Object
- #spawn(node_ids) ⇒ Object
- #winner? ⇒ Boolean
Constructor Details
Instance Attribute Details
#state ⇒ Object
Returns the value of attribute state.
262 263 264 |
# File 'lib/ai/fake.rb', line 262 def state @state end |
Instance Method Details
#apply_moves(moves) ⇒ Object
270 271 272 273 274 275 276 277 278 279 280 281 |
# File 'lib/ai/fake.rb', line 270 def apply_moves(moves) conflicts = {} errors = [] puts "[Moves]" moves.each do |move| conflict = (conflicts[move.to] ||= ConflictState.new(move.to)) remove_soldiers(move) conflict.add_soldiers(move.player_id, move.number_of_soldiers) end conflicts.each { |node_id, conflict| conflict.process(@state[node_id]) } end |
#as_json ⇒ Object
299 300 301 302 303 304 305 306 307 |
# File 'lib/ai/fake.rb', line 299 def as_json @state.map do |node_id, node_state| { 'node_id' => node_state.node_id, 'player_id' => node_state.player_id, 'number_of_soldiers' => node_state.number_of_soldiers } end end |
#inspect ⇒ Object
309 310 311 |
# File 'lib/ai/fake.rb', line 309 def inspect as_json end |
#remove_soldiers(move) ⇒ Object
283 284 285 286 287 288 289 290 291 292 293 |
# File 'lib/ai/fake.rb', line 283 def remove_soldiers(move) origin = @state[move.from] if origin.player_id != move.player_id puts "Trying to move #{move.number_of_soldiers} soldiers from ##{move.from}. Node ##{move.from} belongs to #{origin.player_id}" elsif origin.number_of_soldiers < move.number_of_soldiers puts "Trying to move #{move.number_of_soldiers} soldiers from ##{move.from}. Only #{origin.number_of_soldiers} soldiers available" else origin.number_of_soldiers -= move.number_of_soldiers puts "\t[#{move.player_id}] Moves #{move.number_of_soldiers} soldiers from ##{move.from} to ##{move.to}" end end |
#spawn(node_ids) ⇒ Object
295 296 297 |
# File 'lib/ai/fake.rb', line 295 def spawn(node_ids) node_ids.each { |id| @state[id].number_of_soldiers += 1 if @state[id].player_id } end |
#winner? ⇒ Boolean
313 314 315 |
# File 'lib/ai/fake.rb', line 313 def winner? @state.map{ |node_id, node_state| node_state.player_id }.compact.length == 1 end |