-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.rb
91 lines (81 loc) · 2.67 KB
/
bot.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
63
64
65
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
#!/usr/bin/env ruby
# -*- coding: utf-8 -*-
require_relative 'world-map.rb'
require_relative 'round.rb'
require_relative 'server-settings.rb'
require_relative 'game-logic.rb'
STDOUT.sync = true
class Bot
def initialize()
@settings = ServerSettings.new()
@round = Round.new(@settings)
@world_map = WorldMap.new(@round, @settings)
@game_master = GameLogic.new(@world_map, @round)
end
def server_says_go(raw_line)
command = raw_line.shift
time = raw_line.shift.to_i
case command
when 'place_armies' then
@round.next_round()
response = @game_master.place_armies(time)
if response.nil? || response.empty?
'No moves'
else
response.map do |placement|
format('%s place_armies %i %i',
@settings[:your_bot],
placement[0],
placement[1])
end.join(', ') + ', '
end
when 'attack/transfer'
response = @game_master.attack_or_transfer(time)
if response.nil? || response.empty?
'No moves'
else
response.map do |move|
format('%s attack/transfer %i %i %i',
@settings[:your_bot],
move[0],
move[1],
move[2])
end.join(', ') + ', '
end
else
raise format('Unknown go command %s, please consult the manual', command)
end
end
def pick_starting_region(raw_line)
@game_master.pick_starting_region(raw_line.shift.to_i, raw_line.map!{|id| id.to_i})
end
def run()
while !$stdin.closed?
begin
current_line = $stdin.readline()
next unless current_line
current_line = current_line.strip().split()
next if current_line.empty? # Skip empty lines
command = current_line.shift
case command
when 'setup_map' then @world_map.setup_line(current_line)
when 'settings' then @settings.settings_line(current_line)
when 'update_map' then @world_map.update_map(current_line)
when 'opponent_moves' then @round.opponent_moves = current_line
when 'go' then puts(server_says_go(current_line))
when 'pick_starting_region' then puts(pick_starting_region(current_line))
when 'Round', 'Output' then next
when 'Maximum', 'Response' then next # This is only for test input
else
raise(format('Unknown top command %s, consult the manual on theaigames.com',
command))
end
rescue EOFError => eofError
break
end
end
end
end
if __FILE__ == $0
Bot.new().run()
end