def find_filled_cells(): cells = [] m, n = rob.get_field_size() for i in range(m): for j in range(n): if rob.get_cell_type(i, j) == rob.CELL_FILLED: cells.append((i, j)) return cells
def check_solution(self): if not rob.is_parking_point(): return False cell_status = {True: rob.CELL_FILLED, False: rob.CELL_EMPTY} for i in range(10): for j in range(10): if rob.get_cell_type(i, j) != cell_status[i == j]: return False return True
def check_filled_cells(expected): cells = set(expected) m, n = rob.get_field_size() for i in range(m): for j in range(n): if rob.get_cell_type(i, j) == rob.CELL_FILLED: if (i, j) not in cells: return False else: cells.remove((i, j)) else: if (i, j) in cells: return False return len(cells) == 0
def render_maze(task_id): for w in tk.winfo_children(): w.destroy() m, n = rob.get_field_size() w = CELL_SIZE * n + 2 * X_OFFSET h = CELL_SIZE * m + 2 * Y_OFFSET sw = tk.winfo_screenwidth() sh = tk.winfo_screenheight() x = (sw - w) // 2 y = (sh - h) // 2 tk.title("pyrob :: " + task_id) tk.geometry('{}x{}+{}+{}'.format(w, h, x, y)) global canvas canvas = Canvas(tk, width=w, height=h) canvas.pack() lines = [] cells = [] parking_points = [] for i in range(m): for j in range(n): x = X_OFFSET + j * CELL_SIZE y = Y_OFFSET + i * CELL_SIZE cs = (x, y) ce = (x + CELL_SIZE - 1, y + CELL_SIZE - 1) cells.append((i, j, cs, ce)) if rob.is_parking_cell(i, j): parking_points.append( (x + PARKING_POINT_OFFSET, y + PARKING_POINT_OFFSET)) w = rob.is_blocked(i, j, rob.WALL_LEFT) wt = WALL_THICKNESS if w else GRID_THICKNESS wc = WALL_COLOR if w else GRID_COLOR ws = (x, y) we = (x + wt - 1, y + CELL_SIZE - 1) lines.append((ws, we, wc)) w = rob.is_blocked(i, j, rob.WALL_TOP) wt = WALL_THICKNESS if w else GRID_THICKNESS wc = WALL_COLOR if w else GRID_COLOR ws = (x, y) we = (x + CELL_SIZE - 1, y + wt - 1) lines.append((ws, we, wc)) w = rob.is_blocked(i, j, rob.WALL_RIGHT) wt = WALL_THICKNESS if w else GRID_THICKNESS wc = WALL_COLOR if w else GRID_COLOR ws = (x + CELL_SIZE - wt, y) we = (x + CELL_SIZE - 1, y + CELL_SIZE - 1) lines.append((ws, we, wc)) w = rob.is_blocked(i, j, rob.WALL_BOTTOM) wt = WALL_THICKNESS if w else GRID_THICKNESS wc = WALL_COLOR if w else GRID_COLOR ws = (x, y + CELL_SIZE - wt) we = (x + CELL_SIZE - 1, y + CELL_SIZE - 1) lines.append((ws, we, wc)) lines.append(((X_OFFSET - WALL_THICKNESS, Y_OFFSET - WALL_THICKNESS), (X_OFFSET + n * CELL_SIZE + WALL_THICKNESS, Y_OFFSET + WALL_THICKNESS), WALL_COLOR)) lines.append(((X_OFFSET - WALL_THICKNESS, Y_OFFSET + m * CELL_SIZE), (X_OFFSET + n * CELL_SIZE + WALL_THICKNESS, Y_OFFSET + m * CELL_SIZE + WALL_THICKNESS), WALL_COLOR)) lines.append( ((X_OFFSET - WALL_THICKNESS, Y_OFFSET - WALL_THICKNESS), (X_OFFSET, Y_OFFSET + m * CELL_SIZE + WALL_THICKNESS), WALL_COLOR)) lines.append(((X_OFFSET + n * CELL_SIZE, Y_OFFSET - WALL_THICKNESS), (X_OFFSET + n * CELL_SIZE + WALL_THICKNESS, Y_OFFSET + m * CELL_SIZE + WALL_THICKNESS), WALL_COLOR)) def rect(start, end, *args, **kwargs): canvas.create_rectangle(start[0], start[1], end[0] + 1, end[1] + 1, *args, **kwargs) def make_label( x, y, text, ): f = Frame(canvas, height=CELL_SIZE, width=CELL_SIZE) f.pack_propagate(0) f.place(x=x, y=y) label = Label(f, font=("Helvetica", 14), text=text) label.pack(fill=tkinter.BOTH, expand=1) return label for i in range(m): make_label(X_OFFSET - CELL_SIZE - WALL_THICKNESS, Y_OFFSET + CELL_SIZE * i, str(i + 1)) for j in range(n): make_label(X_OFFSET + CELL_SIZE * j, Y_OFFSET - CELL_SIZE - WALL_THICKNESS, str(j + 1)) for i, j, cs, ce in cells: color = CELL_COLOR_MAP[rob.get_cell_type(i, j)] rect(cs, ce, fill=color, width=0, tags='{}_{}'.format(i, j)) for ws, we, wc in lines: rect(ws, we, fill=wc, width=0) for (x, y) in parking_points: canvas.create_oval(x, y, x + 2 * PARKING_POINT_RADIUS, y + 2 * PARKING_POINT_RADIUS, width=0, fill=PARKING_POINT_COLOR) canvas.create_oval(0, 0, 2 * ROBOT_RADIUS, 2 * ROBOT_RADIUS, tags='robot', width=ROBOT_THICKNESS, outline=ROBOT_COLOR)