Sudoku is an interesting and popular puzzle game with some simple rules. In this article, we will explore how to build a simple Sudoku Game in C++. This is a simple console based game so whether you are a beginner or an experienced C++ programmer, you will be able to follow this article easily.
Features
- Player can play the game to solve the puzzle on the screen.
- If not able to solve then the program will solve it for him.
Code for Sudoku Game in C++
We will use Code::Blocks IDE to develop this program. Open the Code::Blocks IDE and create a new project with the name sudoku. Now in the main.cpp file paste the following code and run it.
#include <iostream>
#include <cstdlib>
#include <cstring>
using namespace std;
const int N = 9;
bool isSafe(int board[N][N], int row, int col, int num) {
// Check if 'num' is already in the same row
for (int i = 0; i < N; i++)
if (board[row][i] == num)
return false;
// Check if 'num' is already in the same column
for (int i = 0; i < N; i++)
if (board[i][col] == num)
return false;
// Check if 'num' is already in the same 3x3 box
int boxRowStart = row - row % 3;
int boxColStart = col - col % 3;
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
if (board[i + boxRowStart][j + boxColStart] == num)
return false;
return true;
}
void printBoard(int grid[N][N]) {
system("cls");
cout << "\t\t\t<================================================================================>" << endl;
cout << "\t\t\t| WELCOME TO SUDOKU Game! |" << endl;
cout << "\t\t\t| Fill in the missing numbers(represented by 0) to solve the puzzle. |" << endl;
cout << "\t\t\t<================================================================================>" << endl;
for (int row = 0; row < N; row++){
for (int col = 0; col < N; col++){
if(col == 3 || col == 6)
cout << " | ";
cout << grid[row][col] <<" ";
}
if(row == 2 || row == 5){
cout << endl;
for(int i = 0; i<N; i++)
cout << "---";
}
cout << endl;
}
}
bool solveSudoku(int board[N][N], int row, int col) {
// If all cells are filled, the puzzle is solved
if (row == N - 1 && col == N)
return true;
// Move to the next row if the current column is N
if (col == N) {
row++;
col = 0;
}
// Skip the cells that already have a value
if (board[row][col] != 0)
return solveSudoku(board, row, col + 1);
// Try filling the current cell with a number from 1 to 9
for (int num = 1; num <= 9; num++) {
if (isSafe(board, row, col, num)) {
board[row][col] = num;
if (solveSudoku(board, row, col + 1))
return true;
board[row][col] = 0;
}
}
return false;
}
bool isSolvedCompletely(int grid[N][N]){
for (int row = 0; row < N; row++)
for (int col = 0; col < N; col++)
if (grid[row][col] == 0)
return false;
return true;
}
void playGame(int board[N][N]){
int ch;
int row, col, num;
while(true){
printBoard(board);
cout << endl << endl;
cout << "Unable to solve? Enter -1 as row, col and num to view the solved sudoku."<<endl;
cout << "Enter row: ";
cin >> row;
cout << "Enter column: ";
cin >> col;
cout << "Enter number: ";
cin >> num;
if(row == -1 || col == -1 || num == -1){
solveSudoku(board, 0, 0);
printBoard(board);
cout << endl;
cout << "Better luck next time!!!" << endl;
return;
}
if (isSolvedCompletely(board))
break;
row--;
col--;
if (!isSafe(board, row, col, num)) {
cout << "Invalid move. Try again." << endl;
continue;
}
board[row][col] = num;
}
// Check if the user has solved it correctly or not
bool solved = true;
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (board[i][j] == 0) {
solved = false;
break;
}
}
}
if (solved) {
cout << "Congratulations! You have solved the puzzle." << endl;
printBoard(board);
}
else {
cout << "Puzzle not solved. Better luck next time." << endl;
}
}
int main() {
system("title Sudoku Game @copyassignment");
system("color B0");
int board[N][N] = {
{3, 0, 6, 5, 0, 8, 4, 0, 0},
{5, 2, 0, 0, 0, 0, 0, 0, 0},
{0, 8, 7, 0, 0, 0, 0, 3, 1},
{0, 0, 3, 0, 1, 0, 0, 8, 0},
{9, 0, 0, 8, 6, 3, 0, 0, 5},
{0, 5, 0, 0, 9, 0, 6, 0, 0},
{1, 3, 0, 0, 0, 0, 2, 5, 0},
{0, 0, 0, 0, 0, 0, 0, 7, 4},
{0, 0, 5, 2, 0, 6, 3, 0, 0}
};
cout << "\t\t\t<================================================================================>" << endl;
cout << "\t\t\t| WELCOME TO SUDOKU Game! |" << endl;
cout << "\t\t\t| Fill in the missing numbers(represented by 0) to solve the puzzle. |" << endl;
cout << "\t\t\t<================================================================================>" << endl;
while (true) {
int choice;
cout << endl << endl;
cout << "\t\t[1] Solve the Sudoku" << endl;
cout << "\t\t[2] Unable to solve? View the solved Sudoku" << endl;
cout << "\t\t[3] Exit" << endl;
cout << "\t\tEnter your choice: ";
cin >> choice;
switch (choice) {
case 1:
playGame(board);
break;
case 2:
if (solveSudoku(board, 0, 0))
{
cout << "Completely Solved Sudoku is: "<< endl;
cout << endl << endl;
for (int row = 0; row < N; row++){
for (int col = 0; col < N; col++){
if(col == 3 || col == 6)
cout << " | ";
cout << board[row][col] <<" ";
}
if(row == 2 || row == 5){
cout << endl;
for(int i = 0; i<N; i++)
cout << "---";
}
cout << endl;
}
cout << endl;
cout << "Better luck next time!!!" << endl;
}
else
cout << "No solution found" << endl;
break;
case 3:
exit(0);
default:
cout << "Invalid choice" << endl;
}
return 0;
}
}
Output for Sudoku Game in C++
Image Output:

Video Output:
Conclusion
At end, we have a fully functional Sudoku Game which you can share with your friends and continue to improve this game. Through this article on Sudoku Game in C++, we have learnt some important concepts of programming like handling user interactions, updating the game board, using backtracking algorithm to solve the Sudoku etc.
Thank you for visiting our website.
Also Read:
- Aam Aadmi vs Corrupt System: How ChatGPT Helped One Guy Expose Govt Fraud, The Story: “Ravi and The Missing Light Pole”
- ChatGPT Asked a person to commit suicide to solve the problem
- Viral Moment: China’s AgiBot X2 Makes History With World’s First Webster Backflip
- Terminator Rising: Albania Hands Power to AI, Echoing a Nightmare of Human Extinction
- What Is Albania’s World-First AI-Generated Minister and How Does It Work?
- Does ChatGPT believe in God? ChatGPT’s Personal Opinion
- ChatGPT vs Human: The Breath-Holding Chat That Ends in “System Failure”
- What Is Vibe Coding? The Future of No-Code Programming and Its Impact on Software Developers
- Struggling to Generate Ghibli-Style AI Images? Here’s the Real Working Tool That Others Won’t Tell You About!
- ChatGPT vs DeepSeek: Who is the winner?
- People are becoming AI Engineer with this free course in 2025: Here is how to join this…
- Apply to Google’s Student Training in Engineering Program (STEP) Intern, 2025
- Self-Driving Car Saves Falling Pedestrian, Showcases Promise of Autonomous Technology
- Instant Karma: Employer Fires Tech Team with AI, Faces Backlash on LinkedIn While Seeking New Developers
- LinkedIn’s COO Reveals the AI Interview Question That Could Land You the Job in 2025
- Elon Musk’s xAI Raises $6 Billion, Valued at $45 Billion
- Google Unveils Veo 2 and Imagen 3: A New Era of AI-Generated Content
- Imagination to Reality, Unlocking the Future: Genesis Physics Engine for 4D Simulation
- Simple Code to compare Speed of Python, Java, and C++?
- Falling Stars Animation on Python.Hub October 2024
- Most Underrated Database Trick | Life-Saving SQL Command
- Python List Methods
- Top 5 Free HTML Resume Templates in 2024 | With Source Code
- How to See Connected Wi-Fi Passwords in Windows?
- 2023 Merry Christmas using Python Turtle
- 23 AI Tools You Won’t Believe are Free
- Write for CopyAssignment.com | Unlock Your Potential: Join CopyAssignment.com as a Blog Writer! 🚀
- Python 3.12.1 is Now Available
- Best Deepfake Apps and Websites You Can Try for Fun
- Amazon launched free Prompt Engineering course: Enroll Now



