Advertisement
here2share

# tk_letters_fitdoku.py

May 21st, 2024
714
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.22 KB | None | 0 0
  1. # tk_letters_fitdoku.py -- zzz game test fails
  2.  
  3. import tkinter as tk
  4. import random
  5. import copy
  6.  
  7. size = 10
  8. cell_size = 40
  9.  
  10. root = tk.Tk()
  11. root.title("tk_letters_fitdoku")
  12.  
  13. canvas = tk.Canvas(root, width=size * cell_size, height=size * cell_size)
  14. canvas.pack()
  15.  
  16. def plot(col, row):
  17.     x1 = col * cell_size
  18.     y1 = row * cell_size
  19.     x2 = x1 + cell_size
  20.     y2 = y1 + cell_size
  21.     canvas.create_rectangle(x1, y1, x2, y2, fill=color, outline='black', tags=color)
  22.  
  23. def create_grid(size):
  24.     for row in range(size):
  25.         for col in range(size):
  26.             plot(col, row)
  27.  
  28. def color_row():
  29.     temp = copy.deepcopy(board)
  30.     row, col = rows.pop(0)
  31.     rows.append((row, col))
  32.     for c in range(col, col+4):
  33.         if board[row][c] != ' ':
  34.             canvas.delete(color)
  35.             return
  36.         temp[row][c] = sss[len(colors)]
  37.         plot(c, row)
  38.     board[:] = temp
  39.     return True
  40.  
  41. def color_column():
  42.     temp = copy.deepcopy(board)
  43.     row, col = columns.pop(0)
  44.     columns.append((row, col))
  45.     for r in range(row, row+4):
  46.         if board[r][col] != ' ':
  47.             canvas.delete(color)
  48.             return
  49.         temp[r][col] = sss[len(colors)]
  50.         plot(col, r)
  51.     board[:] = temp
  52.     return True
  53.  
  54. def restart_game(event):
  55.     global run_game
  56.     canvas.delete("all")
  57.     run_game = 1
  58.  
  59. def write_success():
  60.     for row in range(size):
  61.         x = cell_size * 2.5
  62.         y = row * cell_size + cell_size // 2
  63.         canvas.create_text(x, y, text="success", font=('verdana', 21), fill='gray')
  64.  
  65. sss = 'abcdefghijklmnopqrstuvwxyz'
  66. def test():
  67.     for ttt in board:
  68.         print(ttt)
  69.     print()
  70.  
  71. root.bind("<space>", restart_game)
  72. run_game = 1
  73. while True:
  74.     while run_game:
  75.         color = 'white'
  76.         create_grid(size)
  77.         board = [[' '] * size for _ in range(size)]
  78.         rows = [(i, j) for i in range(10) for j in range(6)]
  79.         columns = [(i, j) for i in range(6) for j in range(10)]
  80.         colors = 'red orange yellow lime aqua blue pink #d2b48c #bfbfbf magenta #cc99ff #e6ffb6'.split()
  81.         random.shuffle(rows)
  82.         random.shuffle(columns)
  83.         stack = [] # to be used for backtracking
  84.  
  85.         while colors:
  86.             color = colors.pop()
  87.             while True:
  88.                 if color_row():
  89.                     break
  90.             # test()
  91.             canvas.update()
  92.             color = colors.pop()
  93.             while True:
  94.                 if color_column():
  95.                     break
  96.             # test()
  97.             canvas.update()
  98.         run_game = 0
  99.         write_success()
  100.     canvas.update()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement