-
Notifications
You must be signed in to change notification settings - Fork 5
/
planetwars.rb
148 lines (124 loc) · 3.45 KB
/
planetwars.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
class Fleet
attr_reader :owner, :num_ships, :source_planet,
:destination_planet, :total_trip_length, :turns_remaining
def initialize(owner, num_ships, source_planet,
destination_planet, total_trip_length,
turns_remaining)
@owner, @num_ships = owner, num_ships
@source_planet = source_planet
@destination_planet = destination_planet
@total_trip_length = total_trip_length
@turns_remaining = turns_remaining
end
end
class Planet
attr_reader :planet_id, :growth_rate, :x, :y
attr_accessor :owner, :num_ships
def initialize(planet_id, owner, num_ships, growth_rate, x, y)
@planet_id, @owner, @num_ships = planet_id, owner, num_ships
@growth_rate, @x, @y = growth_rate, x, y
end
def add_ships(n)
@num_ships += amt
end
def remove_ships(n)
@num_ships -= n
end
end
class PlanetWars
attr_reader :planets, :fleets
def initialize(game_state)
parse_game_state(game_state)
end
def num_planets
@planets.length
end
def get_planet(id)
@planets[id]
end
def num_fleets
@fleets.length
end
def get_fleet(id)
@fleets[id]
end
def my_planets
@planets.select {|planet| planet.owner == 1 }
end
def neutral_planets
@planets.select {|planet| planet.owner == 0 }
end
def enemy_planets
@planets.select {|planet| planet.owner > 1 }
end
def not_my_planets
@planets.reject {|planet| planet.owner == 1 }
end
def my_fleets
@fleets.select {|fleet| fleet.owner == 1 }
end
def enemy_fleets
@fleets.select {|fleet| fleet.owner > 1 }
end
def to_s
s = []
@planets.each do |p|
s << "P #{p.x} #{p.y} #{p.owner} #{p.num_ships} #{p.growth_rate}"
end
@fleets.each do |f|
s << "F #{f.owner} #{f.num_ships} #{f.source_planet} #{f.destination_planet} #{f.total_trip_length} #{f.turns_remaining}"
end
return s.join("\n")
end
def distance(source, destination)
Math::hypot( (source.x - destination.x), (source.y - destination.y) )
end
def travel_time(source, destination)
distance(source, destination).ceil
end
def issue_order(source, destination, num_ships)
puts "#{source} #{destination} #{num_ships}"
STDOUT.flush
end
def is_alive(player_id)
((@planets.select{|p| p.owner == player_id }).length > 0) || ((@fleets.select{|p| p.owner == player_id }).length > 0)
end
def parse_game_state(s)
@planets = []
@fleets = []
lines = s.split("\n")
planet_id = 0
lines.each do |line|
line = line.split("#")[0]
tokens = line.split(" ")
next if tokens.length == 1
if tokens[0] == "P"
return 0 if tokens.length != 6
p = Planet.new(planet_id,
tokens[3].to_i, # owner
tokens[4].to_i, # num_ships
tokens[5].to_i, # growth_rate
tokens[1].to_f, # x
tokens[2].to_f) # y
planet_id += 1
@planets << p
elsif tokens[0] == "F"
return 0 if tokens.length != 7
f = Fleet.new(tokens[1].to_i, # owner
tokens[2].to_i, # num_ships
tokens[3].to_i, # source
tokens[4].to_i, # destination
tokens[5].to_i, # total_trip_length
tokens[6].to_i) # turns_remaining
@fleets << f
else
return 0
end
end
return 1
end
def finish_turn
puts "go"
STDOUT.flush
end
end