<commit_before>from player import Player
from config import (BOARD_WIDTH,
                    BOARD_HEIGHT,
                    DEFAULT_SHIPS)
from ship import (UP,
                  RIGHT,
                  LEFT,
                  DOWN,
                  SHIP_ORIENTATIONS,
                  VECTOR_DICT,
                  )
import random

VERSION = 'v0.1'
(SEARCH,
 DESTROY,
 RANDOM) = OFFENSIVE_MODES = range(3)

def intToPoint(num):
    x = num % BOARD_WIDTH
    y = num / BOARD_WIDTH
    return x, y

def pointToInt(point):
    total = point[0] + BOARD_WIDTH * point[1]
    return total

def isValidPoint(point):
    return (0 <= point[0] < BOARD_WIDTH and
            0 <= point[1] < BOARD_HEIGHT)

class Admiral(Player):
    def initPlayer(self):
        self.name = 'Admiral %s' % VERSION
        self.shipSizes = dict(((x[0], x[1]) for x in DEFAULT_SHIPS))

    def newGame(self):
        self.shots = set()
        self.foundShips = dict(((x[0], []) for x in DEFAULT_SHIPS))
        self.searchMat = []
        self.killMats = dict(((x[0], set()) for x in DEFAULT_SHIPS))

        shipSizes = [x.size for x in self.ships]
        minShipSize = min(shipSizes)

        self.offense = SEARCH

        for i in xrange(BOARD_HEIGHT):
            if i % 2 == 0:
                for j in xrange(BOARD_WIDTH):
                    smallMod = (i % (minShipSize + 1))
                    if j % (minShipSize + 1) == smallMod:
                        self.searchMat.append((i, j))

        print self.searchMat

    def shotHit(self, shot, shipName):
        self.foundShips[shipName].append(shot)

        if len(self.foundShips[shipName]) > 1:
            # Filter out impossible locations
            pass
        else:
            # Build out the full kill matrix
            for i in xrange(1, self.shipSizes[shipName] - 1):
                for direction in SHIP_ORIENTATIONS:
                    vector = (VECTOR_DICT[direction][0] * i,
                              VECTOR_DICT[direction][1] * i)
                    self.killMats[shipName].add((shot[0] + vector[0], shot[1] + vector[1]))

    def placeShips(self):
        for ship in self.ships:
            isValid = False
            while not isValid:
                orientation = random.choice([UP, RIGHT])
                if orientation == UP:
                    location = (random.randint(0, BOARD_WIDTH - 1),
                                random.randint(0, BOARD_HEIGHT - 1 - ship.size))
                else:
                    location = (random.randint(0, BOARD_WIDTH - 1 - ship.size),
                                random.randint(0, BOARD_HEIGHT - 1))
                ship.placeShip(location, orientation)

                if self.isShipPlacedLegally(ship):
                    isValid = True

    def fireShot(self):
        shot = (random.randint(0, BOARD_WIDTH - 1),
                random.randint(0, BOARD_HEIGHT - 1))
<commit_msg>Adding utility methods<commit_after>from player import Player
from config import (BOARD_WIDTH,
                    BOARD_HEIGHT,
                    DEFAULT_SHIPS)
from ship import (UP,
                  RIGHT,
                  LEFT,
                  DOWN,
                  SHIP_ORIENTATIONS,
                  VECTOR_DICT,
                  )
import random, math

VERSION = 'v0.1'
(SEARCH,
 DESTROY,
 RANDOM) = OFFENSIVE_MODES = range(3)

def intToPoint(num):
    x = num % BOARD_WIDTH
    y = num / BOARD_WIDTH
    return x, y

def pointToInt(point):
    total = point[0] + BOARD_WIDTH * point[1]
    return total

def isValidPoint(point):
    return (0 <= point[0] < BOARD_WIDTH and
            0 <= point[1] < BOARD_HEIGHT)

class Admiral(Player):
    def initPlayer(self):
        self.name = 'Admiral %s' % VERSION
        self.shipSizes = dict(((x[0], x[1]) for x in DEFAULT_SHIPS))

    def newGame(self):
        self.shots = set()
        self.foundShips = dict(((x[0], []) for x in DEFAULT_SHIPS))
        self.searchMat = []
        self.killMats = dict(((x[0], set()) for x in DEFAULT_SHIPS))

        shipSizes = [x.size for x in self.ships]
        minShipSize = min(shipSizes)

        self.offense = SEARCH

        for i in xrange(BOARD_HEIGHT):
            if i % 2 == 0:
                for j in xrange(BOARD_WIDTH):
                    smallMod = (i % (minShipSize + 1))
                    if j % (minShipSize + 1) == smallMod:
                        self.searchMat.append((i, j))

        print self.searchMat

    @staticmethod
    def getDistance(x, y):
        dist = math.sqrt(x**2 + y**2) 
        return dist

    def findMinMaxPoint(self, points):
        minPoint = None
        maxPoint = None
        for point in points:
            if not maxPoint:
                maxPoint = point
            if not minPoint:
                minPoint = point
            maxDist = self.getDistance(maxPoint[0], maxPoint[1])
            minDist = self.getDistance(minPoint[0], minPoint[1])
            pointDist = self.getDistance(point[0], point[1])

            if pointDist > maxDist:
                maxPoint = point
            if pointDist < minDist:
                minPoint = point
        return maxPoint, minPoint

    def normalize(self, shot):
        if shot[0] > 0:
            x = 1
        elif shot[0] < 0:
            x = -1
        else:
            x = 0

        if shot[1] > 0:
            y = 1
        elif shot[1] < 0:
            y = -1
        else:
            y = 0

    def shotHit(self, shot, shipName):
        hitShip = self.foundShips[shipName]
        hitShip.append(shot)

        if len(hitShip) > 1:
            # Filter out impossible locations
            maxPoint, minPoint = self.findMinMaxPoint(hitShip)

            shipLine = (maxPoint[0] - minPoint[0],
                        maxPoint[1] - minPoint[1])

            possibleLocations = []
            for remaining in xrange(self.shipSizes[shipName] - len(hitShip)):
                possibleShot = (minPoint[0]

        else:
            # Build out the full kill matrix
            for i in xrange(1, self.shipSizes[shipName] - 1):
                for direction in SHIP_ORIENTATIONS:
                    vector = (VECTOR_DICT[direction][0] * i,
                              VECTOR_DICT[direction][1] * i)
                    self.killMats[shipName].add((shot[0] + vector[0], shot[1] + vector[1]))

    def placeShips(self):
        for ship in self.ships:
            isValid = False
            while not isValid:
                orientation = random.choice([UP, RIGHT])
                if orientation == UP:
                    location = (random.randint(0, BOARD_WIDTH - 1),
                                random.randint(0, BOARD_HEIGHT - 1 - ship.size))
                else:
                    location = (random.randint(0, BOARD_WIDTH - 1 - ship.size),
                                random.randint(0, BOARD_HEIGHT - 1))
                ship.placeShip(location, orientation)

                if self.isShipPlacedLegally(ship):
                    isValid = True

    def fireShot(self):
        shot = (random.randint(0, BOARD_WIDTH - 1),
                random.randint(0, BOARD_HEIGHT - 1))

{'fraction_non_alphanumeric': 0.10218671152228764, 'fraction_numerical': 0.014437902999719652, 'mean_word_length': 1.5185315919519944, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4848838', 'n_tokens_mistral': 2215, 'n_tokens_neox': 2105, 'n_words': 527}