Posted in

c code to make the table of the sudoku game

# Creating a Sudoku Game Table in C++

## Introduction to Sudoku Game

Sudoku is a popular puzzle game that involves a 9×9 grid. The objective is to fill the grid with digits so that each column, each row, and each of the nine 3×3 subgrids that compose the grid (also called “boxes”, “blocks”, or “regions”) contains all of the digits from 1 to 9. The game begins with some of the squares filled in. The rules are simple but the complexity increases with each step, making Sudoku a challenging and rewarding pastime.

## Code to Generate Sudoku Game Table in C++

To create a Sudoku game table in C++, you can follow the steps below. The code provided below will generate a simple Sudoku game table with placeholders for the player to fill in.

“`cpp
#include
#include

void printBoard(const std::vector>& board) {
for (const auto& row : board) {
for (const auto& cell : row) {
std::cout << cell << " "; } std::cout << std::endl; } } bool isValid(const std::vector>& board, int row, int col, int num) {
// Check if ‘num’ is not already placed in the row
for (int i = 0; i < 9; i++) { if (board[row][i] == num) { return false; } } // Check if 'num' is not already placed in the column for (int i = 0; i < 9; i++) { if (board[i][col] == num) { return false; } } // Check if 'num' is not already placed in the 3x3 subgrid int startRow = row - row % 3; int startCol = col - col % 3; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (board[i + startRow][j + startCol] == num) { return false; } } } return true; } void solveSudoku(std::vector>& board, int row = 0, int col = 0) {
if (row == 9) {
return;
}

if (col == 9) {
row++;
col = 0;
}

if (board[row][col] != 0) {
solveSudoku(board, row, col + 1);
} else {
for (int num = 1; num <= 9; num++) { if (isValid(board, row, col, num)) { board[row][col] = num; solveSudoku(board, row, col + 1); if (row == 9 && col == 8) { // If the entire board is filled printBoard(board); } board[row][col] = 0; // Reset the value and try the next one } } } } int main() { std::vector> 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}
};

solveSudoku(board);

return 0;
}
“`

## Frequently Asked Questions (FAQ)

**Q: How do I run the Sudoku code in C++?**

A: To run the Sudoku code in C++, you will need a C++ compiler like g++. Save the code to a file with a `.cpp` extension, for example, `sudoku.cpp`. Then, open a terminal or command prompt, navigate to the directory where the file is saved, and compile it using the following command:

“`sh
g++ -o sudoku sudoku.cpp
“`

After compiling, you can run the program by typing:

“`sh
./sudoku
“`

**Q: Can I change the difficulty of the Sudoku puzzle?**

A: The provided code generates a Sudoku puzzle with a fixed starting configuration. To change the difficulty, you can modify the initial board state with more or fewer filled cells before calling the `solveSudoku` function.

**Q: How can I check if the solution is correct?**

A: The code provided prints the solution if the entire board is filled. You can verify the correctness of the solution by checking that each row, column, and 3×3 subgrid contains all digits from 1 to 9 without repetition.

**Q: Can I create my own Sudoku puzzles?**

A: Yes, you can create your own Sudoku puzzles by modifying the initial state of the `board` vector in the `main` function. Simply replace the zeros with the correct numbers to represent the filled cells of your puzzle.