generated from zhoulisha/Sudoku-Project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboard.py
84 lines (74 loc) · 2.72 KB
/
board.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
from constants import *
# Defines board class
class Board:
def __init__(self, width, height, screen):
self.rows = 9
self.cols = 9
self.width = width
self.height = height
self.screen = screen
self.board = self.initialize_board()
# Draws board
def draw(self):
# draw horizontal lines
board_font = pygame.font.SysFont(None, 75)
for i in range(1, 9):
pygame.draw.line(self.screen, LINE_COLOR, (0, SQUARE_SIZE * i),
(BOARD_WIDTH, SQUARE_SIZE * i), LINE_WIDTH)
# draw vertical lines
for i in range(1, 9):
pygame.draw.line(self.screen, LINE_COLOR, (SQUARE_SIZE * i, 0),
(SQUARE_SIZE * i, BOARD_HEIGHT), LINE_WIDTH)
# draw horizontal box lines
for i in range(1, 3):
pygame.draw.line(self.screen, BOX_COLOR, (0, BOX_SIZE * i),
(BOARD_WIDTH, BOX_SIZE * i), LINE_WIDTH)
# draw vertical box lines
for i in range(1, 3):
pygame.draw.line(self.screen, BOX_COLOR, (BOX_SIZE * i, 0),
(BOX_SIZE * i, BOARD_HEIGHT), LINE_WIDTH)
for i in range(self.rows):
for j in range(self.cols):
if self.board[i][j] != 0:
cell_number = board_font.render(str(self.board[i][j]), 1, (0, 0, 0))
self.screen.blit(cell_number, (j * 720 / 9 + 25, i * 720 / 9 + 20))
# Initializes board
def initialize_board(self):
board = []
for i in range(9):
row = []
for j in range(9):
row.append("-")
board.append(row)
return board
# Prints board
def print_board(self):
for i, row in enumerate(self.board):
for j, col in enumerate(row):
print(self.board[i][j], end=" ")
print()
# Checks if the board if full
def is_full(self):
if any(0 in sublist for sublist in self.board):
return False
else:
return True
# Checks if the board is filled correctly
def check_board(self):
for i in range(9):
if len(set(self.board[i])) != 9:
return False
for i in range(9):
col = [item[i] for item in self.board]
if len(set(col)) != 9:
return False
for i in range(0, 9, 3):
for j in range(0, 9, 3):
vals = self.board[i][j: j + 3]
vals.extend(self.board[i + 1][j: j + 3])
vals.extend(self.board[i + 2][j: j + 3])
if len(set(vals)) != 9:
return False
else:
return True