Sudoku in CPP
Sudoku in CPP
ABSTRACT:
Sudoku game is well famous and popular game among many players all over the world.
This report details the development of a Sudoku game application that is written in Java. The
application is even developed to work in Android systems. In addition, the report details the
implementation of the complexity of the algorithms used to solve any kind of Sudoku puzzle.
Also, how to generate a puzzle with different level of difficulties and make sure there will be
only one solution. The aim of the report is also to discuss the backtracking, brute force
algorithms and other logics in order to create and solve Sudoku puzzles. Furthermore, the user-
friendly environment is considered in the report as the rules of Sudoku are connected to the
interface. Moreover, the report specifics how well these methods of solving solves the puzzle and
achieved the goal of this implementation. The report concludes by evaluating the end application
to analyse how good it met its objectives and the performance of solving algorithms. Finally, the
report summarises the overall achievements of the application development and indicates other
possible extensions
This project aims to develop a Sudoku game and solver implemented in C++. The game
allows players to interactively solve Sudoku puzzles through a command-line interface. The
solver component utilizes backtracking algorithm to efficiently find solutions to Sudoku puzzles.
The game provides options for generating new puzzles of varying difficulties, allowing players
to challenge themselves with puzzles of increasing complexity. Additionally, the game includes
features such as input validation, error detection, and a timer to track solving duration. The
project demonstrates fundamental programming concepts such as data structures, algorithms, and
user input/output handling in C++.
2
EXISTING SYSTEM:
:Data Structure: Represent the Sudoku grid using a 2D array or a custom data structure to
store the numbers.
Input: Read the Sudoku puzzle from a file or accept input from the user.
Validation: Ensure that the initial puzzle is valid, meaning it follows the rules of Sudoku
(no repeated numbers in rows, columns, or 3x3 subgrids)
Displaying the Grid: You need to display the Sudoku grid to the user. This can be done
using the console or a graphical user interface (GUI) library like SFML or Qt.
Input Handling: Allow the user to interact with the grid by entering numbers to fill in the
empty cells. Ensure that the input is valid and follows Sudoku rules.
Game Logic: Implement the game logic to check if the user's input is correct and if the
puzzle is solved
3
.Generating Puzzles: You can either create puzzles manually or generate them
programmatically. Generating puzzles algorithmically involves creating a complete
Sudoku grid and then removing numbers while ensuring the puzzle remains solvable.
Win Condition: Define conditions for winning the game, such as when the puzzle is
solved correctly.
4
PROPOSED SYSTEM:
A proposed system for implementing Sudoku in C++ could involve several key
components. First, you would need a data structure to represent the Sudoku grid, which could be
a two-dimensional array or a custom class to encapsulate the grid's functionality. This structure
would store the numbers in the grid and provide methods for accessing and modifying them.
Next, you would need functions to validate whether a given number can be placed
in a specific cell according to the rules of Sudoku. These rules state that each row, column, and
3x3 subgrid must contain the numbers 1 through 9 without repetition.
Additionally, you would implement functions for solving the Sudoku puzzle, such
as backtracking algorithms or other strategies like constraint propagation. These algorithms
would systematically fill in empty cells while ensuring that the puzzle remains valid at each step.
To interact with the Sudoku grid, you could create a user interface allowing
players to input numbers, display the current state of the puzzle, and receive feedback on their
progress. This interface could be text-based or graphical, depending on the desired level of
complexity.
Overall, the system would consist of a data structure to represent the Sudoku
grid, functions to enforce the rules of the game, algorithms for solving the puzzle, and a user
interface for interaction. By combining these elements, you could create a robust and efficient
Sudoku solver and game platform in C++.
Throughout this article, by "possibilities" I refer to the set of values that aren't
present along the row, column and within the same 3X3 grid which contains the cell that's
referred to. A rule in Sudoku puzzle game says that the there shouldn't be any repetition of
numbers along each row, column and the subdivided 3X3 grids. This is same as saying that each
row and column and the 3X3 sub-grid should contain all the values from one to nine. The
possibilities in each cell can be computed by checking the row, column and the 3X3 sub-grid and
eliminating the ones that are found. This leaves us with values that aren't entered yet, and which
become valid possibilities for the cell.
5
PROJECT DESCRIPTION:
FEATURES:
Input Board: The program should accept a partially filled Sudoku board
as input. You can represent the Sudoku grid as a 2D array or any other
suitable data structure.
IMPLEMENTATION STEPS:
Create a user interface for inputting Sudoku puzzles and displaying the
solved puzzle.
ALGORITHM:
1. Start.
3. First, enter the values of the sudoku and enter 0 for the unassigned cells.
6. If the 8th row and 9th column (0 indexed matrices) are reached, then return true to avoid
backtracking.
7. If the column value becomes 9, move to the next row and column, and now start from 0.
9. If the current position contains a value >0, then iterate for the next column.
10. Use a for loop to enter the values in the assigned cells.
11. Call another user-defined function to check whether the entered number is valid or not.
13. If the assumed number and position are correct, then return true.
14. If the assumption is wrong, then remove the assigned number and proceed with a
different value for the next assumption.
19. Stop.
#include <iostream>
//Checks whether it will be legal to assign number to the given row, col
//Check if we find the same number in the similar row , we return false
if (puzzle[row][x] == number)
9
return false;
//Check if we find the same number in the similar column, we return false
if (puzzle[x][col] == number)
return false;
we return false*/
if (puzzle[i + sRow][j +
sCol] == number)
return false;
return true;
return true;
if (col == N) {
row++;
col = 0;
/If the current position contains value >0, then iterate for next column/
if (puzzle[row][col] > 0)
puzzle[row][col] = number;
11
return true;
puzzle[row][col] = 0;
return false;
// Driver Code
int main()
int puzzle[N][N] = { { 0, 7, 0, 0, 0, 0, 0, 0, 9 },
{ 5, 1, 0, 4, 2, 0, 6, 0, 0 },
{ 0, 8, 0, 3, 0, 0, 7, 0, 0 },
{ 0, 0, 8, 0, 0, 1, 3, 7, 0 },
{ 0, 2, 3, 0, 8, 0, 0, 4, 0 },
{ 4, 0, 0, 9, 0, 0, 1, 0, 0 },
{ 9, 6, 2, 8, 0, 0, 0, 3, 0 },
12
{ 0, 0, 0, 0, 1, 0, 4, 0, 0 },
{ 7, 0, 0, 2, 0, 3, 0, 9, 6 } };
print(puzzle);
if (solution(puzzle, 0, 0))
print(puzzle);
else
return 0;
Before Solving
070000009
510420600
080300700
008001370
023080040
400900100
13
962800030
000010400
700203096
After Solving
374168259
519427683
286395714
698541372
123786945
457932168
962874531
835619427
741253896
1. *Data Structures*: You'll need a data structure to represent the Sudoku grid. A 2D array or a
vector of vectors is a common choice.
14
2. *Input/Output*: Implement functions to input a Sudoku puzzle from the user or from a file,
and to output the solved puzzle.
3. *Validation*: Write functions to check whether a number can be placed in a certain position
according to the Sudoku rules (no duplicate numbers in the same row, column, or 3x3 subgrid).
4. *Backtracking Algorithm*: Use a backtracking algorithm to solve the Sudoku puzzle. The
algorithm tries different numbers in empty cells, and if it reaches a dead end, it backtracks and
tries a different number.
5. *Main Loop*: Your main function should orchestrate the process, taking input, solving the
puzzle, and providing output.
15
MODULES:
Here's a simple example of how you can start implementing a Sudoku solver in C++
:#include <iostream>
#include <vector>
const int N = 9;
return false
return false;
return true;
if (board[row][col] == 0) {
isEmpty = false;
break;
if (!isEmpty) break;
board[row][col] = num;
if (solveSudoku(board))
return true;
board[row][col] = 0;
return false;
int main() {
vector<vector<int>> board = {
{5, 3, 0, 0, 7, 0, 0, 0, 0},
{6, 0, 0, 1, 9, 5, 0, 0, 0},
{0, 9, 8, 0, 0, 0, 0, 6, 0},
{8, 0, 0, 0, 6, 0, 0, 0, 3},
{4, 0, 0, 8, 0, 3, 0, 0, 1},
{7, 0, 0, 0, 2, 0, 0, 0, 6},
{0, 6, 0, 0, 0, 0, 2, 8, 0},
{0, 0, 0, 4, 1, 9, 0, 0, 5},
{0, 0, 0, 0, 8, 0, 0, 7, 9}
};
if (solveSudoku(board))
printBoard(board);
else
18
return 0;
}
19
CONCLUSION:
REFERENCE:
https://codereview.stackexchange.com/questions/37430/sudoku-solver-in-c
https://www.geeksforgeeks.org/sudoku-backtracking-7/amp/
https://copyassignment.com/sudoku-game-in-cpp/
https://www.studytonight.com/post/solve-sudoku-puzzle-in-c-java
https://stackoverflow.com/questions/19022739/sudoku-solver-in-c