-
Notifications
You must be signed in to change notification settings - Fork 2
/
demo.py
66 lines (47 loc) · 2.09 KB
/
demo.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
""" This is a Demo AI to show how the game works.
This will be added by default into all servers so whatever is here will be
the behaviour of the "Demo" player on the server.
"""
from game import AI
import random
class BattleshipsAI(AI):
""" Your class name must be BattleshipsAI and must extend AI. """
# Use something unique as team name. This will identify your bot
# on the server
TEAM_NAME = "Your Team Name"
def __init__(self):
# Initialise data here
pass
def place_ships(self, game):
""" While we have ships to place, place ships. """
while len(game.ships_to_place) > 0:
try:
x = random.randint(0, 9)
y = random.randint(0, 9)
# We need to tell the game which ship (size) we're
# placing, the x and y co-ordinates, and the direction to place
# it.
game.place_ship(game.ships_to_place[0], x, y, game.HORIZONTAL)
except game.CannotPlaceShip:
# This will be raised if we try to overlap ships
# or go outside the boundary (x0-9y0-9)
# If it is raised, ships_to_place won't change
# so we can just loop and try again
pass
def take_shot(self, game):
# We just indicate which location we want to shoot at.
# This will return a tuple
# The first element of the tuple will be True or False indicating
# if anything was hit. The second element will be None unless
# something was destroyed - in which case it will be the size of the
# ship destroyed.
# E.g. If it is a miss - the return value will be
# (False, None)
# If it is a hit, but nothing has been destroyed completely, the return
# value will be
# (True, None)
# If it is a hit, and a "Cruiser" (1x3) has been destroyed, it will be
# (True, 3)
# For this demo we'll do it entirely randomly and ignore the return
# value
game.take_shot(random.randint(0, 9), random.randint(0, 9))