Class: Berlin::Fake::Game

Inherits:
Object
  • Object
show all
Defined in:
lib/ai/fake.rb

Instance Method Summary collapse

Constructor Details

#initialize(number_of_ai) ⇒ Game

Returns a new instance of Game.



220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'lib/ai/fake.rb', line 220

def initialize(number_of_ai)
  @turn = 0

  @city_nodes = Berlin::Fake::MAP_DEFINITION['nodes'].select{ |node| node['type'] == 'city' }.map{ |node| node['id'] }
  @ai_games = 1.upto(number_of_ai).map do |n|
    ai_name = "AI ##{n}"
    node = Berlin::Fake::GAME_STATE.detect{ |node| node['node_id'] == @city_nodes[n - 1] }
    node['player_id'] = ai_name
    node['number_of_soldiers'] = 5
    ai_info = Berlin::Fake::GAME_INFO.dup
    ai_info['player_id']  = ai_name
    ai_info['game_id']    = n

    ai_game = Berlin::AI::Game.new
    ai_game.id                       = ai_info['game_id']
    ai_game.map                      = Berlin::AI::Map.parse(Berlin::Fake::MAP_DEFINITION.merge('player_id' => ai_info['player_id']))
    ai_game.player_id                = ai_info['player_id']
    ai_game.time_limit_per_turn      = ai_info['time_limit_per_turn']
    ai_game.maximum_number_of_turns  = ai_info['maximum_number_of_turns']
    ai_game.number_of_players        = ai_info['number_of_players']
    ai_game
  end

  player_name = "Player"
  player_info = Berlin::Fake::GAME_INFO.dup
  player_info['player_id']  = player_name
  player_info['game_id']    = 0
  node = Berlin::Fake::GAME_STATE.detect{ |node| node['node_id'] == @city_nodes[number_of_ai] }
  node['player_id'] = player_name
  node['number_of_soldiers'] = 5

  @player_game = Berlin::AI::Game.new
  @player_game.id                       = player_info['game_id']
  @player_game.map                      = Berlin::AI::Map.parse(Berlin::Fake::MAP_DEFINITION.merge('player_id' => player_info['player_id']))
  @player_game.player_id                = player_info['player_id']
  @player_game.time_limit_per_turn      = player_info['time_limit_per_turn']
  @player_game.maximum_number_of_turns  = player_info['maximum_number_of_turns']
  @player_game.number_of_players        = player_info['number_of_players']

  @state = Berlin::Fake::State.new(Berlin::Fake::GAME_STATE.dup)
end

Instance Method Details

#buffer_movesObject



292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
# File 'lib/ai/fake.rb', line 292

def buffer_moves
  moves = {}
  [@player_game, *@ai_games].each do |game|
    player_moves = {}

    game.moves.each do |move|
      move[:player_id] = game.player_id
      ref = "#{move[:from]}_#{move[:to]}"
      player_moves[ref] ||= Berlin::Fake::Move.new(game.player_id, move[:from], move[:to], 0)
      player_moves[ref].number_of_soldiers += move[:number_of_soldiers]
    end
    moves[game.player_id] = player_moves.values
  end
  moves
end

#generate_movesObject



308
309
310
311
312
313
314
315
316
317
318
# File 'lib/ai/fake.rb', line 308

def generate_moves
  @ai_games.each do |game|
    game.reset!
    game.update(@turn, @state.as_json)
    Berlin::Fake::Random.on_turn(game)
  end

  @player_game.update(@turn, @state.as_json)
  @player_game.reset!
  Berlin::AI::Player.on_turn(@player_game)
end

#pauseObject



273
274
275
276
# File 'lib/ai/fake.rb', line 273

def pause
  puts "Press any key to continue"
  gets
end

#runObject



262
263
264
265
266
267
268
269
270
271
# File 'lib/ai/fake.rb', line 262

def run
  puts Berlin::Fake::Display.new(@state).as_display
  pause
  while !@state.winner? && @turn < Berlin::Fake::GAME_INFO['maximum_number_of_turns']
    turn
    pause
    puts Berlin::Fake::Display.new(@state).as_display
    pause
  end
end

#spawnObject



288
289
290
# File 'lib/ai/fake.rb', line 288

def spawn
  @state.spawn(@city_nodes)
end

#turnObject



278
279
280
281
282
283
284
285
286
# File 'lib/ai/fake.rb', line 278

def turn
  @turn += 1
  generate_moves
  player_moves = buffer_moves

  @state.apply_moves(player_moves.values.flatten)

  spawn
end