コード例 #1
0
 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()
コード例 #2
0
    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())
コード例 #3
0
 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!")))
コード例 #4
0
    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)
コード例 #5
0
    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)
コード例 #6
0
 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
コード例 #7
0
 def test_receive_fire(self):
     ship = ships.Ship("TestShip", 4, "Black")
     self.assertFalse(self.fleet.receive_fire(Point(1, 2)) == ship)
コード例 #8
0
 def test_is_valid_coordinate2(self):
     self.assertFalse(self.playField.is_valid_coordinate(Point(11, 2)))
コード例 #9
0
 def test_is_valid_coordinate(self):
     self.assertTrue(self.playField.is_valid_coordinate(Point(1, 2)))
コード例 #10
0
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)
コード例 #11
0
 def test_player_get_shot(self):
     self.player.record_shot(Point(1, 2), None)
     self.assertTrue(self.player.get_shot_at(Point(1, 2)))
コード例 #12
0
 def test_tanked_ships(self):
     self.ship.receive_fire(Point(1, 2))
     self.assertFalse(self.ship.is_alive())
コード例 #13
0
 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")
コード例 #14
0
 def test_is_at(self):
     self.assertTrue(self.ship.receive_fire(Point(1, 2)))
コード例 #15
0
 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())
コード例 #16
0
 def test_receive_fire(self):
     self.assertFalse(self.ship.receive_fire(Point(1, 2)))
コード例 #17
0
 def setUp(self):
     self.point = Point(1, 2)
コード例 #18
0
class TestPoint(unittest.TestCase):
    def setUp(self):
        self.point = Point(1, 2)

    def test_str(self):
        self.assertTrue(self.point.__str__() == '(1, 2)')