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:
- You Can Now Run AI Fully Offline on Your Phone — Google’s Gemma 4 Just Changed Everything
- I Built a 24×7 AI Blogging System for WordPress Using Python (Free) — Full Code Inside
- This Reddit User “Hacked” AI With Simple Tricks… And The Results Are Insane
- One “rm -rf” Command Almost Wiped Out $100 Million Worth of Toy Story 2
- How to Make Money with ChatGPT in 2026: A Real Guide That Still Works
- PicoClaw vs OpenClaw: The Tiny AI That Could Replace Powerful AI Agents
- Oracle Layoffs 2026: People Woke Up to an Email… and Lost Their Jobs Instantly
- X’s New Video Update Is Breaking a Basic Feature — And Users Are Not Happy
- The Most Shocking Military Tech Yet: Robot Soldiers That Could Change Warfare Forever
- Sora Shutdown: The Reality Check That Shook AI Video — And What Comes Next
- Aya Expanse supports multiple languages for diverse global applications
- Alibaba releases Page Agent on GitHub for public access
- Google Sheets Gemini reaches new levels of performance and accuracy
- Artificial intelligence boosts cardiac care in rural Australian communities
- NVIDIA GTC 2026 Offers Insights into Future Artificial Intelligence Developments
- Google DeepMind Updates Satellite Embedding Dataset with 2025 Data
- Enhancing hierarchical instruction in advanced large language models
- Meta supports community development near its data centers through grants
- Exploring the world of underwater robotics through coding techniques
- ABB Robotics partners with NVIDIA for large scale physical AI solutions
- Why All AI Models Are Slowly Becoming the Same Model
- 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!



