-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbacktrack.py
86 lines (66 loc) · 2.6 KB
/
backtrack.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import pygame
class Grid:
def __init__(self, board, width, height, win):
self.board = board
self.rows = 9
self.cols = 9
self.cubes = [[Cube(self.board[i][j], i, j, width, height) for j in range(9)] for i in range(9)]
self.width = width
self.height = height
self.model = None
self.update_model()
self.win = win
def update_model(self, transpose=False):
if transpose == False:
self.model = [[self.cubes[i][j].value for j in range(self.cols)] for i in range(self.rows)]
else:
self.model = [[self.cubes[j][i].value for j in range(self.cols)] for i in range(self.rows)]
def draw(self):
# Draw Grid Lines
gap = self.width / 9
for i in range(self.rows+1):
if i % 3 == 0 and i != 0:
thick = 4
else:
thick = 1
pygame.draw.line(self.win, (0,0,0), (0, i*gap), (self.width, i*gap), thick)
pygame.draw.line(self.win, (0, 0, 0), (i * gap, 0), (i * gap, self.height), thick)
# Draw Cubes
for i in range(self.rows):
for j in range(self.cols):
self.cubes[i][j].draw(self.win)
def copy(self):
new_grid = Grid(self.board, self.width, self.height, self.win)
return new_grid
class Cube:
rows = 9
cols = 9
def __init__(self, value, row, col, width, height):
self.value = value
self.temp = 0
self.row = row
self.col = col
self.width = width
self.height = height
def draw(self, win):
fnt = pygame.font.SysFont("comicsans", 40)
gap = self.width / 9
x = self.col * gap
y = self.row * gap
if self.temp != 0 and self.value == 0:
text = fnt.render(str(self.temp), 1, (128,128,128))
win.blit(text, (x+5, y+5))
elif not(self.value == 0):
text = fnt.render(str(self.value), 1, (0, 0, 0))
win.blit(text, (x + (gap/2 - text.get_width()/2), y + (gap/2 - text.get_height()/2)))
def draw_change(self, win, colour):
fnt = pygame.font.SysFont("comicsans", 40)
gap = self.width / 9
x = self.col * gap
y = self.row * gap
pygame.draw.rect(win, (255, 255, 255), (x, y, gap, gap), 0)
text = fnt.render(str(self.value), 1, (0, 0, 0))
win.blit(text, (x + (gap / 2 - text.get_width() / 2), y + (gap / 2 - text.get_height() / 2)))
pygame.draw.rect(win, colour, (x+2, y+2, gap-4, gap-4), 3)
def set(self, val):
self.value = val