Skip to content

raghumanimehta/Sudoku-game

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

60 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Overview

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


Features

1. Cell Interaction and Note Mode

  • 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.

2. Difficulty Levels

  • 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.

3. Sudoku Puzzle Generation

  • 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.

4. Solving and Checking

  • 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.

5. Additional Controls

  • Users can clear the board to reset to the initial state, delete a selected cell's value, or enter numbers using on-screen buttons.

Getting Started

Prerequisites

  • A modern web browser that supports JavaScript and HTML5.
  • Basic knowledge of Sudoku rules and gameplay.

Installing and Running the Game

  1. Clone the repository:

    git clone https://github.com/your-username/sudoku-app.git
    cd sudoku-app
  2. Open the index.html file in your preferred browser:

    open index.html

Documentation

1. SelectedCell Class

  • 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.

Methods:

  • 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.

2. RadioButton Class

  • Purpose: Handles difficulty selection using radio buttons and updates the board accordingly.

Methods:

  • 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.

3. Utility Functions

  • 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.

4. Board and Cell Interaction

  • 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.

5. Additional Game Mechanics

  • 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.

6. Backtracking Algorithm

  • Solving the Board:
    • solveBoard(board: number[][]) - Solves the Sudoku board using backtracking, trying every number in each empty cell until the solution is found.

Game Flow

  1. Select Difficulty: Players can choose from five difficulty levels. The Sudoku board will be generated based on the selected difficulty.
  2. Solve the Puzzle: Players can click on the cells to enter numbers or toggle the note mode for possible candidates.
  3. Check Progress: The "Check" button allows players to verify their current solution.
  4. Auto-Solve: If stuck, players can click the "Solve" button to see the solution to the puzzle.

Contributing

We are open to feature requests, but as of now, we consider the project to be complete.

Acknowledgements

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.

About

Sudoku generator and solver

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 3

  •  
  •  
  •