def test_shoot_grid(): hit_once = battleship.Ship('Destroyer', {(1, 2), (2, 2)}) hit_once.hits = {(1,2)} hit_twice = battleship.Ship('Destroyer', {(1, 2), (2, 2)}) hit_twice.hits = {(1,2), (2,2)} f = [miss_ships, hit_ship, sink_ship] v = [(('MISS', None), {'sizex': 5, 'sizey': 5, 'ships': [battleship.Ship('Destroyer', {(1, 2), (2, 2)})], 'misses': {(1, 1)}}), (('HIT', None), {'sizex': 5, 'sizey': 5, 'ships': [hit_once], 'misses': set()}), (('DESTROYED', hit_twice), {'sizex': 5, 'sizey': 5, 'ships': [hit_twice], 'misses': set()}) ] generic_value_tester(f, v)
def main(): game = battleship.Board(12) # list of all ships to add ship_list = [(battleship.Ship('Nina', [(0, 0), (1, 0), (2, 0), (3, 0)]), (3, 4), 0), (battleship.Ship('Pinta', [(0, 0), (1, 0), (2, 0), (3, 0)]), (0, 0), 0), (battleship.Ship('Santa Maria', [(0, 0), (1, 0), (2, 0), (3, 0)]), (0, 5), 1)] # checks to makes sure rotation works for ship in ship_list: ship[0].rotate(ship[2]) game.add_ship(ship[0], ship[1]) # check colliding ships fail_ship_collide = battleship.Ship('Collide', [(0, 0)]) try: game.add_ship(fail_ship_collide, (3, 4)) except: print('Caught ship collision error') # check for out of bounds ship fail_ship_bounds = battleship.Ship('Bounds', [(0, 0)]) try: game.add_ship(fail_ship_bounds, (100, 100)) except: print('Caught ship bounds error') # checks ship printing for ship in game.get_ships(): ship.print() # checks game printing and proper placement of ships game.print() # checks miss game.attempt_move((5, 5)) game.print() # checks hitting ship game.attempt_move((3, 4)) game.attempt_move((4, 4)) game.attempt_move((5, 4)) game.print() #checks sinking ship game.attempt_move((6, 4)) game.print()
def test_grid(): f = [grid1, grid2, grid3] v = [{'misses': set(), 'ships': [], 'sizex': 10, 'sizey': 11}, {'misses': set(), 'ships': [battleship.Ship('Destroyer', {(1,2),(2,2)})], 'sizex': 100, 'sizey': 110}, {'misses': {(2, 3)}, 'ships': [], 'sizex': 2, 'sizey': 3} ] generic_value_tester(f, v)
def test_blind(): sunk = battleship.Ship('Destroyer', {(1, 2), (2, 2)}) sunk.hits = {(1,2), (2,2)} f = [no_shot, only_miss, one_hit, two_hits] v = [{'sizey': 5, 'sunken_ships': [], 'misses': set(), 'hits': set(), 'sizex': 5}, {'sizey': 5, 'sunken_ships': [], 'misses': {(1, 1)}, 'hits': set(), 'sizex': 5}, {'sizey': 5, 'sunken_ships': [], 'misses': {(1, 1)}, 'hits': {(1, 2)}, 'sizex': 5}, {'sizey': 5, 'sunken_ships': [sunk], 'misses': {(1, 1)}, 'hits': {(1, 2), (2, 2)}, 'sizex': 5} ] generic_value_tester(f, v)
def double_shot(): s = battleship.Ship('', {(1,1), (1,2)}) s.shoot_at_ship((1,1)) res = s.shoot_at_ship((1,1)) return (res, s.__dict__)
def miss(): s = battleship.Ship('', {(1,1)}) res = s.shoot_at_ship((0,0)) return (res, s.__dict__)
def test_load(): tests = {('grid1.grd',): {'sizex': 5, 'sizey': 5, 'ships': [battleship.Ship('Destroyer', {(1, 2), (2, 2)})], 'misses': set()}, ('grid2.grd',): {'sizex': 10, 'sizey': 10, 'ships': [battleship.Ship('Carrier', {(2, 5), (2, 6), (2, 3), (2, 4), (2, 2)}), battleship.Ship('Battleship', {(3, 8), (2, 8), (5, 8), (4, 8)}), battleship.Ship('Cruiser', {(4, 5), (5, 5), (6, 5)}), battleship.Ship('Submarine', {(8, 9), (9, 9), (7, 9)}), battleship.Ship('Destroyer', {(1, 8), (1, 9)})], 'misses': set()}} generic_tester(load_helper, tests)
def grid2(): g = battleship.Grid(100,110) g.ships.append(battleship.Ship('Destroyer', {(1,2),(2,2)})) return g.__dict__
def afloat1(): s1 = battleship.Ship('Destroyer', {(1,2), (2,3)}) return s1.is_afloat()
def afloat2(): s1 = battleship.Ship('Destroyer', {(1,2), (2,3)}) s1.hits = {(1,2), (2,3)} return s1.is_afloat()
def ship_eq4(): s1 = battleship.Ship('Destroyer', {(1,2), (2,3)}) s1.hits = {(1,2)} s2 = battleship.Ship('Destroyer', {(1,2), (2,3)}) return s1 == s2
def ship_eq3(): s1 = battleship.Ship('Destroyer', {(1,2), (2,3)}) s2 = battleship.Ship('Destroyer', {(12,2), (2,3)}) return s1 == s2
def ship2(): s1 = battleship.Ship('Destroyer', {(10,22), (22,32)}) return s1.__dict__
def ship1(): s1 = battleship.Ship('Destroyer', {(1,2), (2,3)}) return s1.__dict__
def setUp(self) -> None: self.ships = [battleship.Ship() for i in range(1, 10)] self.vertical = battleship.Board.VERTICAL self.horizontal = battleship.Board.HORIZONTAL