forked from nickdelgrosso/PyxelSudoku2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sudoku.py
206 lines (160 loc) · 5.24 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
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
"""Sudoku Game"""
# import packages and functions defined in board
import random
from copy import deepcopy
from typing import List, NewType
import pyxel
from board import Board, rowsValid, cols_vald, board_valid, update_board
def generate_random_puzzle():
pass
# Check if the board is valid.
def draw():
global puzzle_board
global solution_board
global is_valid
global cell_selected
global selected_value
global game_won
if game_won:
pyxel.cls(10) # Make background yellow if game is won
# Draw each space
for i, row in enumerate(puzzle_board):
for j, value in enumerate(row):
x_offset = 2
y_offset = 2
x = i * 16 + i + x_offset # where to put the subimage
y = j * 16 + j + y_offset # where to put the subimage
image_size = 16
w = image_size
h = image_size
u = 0
v = value * 16
if cell_selected == (i, j):
transparent_color = 7
else:
transparent_color = 10
pyxel.blt(
x, y, 0, u, v, w, h, transparent_color
) # copy part of image from resource file to the screen.
# Draw the lines of the board
lines_col = 0
pyxel.rect(0 + x_offset, 50 + y_offset, w=16 * 9 + 8, h=1, col=lines_col)
pyxel.rect(0 + x_offset, 101 + y_offset, w=16 * 9 + 8, h=1, col=lines_col)
pyxel.rect(50 + x_offset, 0 + y_offset, h=16 * 9 + 8, w=1, col=lines_col)
pyxel.rect(101 + x_offset, 0 + y_offset, h=16 * 9 + 8, w=1, col=lines_col)
pyxel.rect(0, 156, h=7, w=200, col=0)
for idx in range(9):
if selected_value == idx + 1:
transparent_color = 7
else:
transparent_color = 10
pyxel.blt(
idx * 16 + idx + x_offset,
165,
0,
u=0,
v=(idx + 1) * 16,
w=image_size,
h=image_size,
colkey=transparent_color,
) # copy part of image from resource file to the screen.
def get_board_spot(mouse_x, mouse_y):
return min(int(mouse_x // 17), 8), min(int(mouse_y // 17), 8)
def update():
global puzzle_board
global solution_board
global is_valid
global cell_selected
global selected_value
global game_won
if pyxel.btnp(pyxel.KEY_Q):
pyxel.quit()
# select the board spot when the player clicks the left mouse button
if pyxel.btnp(pyxel.MOUSE_LEFT_BUTTON):
mouse_pos = (pyxel.mouse_x, pyxel.mouse_y)
board_spot = get_board_spot(*mouse_pos)
if mouse_pos[1] < 155:
cell_selected = board_spot
else:
selected_value = board_spot[0] + 1
# update the board spot when the player clicks the right mouse button
if pyxel.btnp(pyxel.MOUSE_RIGHT_BUTTON):
mouse_pos = (pyxel.mouse_x, pyxel.mouse_y)
board_spot = get_board_spot(*mouse_pos)
x, y = board_spot
cell_value = puzzle_board[x][y]
if cell_value != selected_value:
puzzle_board[x][y] = selected_value
else:
puzzle_board[x][y] = 0
is_valid = board_valid(puzzle_board, solution_board)
if board_is_full(puzzle_board):
if board_valid(puzzle_board):
game_won = True
else:
game_won = False
else:
game_won = False
def board_is_full(board):
for row in board:
for val in row:
if val == 0:
return False
else:
return True
def format_board(current_board):
return """
{} {} {} | {} {} {} | {} {} {}
{} {} {} | {} {} {} | {} {} {}
{} {} {} | {} {} {} | {} {} {}
---------------------
{} {} {} | {} {} {} | {} {} {}
{} {} {} | {} {} {} | {} {} {}
{} {} {} | {} {} {} | {} {} {}
---------------------
{} {} {} | {} {} {} | {} {} {}
{} {} {} | {} {} {} | {} {} {}
{} {} {} | {} {} {} | {} {} {}
""".format(
*[val if val else " " for row in current_board for val in row]
)
def fill_board(puzzle):
spots = iter(puzzle)
puzzle_board = [[int(next(spots)) for _ in range(9)] for _ in range(9)]
return puzzle_board # change from a string to a list of list of ints
def read_line_from_puzzlefile(file):
# Read sudoku data
f = open(file)
text = f.read()
# Get one of the puzzles and its corresponding solution
lines = text.splitlines()
line_number = random.randint(0, len(lines))
return lines[line_number]
def format_puzzle(line):
line = line.strip()
puzzle, solution = line.split(",")
return puzzle, solution
game_won = False
line = read_line_from_puzzlefile("sudoku.csv")
puzzle, solution = format_puzzle(line)
# Make a board structure to fill in the data with.
empty_board = [[0 for _ in range(9)] for _ in range(9)]
# Fill Board with puzzle data
puzzle_board = fill_board(puzzle)
solution_board = fill_board(solution)
rowsValid(solution_board)
cols_vald(solution_board)
pyxel.init(156, 183, caption="Sudoku Game")
# change board
selected_value = 1
cell_selected = (0, 0)
update_board(puzzle_board, 4, 4, 8)
pyxel.cls(3)
pyxel.text(1, 1, "8", 0)
is_valid = True
pyxel.load("my_resource.pyxres", True, True)
image = pyxel.image(0)
# start the game #
pyxel.mouse(True)
pyxel.run(update, draw)
print("That was fun, why don't we play again?")