def test_receive_shot_duplicate(self): grid = bge.Grid() grid.add_ship(bge.destroyer, ('A', 2), ('A', 3)) grid.receive_shot(('A', 2)) with pytest.raises(bge.InvalidCoordinate): grid.receive_shot(('A', 2))
def test_receive_shot_hit(self): grid = bge.Grid() grid.add_ship(bge.destroyer, ('A', 1), ('A', 2)) hit = grid.receive_shot(('A', 2)) assert hit
def test_receive_shot_miss(self): grid = bge.Grid() grid.add_ship(bge.destroyer, ('A', 2), ('A', 3)) hit = grid.receive_shot(('A', 4)) assert not hit
def test_add_ship_intersect_existing_ship(self): grid = bge.Grid() grid.add_ship(bge.battleship, ('E', 3), ('E', 4), ('E', 5), ('E', 6)) with pytest.raises(bge.InvalidCoordinate): grid.add_ship(bge.cruiser, ('C', 6), ('D', 6), ('E', 6))
def test_add_ship_non_consecutive_coords(self): grid = bge.Grid() with pytest.raises(bge.InvalidCoordinate): grid.add_ship(bge.cruiser, ('C', 6), ('C', 4), ('C', 7)) with pytest.raises(bge.InvalidCoordinate): grid.add_ship(bge.cruiser, ('A', 1), ('C', 1), ('D', 1))
def test_contains(self): grid = bge.Grid() for i in range(ord('A'), ord('A') + grid.size): for j in range(1, grid.size + 1): assert (chr(i), j) in grid assert ('J', 11) not in grid assert ('K', 1) not in grid
def test_all_ship_coords(self): grid = bge.Grid() grid.add_ship(bge.destroyer, ('A', 1), ('A', 2)) grid.add_ship(bge.submarine, ('D', 9), ('E', 9), ('F', 9)) assert grid._all_ship_coords() == { ('A', 1), ('A', 2), ('D', 9), ('E', 9), ('F', 9), }
def test_ships_afloat(self): grid = bge.Grid() grid.add_ship(bge.destroyer, ('A', 1), ('A', 2)) grid.add_ship(bge.submarine, ('D', 9), ('E', 9), ('F', 9)) grid.shots.update({ ('A', 2), ('D', 9), ('E', 9), ('F', 9), }) assert grid.ships_afloat() == {bge.destroyer}
def test_add_ship(self): grid = bge.Grid() grid.add_ship(bge.battleship, ('A', 1), ('A', 2), ('A', 3), ('A', 4)) assert len(grid.ships) == 1 assert grid.ships[bge.battleship] == ( ('A', 1), ('A', 2), ('A', 3), ('A', 4), )
def test_add_ship_off_grid(self): grid = bge.Grid() with pytest.raises(bge.InvalidCoordinate): grid.add_ship(bge.battleship, ('C', 5), ('B', 5), ('A', 5), (chr(ord('A') - 1), 5)) with pytest.raises(bge.InvalidCoordinate): grid.add_ship(bge.battleship, ('E', 8), ('E', 9), ('E', 10), ('E', 11)) with pytest.raises(bge.InvalidCoordinate): grid.add_ship(bge.battleship, ('H', 5), ('I', 5), ('J', 5), ('K', 5)) with pytest.raises(bge.InvalidCoordinate): grid.add_ship(bge.battleship, ('E', 3), ('E', 2), ('E', 1), ('E', 0))
def test_as_matrix(self): grid = bge.Grid() grid.add_ship(bge.destroyer, ('A', 1), ('A', 2)) grid.add_ship(bge.submarine, ('D', 8), ('D', 9), ('D', 10)) grid.add_ship(bge.cruiser, ('J', 8), ('J', 9), ('J', 10)) grid.add_ship(bge.battleship, ('F', 3), ('G', 3), ('H', 3), ('I', 3)) grid.add_ship(bge.carrier, ('C', 2), ('C', 3), ('C', 4), ('C', 5), ('C', 6)) grid.receive_shot(('A', 1)) grid.receive_shot(('B', 3)) grid.receive_shot(('C', 2)) grid.receive_shot(('C', 5)) grid.receive_shot(('C', 6)) grid.receive_shot(('E', 1)) grid.receive_shot(('H', 2)) grid.receive_shot(('H', 6)) grid.receive_shot(('I', 3)) grid.receive_shot(('J', 5)) grid.receive_shot(('J', 10)) matrix = grid.as_matrix() assert matrix == [ # 1 2 3 4 5 6 7 8 9 10 [True, None, None, None, None, None, None, None, None, None], # A [None, None, False, None, None, None, None, None, None, None], # B [None, True, None, None, True, True, None, None, None, None], # C [None, None, None, None, None, None, None, None, None, None], # D [False, None, None, None, None, None, None, None, None, None], # E [None, None, None, None, None, None, None, None, None, None], # F [None, None, None, None, None, None, None, None, None, None], # G [None, False, None, None, None, False, None, None, None, None], # H [None, None, True, None, None, None, None, None, None, None], # I [None, None, None, None, False, None, None, None, None, True], # J ]
def test_add_ship_unequal_number_coords(self): grid = bge.Grid() with pytest.raises(bge.InvalidCoordinate): grid.add_ship(bge.cruiser, ('C', 4), ('C', 5), ('C', 6), ('C', 7))