This is a Sudoku web application where users can solve Sudoku puzzles of varying difficulty levels. The project features note-taking functionality, different difficulty levels, and the ability to check and solve the puzzle. The game is built using HTML, CSS, and TypeScript, with various utility functions for generating, solving, and validating Sudoku boards.
Try out the demo here
- Selected Cell Management: The
SelectedCell
class manages the interaction with the currently selected cell on the Sudoku board, including the ability to set values and toggle a note mode. - Note Mode: Users can toggle between entering direct values into the cells or using "note mode" to add possible candidates for a cell.
- RadioButton Class: This class allows users to select from five difficulty levels: Easy, Medium, Hard, Very Hard, and Evil. Users can also reset the difficulty and start a new game based on the selected level.
- The game can generate a Sudoku puzzle with a unique solution for any selected difficulty level. Puzzles are generated by starting with a fully solved board and removing a certain number of cells, based on difficulty.
- Manual Check: Users can manually check if their current solution is correct via the "Check" button.
- Auto-Solve: A "Solve" button allows users to automatically solve the puzzle. The solution process uses backtracking.
- Users can clear the board to reset to the initial state, delete a selected cell's value, or enter numbers using on-screen buttons.
- A modern web browser that supports JavaScript and HTML5.
- Basic knowledge of Sudoku rules and gameplay.
-
Clone the repository:
git clone https://github.com/your-username/sudoku-app.git cd sudoku-app
-
Open the
index.html
file in your preferred browser:open index.html
- Purpose: Manages interactions with the selected cell in the Sudoku grid, allowing users to select cells, highlight rows and columns, toggle note mode, and set values.
SelectedCell()
- Constructor that initializes the selected state of all cells.setCell(newCell: HTMLDivElement)
- Sets a new cell as the selected cell and highlights the corresponding row and column.toggleNoteMode()
- Toggles the note mode for the selected cell.isNoteModeActivated()
- Checks if note mode is activated for the selected cell.setValue(value: string)
- Sets a value for the selected cell, either as a note or direct value.setNoteValue(value: string)
- Toggles a note value in the selected cell.resetIsSelected()
- Resets the selected and highlighted state of all cells.
- Purpose: Handles difficulty selection using radio buttons and updates the board accordingly.
RadioButton(currentlySelected: number)
- Initializes radio buttons for difficulty selection.getButtonForDifficulty(difficulty: number)
- Returns the radio button corresponding to the given difficulty level.changeDifficulty(difficulty: number)
- Changes the difficulty level and updates the board.setAllToZero()
- Resets the selected state of all difficulty radio buttons.addEventListeners()
- Adds event listeners to the radio buttons for difficulty selection.
- Puzzle Generation:
generateSudoku(diff: number)
- Generates a valid Sudoku puzzle based on the selected difficulty level.getCompletelyFilledBoard()
- Returns a fully solved Sudoku board.
- Solving:
solveBoard(board: number[][])
- Solves the board using backtracking.isSolved(board: number[][])
- Checks if the Sudoku board is fully solved.
- Validation:
isValid(board: number[][], row: number, col: number, num: number)
- Checks if a number can be placed in a cell.inRow(board: number[][], row: number, col: number, num: number)
- Checks if a number is present in the row.inCol(board: number[][], row: number, col: number, num: number)
- Checks if a number is present in the column.inBox(board: number[][], row: number, col: number, num: number)
- Checks if a number is present in the 3x3 box.
- Helper Functions:
getRandomInt(min: number, max: number)
- Returns a random integer between min and max.deepCopy<T>(array: T)
- Returns a deep copy of the array.getBoardWithZeros()
- Returns a 9x9 board initialized with zeros.duplicateBoard(board: number[][])
- Returns a copy of the given board.
- Board Initialization:
initializeBoard()
- Initializes the board layout, assigns event listeners to the cells, and displays the board.
- Cell Input:
setNumberButtons()
- Adds event listeners to the number buttons for entering values into the selected cell.setDeleteButton()
- Adds an event listener to delete the value in the selected cell.
- Board Checking and Solving:
setCheckButton()
- Adds an event listener to check if the puzzle is solved.setClearButton()
- Adds an event listener to clear the board and reset it to the initial state.setSolveButton()
- Adds an event listener to solve the board automatically.setTogleNoteModeButton()
- Adds an event listener to toggle note mode for the selected cell.
- Check if the Board is Solved:
isSolved(board: number[][])
- Checks if the current board is solved by validating all rows, columns, and boxes.
- Helper Functions for Sudoku Mechanics:
hasUnique(board: number[][])
- Checks if the current board has a unique solution.hasACopy(i: number, j: number, array: number[][])
- Checks if a given cell is already in the list of visited cells.filterArray(board: number[][], array: number[], row: number, col: number)
- Filters out numbers that are invalid for the given row, column, or box.
- Solving the Board:
solveBoard(board: number[][])
- Solves the Sudoku board using backtracking, trying every number in each empty cell until the solution is found.
- Select Difficulty: Players can choose from five difficulty levels. The Sudoku board will be generated based on the selected difficulty.
- Solve the Puzzle: Players can click on the cells to enter numbers or toggle the note mode for possible candidates.
- Check Progress: The "Check" button allows players to verify their current solution.
- Auto-Solve: If stuck, players can click the "Solve" button to see the solution to the puzzle.
We are open to feature requests, but as of now, we consider the project to be complete.
This project was inspired by the love of Sudoku puzzles and is designed to help users practice solving them while also experimenting with TypeScript and web technologies.