Example #1
0
    def gen_next_point(self, prev_point):
        """
		Take last created position tuple and find next not used adjacent position
		"""
        adj_cells = []
        new_point = prev_point
        prev_x = prev_point[0]
        prev_y = prev_point[1]

        n_cell = (prev_x, prev_y + 1)
        ne_cell = (prev_x + 1, prev_y + 1)
        e_cell = (prev_x + 1, prev_y)
        se_cell = (prev_x + 1, prev_y - 1)
        s_cell = (prev_x, prev_y - 1)
        sw_cell = (prev_x - 1, prev_y - 1)
        w_cell = (prev_x - 1, prev_y)
        nw_cell = (prev_x - 1, prev_y + 1)

        # Check cells for n,s,e,w
        if self.in_bounds(n_cell):
            adj_cells.append(n_cell)
        if self.in_bounds(s_cell):
            adj_cells.append(s_cell)
        if self.in_bounds(e_cell):
            adj_cells.append(e_cell)
        if self.in_bounds(w_cell):
            adj_cells.append(w_cell)

        # Check cells for ne, nw, se, sw
        if self.in_bounds(ne_cell):
            adj_cells.append(ne_cell)
        if self.in_bounds(se_cell):
            adj_cells.append(se_cell)
        if self.in_bounds(nw_cell):
            adj_cells.append(nw_cell)
        if self.in_bounds(sw_cell):
            adj_cells.append(sw_cell)

        # Loop untill point not on board is found
        while new_point in self.board_points:
            if not adj_cells:
                raise ValueError('Ran out of cells.')
            logger.write("Searching for new point...")
            new_point = adj_cells[random.randint(0, len(adj_cells) - 1)]
            adj_cells.remove(new_point)

        return new_point
Example #2
0
    def bk_trk(self, c, cur_sol, solutions, explored):
        #logger.write(f'Current cell: {c.board_pos}')
        # Too many solutions check
        if len(solutions) >= 3 and not self.sol_pool:
            return

        # Solution = true to find next board pos
        if self.is_solution(c, solution=True, cur_sol=cur_sol):
            logger.write('Solution found!')
            cur_sol.append(c)
            solutions.append(cur_sol)
            return solutions
        # Solution = true to skip valid move check
        elif self.valid_move(c, cur_sol, solution=True):
            cur_sol.append(c)
            explored.append(c)
        else:
            return

        # logger.write(f'Neighbors: {[n.board_pos for n in c.get_nbrs()]}')
        for nbr in c.get_nbrs():
            # if nbr not in explored:
            self.bk_trk(nbr, cur_sol.copy(), solutions, explored.copy())
Example #3
0
    def add_cell(self, position, num):
        """
		Take a position tuple and create a new cell object with that point
		"""
        logger.write(f'Adding new point -> {position}')
        new_cell = Cell(position)
        new_cell.value = num
        if not self.board_cells:
            logger.write(f'Starting point -> {new_cell.board_pos}')
            self.start_cell = new_cell
        elif len(self.board_cells) is self.b_size - 1:
            logger.write(f'End point -> {new_cell.board_pos}')
            self.end_cell = new_cell
        self.board_points[position] = new_cell
        self.board_cells.append(new_cell)
Example #4
0
    def gen_map(self):
        start_point = (random.randint(1, self.b_size),
                       random.randint(1, self.b_size))
        self.add_cell(start_point, 1)
        old_point = start_point

        # Loop through max map size
        for pnts_lft in range(1, self.b_size):
            new_point = self.gen_next_point(old_point)
            self.add_cell(new_point, pnts_lft + 1)
            old_point = new_point
        logger.write(f'Total map size -> {len(self.board_cells)}')
        logger.write('Finding neighbors...')
        self.find_nbrs()
        logger.write('Neighbors found')
Example #5
0
 def __init__(self):
     self.puzzle = Puzzle(size=10, sol_pool=True)
     self.puzzle.gen_map()
     self.puzzle.reslv_pzl()
     logger.write(
         [self.puzzle.fix_pnts[k].value for k in self.puzzle.fix_pnts])
Example #6
0
 def print_board_vals(self):
     for cell in self.board_cells:
         logger.write(f'Cell: {cell.board_pos}, Value: {cell.value}')
Example #7
0
    def reslv_pzl(self):
        """
		Attempts to resolve the current puzzle.
		"""
        cur_sol = []
        solutions = []
        explored = []
        logger.write('Finding solutions...')
        start = self.start_cell
        for nbr in start.get_nbrs():
            self.bk_trk(nbr, [start], solutions, [start])

        if (len(solutions) > 2) and not self.sol_pool:
            logger.write('Too many solutions!')
        elif solutions:
            count = 1
            for solution in solutions:
                logger.write(f'Solution {str(count)}')
                logger.write([cell.value for cell in solution])
                logger.write([cell.board_pos for cell in solution])
                count += 1
            # Take solutions and find enforced points
            self.fix_pnts = self.rslv_fix_pnts(solutions)
        else:
            logger.write('No solutions found')
            logger.write(f'Last solution {[cell.value for cell in cur_sol]}')