Sudoku game in cpp

 Creating a Sudoku game in C++ is a fun and educational project. Sudoku is a logic-based number-placement puzzle, and building a Sudoku game involves generating valid Sudoku grids, allowing user input, and checking the solution. Below, I'll provide a basic outline of how to create a simple command-line Sudoku game in C++:


Source code :

#include <iostream>

#include <vector>

#include <cstdlib>

#include <ctime>


const int SIZE = 9;


void printBoard(const std::vector<std::vector<int>>& board) {

    for (int i = 0; i < SIZE; ++i) {

        for (int j = 0; j < SIZE; ++j) {

            std::cout << board[i][j] << " ";

            if ((j + 1) % 3 == 0) {

                std::cout << "| ";

            }

        }

        std::cout << std::endl;

        if ((i + 1) % 3 == 0 && i < SIZE - 1) {

            std::cout << "---------------------\n";

        }

    }

}


bool isValidMove(const std::vector<std::vector<int>>& board, int row, int col, int num) {

    // Check row and column

    for (int i = 0; i < SIZE; ++i) {

        if (board[row][i] == num || board[i][col] == num) {

            return false;

        }

    }


    // Check 3x3 box

    int boxRow = row - (row % 3);

    int boxCol = col - (col % 3);

    for (int i = boxRow; i < boxRow + 3; ++i) {

        for (int j = boxCol; j < boxCol + 3; ++j) {

            if (board[i][j] == num) {

                return false;

            }

        }

    }


    return true;

}


bool solveSudoku(std::vector<std::vector<int>>& board) {

    for (int row = 0; row < SIZE; ++row) {

        for (int col = 0; col < SIZE; ++col) {

            if (board[row][col] == 0) {

                for (int num = 1; num <= SIZE; ++num) {

                    if (isValidMove(board, row, col, num)) {

                        board[row][col] = num;

                        if (solveSudoku(board)) {

                            return true;

                        }

                        board[row][col] = 0;

                    }

                }

                return false;

            }

        }

    }

    return true;

}


int main() {

    std::vector<std::vector<int>> board(SIZE, std::vector<int>(SIZE, 0));


    // Seed the random number generator for puzzle generation

    std::srand(std::time(0));


    // Generate a Sudoku puzzle

    solveSudoku(board);


    // Remove a few numbers to make it a puzzle

    int numToRemove = 45;  // Adjust difficulty by changing the number of removed cells

    while (numRemoved > 0) {

        int row = std::rand() % SIZE;

        int col = std::rand() % SIZE;

        if (board[row][col] != 0) {

            board[row][col] = 0;

            numRemoved--;

        }

    }


    // Print the initial puzzle

    std::cout << "Sudoku Puzzle:" << std::endl;

    printBoard(board);


    // Allow user to play the game

    // Implement the game loop here


    return 0;

}

```


This code provides a basic structure for a Sudoku game. You can implement the game loop to allow users to interact with the Sudoku puzzle, input their guesses, and check the solution. Adjust the difficulty by changing the number of removed cells. You can also implement features like checking for the solution and providing hints to the player.


This is a simplified version, and you can add more features, error handling, and user-friendly interfaces if desired.

Next Post Previous Post
No Comment
Add Comment
comment url