def check_boats(self, start, end): if start.x == end.x: for y in range(start.y, end.y + 1): if (self.boxes[start.x][y].is_boat): return Coordinate(start.x, y) return if start.y == end.y: for x in range(start.x, end.x + 1): if (self.boxes[x][start.y].is_boat): return Coordinate(x, start.y) return
def place_random_boats(self): i = 0 while i < len(self.unplaced_boats): boat = self.unplaced_boats[i] x = random.randrange(0, self.width) y = random.randrange(0, self.height) direction = random.randint(0, 3) try: if direction == Table.DIRECTION_TOP: if (y > boat): self.place_boat(Coordinate(x, y - boat + 1), Coordinate(x, y)) i += 1 continue if direction == Table.DIRECTION_RIGHT: if (self.width - x >= boat): self.place_boat(Coordinate(x, y), Coordinate(x + boat - 1, y)) i += 1 continue if direction == Table.DIRECTION_BOTTOM: if (self.height - y >= boat): self.place_boat(Coordinate(x, y), Coordinate(x, y + boat - 1)) i += 1 continue if direction == Table.DIRECTION_LEFT: if (x > boat): self.place_boat(Coordinate(x - boat + 1, y), Coordinate(x, y)) i += 1 continue except: continue
def check_explored(self, hit): coordinates = hit.coordinates x = coordinates.x y = coordinates.y top_coord = Coordinate(x, y - 1) right_coord = Coordinate(x + 1, y) bottom_coord = Coordinate(x, y + 1) left_coord = Coordinate(x - 1, y) if top_coord.y > -1 and not self.is_hit_at(top_coord): return top_coord if right_coord.x < self.width and not self.is_hit_at(right_coord): return right_coord if bottom_coord.y < self.height and not self.is_hit_at(bottom_coord): return bottom_coord if left_coord.x > -1 and not self.is_hit_at(left_coord): return left_coord
def validate_coordinate_input(coord): coord = coord.upper().replace(' ', '') if len(coord) != 2: raise Exception( "The coordinates should be in the form of 'A0' or '0A'") coord = Coordinate(coord[0], coord[1]) if coord.x in map_letter_to_number: if not is_int(coord.y): raise Exception( "The coordinates should be in the form of 'A0' or '0A'") coord.y = int(coord.y) if coord.y not in map_number_to_letter: raise Exception( "The coordinates on the Y-axis are from '0' to '9'") return Coordinate(coord.y, map_letter_to_number[coord.x]) if coord.y not in map_letter_to_number: raise Exception("The coordinates on the X-axi are from 'A' to 'J'") if not is_int(coord.x): raise Exception( "The coordinates should be in the form of 'A0' or '0A'") coord.x = int(coord.x) if coord.x not in map_number_to_letter: raise Exception("The coordinates on the Y-axis are from '0' to '9'") return Coordinate(coord.x, map_letter_to_number[coord.y])
def get_exact_coordinate(self, other): for x in range(self.height): for y in range(self.width): if other.boxes[x][y].is_boat and not other.boxes[x][y].is_hit: return Coordinate(x, y)
def get_strategic_coordinate(self, other): for x in range(self.height): for y in range(self.width): if x % 2 == y % 2 or other.boxes[x][y].is_hit: continue return Coordinate(x, y)
def get_random_coordinate(self): x = random.randrange(0, self.width) y = random.randrange(0, self.height) return Coordinate(x, y)