Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions .idea/Sudoku-Project.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/inspectionProfiles/profiles_settings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 46 additions & 0 deletions cell.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import pygame
class Cell:
def __init__(self, value, row, col, screen):
self.value = value
self.sketched_value = 0
self.row = row
self.col = col
self.screen = screen
self.size = size
self.selected = False
"""
Initializes a Cell object
Parameters: value (the value in the cell), row (row index of the cell), col (column index of the cell), screen (pygame screen where the cell is drawn)
"""
def set_cell_value(self, value):
self.value = value
"""
sets the value of the cell
"""
def set_sketched_value(self, value):
self.sketched_value = value
"""
sets the sketched value of the cell
"""

def draw(self):
cell_size = 50
x = self.col * cell_size
y = self.row * cell_size

#draws cell border
pygame.draw.rect(self.screen, (0, 0, 0), (x, y, cell_size, cell_size), 1)
# if cell is selected, it will be highlighted red
if self.selected:
pygame.draw.rect(self.screen, (100, 0, 0), (x, y, cell_size, cell_size), 5)
font = pygame.font.Font(None, 40)
#draws the value (non-user input) if it exists
if self.value != 0:
num = font.render(str(self.value), 0, (0, 0, 0))
self.screen.blit(num, num.get_rect(center=((self.col *cell_size + cell_size/2), (self.row * cell_size + cell_size/2))))
#draws sketched value (user input) if it exists
elif self.sketched_value != 0:
sketch_num = font.render(str(self.sketched_value), 0, (50, 50, 50))
self.screen.blit(sketch_num, sketch_num.get_rect(topleft=(x, y)))


64 changes: 52 additions & 12 deletions sudoku_generator.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

import math,random

"""
Expand All @@ -23,7 +24,10 @@ class SudokuGenerator:
None
'''
def __init__(self, row_length, removed_cells):
pass
self.row_length = row_length
self.removed_cells = removed_cells
self.board = [[0 for i in range(self.row_length)] for j in range(self.row_length)] #Using 0 to mean there is no value placed
self.box_length = int(row_length ** 0.5)

'''
Returns a 2D python list of numbers which represents the board
Expand All @@ -32,7 +36,7 @@ def __init__(self, row_length, removed_cells):
Return: list[list]
'''
def get_board(self):
pass
return self.board

'''
Displays the board to the console
Expand All @@ -42,7 +46,10 @@ def get_board(self):
Return: None
'''
def print_board(self):
pass
for i in range(self.row_length):
for j in range(self.row_length):
print(self.board[i][j], end=" ")
print()

'''
Determines if num is contained in the specified row (horizontal) of the board
Expand All @@ -55,7 +62,10 @@ def print_board(self):
Return: boolean
'''
def valid_in_row(self, row, num):
pass
for col in range(self.row_length):
if self.board[row][col] == num:
return False
return True

'''
Determines if num is contained in the specified column (vertical) of the board
Expand All @@ -68,7 +78,10 @@ def valid_in_row(self, row, num):
Return: boolean
'''
def valid_in_col(self, col, num):
pass
for row in range(self.row_length):
if self.board[row][col] == num:
return False
return True

'''
Determines if num is contained in the 3x3 box specified on the board
Expand All @@ -83,7 +96,11 @@ def valid_in_col(self, col, num):
Return: boolean
'''
def valid_in_box(self, row_start, col_start, num):
pass
for i in range(row_start, row_start+3):
for j in range(col_start, col_start+3):
if self.board[i][j] == num:
return False
return True

'''
Determines if it is valid to enter num at (row, col) in the board
Expand All @@ -96,8 +113,10 @@ def valid_in_box(self, row_start, col_start, num):
Return: boolean
'''
def is_valid(self, row, col, num):
pass

#Because valid_in_box needs the box's starting row and col
row_start = (row//3) * 3
col_start = (col//3) * 3
return self.valid_in_col(col, num) and self.valid_in_row(row, num) and self.valid_in_box(row_start, col_start, num)
'''
Fills the specified 3x3 box with values
For each position, generates a random digit which has not yet been used in the box
Expand All @@ -109,7 +128,15 @@ def is_valid(self, row, col, num):
Return: None
'''
def fill_box(self, row_start, col_start):
pass
for row in range(row_start, row_start+3):
for col in range(col_start, col_start+3):
if self.board[row][col] == 0:
num = random.randint(1, 9)
while not self.is_valid(row, col, num): #Ensures a valid number is chosen first
num = random.randint(1, 9)

self.board[row][col] = num


'''
Fills the three boxes along the main diagonal of the board
Expand All @@ -119,7 +146,9 @@ def fill_box(self, row_start, col_start):
Return: None
'''
def fill_diagonal(self):
pass
self.fill_box(0,0)
self.fill_box(3,3)
self.fill_box(6,6)

'''
DO NOT CHANGE
Expand Down Expand Up @@ -185,7 +214,18 @@ def fill_values(self):
Return: None
'''
def remove_cells(self):
pass
successfully_removed = 0

row = random.randint(0, self.row_length - 1)
col = random.randint(0, self.row_length - 1)

while successfully_removed < self.removed_cells:
while self.board[row][col] == 0: # Finds a valid row, col with a value to remove
row = random.randint(0, self.row_length - 1)
col = random.randint(0, self.row_length - 1)

self.board[row][col] = 0
successfully_removed += 1

'''
DO NOT CHANGE
Expand All @@ -208,4 +248,4 @@ def generate_sudoku(size, removed):
board = sudoku.get_board()
sudoku.remove_cells()
board = sudoku.get_board()
return board
return board