-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathRandomBot.py
More file actions
91 lines (74 loc) · 3.22 KB
/
RandomBot.py
File metadata and controls
91 lines (74 loc) · 3.22 KB
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 python
#
"""
// RandomBot - an example bot that picks up one of his planets and send half of the ships
// from that planet to a random target planet.
//
// Not a very clever bot, but showcases the functions that can be used.
// Overcommented for educational purposes.
"""
# Import the module that implements the random number generator.
import random
# Import the PlanetWars class from the PlanetWars module.
from PlanetWars import PlanetWars
# DoTurn: function that gets called every turn.
# This is where to implement the strategies.
def DoTurn(pw):
# The source variable will contain the planet from which we send the ships.
source = None
# The dest variable will contain the destination, the planet to which we send the ships.
dest = None
# Get a list of all my planets and store it in the variable my_planets.
my_planets = pw.MyPlanets()
# The number of planets I own is the length of this list.
number_of_my_planets = len(my_planets)
# An example debug statement. These statements are useful when you don't understand the behaviour of your bot.
pw.log("I have", number_of_my_planets, "planets. (example debug statement, you can remove or change me!)")
# If I own at least a planet, i.e. there is at least one planet in the list.
if number_of_my_planets > 0:
# Pick the index of a planet at random by choosing a random integer
# between 0 (index of the first element of the list) and the last index
# (number_of_my_planets - 1).
random_my_planet_index = random.randint(0, number_of_my_planets - 1)
# Get the planet at that index.
source = my_planets[random_my_planet_index]
# Get a list of all planets which are either enemy or neutral (not mine).
not_my_planets = pw.NotMyPlanets()
# The number of planets I don't own is the length of this list.
number_of_not_my_planets = len(not_my_planets)
# If there is at least a planet I don't own, i.e. there is at least one planet in the list.
if number_of_not_my_planets > 0:
# Pick the index of a planet at random by choosing a random integer
# between 0 (index of the first element of the list) and the last index
# (number_of_my_planets - 1).
random_not_my_planet_index = random.randint(0, number_of_not_my_planets - 1)
# Get the planet at that index.
dest = not_my_planets[random_not_my_planet_index]
# If the source and dest variables contain actual planets, then
# send half of the ships from source to dest.
if (not source is None and not dest is None) :
pw.IssueOrder(source, dest)
# Don't change from this point on. Also not necessary to understand all the details.
# Machinery that reads the status of the game and puts it into PlanetWars.
# It calls DoTurn.
def main():
map_data = ''
while(True):
current_line = raw_input()
if len(current_line) >= 2 and current_line.startswith("go"):
pw = PlanetWars(map_data)
DoTurn(pw)
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 ...'