-
Notifications
You must be signed in to change notification settings - Fork 0
/
check.py
110 lines (92 loc) · 2.91 KB
/
check.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
from random import randrange
uniqueNum = False
taileLevel = [[0, 1, 2], [3, 4, 5], [6, 7, 8]]
# A function to check if the board is full
def isBoardComplete(board):
for row in range(0, 9):
for column in range(0, 9):
if board[row][column] == "_":
return False
return True
def createRandomPosition():
j = 0
randomPosition = ()
while j < 2:
randomPosition += (randrange(0, 9),)
j += 1
return randomPosition
def copyBoard(board):
tempRow = 0
tempColumn = 0
tempBoard = []
while tempRow < 9:
tempBoard.append([])
while tempColumn < 9:
tempBoard[tempRow].append(board[tempRow][tempColumn])
tempColumn += 1
tempRow += 1
tempColumn = 0
return tempBoard
# Generate a list of value's row
def createRowList(board, row):
myList = board[row]
return myList
# Generate a list of value's column
def createColumnList(board, column):
myList = []
for index in range(len(board)):
columnValue = board[index][column]
myList.append(columnValue)
return myList
# use the value position to determine which is the initial position of value's Taile
# posible values are: (0,0) (0,3) (0,6) (3,0) (3,3) (3,6) (6,0) (6,3) (6,6)
def createTailePosition(position):
if position in taileLevel[0]:
position = 0
return position
elif position in taileLevel[1]:
position = 3
return position
elif position in taileLevel[2]:
position = 6
return position
# Generate a list of value's taile
def createTaileList(board, row, column):
myList = []
initTaileRow = createTailePosition(row)
columnIndex = createTailePosition(column)
initTaileColumn = columnIndex
endTaileRow = initTaileRow + 2
endTaileColumn = columnIndex + 2
while initTaileRow <= endTaileRow:
while columnIndex <= endTaileColumn:
taileValue = board[initTaileRow][columnIndex]
myList.append(taileValue)
columnIndex += 1
columnIndex = initTaileColumn
initTaileRow += 1
return myList
# Check if a value is unique in the board
def isNumUnique(board, position, num):
row = position[0]
column = position[1]
rowList = createRowList(board, row)
# Check if the number is repeated in row
if str(num) in rowList:
uniqueNum = False
return uniqueNum
else:
# Check if the number is repeated in column
columnList = createColumnList(board, column)
if str(num) in columnList:
uniqueNum = False
return uniqueNum
else:
# Check if the number is repeated in taile
taileList = createTaileList(board, row, column)
if str(num) in taileList:
uniqueNum = False
return uniqueNum
else:
uniqueNum = True
return uniqueNum