Skip to content

Instantly share code, notes, and snippets.

@mspraggs
Created April 17, 2015 13:02
Show Gist options
  • Save mspraggs/fece0eb6efc29cd0e425 to your computer and use it in GitHub Desktop.
Save mspraggs/fece0eb6efc29cd0e425 to your computer and use it in GitHub Desktop.
Attempted battleships AI: Select random coordinate and try up, down, left and right coordinates from this point
""" 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 = "Spraggs and Associates"
def __init__(self):
# Initialise data here
self.hit_last = False
self.coordinates = []
self.results = []
self.moves_to_try = []
def get_possible_coords(self, coords):
"""Return a list of possible coordinates offset by 1 from the current
one"""
out = []
for i in [0, 1]:
for offset in [-1, 1]:
new_coords = list(coords)
new_coords[i] += offset
if (0 <= coords[i] + offset <= 9
and tuple(new_coords) not in self.coordinates):
out.append(tuple(new_coords))
if not out:
return [self.random_move()]
else:
return out
def random_move(self):
coords = self.coordinates[-1] if self.coordinates else (0, 0)
while coords in self.coordinates:
coords = random.randint(0, 9), random.randint(0, 9)
return coords
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
# We could just cheat...
#game.opponent._Player__board.ships = []
#return
coords = self.coordinates[-1] if self.coordinates else (0, 0)
hit_last = self.results[-1] if self.results else False
print(self.moves_to_try)
while coords in self.coordinates:
if not self.moves_to_try:
self.moves_to_try = self.get_possible_coords(coords)
coords = self.moves_to_try.pop(0)
hit_last, size = game.take_shot(*coords)
self.results.append(hit_last)
self.coordinates.append(coords)
print(self.results[-1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment