Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

working on the main function #24

Open
wants to merge 22 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
6d40c8b
i think this is right
Kris-p111 Nov 26, 2024
bee6531
new stuff
Kris-p111 Nov 26, 2024
378b577
new stuff
Kris-p111 Dec 1, 2024
03e1563
new stuff
Kris-p111 Dec 2, 2024
14a3a5b
new stuff
Kris-p111 Dec 2, 2024
0b97fcd
new stuff
Kris-p111 Dec 3, 2024
d682202
Leah - Updated the main with the buttons and sketch/place_number func…
ljsteff Dec 5, 2024
df2a793
Merge remote-tracking branch 'origin/main'
ljsteff Dec 5, 2024
0205028
Leah - Updated the main with the buttons and sketch/place_number func…
ljsteff Dec 5, 2024
e4be3df
Leah - Updated the main with random number functions and difficulty s…
ljsteff Dec 8, 2024
c045845
Leah - Updated the main with random number functions and difficulty s…
ljsteff Dec 8, 2024
d9b1267
Leah - Updated the main with random number functions and difficulty s…
ljsteff Dec 8, 2024
cb89279
Leah - Updated the main with random number functions and difficulty s…
ljsteff Dec 8, 2024
215daf3
Leah - Updated the main with random number functions and difficulty s…
ljsteff Dec 8, 2024
d7eeada
Leah - pygame.quit() fix
ljsteff Dec 8, 2024
2275f44
Leah - pygame.quit() fix
ljsteff Dec 8, 2024
d8bae55
Leah - started the inputting of the button drawing (restart, reset, q…
ljsteff Dec 11, 2024
d61d64b
Updates from Krishna - fixing the implementation of the other buttons…
ljsteff Dec 11, 2024
6731c4f
Leah - Game over screen is up to date and you can press m to go back …
ljsteff Dec 12, 2024
84305f0
Leah - Fixed game over screen to be implemented with new reset button…
ljsteff Dec 13, 2024
da8c92a
Leah - Arrow keys fixed
ljsteff Dec 13, 2024
980fba4
Leah -Final Code!
ljsteff Dec 13, 2024
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
195 changes: 195 additions & 0 deletions board.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
import pygame
from constants import *
from cell import Cell
from sudoku_generator import SudokuGenerator


class Board:
def __init__(self, width, height, screen, difficulty):
self.board = [[0 for _ in range(9)] for _ in range(9)]
self.width = 750
self.height = 900
self.screen = pygame.display.set_mode((width, height))
self.difficulty = difficulty
self.board_rows = 9
self.board_cols = 9
self.board_line_width = 5
self.cell_size = 82



# difficulty_levels = {"easy": 30, "medium": 40, "hard": 50}
# if self.difficulty in difficulty_levels:
# self.empty_cells = difficulty_levels[self.difficulty]
# else:
# raise ValueError("Invalid difficulty level")

self.original = [row[:] for row in self.board]
self.cells = [[Cell(self.board[row][col], row, col, self.screen)
for col in range(self.board_cols)]
for row in range(self.board_rows)]
self.selected = (0, 0)
def draw(self):
# Calculate the width and height of the board
BOARD_WIDTH = 9 * CELL_SIZE
BOARD_HEIGHT = 9 * CELL_SIZE

# Calculate the starting position
board_start_x = (WIDTH - BOARD_WIDTH) // 2
board_start_y = (HEIGHT - BOARD_HEIGHT) // 2 - 70 # move board up ~ 70 px

# Draw the cells
for row in self.cells:
for cell in row:
cell_rect = pygame.Rect(board_start_x + cell.col * CELL_SIZE, board_start_y + cell.row * CELL_SIZE,
CELL_SIZE, CELL_SIZE)
pygame.draw.rect(self.screen, SCREEN_COLOR, cell_rect)
cell.draw(self.screen)

# Draw the squares for the board
for i in range(0, 4):
pygame.draw.line(self.screen, BLACK,
(board_start_x, board_start_y + (3 * i) * CELL_SIZE),
(board_start_x + BOARD_WIDTH, board_start_y + (3 * i) * CELL_SIZE),
BOARD_LINE_WIDTH)

for i in range(0, 4):
pygame.draw.line(self.screen, BLACK,
(board_start_x + (3 * i) * CELL_SIZE, board_start_y),
(board_start_x + (3 * i) * CELL_SIZE, board_start_y + BOARD_HEIGHT),
BOARD_LINE_WIDTH)



# Marks the cell at (row, col) in the board as the current selected cell
def select(self, row, col):
for i in range(self.board_rows):
for j in range(self.board_cols):
if i == row and j == col:
self.cells[i][j].selected = True
else:
self.cells[i][j].selected = False

# returns a tuple of the (row, col) of the cell which was clicked
def click(self, x, y):
# Calculate the width and height of the board
BOARD_WIDTH = 9 * CELL_SIZE
BOARD_HEIGHT = 9 * CELL_SIZE

# Calculate the starting position
board_start_x = (WIDTH - BOARD_WIDTH) // 2
board_start_y = (HEIGHT - BOARD_HEIGHT) // 2 - 70 # move board up ~ 70 px

# if coordinates inside the board (which calculated by adding board's width and height to the starting points)
if board_start_x <= x < board_start_x + BOARD_WIDTH and board_start_y <= y < board_start_y + BOARD_HEIGHT:
clicked_row = (y - board_start_y) // CELL_SIZE
clicked_col = (x - board_start_x) // CELL_SIZE
return clicked_row, clicked_col

return None

# allows user to remove the cell values and sketched value that are filled by themselves
def clear(self):
for i in range(self.board_rows):
for j in range(self.board_cols):
if self.cells[i][j].selected:
# check if it's an empty cell in the original board
if self.board[i][j] == 0:
self.cells[i][j].sketched_value = 0
self.cells[i][j].value = 0

def sketch(self, value):
for row in range(self.board_rows):
for col in range(self.board_cols):
if self.cells[col][row].selected:
if self.board[col][row] == 0:
self.cells[col][row].set_sketched_value(value)
# sketched_val = str(value)
# font = pygame.font.SysFont('Times New Roman', 100)
# number_print = font.render(sketched_val, True, 'Grey')
# self.screen.blit(number_print, (selected_cell[0], selected_cell[1]))
# pygame.display.update()

def place_number(self, value):
#self.value = value
#sketched_val = str(self.value)
for row in range(self.board_rows):
for col in range(self.board_cols):
if self.cells[col][row].selected:
if self.board[col][row] == 0:
self.cells[col][row].set_cell_value(value)
self.cells[col][row].set_sketched_value(0)
self.board[col][row] = value # Update the board
self.draw() # Refresh the board display
# x = selected_cell[0] * 82
# y = selected_cell[1] * 82
# font = pygame.font.SysFont('Times New Roman', 50)
# number_print = font.render(sketched_val, True, 'Black')
# for event in pygame.event.get():
# if event.type == pygame.KEYDOWN:
# if event.key == pygame.K_RETURN:
# self.screen.blit(number_print, (x, y))

def reset_to_original(self):

for row in range(self.board_rows):
for col in range(self.board_cols):
if self.original[row][col] == 0: # If the cell was empty originally
self.board[row][col] = 0

# Recreate the cells to reflect the reset
self.cells = [[Cell(self.board[row][col], row, col, self.screen)
for col in range(self.board_cols)]
for row in range(self.board_rows)]

return self.cells

def is_full(self):
# self.board = [[0 for _ in range(9)] for _ in range(9)]
for row in self.board:
for cell in row:
if cell == 0:
return False
return True

def update_board(self):
# for row in self.board:
# for col in self.board:
# font = pygame.font.SysFont('Times New Roman', 50)
# number_print = font.render(str(self.value), True, 'Black')
# x = col * 82
# y = row * 82
# self.screen.blit(number_print, (x, y))
# pygame.display.update()
for row in range(self.board_rows):
for col in range(self.board_cols):
if self.cells[col][row].sketched_value != 0:
self.cells[col][row].value = self.cells[col][row].sketched_value

def find_empty(self):
# loop through cells and find empty, then return row and col as tuple
for row in self.board:
for col in self.board:
if self.board[row][col] == '0':
tup = (row, col)
return tup

def check_board(self):
def valid(values):
values = [v for v in values if v != 0]
return sorted(values) == list(range(1, 10))

for row in range(self.board_rows):
if not valid(self.board[row]):
return False
for col in range(self.board_cols):
if not valid(self.board[col]):
return False
for box_row in range(0, 9, 3):
for box_col in range(0, 9, 3):
box = [self.board[row][col]
for row in range(box_row, box_row + 3)
for col in range(box_col, box_col + 3)]
if not valid(box):
return False
return True
55 changes: 55 additions & 0 deletions cell.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import pygame
from constants import *


class Cell:
def __init__(self, value, row, col, screen):
self.value = value
self.row = row
self.col = col
self.screen = screen
self.sketched_value = 0
self.selected = False


def set_cell_value(self, value):
self.value = value

def set_sketched_value(self, value):
self.sketched_value = value

def draw(self, screen):
# Calculate the width and height of the Sudoku board
BOARD_WIDTH = 9 * CELL_SIZE
BOARD_HEIGHT = 9 * CELL_SIZE

# Calculate the starting position to center the board
board_start_x = (WIDTH - BOARD_WIDTH) // 2
board_start_y = (HEIGHT - BOARD_HEIGHT) // 2 - 70 # move board up ~ 70 px

# Draw the cell rectangle
cell_rect = pygame.Rect(board_start_x + self.col * CELL_SIZE, board_start_y + self.row * CELL_SIZE, CELL_SIZE,
CELL_SIZE)
pygame.draw.rect(screen, BLACK, cell_rect, CELL_LINE_WIDTH)

# Draw a thicker border for the cell if selected
if self.selected:
pygame.draw.rect(screen, BLUE, cell_rect, 6)

# Draw the cell value (if not zero)
if self.value != 0 and self.sketched_value == 0:
cell_font = pygame.font.Font(None, 55)
cell_surf = cell_font.render(str(self.value), True, BLACK)
cell_rect = cell_surf.get_rect(
center=(board_start_x + self.col * CELL_SIZE + CELL_SIZE // 2,
board_start_y + self.row * CELL_SIZE + CELL_SIZE // 2))
screen.blit(cell_surf, cell_rect)
# Draw the value using user input
if self.sketched_value != 0:
cell_font = pygame.font.Font(None, 50)
cell_surf = cell_font.render(str(self.sketched_value), True, USER_NUMBER_COLOR)
# draw the value in the upper left corner (not center)
cell_rect = cell_surf.get_rect(
center=((board_start_x + self.col * CELL_SIZE + CELL_SIZE // 2) - 12,
(board_start_y + self.row * CELL_SIZE + CELL_SIZE // 2) - 12))
screen.blit(cell_surf, cell_rect)
20 changes: 20 additions & 0 deletions constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
WIDTH = 750
HEIGHT = 900
BOARD_ROWS = 4
BOARD_COLS = 3
BOARD_LINE_WIDTH = 5
CELL_LINE_WIDTH = 1
CELLS_IN_SQUARE = 3
NUMBER_FONT = 100
USER_NUMBER_COLOR = (105, 105, 105)
NUMBER_COLOR = (0, 0, 0)
MESSAGE_FONT = 50
CELL_SIZE = 82
RED = (255, 0, 0)
WHITE = (240, 248, 255)
BG_COLOR = (224, 255, 255)
BLUE = (0, 0, 255)
DARK_BLUE = (25, 25, 112)
DODGER_BLUE = (0, 90, 156)
SCREEN_COLOR = (173, 216, 230)
BLACK = (0, 0, 0)
Loading