Issah45d

Puzzle Problem

Nov 3rd, 2025
999
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.62 KB | None | 0 0
  1. from enum import Enum
  2.  
  3. # import list libaray for the type declarations in the functions
  4. from typing import List
  5.  
  6. class Edge(Enum):
  7.     FLAT = 1
  8.     BUMP = 2
  9.     INSET = 3
  10.  
  11. class Piece:
  12.     def __init__(self, edges: List[Edge]):
  13.         # eEvery list thing should contain 4 edges
  14.         self.edges = edges
  15.    
  16.     # we assume
  17.  
  18.     def rotate_clockwise(self):
  19.         pass
  20.  
  21.     def match(self, other_piece: 'Piece') -> bool:
  22.         pass
  23.  
  24. def arrange_puzzle(pieces: List[Piece], rows: int, cols: int) -> List[List[Piece]]:
  25.     placed = []
  26.  
  27.     create the grid
  28.     puzzle = [[None] * cols for _ in range(rows)]
  29.  
  30.     while pieces:
  31.         piece = pieces.pop(0)
  32.  
  33.         placed_it = False
  34.         for _ in range(4):  # try all rotations
  35.             for r in range(rows):
  36.                 for c in range(cols):
  37.                     # find all the emppty spts
  38.                     if puzzle[r][c] is None:
  39.                         # check if the pieceses match with the pieces around it
  40.                         if (r > 0 and puzzle[r-1][c] and not puzzle[r-1][c].match(piece)):
  41.                             continue
  42.                         if (c > 0 and puzzle[r][c-1] and not puzzle[r][c-1].match(piece)):
  43.                             continue
  44.  
  45.                         puzzle[r][c] = piece
  46.                         placed.append(piece)
  47.                         placed_it = True
  48.                         break
  49.                 if placed_it:
  50.                     break
  51.             if placed_it:
  52.                 break
  53.             else:
  54.                 piece.rotate_clockwise()  # rotate and try again
  55.  
  56.     return puzzle
  57.  
  58.  
Advertisement
Add Comment
Please, Sign In to add comment