este codigo tiene un error: ""import pygame import sys import random from collections…
- User
este codigo tiene un error: ""import pygame import sys import random from collections import deque import heapq
Configuración de Pygame
pygame.init()
Constantes
TILE_SIZE = 100 GRID_SIZE = 4 WINDOW_HEIGHT = TILE_SIZE * GRID_SIZE + 50 # Espacio extra para el botón WINDOW_WIDTH = TILE_SIZE * GRID_SIZE FPS = 30 # Frames por segundo
Colores
BACKGROUND_COLOR = (255, 255, 255) TILE_COLOR = (0, 102, 204) TEXT_COLOR = (255, 255, 255) BUTTON_COLOR = (0, 204, 102)
Inicializar pantalla
screen = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT)) pygame.display.set_caption('Puzzle 15')
Fuente
font = pygame.font.Font(None, 72) button_font = pygame.font.Font(None, 48)
def create_initial_board(): numbers = list(range(1, GRID_SIZE * GRID_SIZE)) numbers.append(0) random.shuffle(numbers) board = [numbers[i:i + GRID_SIZE] for i in range(0, len(numbers), GRID_SIZE)] return board
def draw_board(board): screen.fill(BACKGROUND_COLOR) for row in range(GRID_SIZE): for col in range(GRID_SIZE): value = board[row][col] if value != 0: x = col * TILE_SIZE y = row * TILE_SIZE pygame.draw.rect(screen, TILE_COLOR, (x, y, TILE_SIZE, TILE_SIZE)) text = font.render(str(value), True, TEXT_COLOR) text_rect = text.get_rect(center=(x + TILE_SIZE // 2, y + TILE_SIZE // 2)) screen.blit(text, text_rect)
# Dibujar botón de resolver solve_button = pygame.Rect(0, WINDOW_HEIGHT - 50, WINDOW_WIDTH, 50) pygame.draw.rect(screen, BUTTON_COLOR, solve_button) button_text = button_font.render('Resolver', True, TEXT_COLOR) button_rect = button_text.get_rect(center=solve_button.center) screen.blit(button_text, button_rect)def find_empty_tile(board): for row in range(GRID_SIZE): for col in range(GRID_SIZE): if board[row][col] == 0: return (row, col) return None
def is_valid_move(empty_tile, click_pos): empty_row, empty_col = empty_tile click_row, click_col = click_pos return (abs(empty_row - click_row) == 1 and empty_col == click_col) or (abs(empty_col - click_col) == 1 and empty_row == click_row)
def swap_tiles(board, tile1, tile2): board[tile1[0]][tile1[1]], board[tile2[0]][tile2[1]] = board[tile2[0]][tile2[1]], board[tile1[0]][tile1[1]]
def is_solved(board): correct = list(range(1, GRID_SIZE * GRID_SIZE)) + [0] flat_board = sum(board, []) return correct == flat_board
def get_neighbors(position): row, col = position neighbors = [ (row + 1, col), (row - 1, col), (row, col + 1), (row, col - 1) ] valid_neighbors = [(r, c) for r, c in neighbors if 0 <= r < GRID_SIZE and 0 <= c < GRID_SIZE] return valid_neighbors
def bfs_solve(start_board): start = tuple(map(tuple, start_board)) goal = tuple(tuple(range(1, GRID_SIZE * GRID_SIZE)) + (0,)) queue = deque([(start, [])]) visited = set([start])
while queue: current, path = queue.popleft() if current == goal: return path empty_pos = next((i, j) for i, row in enumerate(current) for j, val in enumerate(row) if val == 0) for neighbor in get_neighbors(empty_pos): new_board = [list(row) for row in current] swap_tiles(new_board, empty_pos, neighbor) new_board_tuple = tuple(map(tuple, new_board)) if new_board_tuple not in visited: visited.add(new_board_tuple) queue.append((new_board_tuple, path + [neighbor])) return None # Si no hay solución - Assistant
- User
ahora da este error: ""Traceback (most recent call last): File "c:\Users\david\Documents\Proyectos Python\Test python\slider.py", line 224, in
main() File "c:\Users\david\Documents\Proyectos Python\Test python\slider.py", line 202, in main
solution_path = a_star_solve(board) ^^^^^^^^^^^^^^^^^^^ File "c:\Users\david\Documents\Proyectos Python\Test python\slider.py", line 181, in a_star_solve heapq.heappush(open_list, {'board': new_board, 'cost': new_cost, 'parent': node}) TypeError: '<' not supported between instances of 'dict' and 'dict'"" - Assistant
- User
sigue dando el mismo error!!: ""Traceback (most recent call last): File "c:\Users\david\Documents\Proyectos Python\Test python\slider.py", line 215, in main() File "c:\Users\david\Documents\Proyectos Python\Test python\slider.py", line 193, in main solution_path = a_star_solve(board) ^^^^^^^^^^^^^^^^^^^ File "c:\Users\david\Documents\Proyectos Python\Test python\slider.py", line 171, in a_star_solve heapq.heappush(open_list, (total_cost(new_node), new_node)) TypeError: '<' not supported between instances of 'dict' and 'dict'""
- Assistant