def test_adjacent_selection_direction(self):
     board = [['', 'H', '', '', ''],
              ['', 'H', '', '', ''],
              ['L', '', 'M', '', ''],
              ['H', 'M', 'H', 'H', 'H'],
              ['', 'M', '', 'H', '']]
     test_positions = [(0, 0, 'left'), (0, 2, 'right'), (1, 0, 'left'), (1, 2, 'right'), (2, 1, 'bottom'),
                       (2, 3, 'top'), (2, 4, 'top'), (4, 0, 'bottom'), (4, 2, 'left'), (4, 4, 'right')]
     hit_positions = ship_target.adjacent_to_hits(board)
     for tp in test_positions:
         if tp in hit_positions:
             self.assertEqual(tp[2], hit_positions['direction'])
 def test_adjacent_selection_segment_length(self):
     board = [['', 'H', '', '', ''],
              ['', 'H', '', '', ''],
              ['L', '', 'M', '', ''],
              ['H', 'M', 'H', 'H', 'H'],
              ['', 'M', '', 'H', '']]
     test_positions = [(0, 0, 1), (0, 2, 1), (1, 0, 1), (1, 2, 1), (2, 1, 2), (2, 3, 2), (2, 4, 1), (4, 0, 1),
                       (4, 2, 2), (4, 4, 2)]
     hit_positions = ship_target.adjacent_to_hits(board)
     for tp in test_positions:
         if tp in hit_positions:
             self.assertEqual(tp[2], hit_positions['seq_length'])
Example #3
0
 def _possible_hits(self, opp_board, opp_ships):
     """
     Helper function that first obtains all coordinates adjacent to hits and then for each one computes the score.
     :param opp_board: a 2D numpy array containing a string representation of the board.
     :param opp_ships: a list of ints, where each int is the length of a ship on the board.
     :return: a dictionary of the structure {coordinate_of_hit:{possible_alignments:some_int, other vars...}}.
     E.g {(1,1):{"possible_alignments":4,...}
     """
     hit_options = ship_target.adjacent_to_hits(opp_board)
     for hit in hit_options:
         possible_ship_count = ship_target.possible_hit_scores(
             opp_board, opp_ships, hit, hit_options[hit], self.heuristics)
         hit_options[hit]['score'] = possible_ship_count
     return hit_options
    def test_ajacent_selection_prioritise_length(self):
        board = [['', 'H', '', '', ''],
                 ['', 'H', '', '', ''],
                 ['L', '', 'M', '', ''],
                 ['H', 'H', 'H', 'H', 'H'],
                 ['', 'M', '', 'H', 'H']]

        # (2,1) should yield 2 instead of 1 and (4,2) should yield 2 instead of 1.
        test_positions = [(0, 0, 1), (0, 2, 1), (1, 0, 1), (1, 2, 1), (2, 1, 2), (2, 3, 2), (2, 4, 2), (4, 0, 1),
                          (4, 2, 2)]
        hit_positions = ship_target.adjacent_to_hits(board)

        for tp in test_positions:
            if tp in hit_positions:
                self.assertEqual(tp[2], hit_positions['seq_length'])
    def test_adjacent_selection_long_segments(self):
        board = [['', 'H', '', '', ''],
                 ['', 'H', '', '', ''],
                 ['L', '', 'M', '', ''],
                 ['H', 'M', 'H', 'H', 'H'],
                 ['', 'M', '', 'H', '']]
        test_positions = [(0, 0), (0, 2), (1, 0), (1, 2), (2, 1), (2, 3), (2, 4), (4, 0), (4, 2), (4, 4)]
        hit_positions = ship_target.adjacent_to_hits(board)

        for tp in test_positions:
            found = False
            if tp in hit_positions:
                found = True
            # Checks that all the required finds are there.
            self.assertTrue(found)
        # Checks that there are no extra finds.
        self.assertEqual(len(hit_positions), len(test_positions))