Exemplo n.º 1
0
    def test__clone(self):
        """
                TestBoard.test__clone()
        """
        h1 = Board(['A', 'B'])
        h1.set_a_tile( (0,0), player_id='A', height=9 )
        h1.set_a_tile( (0,1), player_id='B', height=4 )

        h2 = h1.clone()

        h2.players_id[-1] = 'C'
        h2[(0,1)].height=5
        h2[(0,1)].player_id = 'A'

        h2.set_a_tile( (2,2), player_id='A', height=3 )

        self.assertEqual( h1.players_id[-1], 'B' )
        self.assertEqual( h2.players_id[-1], 'C' )

        self.assertEqual( h1[(0,1)].height, 4 )
        self.assertEqual( h2[(0,1)].height, 5 )

        self.assertEqual( h1[(0,1)].player_id, 'B' )
        self.assertEqual( h2[(0,1)].player_id, 'A' )

        self.assertEqual( (2,2) not in h1, True )
        self.assertEqual( (2,2) in h2, True )
Exemplo n.º 2
0
    def load(self, _file_name):
        """
                Game.load()

                !! self.backup_file will not be modified.

                Load a backup file and initialize self from it.

                ________________________________________________________________

                RETURNED VALUE : a boolean (True if the file has been successfully
                                 loaded, False otherwhise)
        """
        self.current_ui.debug("Game.load() : loading a backup file from "+_file_name)

        # resetting some data :
        #
        #   NB : self.backup_file is not modified by this function.
        #
        self.initial_number_of_tokens = 0
        self.players = OrderedDict()
        self.board = Board()
        self.board.players_id = []
        self.game_over = False          # False if there's no section "results". See below.
        self.current_gameturn = 0
        self.played_moves = []          # a list of Move objects

        return GameBackupFile.load(_file_name=_file_name,
                                   _game_obj=self)
Exemplo n.º 3
0
    def test__is_it_a_correct_move(self):
        """
                TestBoard.test__is_it_a_correct_move()

                test of Board.is_it_a_correct_move
        """
        h = Board(['A','B'])
        h.set_a_tile( (1,1), player_id='A', height=2 )
        h.set_a_tile( (2,1), player_id='B', height=2 )
        h.set_a_tile( (1,0), player_id='B', height=3 )
        h.set_a_tile( (0,0), player_id='B', height=1 )
        self.assertEqual(h.is_it_a_correct_move(Move(((1,1),(2,1)))), True)
        self.assertEqual(h.is_it_a_correct_move(Move(((2,1),(1,1)))), True)
        self.assertEqual(h.is_it_a_correct_move(Move(((0,0),(1,0)))), False)
        self.assertEqual(h.is_it_a_correct_move(Move(((1,1),(1,0)))), False)
        self.assertEqual(h.is_it_a_correct_move(Move(((-1,-1),(2,1)))), False)
Exemplo n.º 4
0
    def test__get_tiles_coordinates_around(self):
        
        """
                TestBoard.test__get_tiles_coordinates_around

                test of Board.get_tiles_coordinates_around
        """
        # (1) ..................................................................
        self.assertEqual( (-1,0) in Board.get_tiles_coordinates_around( (0,0) ), True )
        self.assertEqual( (0,0) in Board.get_tiles_coordinates_around( (-1,0) ), True )

        # (2) ..................................................................
        self.assertEqual( (1,2) in Board.get_tiles_coordinates_around( (1,3) ), True )
        self.assertEqual( (1,3) in Board.get_tiles_coordinates_around( (1,2) ), True )

        # (3) ..................................................................
        self.assertEqual( (4,2) in Board.get_tiles_coordinates_around( (5,1) ), True )
        self.assertEqual( (5,1) in Board.get_tiles_coordinates_around( (4,2) ), True )

        # (4) ..................................................................
        self.assertEqual( (1,1) in Board.get_tiles_coordinates_around( (0,0) ), False )
        self.assertEqual( (0,0) in Board.get_tiles_coordinates_around( (1,1) ), False )
Exemplo n.º 5
0
    def __init__(self,
                 _ui,
                 _initial_number_of_tokens=0,
                 _players=None,
                 _backup_file=None):
        """
                _players : a list of Player objects; the first player in the list plays the first.
        """
        self.current_ui = _ui

        self.initial_number_of_tokens = _initial_number_of_tokens

        self.players = OrderedDict()    # player_id : Player object
        if _players is not None:
            for player in _players:
                self.players[player.player_id] = player

        self.backup_file = _backup_file
        if _backup_file is None:
            self.current_ui.debug("no backup file.")
        else:
            self.current_ui.debug("backup file : "+self.backup_file.name())

        self.board = Board()
        self.board.players_id = list(self.players.keys())

        # (int, str) : player_id of the player who has to play to next move.
        #
        # the integer is the index in self.players matching player_id.
        #
        # e.g. (0, "HAL")
        #
        if _players is None:
            self.who_plays_the_next_move = []
        else:
            self.who_plays_the_next_move = [0, list(self.players.keys())[0]]

        self.game_over = False          # True when the game is over.
        self.current_gameturn = 0

        self.played_moves = []          # [((str)player_id, (Move)played move), ...]
Exemplo n.º 6
0
    def test__get_gains(self):
        """
                TestBoard.test__get_gains

                test of Board.get_gains
        """
        h = Board(['A', 'B', 'C', 'D'])
        h.set_a_tile( (0,0), player_id='A', height=1 )
        h.set_a_tile( (1,0), player_id='B', height=2 )
        h.set_a_tile( (2,0), player_id='C', height=3 )
        h.set_a_tile( (4,5), player_id='C', height=3 )

        gains = h.get_gains()
        self.assertEqual( gains['A'], 1 )
        self.assertEqual( gains['B'], 2 )
        self.assertEqual( gains['C'], 6 )
        self.assertEqual( gains['D'], 0 )
Exemplo n.º 7
0
    def test__equ_through_essen_digest_to(self):
        """
                TestBoard.test__equ_through_essen_digest_to

                test of TestBoard.test__equ_through_essen_digest_to
        """
        # (1) ..................................................................
        h1 = Board(['A','B'])
        h2 = Board(['A','B'])
        self.assertEqual( h1.equ_through_essen_digest_to(h2), True )
        self.assertEqual( h2.equ_through_essen_digest_to(h1), True )

        # (2) ..................................................................
        h1 = Board(['A','B'])
        h1.set_a_tile( (0,0), player_id='A', height=1 )
        h1.set_a_tile( (1,0), player_id='B', height=2 )

        h2 = Board(['A','B'])
        h2.set_a_tile( (0,0), player_id='A', height=1 )
        h2.set_a_tile( (-1,-1), player_id='B', height=2 )

        self.assertEqual( h1.equ_through_essen_digest_to(h2), True )
        self.assertEqual( h2.equ_through_essen_digest_to(h1), True )

        # (3) ..................................................................
        h1 = Board(['A','B', 'C'])
        h1.set_a_tile( (0,0), player_id='A', height=1 )
        h1.set_a_tile( (1,0), player_id='B', height=2 )

        h2 = Board(['A','B'])
        h2.set_a_tile( (0,0), player_id='A', height=1 )
        h2.set_a_tile( (-1,-1), player_id='B', height=2 )

        self.assertEqual( h1.equ_through_essen_digest_to(h2), True )
        self.assertEqual( h2.equ_through_essen_digest_to(h1), True )

        # (4) ..................................................................
        h1 = Board(['A','B',])
        h1.set_a_tile( (0,0), player_id='A', height=1 )
        h1.set_a_tile( (1,0), player_id='B', height=2 )
        h1.set_a_tile( (5,5), player_id='B', height=2 )

        h2 = Board(['A','B'])
        h2.set_a_tile( (0,0), player_id='A', height=1 )
        h2.set_a_tile( (-1,-1), player_id='B', height=2 )

        self.assertEqual( h1.equ_through_essen_digest_to(h2), False )
        self.assertEqual( h2.equ_through_essen_digest_to(h1), False )
Exemplo n.º 8
0
class Game(object):
    """
        Game class
    """

    #///////////////////////////////////////////////////////////////////////////
    def __init__(self,
                 _ui,
                 _initial_number_of_tokens=0,
                 _players=None,
                 _backup_file=None):
        """
                _players : a list of Player objects; the first player in the list plays the first.
        """
        self.current_ui = _ui

        self.initial_number_of_tokens = _initial_number_of_tokens

        self.players = OrderedDict()    # player_id : Player object
        if _players is not None:
            for player in _players:
                self.players[player.player_id] = player

        self.backup_file = _backup_file
        if _backup_file is None:
            self.current_ui.debug("no backup file.")
        else:
            self.current_ui.debug("backup file : "+self.backup_file.name())

        self.board = Board()
        self.board.players_id = list(self.players.keys())

        # (int, str) : player_id of the player who has to play to next move.
        #
        # the integer is the index in self.players matching player_id.
        #
        # e.g. (0, "HAL")
        #
        if _players is None:
            self.who_plays_the_next_move = []
        else:
            self.who_plays_the_next_move = [0, list(self.players.keys())[0]]

        self.game_over = False          # True when the game is over.
        self.current_gameturn = 0

        self.played_moves = []          # [((str)player_id, (Move)played move), ...]

    #///////////////////////////////////////////////////////////////////////////
    def begin(self):
        """
                Game.begin()

                Function to be called at the beginning of a game, just before
                the first move.
        """
        self.current_ui.debug("================")
        self.current_ui.debug("=== new game ===")
        self.current_ui.debug("================")
        self.current_ui.debug("players : "+repr(self.players))
        self.current_ui.debug("initial number of tokens : "+str(self.initial_number_of_tokens))

        self.current_gameturn = 1

        self.backup_file.write_the_settings(self.players, self.initial_number_of_tokens)
        self.backup_file.write_the_initial_position(self.board)
        self.backup_file.write_new_turn(self.current_gameturn)

    #///////////////////////////////////////////////////////////////////////////
    def compute_who_plays_the_next_move(self):
        """
                Game.compute_who_plays_the_next_move()

                Initialize self.who_plays_the_next_move and self.current_gameturn
                from the current state of self.
        """
        if self.who_plays_the_next_move[0]+1 != len(self.players):
            # let's go on with the next player :
            self.who_plays_the_next_move[0] += 1
            self.who_plays_the_next_move[1] = list(self.players.keys())[self.who_plays_the_next_move[0]]
        else:
            # we go on with the first player :
            self.who_plays_the_next_move = [0, list(self.players.keys())[0]]
            self.current_gameturn += 1

    #///////////////////////////////////////////////////////////////////////////
    def end(self):
        """
                Game.end()

                Function to be called at the end of a game, just after the last
                move.
        """
        self.current_ui.debug("=======================")
        self.current_ui.debug("=== end of the game ===")
        self.current_ui.debug("gains : ")

        sorted_gains = sorted(self.board.get_gains().items(), key=operator.itemgetter(1))
        sorted_gains.reverse()
        for player_id, gain in sorted_gains:
            self.current_ui.debug("  {0} : {1}".format(player_id, gain))

        self.current_ui.debug("winner(s) : " + str(self.board.who_is_the_winner()))
        self.current_ui.debug("number of turn(s) : "+str(self.current_gameturn))
        self.current_ui.debug("=======================")

        self.backup_file.write_the_gains(winners=self.board.who_is_the_winner(),
                                         sorted_gains=sorted_gains)

    #///////////////////////////////////////////////////////////////////////////
    def is_the_game_over(self):
        """
                Game.is_the_game_over()

                Initialize and return the content of self.game_over .
        """
        self.game_over = self.board.is_the_game_over()
        return self.game_over

    #///////////////////////////////////////////////////////////////////////////
    def load(self, _file_name):
        """
                Game.load()

                !! self.backup_file will not be modified.

                Load a backup file and initialize self from it.

                ________________________________________________________________

                RETURNED VALUE : a boolean (True if the file has been successfully
                                 loaded, False otherwhise)
        """
        self.current_ui.debug("Game.load() : loading a backup file from "+_file_name)

        # resetting some data :
        #
        #   NB : self.backup_file is not modified by this function.
        #
        self.initial_number_of_tokens = 0
        self.players = OrderedDict()
        self.board = Board()
        self.board.players_id = []
        self.game_over = False          # False if there's no section "results". See below.
        self.current_gameturn = 0
        self.played_moves = []          # a list of Move objects

        return GameBackupFile.load(_file_name=_file_name,
                                   _game_obj=self)

    #///////////////////////////////////////////////////////////////////////////
    def next_move(self):
        """
                Game.next_move()

                The player <self.who_plays_the_next_move> plays.
        """
        current_player = self.players[self.who_plays_the_next_move[1]]

        self.current_ui.debug("turn #{0}, current player being {1}".format(self.current_gameturn,
                                                                           current_player))

        if current_player.nature == PLAYER_NATURE__HUMAN:
            move = self.current_ui.your_move_please(_player_object=current_player,
                                                    _board_object=self.board,
                                                    _game=self)

            if not move.is_null():
                self.board.apply_a_move(move)

                self.backup_file.write_a_move(move_number=len(self.played_moves),
                                              move=move,
                                              player_id=current_player.player_id)
                self.backup_file.write_a_board(self.board)
                self.backup_file.write("\n")

                self.played_moves.append((current_player.player_id, move))

        elif current_player.nature == PLAYER_NATURE__RANDOM:
            moves = self.board.get_the_moves_a_player_may_play(current_player.player_id)

            len_moves = len(moves)
            if len_moves > 0:
                randomly_selected_move = moves[random.randrange(len_moves)]
                self.current_ui.message("Player {0} " \
                                        "randomly choosed to play {1}.".format(current_player,
                                                                               randomly_selected_move))
                self.board.apply_a_move(randomly_selected_move)

                self.backup_file.write_a_move(move_number=len(self.played_moves),
                                              move=randomly_selected_move,
                                              player_id=current_player.player_id)
                self.backup_file.write_a_board(self.board)
                self.backup_file.write("\n")

                self.played_moves.append((current_player.player_id, randomly_selected_move))

                self.current_ui.pause_to_see_the_last_move()
            else:
                self.current_ui.message("Player {0} can't play.".format(current_player))

        else:
            # todo : error
            pass

        # next player ?
        self.compute_who_plays_the_next_move()

        if self.who_plays_the_next_move[0] == 0:
            self.backup_file.write_new_turn(self.current_gameturn)

    #///////////////////////////////////////////////////////////////////////////
    def set_randomly_the_initial_pos(self):
        """
                Game.set_randomly_the_initial_pos()

                    -> set randomly the initial position for the current players

                set the initial position on self.board according to self.players
        """
        self.board.clear()
        self.board.players_id = [player_id for player_id in self.players]

        number_of_players = len(self.players)

        # first token :
        if number_of_players == 2:
            self.board.set_a_tile(xy=(0, 0), player_id=self.board.players_id[0], height=1)
            self.board.set_a_tile(xy=(1, 0), player_id=self.board.players_id[1], height=1)

        elif number_of_players == 3:
            self.board.set_a_tile(xy=(0, 0), player_id=self.board.players_id[0], height=1)
            self.board.set_a_tile(xy=(1, 0), player_id=self.board.players_id[1], height=1)
            self.board.set_a_tile(xy=(0, 1), player_id=self.board.players_id[2], height=1)

        elif number_of_players == 4:
            self.board.set_a_tile(xy=(0, 0), player_id=self.board.players_id[0], height=1)
            self.board.set_a_tile(xy=(1, 0), player_id=self.board.players_id[1], height=1)
            self.board.set_a_tile(xy=(0, 1), player_id=self.board.players_id[2], height=1)
            self.board.set_a_tile(xy=(1, 1), player_id=self.board.players_id[3], height=1)

        else:
            # $$$ todo :error
            pass

        # other tokens :
        # ... for each player, for each token to be put on the board ...
        for _ in range(1, self.initial_number_of_tokens):       # _ : token_number
            for player_id in self.players:

                # ... we get all the available coordinates where the player could put a token :
                available_xy = []

                for xy_center in self.board:
                    for xy in self.board.get_tiles_coordinates_around(xy_center):
                        # if xy is free and is connected to (at least) two other tokens, it's ok :
                        if xy not in self.board and self.board.how_many_adjacent_tiles_around(xy) >= 2:
                            available_xy.append(xy)

                # ... and we choose randomly the coordinates :
                self.board.set_a_tile(xy=available_xy[random.randrange(len(available_xy))],
                                      player_id=player_id,
                                      height=1)
Exemplo n.º 9
0
    def test__copy(self):
        """
                TestBoard.test__copy()
        """
        # (1) ..................................................................
        h1=Board(['A','B', 'C'])
        h1.set_a_tile( (0,0), player_id='A', height=1 )
        h1.set_a_tile( (1,0), player_id='B', height=2 )
        h1.set_a_tile( (2,0), player_id='C', height=3 )
        h2=h1.copy()
        self.assertEqual(h1, h2)

        # (2) ..................................................................
        h1=Board(['A','B', 'C'])
        h1.set_a_tile( (0,0), player_id='A', height=1 )
        h1.set_a_tile( (1,0), player_id='B', height=2 )
        h1.set_a_tile( (2,0), player_id='C', height=3 )
        h2=h1.copy()
        h1.set_a_tile( (3,0), player_id='C', height=3 )
        self.assertNotEqual(h1, h2)

        # (3) ..................................................................
        # are h1 and h2 independant ?
        h1=Board(['A','B', 'C'])
        h1.set_a_tile( (0,0), player_id='A', height=1 )
        h1.set_a_tile( (1,0), player_id='B', height=2 )
        h1.set_a_tile( (2,0), player_id='C', height=3 )
        h2=h1.copy()
        h2.players_id=['A', 'B', 'D']
        h1[(0, 0)].height=10
        self.assertEqual(h1.players_id, ['A','B', 'C'])
        self.assertEqual(h2[0, 0].height, 1)
Exemplo n.º 10
0
 def next_xy(self):
     """
             Return the next (x,y) computed from the current (x,y)+rot_for_the_next
     """
     return Board.get_tiles_coordinates_around(xy)[self.rot_for_the_next]
Exemplo n.º 11
0
    def test__apply_a_dxdy_translation(self):
        """
                TestBoard.test__apply_a_dxdy_translation
        """
        # (1) ..................................................................
        h1 = Board()
        h1.apply_a_dxdy_translation(10,10)
        h2 = Board()
        self.assertEqual( h1.compare_after_normalization(h2), True )
        self.assertEqual( h2.compare_after_normalization(h1), True )

        # (2) ..................................................................
        h1 = Board(['A',])
        h1.set_a_tile( (0,0), player_id='A', height=3)
        h1.apply_a_dxdy_translation(1,1)
        h2 = Board(['A',])
        h2.set_a_tile( (1,0), player_id='A', height=3)
        self.assertEqual( h1.compare_after_normalization(h2), True )
        self.assertEqual( h2.compare_after_normalization(h1), True )

        # (3) ..................................................................
        h1 = Board(['A', 'B'])
        h1.set_a_tile( (0,0), player_id='A', height=3)
        h1.set_a_tile( (1,0), player_id='B', height=2)
        h1.apply_a_dxdy_translation(1,1)
        h2 = Board(['A', 'B'])
        h2.set_a_tile( (1,1), player_id='A', height=3)
        h2.set_a_tile( (2,2), player_id='B', height=2)
        self.assertEqual( h1.compare_after_normalization(h2), True )
        self.assertEqual( h2.compare_after_normalization(h1), True )

        # (4) ..................................................................
        h1 = Board(['A',])
        h1.set_a_tile( (1,0), player_id='A', height=3)
        h1.apply_a_dxdy_translation(-1,0)
        h2 = Board(['A',])
        h2.set_a_tile( (0,0), player_id='A', height=3)
        self.assertEqual( h1.compare_after_normalization(h2), True )
        self.assertEqual( h2.compare_after_normalization(h1), True )

        # (5) ..................................................................
        h1 = Board(['A',])
        h1.set_a_tile( (1,0), player_id='A', height=3)
        h1.apply_a_dxdy_translation(-1,-1)
        h2 = Board(['A',])
        h2.set_a_tile( (0,-1), player_id='A', height=3)
        self.assertEqual( h1.compare_after_normalization(h2), True )
        self.assertEqual( h2.compare_after_normalization(h1), True )

        # (6) ..................................................................
        h1 = Board(['A', 'B'])
        h1.set_a_tile( (0,0), player_id='A', height=1)
        h1.set_a_tile( (1,0), player_id='B', height=2)
        h1.apply_a_dxdy_translation(0,0)
        h2 = Board(['A', 'B'])
        h2.set_a_tile( (0,0), player_id='A', height=1)
        h2.set_a_tile( (1,0), player_id='B', height=2)
        self.assertEqual( h1.compare_after_normalization(h2), True )
        self.assertEqual( h2.compare_after_normalization(h1), True )
Exemplo n.º 12
0
    def test__search_which_tile_is_connec_to(self):
        """
                TestBoard.test__search_which_tile_is_connec_to()

                test of Board.search_which_tile_is_connec_to()
        """

        # (1) ..................................................................
        h = Board(['0', '1'])
        
        # creating an island...
        h.set_a_tile( (0,0), player_id='0', height=3 )
        h.set_a_tile( (0,1), player_id='1', height=2)

        # ... and asking : does this tile is connected to the island ?
        (is_connected, xy) = h.search_which_tile_is_connec_to( (1,1) )

        self.assertEqual( is_connected, True )
        self.assertEqual( xy[0], 0 )
        self.assertEqual( xy[1], 1 )

        # (2) ..................................................................
        h = Board(['0', '1'])

        # creating an island...
        h.set_a_tile( (0,0), player_id='0', height=3 )
        h.set_a_tile( (0,1), player_id='1', height=2)

        # ... and asking : does this tile is connected to the island ?
        (is_connected,x,y) = h.search_which_tile_is_connec_to( (2,2) )
        
        self.assertEqual( is_connected, False )

        # (3) ..................................................................
        h = Board(['0',])

        # creating an island...
        h.set_a_tile( (0,0), player_id='0', height=3 )

        # ... and asking : does this tile is connected to the island ?
        (is_connected,x,y) = h.search_which_tile_is_connec_to( (1,1) )
        
        self.assertEqual( is_connected, False )
Exemplo n.º 13
0
    def test__essential_digest(self):
        """
                TestBoard.test__essential_digest

                test of Board.essential_digest()
        """
        # (1) ..................................................................
        h1 = Board()
        h2 = Board()
        self.assertEqual( h1.essential_digest(), h2.essential_digest() )

        # (2) ..................................................................
        h1 = Board(['A','B', 'C'])
        h2 = Board()
        self.assertEqual( h1.essential_digest(), h2.essential_digest() )

        # (3) ..................................................................
        h1 = Board(['A','B', 'C'])
        h1.set_a_tile( (0,0), player_id='A', height=1 )
        h1.set_a_tile( (1,0), player_id='B', height=2 )
        h1.set_a_tile( (2,0), player_id='C', height=3 )
        h2 = Board(['A', 'B', 'C'])
        h2.set_a_tile( (0,2), player_id='C', height=3 )
        h2.set_a_tile( (1,1), player_id='B', height=2 )
        h2.set_a_tile( (2,2), player_id='A', height=1 )
        self.assertNotEqual( h1.essential_digest(), h2.essential_digest() )
Exemplo n.º 14
0
    def test__is_the_game_over(self):
        """
                TestBoard.test__is_the_game_over()

                test of Board.is_the_game_over()
        """

        # (1)
        h = Board(['A','B'])
        h.set_a_tile( (1,1), player_id='A', height=1 )
        h.set_a_tile( (2,1), player_id='B', height=1 ) 
        self.assertEqual( h.is_the_game_over(), False)

        # (2)
        h = Board(['A','B'])
        h.set_a_tile( (1,1), player_id='A', height=2 )
        h.set_a_tile( (2,1), player_id='B', height=1 ) 
        self.assertEqual( h.is_the_game_over(), False)

        # (3)
        h = Board(['A','B'])
        h.set_a_tile( (1,1), player_id='A', height=2 )
        h.set_a_tile( (4,1), player_id='B', height=1 ) 
        self.assertEqual( h.is_the_game_over(), True)
Exemplo n.º 15
0
    def test__how_many_adjacent_tiles_around(self):
        """
                TestBoard.test__how_many_adjacent_tiles_around()

                test of Board.how_many_adjacent_tiles_around()
        """
        # (1) ..................................................................
        h = Board()
        self.assertEqual( h.how_many_adjacent_tiles_around((0,0)), 0 )
        self.assertEqual( h.how_many_adjacent_tiles_around((-99,9)), 0 )
        
        # (2) ..................................................................
        h = Board(['A','B'])
        h.set_a_tile( (1,1), player_id='A', height=3 )
        h.set_a_tile( (2,1), player_id='B', height=3 ) 
        self.assertEqual( h.how_many_adjacent_tiles_around((1,1)), 1 )
        self.assertEqual( h.how_many_adjacent_tiles_around((2,1)), 1 )
        self.assertEqual( h.how_many_adjacent_tiles_around((1,0)), 2 )
        self.assertEqual( h.how_many_adjacent_tiles_around((2,0)), 1 )
        self.assertEqual( h.how_many_adjacent_tiles_around((2,1)), 1 )
        self.assertEqual( h.how_many_adjacent_tiles_around((2,2)), 2 )
        self.assertEqual( h.how_many_adjacent_tiles_around((1,2)), 1 )
        self.assertEqual( h.how_many_adjacent_tiles_around((3,1)), 1 )
        self.assertEqual( h.how_many_adjacent_tiles_around((0,0)), 0 )
Exemplo n.º 16
0
    def test__compare_after_normalization(self):
        
        """
                TestBoard.test__compare_after_normalization

                test of Board.compare_after_normalization
        """

        # (1) ..................................................................
        #
        # empty tiles are equal.
        #
        h1 = Board()
        h2 = Board()
        self.assertEqual( h1.compare_after_normalization(h2), True )
        self.assertEqual( h2.compare_after_normalization(h1), True )

        # (2) ..................................................................
        #
        # trivial comparison.
        #
        h1 = Board(['A',])
        h1.set_a_tile( (0,5), player_id='A', height=3 )
        h2 = Board()
        self.assertEqual( h1.compare_after_normalization(h2), False )
        self.assertEqual( h2.compare_after_normalization(h1), False )

        # (3) ..................................................................
        #
        # trivial comparison.
        #
        h1 = Board(['A',])
        h1.set_a_tile( (0,0), player_id='A', height=9 )
        h2 = Board(['A',])
        h2.set_a_tile( (0,0), player_id='A', height=9 )
        self.assertEqual( h1.compare_after_normalization(h2), True )
        self.assertEqual( h2.compare_after_normalization(h1), True )

        # (4) ..................................................................
        #
        # identity through translation.
        #
        h1 = Board(['A',])
        h1.set_a_tile( (0,0), player_id='A', height=9 )
        h2 = Board(['ABC',])
        h2.set_a_tile( (8,5), player_id='ABC', height=9 )
        self.assertEqual( h1.compare_after_normalization(h2), True )
        self.assertEqual( h2.compare_after_normalization(h1), True )

        # (5) ..................................................................
        #
        # player's name has no influence on the comparison.
        #
        h1 = Board(['A','B'])
        h1.set_a_tile( (0,0), player_id='A', height=9 )
        h1.set_a_tile( (0,1), player_id='B', height=4 )
        h2 = Board(['ABC', 'DEF'])
        h2.set_a_tile( (0,0), player_id='ABC', height=9 )
        h2.set_a_tile( (0,1), player_id='DEF', height=4 )
        self.assertEqual( h1.compare_after_normalization(h2), True )
        self.assertEqual( h2.compare_after_normalization(h1), True )

        # (6) ..................................................................
        #
        # order in players' id matters.
        #
        h1 = Board(['A','B'])
        h1.set_a_tile( (0,0), player_id='A', height=9 )
        h1.set_a_tile( (0,1), player_id='B', height=4 )
        h2 = Board(['DEF', 'ABC'])
        h2.set_a_tile( (0,0), player_id='ABC', height=9 )
        h2.set_a_tile( (0,1), player_id='DEF', height=4 )
        self.assertEqual( h1.compare_after_normalization(h2), False )
        self.assertEqual( h2.compare_after_normalization(h1), False )

        # (7) ..................................................................
        #
        # identity through rotation.
        #
        h1 = Board(['A','B', 'C'])
        h1.set_a_tile( (0,0), player_id='A', height=1 )
        h1.set_a_tile( (1,0), player_id='B', height=2 )
        h1.set_a_tile( (2,0), player_id='C', height=3 )
        h2 = Board(['A', 'B', 'C'])
        h2.set_a_tile( (0,2), player_id='C', height=3 )
        h2.set_a_tile( (1,1), player_id='B', height=2 )
        h2.set_a_tile( (2,2), player_id='A', height=1 )
        self.assertEqual( h1.compare_after_normalization(h2), True )
        self.assertEqual( h2.compare_after_normalization(h1), True )

        # (8) ..................................................................
        #
        # heights matter.
        #
        h1 = Board(['A',])
        h1.set_a_tile( (0,0), player_id='A', height=4 )
        h2 = Board(['A',])
        h2.set_a_tile( (0,0), player_id='A', height=9 )
        self.assertEqual( h1.compare_after_normalization(h2), True )
        self.assertEqual( h2.compare_after_normalization(h1), True )
Exemplo n.º 17
0
    def test__find_a_connection_betw_2_isl(self):
        
        """
                TestBoard.test__find_a_connection_betw_2_isl

                test of Board.find_a_connection_betw_2_isl
        """
        
        # (1) ..................................................................
        # creating a first island...
        h1 = Board(['0', '1'])
        h1.set_a_tile( (0,0), player_id='0', height=3 )
        h1.set_a_tile( (0,1), player_id='1', height=2)
        # ... and a second one :
        h2 = Board(['0',])
        h2.set_a_tile( (5,5), player_id='0', height=3 )

        connection = Board.find_a_connection_betw_2_isl(h1, h2)
        self.assertEqual(connection, False)

        connection = Board.find_a_connection_betw_2_isl(h2, h1)
        self.assertEqual(connection, False)

        # (2) ..................................................................
        # creating a first island...
        h1 = Board(['0', '1'])
        h1.set_a_tile( (0,0), player_id='0', height=3 )
        h1.set_a_tile( (0,1), player_id='1', height=2)
        # ... and a second one :
        h2 = Board(['0',])
        h2.set_a_tile( (1,0), player_id='0', height=3 )

        connection = Board.find_a_connection_betw_2_isl(h1, h2)
        self.assertEqual(connection, True)

        connection = Board.find_a_connection_betw_2_isl(h2, h1)
        self.assertEqual(connection, True)

        # (3) ..................................................................
        # creating a first island...
        h1 = Board(['0',])
        h1.set_a_tile( (0,0), player_id='0', height=3 )
        # ... and a second one :
        h2 = Board(['0',])
        h2.set_a_tile( (1,1), player_id='0', height=3 )

        connection = Board.find_a_connection_betw_2_isl(h1, h2)
        self.assertEqual(connection, False)

        connection = Board.find_a_connection_betw_2_isl(h2, h1)
        self.assertEqual(connection, False)
Exemplo n.º 18
0
    def test__get_the_moves_a_player_may_play(self):
        """
                TestBoard.test__get_the_moves_a_player_may_play

                test of Board.get_the_moves_a_player_may_play
        """
        # (1) ..................................................................
        h = Board(['A','B', 'C', 'D'])

        self.assertEqual( len(h.get_the_moves_a_player_may_play('A')), 0 )
        
        # (2) ..................................................................
        h = Board(['A','B', 'C', 'D'])
        h.set_a_tile( (0,0), player_id='A', height=1 )
        h.set_a_tile( (1,0), player_id='B', height=2 )
        h.set_a_tile( (2,0), player_id='C', height=3 )

        self.assertEqual( len(h.get_the_moves_a_player_may_play('A')), 0 )

        self.assertEqual( len(h.get_the_moves_a_player_may_play('B')), 1 )
        self.assertEqual( Move(((1,0),(0,0))) in h.get_the_moves_a_player_may_play('B'), True )

        self.assertEqual( len(h.get_the_moves_a_player_may_play('C')), 1 )
        self.assertEqual( Move(((2,0),(1,0))) in h.get_the_moves_a_player_may_play('C'), True )
Exemplo n.º 19
0
    def test__get_islands(self):
        """
                TestBoard.test__get_islands

                test of Board.get_islands
        """
        
        # (1) ..................................................................
        h = Board(['0','1'])
        h.set_a_tile( (0,0), player_id='0', height=3)
        h.set_a_tile( (0,1), player_id='1', height=2)
        h.set_a_tile( (1,1), player_id='1', height=2)
        self.assertEqual( len(h.get_islands()), 1 )

        # (2) ..................................................................
        h = Board()
        self.assertEqual( len(h.get_islands()), 0 )

        # (3) ..................................................................
        h = Board(['0','1'])
        h.set_a_tile( (0,0), player_id='0', height=3)
        h.set_a_tile( (0,1), player_id='1', height=2)
        h.set_a_tile( (1,1), player_id='1', height=2)
        h.set_a_tile( (5,5), player_id='1', height=2)

        self.assertEqual( len(h.get_islands()), 2 )

        # (3) ..................................................................
        h = Board(['0','1', '2'])
        h.set_a_tile( (0,0), player_id='0', height=9 )
        h.set_a_tile( (1,0), player_id='1', height=1 )
        h.set_a_tile( (0,1), player_id='2', height=2 )
        h.set_a_tile( (1,1), player_id='0', height=1 )
        h.set_a_tile( (2,2), player_id='1', height=2 )
        h.set_a_tile( (1,2), player_id='2', height=2 )
        h.set_a_tile( (3,2), player_id='2', height=2 )
        h.set_a_tile( (4,2), player_id='2', height=2 )
        h.set_a_tile( (5,1), player_id='2', height=2 )
        h.set_a_tile( (8,8), player_id='2', height=2 )
        h.set_a_tile( (9,9), player_id='2', height=2 )
        h.set_a_tile( (5,5), player_id='2', height=2 )

        self.assertEqual( len(h.get_islands()), 4 )
Exemplo n.º 20
0
    def test__apply_a_60_degrees_rotation(self):
        """
                TestBoard.test__apply_a_60_degrees_rotation()
        """
        # (1) ..................................................................        
        h1 = Board(['A','B', 'C'])
        h1.set_a_tile( (0,0), player_id='A', height=1 )
        h1.set_a_tile( (1,0), player_id='B', height=2 )
        h1.set_a_tile( (2,0), player_id='C', height=3 )
        h2 = Board(['A', 'B', 'C'])
        h2.set_a_tile( (0,0), player_id='A', height=1 )
        h2.set_a_tile( (0,1), player_id='B', height=2 )
        h2.set_a_tile( (1,1), player_id='C', height=3 )

        h1.apply_a_60_degrees_rotation()
        
        self.assertEqual( h1, h2 )

        # (2) ..................................................................
        #
        # 6*60° = 360° .
        # 
        h1 = Board(['A','B', 'C'])
        h1.set_a_tile( (0,0), player_id='A', height=1 )
        h1.set_a_tile( (1,0), player_id='B', height=2 )
        h1.set_a_tile( (2,0), player_id='C', height=3 )

        h1.apply_a_60_degrees_rotation()
        h1.apply_a_60_degrees_rotation()
        h1.apply_a_60_degrees_rotation()
        h1.apply_a_60_degrees_rotation()
        h1.apply_a_60_degrees_rotation()
        h1.apply_a_60_degrees_rotation()
        
        h2 = Board(['A', 'B', 'C'])
        h2.set_a_tile( (0,0), player_id='A', height=1 )
        h2.set_a_tile( (1,0), player_id='B', height=2 )
        h2.set_a_tile( (2,0), player_id='C', height=3 )

        self.assertEqual( h1, h2 )