async def get_ships(self, pn): self.pn = pn print("Player {} pick your ships!".format(pn)) print( "Use: R C D for input (R: row; C: column; D = A for across, D for down)" ) b = Board() for ship, size in ships: possibles = [ p for p in possible_placements(b, size) if all({(c.x + dx, c.y + dy) not in b.sea for dx in (-1, 0, 1) for dy in (-1, 0, 1) for c in p.cells}) ] place = random.choice(possibles) b.add_ship(place.x, place.y, place.dx, place.dy, place.size) print('Adding {} at ({}, {}) - ({}, {})'.format( ship, place.x, place.y, place.x + place.dx * (place.size - 1), place.y + place.dy * (place.size - 1))) assert await self.game.add_ship(pn, place.x, place.y, place.dx, place.dy, place.size) print() await self.display() print() print("Your final board:") await self.display()
def test_ship_must_not_be_adjacent_to_another(): b = Board() b.add_ship(5, 5, 0, 0, 1) for dx in -1, 0, 1: for dy in -1, 0, 1: with pytest.raises(ValueError): b.add_ship(5 + dx, 5 + dy, 0, 0, 1)
def test_ship_must_lie_on_board(): b = Board() with pytest.raises(ValueError): b.add_ship(0, 0, -1, 0, 2) with pytest.raises(ValueError): b.add_ship(1, 1, 0, -1, 3) with pytest.raises(ValueError): b.add_ship(9, 9, 1, 0, 2) with pytest.raises(ValueError): b.add_ship(0, 0, 0, 1, 11)