-
Notifications
You must be signed in to change notification settings - Fork 0
/
board.py
57 lines (48 loc) · 1.93 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
import math
class Board:
"""Class representing a sudoku board."""
def __init__(self, size=9, board=None):
self.size = size
self.board = board
if self.board is None:
self.board = self.__generate_board()
def __generate_board(self):
"""Fill """
board = [[i+j*self.size for i in range(self.size)] for j in range(self.size)]
return board
def is_filled(self):
for i in range(self.size**2):
if self.board[i//self.size][i%self.size] == ' ':
return False
return True
def valid_rows(self):
for row in range(self.size):
current_list = set()
for col in range(self.size):
if self.board[row][col] in current_list:
return False
elif self.board[row][col] != ' ':
current_list.add(self.board[row][col])
return True
def valid_cols(self):
for col in range(self.size):
current_list = set()
for row in range(self.size):
if self.board[row][col] in current_list:
return False
elif self.board[row][col] != ' ':
current_list.add(self.board[row][col])
return True
def valid_boxes(self):
for i in range(0, self.size, int(math.sqrt(self.size))):
for j in range(0, self.size, int(math.sqrt(self.size))):
current_list = set()
for k in range(int(math.sqrt(self.size))):
for l in range(int(math.sqrt(self.size))):
if self.board[i+k][j+l] in current_list:
return False
elif self.board[i+k][j+l] != ' ':
current_list.add(self.board[i+k][j+l])
return True
def is_valid(self):
return self.valid_rows() and self.valid_cols() and self.valid_boxes()