Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # tk_letters_fitdoku.py -- zzz game test fails
- import tkinter as tk
- import random
- import copy
- size = 10
- cell_size = 40
- root = tk.Tk()
- root.title("tk_letters_fitdoku")
- canvas = tk.Canvas(root, width=size * cell_size, height=size * cell_size)
- canvas.pack()
- def plot(col, row):
- x1 = col * cell_size
- y1 = row * cell_size
- x2 = x1 + cell_size
- y2 = y1 + cell_size
- canvas.create_rectangle(x1, y1, x2, y2, fill=color, outline='black', tags=color)
- def create_grid(size):
- for row in range(size):
- for col in range(size):
- plot(col, row)
- def color_row():
- temp = copy.deepcopy(board)
- row, col = rows.pop(0)
- rows.append((row, col))
- for c in range(col, col+4):
- if board[row][c] != ' ':
- canvas.delete(color)
- return
- temp[row][c] = sss[len(colors)]
- plot(c, row)
- board[:] = temp
- return True
- def color_column():
- temp = copy.deepcopy(board)
- row, col = columns.pop(0)
- columns.append((row, col))
- for r in range(row, row+4):
- if board[r][col] != ' ':
- canvas.delete(color)
- return
- temp[r][col] = sss[len(colors)]
- plot(col, r)
- board[:] = temp
- return True
- def restart_game(event):
- global run_game
- canvas.delete("all")
- run_game = 1
- def write_success():
- for row in range(size):
- x = cell_size * 2.5
- y = row * cell_size + cell_size // 2
- canvas.create_text(x, y, text="success", font=('verdana', 21), fill='gray')
- sss = 'abcdefghijklmnopqrstuvwxyz'
- def test():
- for ttt in board:
- print(ttt)
- print()
- root.bind("<space>", restart_game)
- run_game = 1
- while True:
- while run_game:
- color = 'white'
- create_grid(size)
- board = [[' '] * size for _ in range(size)]
- rows = [(i, j) for i in range(10) for j in range(6)]
- columns = [(i, j) for i in range(6) for j in range(10)]
- colors = 'red orange yellow lime aqua blue pink #d2b48c #bfbfbf magenta #cc99ff #e6ffb6'.split()
- random.shuffle(rows)
- random.shuffle(columns)
- stack = [] # to be used for backtracking
- while colors:
- color = colors.pop()
- while True:
- if color_row():
- break
- # test()
- canvas.update()
- color = colors.pop()
- while True:
- if color_column():
- break
- # test()
- canvas.update()
- run_game = 0
- write_success()
- canvas.update()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement