def draw_board(self, turn_number: int, player): if player.is_computer(): pass else: print() print(f"{player.name}, turn #{turn_number}") cols = string.ascii_uppercase[:self.play_field.width] print(self.numbers_spacer, cols, self.SPACER, self.numbers_spacer, cols, sep='') for y in range(self.play_field.height): print(self.numbers_column.format(y + 1), end='') for x in range(self.play_field.width): shot = player.get_shot_at(Point(x, y)) if shot: print('*' if shot.hit else '○', end='') else: print('·', end='') print(self.SPACER, self.numbers_column.format(y + 1), sep='', end='') for x in range(self.play_field.width): pos = Point(x, y) oppo = pos in player.opponent_shots char = '○' if oppo else '·' for ship in player.fleet: if pos in ship.all_positions: char = '*' if oppo else '═' if ship.position[ 1] == Orientation.HORIZONTAL else '║' print(char, end='') print()
def test_is_at(self): self.ship.all_positions.add(Point(1, 2)) self.assertTrue(self.ship.receive_fire(Point(1, 2))) self.assertTrue(self.ship.hits) self.assertFalse(self.ship.is_alive())
def test_player_draw_damage(self): hit, sunk_ship = self.computer.receive_fire(Point(1, 2)) self.player.record_shot(Point(1, 2), hit) self.assertTrue( (self.ui.draw_damage(self.player, Point(1, 2), hit, sunk_ship) and ("Hawk-eyed Human fired at B3 and missed!")) or (self.ui.draw_damage(self.player, Point(1, 2), hit, sunk_ship) and ("Hawk-eyed Human fired at B3 and Hit!")))
def setUp(self): self.ship = ships.Ship("TestShip", 4, "White") play_field = PlayField(BATTLEFIELD_COLUMNS, BATTLEFIELD_ROWS) self.ui = AsciiUI(play_field) self.playField = ships.PlayField(10, 10) player_fleet = Fleet.standard_fleet() player_fleet.random_positioning(play_field) self.player = Player("Hawk-eyed Human", play_field, player_fleet) computer_fleet = Fleet.standard_fleet() computer_fleet.random_positioning(play_field) self.ship.all_positions.add(Point(1, 2)) self.computer = ComputerPlayer(play_field, computer_fleet)
def draw_board(self, turn_number: int, player, ship_list): if player.is_computer(): print() print(colored(f"{player.name}, turn #{turn_number}", 'yellow')) time.sleep(1) print(colored(f"{player.name} is thinking...", 'yellow')) time.sleep(1) print() else: print() print(colored(f"{player.name}, turn #{turn_number}", 'cyan')) print() cols = string.ascii_uppercase[:self.play_field.width] print(self.numbers_spacer, cols, self.SPACER, self.numbers_spacer, cols, sep='') for y in range(self.play_field.height): print(self.numbers_column.format(y + 1), end='') for x in range(self.play_field.width): shot = player.get_shot_at(Point(x, y)) if shot: print(colored('*', 'red') if shot.hit else colored('○', 'blue'), end='') else: print('·', end='') print(self.SPACER, self.numbers_column.format(y + 1), sep='', end='') for x in range(self.play_field.width): pos = Point(x, y) oppo = pos in player.opponent_shots char = colored('○', 'blue') if oppo else colored('·', 'blue') for ship in player.fleet: if pos in ship.all_positions: char = colored('*', 'red') if oppo else colored('═', 'grey') if ship.position[1] == Orientation.HORIZONTAL else colored('║', 'grey') print(char, end='') print() print() print('****ENEMY SHIPS****') for final_ship in ship_list: print(final_ship)
def col_row_to_point(self, input_str): """ Converts a col_row such as `'D1'` to the corresponding point such as `Point(5, 0)`. """ try: letter = input_str[0].upper() x = string.ascii_uppercase.index(letter) y = int(input_str[1:]) - 1 p = Point(x, y) if self.play_field.is_valid_coordinate(p): return p except IndexError: pass except ValueError: pass return None
def test_receive_fire(self): ship = ships.Ship("TestShip", 4, "Black") self.assertFalse(self.fleet.receive_fire(Point(1, 2)) == ship)
def test_is_valid_coordinate2(self): self.assertFalse(self.playField.is_valid_coordinate(Point(11, 2)))
def test_is_valid_coordinate(self): self.assertTrue(self.playField.is_valid_coordinate(Point(1, 2)))
def step_impl(context, ship_length, x, y): context.ship_length = ship_length context.ship = Ship('test', ship_length, 'blue') context.ship.update_position(Point(x, y), Orientation.HORIZONTAL)
def test_player_get_shot(self): self.player.record_shot(Point(1, 2), None) self.assertTrue(self.player.get_shot_at(Point(1, 2)))
def test_tanked_ships(self): self.ship.receive_fire(Point(1, 2)) self.assertFalse(self.ship.is_alive())
def test_receive_fire2(self): ship2 = ships.Ship("TestShip2", 4, "White") ship2.all_positions.add(Point(1, 2)) ship2.receive_fire(Point(1, 2)) self.fleet.ships.append(ship2) self.assertFalse(self.fleet.receive_fire(Point(1, 2)) == "TestShip2")
def test_is_at(self): self.assertTrue(self.ship.receive_fire(Point(1, 2)))
def test_is_alive2(self): self.ship.all_positions.add(Point(1, 2)) self.ship._hits.add(Point(1, 3)) self.assertTrue(self.ship.is_alive())
def test_receive_fire(self): self.assertFalse(self.ship.receive_fire(Point(1, 2)))
def setUp(self): self.point = Point(1, 2)
class TestPoint(unittest.TestCase): def setUp(self): self.point = Point(1, 2) def test_str(self): self.assertTrue(self.point.__str__() == '(1, 2)')