def __init__(self, n, force_state=None): #Since were calling a 3x3 grid and 8-puzzle #In order to get rows and columns, we need to take the sqrt of n+1 self.side_length = int(math.sqrt(n + 1)) #Our grid is a square so we can just use one var - side_length - for most functions Board.__init__(self, self.side_length, self.side_length, self.side_length) self.force_state = force_state #If there is a force_state, set the grid to it if (force_state != None): self.set_state(force_state)
def __init__(self, size): """Initializes a blank board according to the user's input size and populates the board with the appropriate values""" # Check for valid input size if (math.sqrt(size + 1) % 1) == 0: self.column = int(math.sqrt(size + 1)) else: raise ValueError("%s is an invalid board size" % size) self.row = self.column Board.__init__(self, self.column, self.row) # Generate the list of lists representation of the board self.mult_tiles = [] count = 1 for i in range(self.column): tile = [] for j in range(self.row): if count > size: tile.append(None) else: tile.append(count) count += 1 random.shuffle(tile) self.mult_tiles.append(tile) random.shuffle(self.mult_tiles) # Populate the board for i in range(self.column): for j in range(self.row): Board.place(self, i, j, self.mult_tiles[i][j]) # Grab position of blank tile for i in range(self.column): for j in range(self.row): if self.get(i, j) == None: self.blank_tile = [i, j]