def add_fleet(self, ship: Ship): new_fleet = self.fleet + [ship] f = Field(fleet=new_fleet, player_name=self.player_name) try: validate_field(f.get_view()[1], is_setup_stage=True) self.fleet = new_fleet except AssertionError as x: raise BaseException('field is incorrect', x, ship)
def test_validate_field_out_of_bounds(self) -> None: field_ship_outside_bounds = [ [1, 2, 1, 1, 1, 2, 1, 1, 1, 2], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 2, 2, 2, 2, 1, 1], [1, 1, 2, 1, 1, 1, 1, 1, 1, 1], [1, 1, 2, 1, 2, 2, 1, 1, 1, 1], [1, 1, 2, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 2, 1, 2, 2, 1, 1], [1, 1, 1, 1, 2, 1, 1, 1, 1, 1], [1, 1, 1, 1, 2, 1, 1, 1, 2, 2], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 2, 1, 1, 1, 1, 1, 1, 1, 1], ] with self.assertRaises(Exception) as ctx: validate_field(field_ship_outside_bounds) self.assertEqual(ctx.exception.args[0], 'Ship outside bounds ([Coord.Coord((10,1))])')
def test_validate_field_colliding_ships(self) -> None: field_colliding_ships = [ [1, 2, 1, 1, 1, 1, 1, 1, 1, 2], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 2, 2, 2, 2, 1, 1], [1, 1, 2, 1, 1, 1, 1, 1, 1, 1], [1, 1, 2, 1, 2, 2, 1, 1, 1, 1], [1, 1, 2, 1, 1, 1, 1, 1, 1, 1], [1, 2, 1, 1, 2, 1, 2, 2, 1, 1], [1, 1, 1, 1, 2, 1, 1, 1, 1, 1], [1, 1, 1, 1, 2, 1, 1, 1, 2, 2], [1, 2, 1, 1, 1, 1, 1, 1, 1, 1], ] with self.assertRaises(Exception) as ctx: validate_field(field_colliding_ships) self.assertEqual( ctx.exception.args[0], 'Fleet config is invalid (ships are touching or extra/missing ship)' )
def test_validate_field_wrong_shape(self) -> None: field_wrong_shape = [ [1, 2, 1, 1, 1, 1, 1, 1, 1, 2], [1, 1, 1, 1, 1, 1, 2, 1, 1, 1], [1, 1, 1, 1, 2, 2, 2, 1, 1, 1], [1, 1, 2, 1, 1, 1, 1, 1, 1, 1], [1, 1, 2, 1, 2, 2, 1, 1, 1, 1], [1, 1, 2, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 2, 1, 2, 2, 1, 1], [1, 2, 1, 1, 2, 1, 1, 1, 1, 1], [1, 1, 1, 1, 2, 1, 1, 1, 2, 2], [1, 2, 1, 1, 1, 1, 1, 1, 1, 1], ] with self.assertRaises(Exception) as ctx: validate_field(field_wrong_shape) self.assertEqual( ctx.exception.args[0], 'There is a deformed ship somewhere on the field ' '([Coord.Coord((1,6)), Coord.Coord((2,4)), Coord.Coord((2,5)), ' 'Coord.Coord((2,6))])')
def test_validate_field_ok(self) -> None: field_ok = [ [1, 2, 1, 1, 1, 1, 1, 1, 1, 2], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 2, 2, 2, 2, 1, 1], [1, 1, 2, 1, 1, 1, 1, 1, 1, 1], [1, 1, 2, 1, 2, 2, 1, 1, 1, 1], [1, 1, 2, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 2, 1, 2, 2, 1, 1], [1, 2, 1, 1, 2, 1, 1, 1, 1, 1], [1, 1, 1, 1, 2, 1, 1, 1, 2, 2], [1, 2, 1, 1, 1, 1, 1, 1, 1, 1], ] self.assertTrue(validate_field(field_ok))