Пример #1
0
    def get_goal_state(self, size_of_board):
        """ This method returns a board with the N-puzzle goal state
        :param size_of_board: does not include the addition blank tile; size_of_board = N in an N puzzle, which is one less than the total amount of tiles on the board
        :type size_of_board: int
        :return: Board object"""
        #todo : maybe I want to return a Board.board
        total_num_of_tiles = size_of_board + 1
        sqrt_of_total_number_of_tiles = int(sqrt(total_num_of_tiles))
        row_num = sqrt_of_total_number_of_tiles
        col_num = sqrt_of_total_number_of_tiles
        odd_halfway_point = total_num_of_tiles // 2 + 1

        # temp_tile_board = TileBoard(self.n)  # CANNOT create object of a class inside a method of that class, todo: Why is this not good practice; recursion?
        temp_board = Board(row_num, col_num)
        item_value = 1  # item being placed in Board.board
        for i in range(temp_board.get_rows()):
            for j in range(temp_board.get_cols()):
                if item_value == odd_halfway_point:
                    temp_board.place(i, j, None)
                else:
                    if item_value >= odd_halfway_point + 1:  # Note: <= saved the day
                        temp_board.place(i, j, item_value - 1)
                    else:
                        temp_board.place(i, j, item_value)
                item_value += 1
        return temp_board
Пример #2
0
 def build_board(self):
     ctr = 0
     for row in range(0, Board.get_rows(self)):
         for col in range(0, Board.get_cols(self)):
             Board.place(self, row, col, self.numList[ctr])
             if (self.numList[ctr] == None
                 ):  #if null spot then save current player pos
                 self.currentPos[0] = row
                 self.currentPos[1] = col
             ctr += 1
Пример #3
0
 def convert_to_board(self, list_to_convert):
     """ This method converts a one dimensional list to a multi-dimensional board
     :param list_to_convert
     :type list_to_convert: one dimensional list
     :return: Board.board: multi-dimensional list
     """
     temp_board = Board(self.sqrt_total_number_of_tiles,
                        self.sqrt_total_number_of_tiles)
     current_idx = 0
     for i in range(temp_board.get_rows()):
         for j in range(temp_board.get_cols()):
             temp_board.place(i, j, list_to_convert[current_idx])
             current_idx += 1
     return temp_board.board
Пример #4
0
    def get_actions(self):
        #create list of tuples as moves
        self.possMoves = []

        #iterate thru poss moves
        if ((self.currentPos[0] - 1) >= 0):  # UP
            self.possMoves.append([-1, 0])
        if ((self.currentPos[0] + 1) < Board.get_rows(self)):  # DOWN
            self.possMoves.append([1, 0])

        if ((self.currentPos[1] - 1) >= 0):  #LEFT
            self.possMoves.append([0, -1])
        if ((self.currentPos[1] + 1) < Board.get_cols(self)):  #RIGHT
            self.possMoves.append([0, 1])

        return self.possMoves