-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathRageBot.py
112 lines (97 loc) · 3.07 KB
/
RageBot.py
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
#!/usr/bin/env python
#
"""
// The DoTurn function is where your code goes. The PlanetWars object contains
// the state of the game, including information about all planets and fleets
// that currently exist. Inside this function, you issue orders using the
// pw.IssueOrder() function. For example, to send 10 ships from planet 3 to
// planet 8, you would say pw.IssueOrder(3, 8, 10).
//
// There is already a basic strategy in place here. You can use it as a
// starting point, or you can throw it out entirely and replace it with your
// own. Check out the tutorials and articles on the contest website at
// http://www.ai-contest.com/resources.
"""
from PlanetWars import PlanetWars
import random
import sys
def DoTurn(pw, group_ids, nickname):
mes = random.randint(-214783648, 2147483647)
pw.SendMessage(nickname,mes)
# (1) If we currently have a fleet in flight, just do nothing.
if len(pw.MyFleets()) >= 1:
return
# (2) Find my strongest planet.
source = -1
source_score = -999999.0
source_num_ships = 0
my_planets = pw.MyPlanets()
for p in my_planets:
score = float(p.NumShips())
if score > source_score:
source_score = score
source = p.PlanetID()
source_num_ships = p.NumShips()
if int(source_num_ships) <= 50:
return
# (3) Find the weakest enemy or neutral planet.
dest = -1
dest_score = -999999.0
not_my_planets = pw.NotMyPlanets()
# (3.1) remove group members planets from list
tmp = []
for p in not_my_planets:
if p.Owner() in group_ids:
continue
tmp.append(p)
not_my_planets = tmp
# (3.2) remove planets which awaiting group member fleets
planet_ids = [] # not_my_planets ids
if not_my_planets:
planet_ids = ([p.PlanetID() for p in not_my_planets])
gfp_ids = [] # group_fleet_planet_ids
for fl in pw.EnemyFleets():
if fl.Owner() in group_ids and fl.DestinationPlanet() in planet_ids:
gfp_ids.append(fl.DestinationPlanet())
gfp_ids = set(gfp_ids)
for p in not_my_planets:
score = 1.0 / (1 + p.NumShips())
if score > dest_score:
dest_score = score
dest = p.PlanetID()
# (4) Send half the ships from my strongest planet to the weakest
# planet that I do not own.
if source >= 0 and dest >= 0:
num_ships = int(source_num_ships * .75)
pw.IssueOrder(source, dest, num_ships)
def main():
f = open('MyBot3.log', 'w')
nickname = 'X'
group_ids = []
if '-g' in sys.argv:
group_ids = [int(k) for k in sys.argv[2].split(',')]
if '-n' in sys.argv:
nickname = str(sys.argv[4])
f.write('group: ' + str(group_ids) + '\n')
f.write('nick: ' + str(nickname) + '\n')
map_data = ''
while(True):
current_line = raw_input()
f.write(current_line + '\n')
if len(current_line) >= 1 and current_line.startswith("."):
pw = PlanetWars(map_data)
DoTurn(pw, group_ids, nickname)
pw.FinishTurn()
map_data = ''
else:
map_data += current_line + '\n'
if __name__ == '__main__':
try:
import psyco
psyco.full()
except ImportError:
pass
try:
main()
except KeyboardInterrupt:
print 'ctrl-c, leaving ...'