Пример #1
0
    def test_attack(self):
        grid = Grid()
        grid.add_ship(Ship((0, 0), EAST, SUBMARINE))
        grid.add_ship(Ship((9, 9), WEST, BATTLESHIP))

        self.assertEqual(grid.attack(0, 0), (SUBMARINE, False))
        self.assertEqual(grid.attack(1, 0), (SUBMARINE, False))
        self.assertEqual(grid.attack(2, 0), (SUBMARINE, True))
        self.assertEqual(grid.attack(3, 0), None)
        self.assertEqual(grid.attack(0, 1), None)
        self.assertEqual(grid.attack(9, 9), (BATTLESHIP, False))
        self.assertEqual(grid.attack(8, 9), (BATTLESHIP, False))
        self.assertEqual(grid.attack(7, 9), (BATTLESHIP, False))
        self.assertEqual(grid.attack(6, 9), (BATTLESHIP, True))
        self.assertEqual(grid.attack(5, 9), None)
        self.assertEqual(grid.attack(5, 5), None)
        self.assertEqual(grid.attack(7, 5), None)

        self.assertEqual(
            grid._pegs,
            [Peg(0, 0, True),
             Peg(1, 0, True),
             Peg(2, 0, True),
             Peg(3, 0, False),
             Peg(0, 1, False),
             Peg(9, 9, True),
             Peg(8, 9, True),
             Peg(7, 9, True),
             Peg(6, 9, True),
             Peg(5, 9, False),
             Peg(5, 5, False),
             Peg(7, 5, False)]
        )
Пример #2
0
def Bar(rot):
    """Tetris Ship 'Bar'
       .
       Shape: B B B B
    """

    retval = Ship("Bar", [(0, 0), (1, 0), (2, 0), (3, 0)])
    retval.rotate(rot)
    return retval
Пример #3
0
def Tee(rot):
    """Tetris Ship 'Tee'
       .
       .        T
       Shape: T T T
    """

    retval = Ship("Tee", [(0, 0), (1, 0), (0, 1), (-1, 0)])
    retval.rotate(rot)
    return retval
Пример #4
0
def Square(rot):
    """Tetris Ship 'Square'
       .
       .      S S
       Shape: S S
    """

    retval = Ship("Square", [(0, 0), (1, 0), (0, 1), (1, 1)])
    retval.rotate(rot)
    return retval
Пример #5
0
def R_ZigZag(rot):
    """Tetris Ship 'Right ZigZag'
       .
       .        R
       .      R R
       Shape: R
    """

    retval = Ship("R Zig-Zag", [(0, 0), (0, 1), (1, 1), (1, 2)])
    retval.rotate(rot)
    return retval
Пример #6
0
def L_ZigZag(rot):
    """Tetris Ship 'Left ZigZag'
       .
       .      L
       .      L L
       Shape:   L
    """

    retval = Ship("L Zig-Zag", [(0, 0), (0, 1), (-1, 1), (-1, 2)])
    retval.rotate(rot)
    return retval
Пример #7
0
def L_Boot(rot):
    """Tetris Ship 'Left Boot'
       .
       .      L
       .      L
       Shape: L L
    """

    retval = Ship("L Boot", [(0, 0), (1, 0), (0, 1), (0, 2)])
    retval.rotate(rot)
    return retval
Пример #8
0
def R_Boot(rot):
    """Tetris Ship 'Right Boot'
       .
       .        R
       .        R
       Shape: R R
    """

    retval = Ship("R Boot", [(0, 0), (-1, 0), (0, 1), (0, 2)])
    retval.rotate(rot)
    return retval
Пример #9
0
 def test_parse_all_ships_cases_spaces(self):
     all_ships = parse_all_ships(
         '    A3    e,b1 S    ,   c10 w, I2     N    ,J8    n    '
     )
     expected = (
         Ship((2, 0), EAST, CARRIER),
         Ship((0, 1), SOUTH, BATTLESHIP),
         Ship((9, 2), WEST, CRUISER),
         Ship((1, 8), NORTH, SUBMARINE),
         Ship((7, 9), NORTH, DESTROYER)
     )
     self.assertEqual(all_ships, expected)
Пример #10
0
    def test_bow_on_grid(self):
        with self.assertRaises(ShipOffGridError):
            Ship((0, 5), WEST, SUBMARINE)

        with self.assertRaises(ShipOffGridError):
            Ship((8, 1), EAST, SUBMARINE)

        with self.assertRaises(ShipOffGridError):
            Ship((3, 0), NORTH, SUBMARINE)

        with self.assertRaises(ShipOffGridError):
            Ship((3, 8), SOUTH, SUBMARINE)
Пример #11
0
    def test_stern_on_grid(self):
        with self.assertRaises(ShipOffGridError):
            Ship((-1, 5), WEST, SUBMARINE)

        with self.assertRaises(ShipOffGridError):
            Ship((12, 5), WEST, SUBMARINE)

        with self.assertRaises(ShipOffGridError):
            Ship((5, -3), WEST, SUBMARINE)

        with self.assertRaises(ShipOffGridError):
            Ship((5, 15), WEST, SUBMARINE)
Пример #12
0
 def test_add_ships(self):
     grid = Grid()
     ships = [
         Ship((0, 0), EAST, SUBMARINE),
         Ship((5, 5), WEST, SUBMARINE),
         Ship((9, 9), WEST, SUBMARINE)
     ]
     grid.add_ship(ships[0])
     self.assertEqual(grid._ships, [ships[0]])
     grid.add_ship(ships[1])
     self.assertEqual(grid._ships, [ships[0], ships[1]])
     grid.add_ship(ships[2])
     self.assertEqual(grid._ships, [ships[0], ships[1], ships[2]])
Пример #13
0
 def test_get_bow_point(self):
     self.assertEqual(
         Ship((0, 5), NORTH, SUBMARINE)._get_bow_point(), (0, 3)
     )
     self.assertEqual(
         Ship((0, 5), SOUTH, SUBMARINE)._get_bow_point(), (0, 7)
     )
     self.assertEqual(
         Ship((5, 0), EAST, SUBMARINE)._get_bow_point(), (7, 0)
     )
     self.assertEqual(
         Ship((5, 0), WEST, SUBMARINE)._get_bow_point(), (3, 0)
     )
Пример #14
0
    def test_no_ships_overlap(self):
        grid = Grid()
        grid.add_ship(Ship((2, 5), EAST, SUBMARINE))

        with self.assertRaises(ShipsOverlapError):
            grid.add_ship(Ship((0, 5), EAST, SUBMARINE))

        with self.assertRaises(ShipsOverlapError):
            grid.add_ship(Ship((4, 5), WEST, SUBMARINE))

        with self.assertRaises(ShipsOverlapError):
            grid.add_ship(Ship((2, 7), NORTH, SUBMARINE))

        with self.assertRaises(ShipsOverlapError):
            grid.add_ship(Ship((2, 3), SOUTH, SUBMARINE))
Пример #15
0
 def setUpClass(cls):
     from battleship import Ship, Aircraft
     from battleship import PatrolBoat, Submarine
     cls.base_ship = Ship(['G1', 'G2', 'G3'], 'h')
     cls.aircraft_carrier = Aircraft(['E1', 'F1', 'G1', 'H1', 'I1'], 'v')
     cls.patrol_boat = PatrolBoat(['E2', 'F2'], 'v')
     cls.submarine = Submarine(['E3', 'F3', 'G3'], 'v')
Пример #16
0
def Carrier(rot):
    return Ship(
        "Aircraft Carrier",
        rotate_shape(
            [Pos(0, 0), Pos(1, 0),
             Pos(2, 0), Pos(3, 0),
             Pos(4, 0)], rot))
Пример #17
0
 def test_get_points(self):
     self.assertEqual(
         tuple(Ship((0, 0), EAST, SUBMARINE).get_points()),
         ((0, 0), (1, 0), (2, 0))
     )
     self.assertEqual(
         tuple(Ship((0, 0), SOUTH, SUBMARINE).get_points()),
         ((0, 0), (0, 1), (0, 2))
     )
     self.assertEqual(
         tuple(Ship((9, 0), WEST, SUBMARINE).get_points()),
         ((9, 0), (8, 0), (7, 0))
     )
     self.assertEqual(
         tuple(Ship((0, 9), NORTH, SUBMARINE).get_points()),
         ((0, 9), (0, 8), (0, 7))
     )
Пример #18
0
 def test_create_horizontal_ship_with_bow_at_edge(self):
     stern_point = (4, 0)
     direction = WEST
     name = CARRIER
     ship = Ship(stern_point, direction, name)
     self.assertEqual(ship._stern_point, stern_point)
     self.assertEqual(ship._direction, direction)
     self.assertEqual(ship._name, name)
Пример #19
0
 def test_create_destroyer(self):
     stern_point = (7, 4)
     direction = NORTH
     name = DESTROYER
     ship = Ship(stern_point, direction, name)
     self.assertEqual(ship._stern_point, stern_point)
     self.assertEqual(ship._direction, direction)
     self.assertEqual(ship._name, name)
Пример #20
0
 def test_is_dead(self):
     grid = Grid()
     grid.add_ship(Ship((0, 0), EAST, SUBMARINE))
     self.assertFalse(grid.is_dead())
     grid.attack(0, 0)
     grid.attack(1, 0)
     grid.attack(2, 0)
     self.assertTrue(grid.is_dead())
Пример #21
0
    def test_total_sunk(self):
        grid = Grid()
        grid.add_ship(Ship((0, 0), EAST, SUBMARINE))
        grid.add_ship(Ship((9, 9), WEST, BATTLESHIP))

        self.assertEqual(grid.total_sunk(), 0)
        grid.attack(0, 0)
        grid.attack(1, 0)
        self.assertEqual(grid.total_sunk(), 0)
        grid.attack(2, 0)
        self.assertEqual(grid.total_sunk(), 1)
        grid.attack(9, 9)
        grid.attack(8, 9)
        self.assertEqual(grid.total_sunk(), 1)
        grid.attack(7, 9)
        grid.attack(6, 9)
        self.assertEqual(grid.total_sunk(), 2)
Пример #22
0
 def test_create_carrier(self):
     stern_point = (4, 9)
     direction = EAST
     name = CARRIER
     ship = Ship(stern_point, direction, name)
     self.assertEqual(ship._stern_point, stern_point)
     self.assertEqual(ship._direction, direction)
     self.assertEqual(ship._name, name)
Пример #23
0
 def test_create_cruiser(self):
     stern_point = (4, 2)
     direction = WEST
     name = CRUISER
     ship = Ship(stern_point, direction, name)
     self.assertEqual(ship._stern_point, stern_point)
     self.assertEqual(ship._direction, direction)
     self.assertEqual(ship._name, name)
Пример #24
0
 def test_create_battleship(self):
     stern_point = (2, 9)
     direction = NORTH
     name = BATTLESHIP
     ship = Ship(stern_point, direction, name)
     self.assertEqual(ship._stern_point, stern_point)
     self.assertEqual(ship._direction, direction)
     self.assertEqual(ship._name, name)
Пример #25
0
 def test_create_submarine(self):
     stern_point = (3, 5)
     direction = EAST
     name = SUBMARINE
     ship = Ship(stern_point, direction, name)
     self.assertEqual(ship._stern_point, stern_point)
     self.assertEqual(ship._direction, direction)
     self.assertEqual(ship._name, name)
Пример #26
0
 def test_create_vertical_ship_with_bow_at_edge(self):
     stern_point = (0, 2)
     direction = NORTH
     name = CRUISER
     ship = Ship(stern_point, direction, name)
     self.assertEqual(ship._stern_point, stern_point)
     self.assertEqual(ship._direction, direction)
     self.assertEqual(ship._name, name)
Пример #27
0
def ship(ship_data):
    ship = Ship(**ship_data)
    for _ in range(4):
        ship.hit_positions.append({
            'x': random.randint(0, 16),
            'y': random.randint(0, 16),
            'hit': False
        })
    return ship
Пример #28
0
 def test_peg_exists(self):
     grid = Grid()
     grid.add_ship(Ship((0, 0), EAST, SUBMARINE))
     grid.attack(0, 0)
     with self.assertRaises(PegExistsError):
         grid.attack(0, 0)
     grid.attack(5, 5)
     with self.assertRaises(PegExistsError):
         grid.attack(5, 5)
Пример #29
0
class ShipTests(unittest.TestCase):
    def setUp(self):
        self.ship = Ship((1, 1), (1, 5), 'Carrier')

    def test_ship_length(self):
        self.assertEqual(self.ship.length, 5)

    def test_ship_occupied_coords(self):
        self.assertEqual(self.ship.points[0], {'coords': (1, 1), 'mark': ' '})
        self.assertEqual(self.ship.points[-1], {'coords': (1, 5), 'mark': ' '})

    def test_ship_hp(self):
        self.assertEqual(self.ship.hp, 5)

    def test_ship_hit(self):
        with self.assertRaises(ShipError):
            self.ship.register_hit((2, 2))
        self.ship.register_hit((1, 3))
        self.assertEqual(self.ship.hp, 4)
Пример #30
0
 def test_target_grid_str(self):
     grid = Grid()
     grid.add_ship(Ship((0, 0), EAST, SUBMARINE))
     grid.add_ship(Ship((8, 1), SOUTH, CARRIER))
     grid.attack(0, 0)
     grid.attack(5, 5)
     grid.attack(7, 2)
     grid.attack(8, 3)
     self.assertEqual(
         grid.target_grid_str(),
         '  1 2 3 4 5 6 7 8 9 10\n'
         'A x . . . . . . . . . \n'
         'B . . . . . . . . . . \n'
         'C . . . . . . . o . . \n'
         'D . . . . . . . . x . \n'
         'E . . . . . . . . . . \n'
         'F . . . . . o . . . . \n'
         'G . . . . . . . . . . \n'
         'H . . . . . . . . . . \n'
         'I . . . . . . . . . . \n'
         'J . . . . . . . . . . \n'
     )
Пример #31
0
 def setUp(self):
     self.ship = Ship((1, 1), (1, 5), 'Carrier')
Пример #32
0
def handle_place_ship(msg):
  
  global bat
  global cru
  global car
  global sub
  global des


  if msg["ship"] in player_ships[msg["id"]]:
    if (msg["ship"].title() == "Battleship"):

      if (bat[msg["id"]] > 0):
        player_ships[msg["id"]].append(msg["ship"])
        bat[msg["id"]]-=1


      else:
        send_alert(msg["ship"].title() + " already placed.")
        return
    elif (msg["ship"].title() == "Cruiser"):
      if (cru[msg["id"]] > 0):
        player_ships[msg["id"]].append(msg["ship"])
        cru[msg["id"]]-=1

      else:
        send_alert(msg["ship"].title() + " already placed.")
        return
    elif (msg["ship"].title() == "Carrier"):
      if (car[msg["id"]] > 0):
        player_ships[msg["id"]].append(msg["ship"])
        car[msg["id"]]-=1
      else:
        send_alert(msg["ship"].title() + " already placed.")
        return
    elif (msg["ship"].title() == "Submarine"):
      if (sub[msg["id"]] > 0):
        player_ships[msg["id"]].append(msg["ship"])
        sub[msg["id"]]-=1
      else:
        send_alert(msg["ship"].title() + " already placed.")
        return
    elif (msg["ship"].title() == "Destroyer"):
      if (des[msg["id"]] > 0):
        player_ships[msg["id"]].append(msg["ship"])
        des[msg["id"]]-=1
      else:
        send_alert(msg["ship"].title() + " already placed.")
        return
  else:
    if (msg["ship"].title() == "Battleship"):

      player_ships[msg["id"]].append(msg["ship"])
      bat[msg["id"]]-=1


    elif (msg["ship"].title() == "Cruiser"):

      player_ships[msg["id"]].append(msg["ship"])
      cru[msg["id"]]-=1

    elif (msg["ship"].title() == "Carrier"):

      player_ships[msg["id"]].append(msg["ship"])
      car[msg["id"]]-=1

    elif (msg["ship"].title() == "Submarine"):

      player_ships[msg["id"]].append(msg["ship"])
      sub[msg["id"]]-=1

    elif (msg["ship"].title() == "Destroyer"):

      player_ships[msg["id"]].append(msg["ship"])

      des[msg["id"]]-=1


  try:
    player_id = msg["id"]
    player_no = player_numbers[player_id]
    game = games[players[player_id]]
    ship = Ship(
      int(msg["location"]), 
      type=msg["ship"], 
      direction=msg["direction"])
    game.addShip(player_no, ship)
  except ValueError as e:
    send_alert(str(e))
  else:
    alert_ship_placement(msg)
    send(msg)
    if game.ready():
      send_alert("All ships placed... Player 1 ready to fire!", 
        players[player_id])
      send({"type":"game-begun"},room=players[player_id])