def test_non_overlapping_ships(self): ship1 = Ship(x=2, y=4, length=4, direction=Ship.CARDINAL_DIRECTIONS['SOUTH']) ship2 = Ship(x=4, y=2, length=4, direction=Ship.CARDINAL_DIRECTIONS['SOUTH']) self.assertFalse(are_ships_overlapping(ship1, ship2))
def test_post_logged_in_playing_defeat(self): self.client.login(username=self.user1.username, password='******') ship = Ship(team=self.team2, x=1, y=1, length=1, direction=Ship.CARDINAL_DIRECTIONS['SOUTH']) ship.save() url = reverse('attack', args=[self.game.id]) resp = self.client.post(url, { 'target_x': 1, 'target_y': 1, 'target_team': self.team2.id, }, follow=True) game_url = 'http://testserver{}'.format( reverse('game', args=[self.game.id])) self.assertIn((game_url, 302), resp.redirect_chain) pq = PyQuery(resp.content) # Assert error is shown self.assertEqual(len(pq('.alert-success')), 2) self.assertIn('Hit', pq('.alert-success').text()) self.assertIn('You defeated user2', pq('.alert-success').text())
def make_ships(team, lengths): """Generates ships of predetermined lengths and randomly arranges them to fit on the board.""" ships = [] for length in lengths: overlapping = False valid_position = False x = None y = None direction = None while overlapping or not valid_position: x = random.randrange(0, GAME_SIZE) y = random.randrange(0, GAME_SIZE) direction = Ship.CARDINAL_DIRECTIONS[random.choice( list(Ship.CARDINAL_DIRECTIONS.keys()))] ship = Ship(team=team, x=x, y=y, length=length, direction=direction) valid_position = is_valid_ship_position(ship) overlapping = False for existing_ship in ships: overlapping = \ overlapping or are_ships_overlapping(ship, existing_ship) ships.append(ship) return ships
def test_ship_creation(self): """Test that Ship instances are created correctly.""" game = Game() game.save() user = User(username='******', password='******') user.save() player = Player(user=user) player.save() team = Team(player=player, game=game) team.save() ship = Ship( team=team, x=0, y=0, length=3, direction=Ship.CARDINAL_DIRECTIONS['WEST'] ) self.assertTrue(isinstance(ship, Ship)) self.assertEqual( str(ship), 'Game 1 - user\'s 3L at (0, 0) facing West' )
def setUp(self): self.game = Game() self.game.save() self.user1 = User.objects.create_user('user1', '', 'password') self.user2 = User.objects.create_user('user2', '', 'password') self.player1 = Player(user=self.user1) self.player2 = Player(user=self.user2) self.player1.save() self.player2.save() self.team1 = Team(player=self.player1, game=self.game) self.team2 = Team(player=self.player2, game=self.game) self.team1.save() self.team2.save() self.ship = Ship( team=self.team2, x=3, y=3, length=3, direction=Ship.CARDINAL_DIRECTIONS['SOUTH'] ) self.ship.save() self.shot_miss = Shot( game=self.game, attacking_team=self.team1, defending_team=self.team2, x=2, y=3 ) self.shot_miss.save() self.shot_hit = Shot( game=self.game, attacking_team=self.team1, defending_team=self.team2, x=3, y=5 ) self.shot_hit.save()
def test_starts_too_far_south(self): ship = Ship(x=4, y=10, length=4, direction=Ship.CARDINAL_DIRECTIONS['NORTH']) self.assertFalse(is_valid_ship_position(ship))
def test_valid_position(self): ship = Ship(x=2, y=4, length=4, direction=Ship.CARDINAL_DIRECTIONS['EAST']) self.assertTrue(is_valid_ship_position(ship))
def test_ends_too_far_east(self): ship = Ship(x=8, y=5, length=4, direction=Ship.CARDINAL_DIRECTIONS['EAST']) self.assertFalse(is_valid_ship_position(ship))