напиши алгоритм для решения blockudoku с графическим интерфейсом, сверху основная сетка,…
- User
напиши алгоритм для решения blockudoku с графическим интерфейсом, сверху основная сетка, снизу 3 сетки 5x5 для пользовательских фигур, т.е. их можно нарисовать. не забудь про логику самой игры.
- Assistant
Certainly! Below is a Python program that implements a Blockudoku solver with a graphical interface using the
pygamelibrary. The interface includes:- Main Grid: A 9x9 grid displayed at the top.
- Custom Shape Grids: Three 5x5 grids at the bottom where users can draw their own shapes.
The game logic includes:
- Allowing users to draw custom shapes.
- Placing shapes onto the main grid.
- Clearing filled rows, columns, and 3x3 squares.
Note: To run this program, you need to have
pygameinstalled. You can install it usingpip install pygame.import pygame import sys # Initialize Pygame pygame.init() # Define Colors WHITE = (255, 255, 255) GRAY = (200, 200, 200) LIGHT_GRAY = (230, 230, 230) BLACK = (0, 0, 0) BLUE = (100, 100, 255) GREEN = (100, 255, 100) RED = (255, 100, 100) # Set up the display WIDTH = 540 HEIGHT = 800 WINDOW_SIZE = (WIDTH, HEIGHT) screen = pygame.display.set_mode(WINDOW_SIZE) pygame.display.set_caption("Blockudoku Solver with Custom Shapes") # Define grid sizes MAIN_GRID_SIZE = 9 MAIN_CELL_SIZE = WIDTH // MAIN_GRID_SIZE CUSTOM_GRID_SIZE = 5 CUSTOM_CELL_SIZE = MAIN_CELL_SIZE # For simplicity # Position of the custom grids CUSTOM_GRID_MARGIN = 20 CUSTOM_GRID_Y = HEIGHT - (CUSTOM_GRID_SIZE * CUSTOM_CELL_SIZE + CUSTOM_GRID_MARGIN * 2) # Positions for the three custom grids CUSTOM_GRID_POSITIONS = [ (CUSTOM_GRID_MARGIN, CUSTOM_GRID_Y), (WIDTH // 2 - CUSTOM_GRID_SIZE * CUSTOM_CELL_SIZE // 2, CUSTOM_GRID_Y), (WIDTH - CUSTOM_GRID_MARGIN - CUSTOM_GRID_SIZE * CUSTOM_CELL_SIZE, CUSTOM_GRID_Y) ] # Initialize grids main_grid = [[0 for _ in range(MAIN_GRID_SIZE)] for _ in range(MAIN_GRID_SIZE)] custom_grids = [[[0 for _ in range(CUSTOM_GRID_SIZE)] for _ in range(CUSTOM_GRID_SIZE)] for _ in range(3)] # Hold the currently selected shape selected_shape = None selected_shape_idx = None offset_x = 0 offset_y = 0 def draw_main_grid(): # Draw the main grid for row in range(MAIN_GRID_SIZE): for col in range(MAIN_GRID_SIZE): rect = pygame.Rect(col * MAIN_CELL_SIZE, row * MAIN_CELL_SIZE, MAIN_CELL_SIZE, MAIN_CELL_SIZE) pygame.draw.rect(screen, WHITE, rect) pygame.draw.rect(screen, GRAY, rect, 1) if main_grid[row][col]: pygame.draw.rect(screen, BLUE, rect) # Draw bold lines for 3x3 subgrids for i in range(0, MAIN_GRID_SIZE + 1, 3): # Vertical lines pygame.draw.line(screen, BLACK, (i * MAIN_CELL_SIZE, 0), (i * MAIN_CELL_SIZE, MAIN_GRID_SIZE * MAIN_CELL_SIZE), 3) # Horizontal lines pygame.draw.line(screen, BLACK, (0, i * MAIN_CELL_SIZE), (MAIN_GRID_SIZE * MAIN_CELL_SIZE, i * MAIN_CELL_SIZE), 3) def draw_custom_grids(): for idx, grid_pos in enumerate(CUSTOM_GRID_POSITIONS): x_offset, y_offset = grid_pos # Draw grid background grid_rect = pygame.Rect(x_offset, y_offset, CUSTOM_GRID_SIZE * CUSTOM_CELL_SIZE, CUSTOM_GRID_SIZE * CUSTOM_CELL_SIZE) pygame.draw.rect(screen, LIGHT_GRAY, grid_rect) # Draw cells for row in range(CUSTOM_GRID_SIZE): for col in range(CUSTOM_GRID_SIZE): rect = pygame.Rect( x_offset + col * CUSTOM_CELL_SIZE, y_offset + row * CUSTOM_CELL_SIZE, CUSTOM_CELL_SIZE, CUSTOM_CELL_SIZE ) pygame.draw.rect(screen, WHITE, rect) pygame.draw.rect(screen, GRAY, rect, 1) if custom_grids[idx][row][col]: pygame.draw.rect(screen, GREEN, rect) def handle_custom_grid_click(pos): global selected_shape, selected_shape_idx, offset_x, offset_y x, y = pos for idx, grid_pos in enumerate(CUSTOM_GRID_POSITIONS): x_offset, y_offset = grid_pos grid_width = CUSTOM_GRID_SIZE * CUSTOM_CELL_SIZE grid_height = CUSTOM_GRID_SIZE * CUSTOM_CELL_SIZE if x_offset <= x < x_offset + grid_width and y_offset <= y < y_offset + grid_height: col = int((x - x_offset) // CUSTOM_CELL_SIZE) row = int((y - y_offset) // CUSTOM_CELL_SIZE) # Toggle the cell custom_grids[idx][row][col] = 1 - custom_grids[idx][row][col] return True return False def handle_shape_selection(pos): global selected_shape, selected_shape_idx, offset_x, offset_y x, y = pos for idx, grid_pos in enumerate(CUSTOM_GRID_POSITIONS): x_offset, y_offset = grid_pos grid_width = CUSTOM_GRID_SIZE * CUSTOM_CELL_SIZE grid_height = CUSTOM_GRID_SIZE * CUSTOM_CELL_SIZE if x_offset <= x < x_offset + grid_width and y_offset <= y < y_offset + grid_height: selected_shape = [row[:] for row in custom_grids[idx]] selected_shape_idx = idx offset_x = x - x_offset offset_y = y - y_offset return True return False def place_shape_on_main_grid(pos): global selected_shape, selected_shape_idx if selected_shape is None: return x, y = pos col = int(x // MAIN_CELL_SIZE) row = int(y // MAIN_CELL_SIZE) # Attempt to place the shape at position (row, col) if can_place_shape(row, col, selected_shape): apply_shape(row, col, selected_shape) # Clear the custom shape grid custom_grids[selected_shape_idx] = [[0 for _ in range(CUSTOM_GRID_SIZE)] for _ in range(CUSTOM_GRID_SIZE)] selected_shape = None selected_shape_idx = None # Check for lines or squares to clear clear_lines_and_squares() def can_place_shape(row, col, shape): shape_size = len(shape) for i in range(shape_size): for j in range(shape_size): if shape[i][j]: grid_row = row + i grid_col = col + j if grid_row >= MAIN_GRID_SIZE or grid_col >= MAIN_GRID_SIZE: return False if main_grid[grid_row][grid_col]: return False return True def apply_shape(row, col, shape): shape_size = len(shape) for i in range(shape_size): for j in range(shape_size): if shape[i][j]: main_grid[row + i][col + j] = 1 def clear_lines_and_squares(): # Clear full rows rows_to_clear = [i for i in range(MAIN_GRID_SIZE) if all(main_grid[i][j] for j in range(MAIN_GRID_SIZE))] for row in rows_to_clear: for j in range(MAIN_GRID_SIZE): main_grid[row][j] = 0 # Clear full columns cols_to_clear = [j for j in range(MAIN_GRID_SIZE) if all(main_grid[i][j] for i in range(MAIN_GRID_SIZE))] for col in cols_to_clear: for i in range(MAIN_GRID_SIZE): main_grid[i][col] = 0 # Clear full 3x3 squares for box_row in range(0, MAIN_GRID_SIZE, 3): for box_col in range(0, MAIN_GRID_SIZE, 3): if all(main_grid[box_row + i][box_col + j] for i in range(3) for j in range(3)): for i in range(3): for j in range(3): main_grid[box_row + i][box_col + j] = 0 def draw_selected_shape(pos): if selected_shape is None: return x, y = pos x -= offset_x y -= offset_y shape_size = len(selected_shape) for i in range(shape_size): for j in range(shape_size): if selected_shape[i][j]: rect = pygame.Rect( x + j * MAIN_CELL_SIZE, y + i * MAIN_CELL_SIZE, MAIN_CELL_SIZE, MAIN_CELL_SIZE ) pygame.draw.rect(screen, RED, rect) def main(): clock = pygame.time.Clock() dragging = False while True: screen.fill(LIGHT_GRAY) draw_main_grid() draw_custom_grids() for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() elif event.type == pygame.MOUSEBUTTONDOWN: if event.button == 1: # Left Mouse Button pos = pygame.mouse.get_pos() if not handle_custom_grid_click(pos): if handle_shape_selection(pos): dragging = True elif event.type == pygame.MOUSEBUTTONUP: if event.button == 1 and dragging: pos = pygame.mouse.get_pos() place_shape_on_main_grid(pos) dragging = False selected_shape = None elif event.type == pygame.MOUSEMOTION: if dragging: pass # Do nothing here, we will use the position directly if dragging and selected_shape is not None: pos = pygame.mouse.get_pos() draw_selected_shape(pos) pygame.display.flip() clock.tick(60) if __name__ == "__main__": main()Explanation of Key Components:
-
Custom Shape Drawing:
- Users can click on the cells of the custom grids to toggle them on or off, drawing custom shapes.
- This is handled in
handle_custom_grid_click().
-
Shape Selection and Dragging:
- When a user clicks on a custom shape grid and drags, the shape is selected.
- The shape follows the mouse cursor.
- This is managed using the
selected_shapevariable anddraw_selected_shape()function.
-
Placing Shapes on Main Grid:
- When the user releases the mouse button over the main grid, the program attempts to place the shape.
- It checks if the placement is valid using
can_place_shape(). - If valid, the shape is placed on the main grid using
apply_shape(). - After placement, it clears any filled rows, columns, or 3x3 squares using
clear_lines_and_squares().
-
Game Logic:
- The game checks for complete rows, columns, and 3x3 squares and clears them.
- The clearing logic is in
clear_lines_and_squares().
How to Use the Program:
-
Drawing Custom Shapes:
- Click on the cells within the custom grids at the bottom to toggle them on or off.
- Design your desired shape.
-
Placing Shapes:
- Click and hold on a custom shape and drag it towards the main grid.
- Release the mouse button over the main grid to place it.
- The shape will only be placed if it fits entirely within the grid and doesn't overlap existing blocks.
-
Automatic Clearing:
- After placing a shape, any complete rows, columns, or 3x3 squares will be cleared automatically.
Important Notes:
-
Shape Placement Validation:
- The program ensures that shapes are only placed in valid positions.
- It prevents overlapping and out-of-bounds placement.
-
Resetting Custom Shapes:
- After placing a shape, the corresponding custom shape grid is cleared, allowing you to draw a new shape.
-
Extensibility:
- You can extend the game logic to include scoring, game over conditions, and more.
Dependencies:
Make sure you have
pygameinstalled:pip install pygameEnjoy creating custom shapes and solving the Blockudoku puzzles!