Skip to content

Commit 6e38a3e

Browse files
committed
done with main functionality
1 parent ea737a2 commit 6e38a3e

File tree

1 file changed

+88
-5
lines changed

1 file changed

+88
-5
lines changed

Sudoku.pyde

Lines changed: 88 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,92 @@
1+
sudoku = [
2+
[3,8,5,0,0,0,0,0,0],
3+
[9,2,1,0,0,0,0,0,0],
4+
[6,4,7,0,0,0,0,0,0],
5+
[0,0,0,1,2,3,0,0,0],
6+
[0,0,0,7,8,4,0,0,0],
7+
[0,0,0,6,9,5,0,0,0],
8+
[0,0,0,0,0,0,8,7,3],
9+
[0,0,0,0,0,0,9,6,2],
10+
[0,0,0,0,0,0,1,4,5],
11+
]
12+
13+
gridx = 603
14+
gridy = 603
115
def setup():
2-
size(480, 480)
16+
size(gridx, gridy)
17+
background(255)
318

419
def draw():
5-
if mousePressed:
6-
fill(0)
20+
drawGrid(sudoku)
21+
mapNums(sudoku)
22+
solveRow(sudoku)
23+
24+
def drawGrid(board):
25+
x1 = 0;
26+
x2 = gridx
27+
y1 = 0;
28+
y2 = 0;
29+
for i in range(len(board)+1):
30+
if y1 % (gridx//3)//3 == 0:
31+
strokeWeight(3)
32+
else:
33+
strokeWeight(1)
34+
line(x1, y1, x2, y2)
35+
line(y1, x1, y2, x2);
36+
y1 += gridy//len(board)
37+
y2 += gridy//len(board)
38+
39+
def mapNums(board):
40+
x1 = gridx//9 - (0.5*(gridx//9))
41+
y1 = gridy//9 - (0.5*(gridy//9))
42+
textSize(40)
43+
textAlign(CENTER, CENTER)
44+
fill(0);
45+
for row in range(len(board)):
46+
for col in range(len(board[0])):
47+
if board[row][col] != 0:
48+
text(board[row][col], x1, y1)
49+
x1+=gridx//9
50+
y1+=gridy//9
51+
x1 = gridx//9 - (0.5*(gridx//9))
52+
53+
def solveRow(board):
54+
if isCompleted(board) == False:
55+
return True
756
else:
8-
fill(255)
9-
ellipse(mouseX, mouseY, 80, 80)
57+
r = isCompleted(board)[0]
58+
c = isCompleted(board)[1]
59+
if board[r][c] == 0:
60+
for count in range(1, 10):
61+
if isValid(r, c, count, board):
62+
board[r][c] = count
63+
if solveRow(board):
64+
return True
65+
board[r][c] = 0
66+
return False
67+
68+
def isValid(row, col, num, board):
69+
if num != 0:
70+
for c in range(len(board[0])):
71+
if board[row][c] == num:
72+
return False
73+
for r in range(len(board)):
74+
if board[r][col] == num:
75+
return False
76+
row_pos = row//3
77+
col_pos = col//3
78+
start_row = row_pos * 3
79+
start_col = col_pos * 3
80+
for r in range(3):
81+
for c in range(3):
82+
if board[r + start_row][c + start_col] == num:
83+
return False
84+
return True
85+
86+
def isCompleted(board):
87+
for r in range(len(board)):
88+
for c in range(len(board[0])):
89+
if board[r][c] == 0:
90+
return r, c
91+
return False
92+

0 commit comments

Comments
 (0)