-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcell.py
62 lines (51 loc) · 1.81 KB
/
cell.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
class Cell:
'''Represents a single cell on a Sudoku Board'''
def __init__(self):
self._num = None
self.sections = []
self.triedNums = set()
self.locked = False
self._maxNum = 9
def getNum(self):
'''Gets number currently contained in the cell'''
return self._num
def setNum(self, num):
'''Sets _num if num is a legal entry in sudoku logic'''
for section in self.sections:
if num in section.nums():
raise ValueError('Number already exists in section.')
self.triedNums.add(num)
self._num = num
def getMaxNum(self):
return self._maxNum
def setMaxNum(self, maxNum):
self._maxNum = maxNum
def addSection(self, section):
'''Adds a section that the cell belongs to.'''
self.sections.append(section)
def tryNextNum(self):
'''Finds and returns the next legal digit not in triedNums'''
if self.locked:
return False
takenNumbers = self.triedNums
for section in self.sections:
takenNumbers.update(section.nums())
for num in range(1,self._maxNum+1):
if num not in takenNumbers:
return num
return False
def lock(self):
'''Locks the cell so that the solver can't change its value'''
self.locked = True
def unlock(self):
'''Unlocks the cell, allowing the solver to edit it'''
self.locked = False
def isLocked(self):
'''Checks if the cell is locked'''
return self.locked
def resetTriedNums(self):
'''Clears previously attempted numbers and the current value'''
self.triedNums = set()
self._num = None
num = property(getNum, setNum)
maxNum = property(getMaxNum, setMaxNum)