-
Notifications
You must be signed in to change notification settings - Fork 0
/
sudoku.py
71 lines (61 loc) · 1.81 KB
/
sudoku.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
# Sudoku Game
import numpy as np
import random
def sudokuGame():
sudoku_list = [1, 2, 3, 4, 5, 6, 7, 8, 9]
sudoku = []
random.shuffle(sudoku_list)
newSudoku = sudoku_list.copy()##
sudoku.append(newSudoku)
print(sudoku)# First Sudoku row.
while len(sudoku) < 9:
choices = sudoku_list.copy()
nextRow = []
while len(nextRow) < 9 and len(choices) > 0:
random.shuffle(sudoku_list) ##
randomNum = random.choice(choices)
while not (checkRow(nextRow, randomNum) and checkCol(sudoku, nextRow, randomNum) and checkBlock(sudoku, nextRow, randomNum) and checkLast(sudoku, randomNum)):
choices.remove(randomNum)
if len(choices) > 0:
randomNum = random.choice(choices)
print(randomNum)
nextRow.append(randomNum)
print(nextRow)
sudoku.append(nextRow)
print(sudoku)
print(sudoku)
def checkRow(aNewRow, num):
if num in aNewRow:
return False
else:
return True
def checkCol(sudoku, aNewRow, num):
l = len(aNewRow)
# while l < 10:
for row in sudoku:
if row[l] == num:
return False
return True
def checkBlock(sudoku, aNewRow, num):
r = len(sudoku)
i = len(aNewRow)
fr, tr = (r//3)*3, r
fc, tc = (i//3)*3, (i//3)*3+3
for row in sudoku[fr:tr]:
if num in row[fc:tc]:
return False
return True
def checkLast(sudoku, num):
r = len(sudoku)
# i = len(aNewRow)
fr, tr = (r//3)*3, r
# fc, tc = (i//3)*3, (i//3)*3+3
if (1 < r > 4) or (4 < r > 7) or (7 < r >= 9):
for row in sudoku[fr:tr]:
a = 0
if num in row[6:9]:
a += 1
if a == 0:
return False
return True
sudokuGame()