Ad

I have edited some parts in the code to make it work on other compilers such as C++ Shell

Header Inclusion

Added #include <random> to include support for modern random number generation.

generateRandomNumber Function

Original Code:

random_shuffle(digits.begin(), digits.end());

Used the deprecated random_shuffle function.

Modified Code:

random_device rd;  // Obtain a random number from hardware
mt19937 g(rd());  // Seed the generator
shuffle(digits.begin(), digits.end(), g);

Replaced random_shuffle with shuffle using the modern mt19937 random number generator and random_device for seeding.

Main Function:(In case of other compliers only)

Original Code:

int _main() {

Used _main as the function name.

Modified Code:

int main() {

Changed _main to main to adhere to standard naming conventions for the entry point of the program.

Random Seed Initialization:

Original Code:

srand(time(0));

Used srand with time(0) to seed the random number generator.

Modified Code:

srand(static_cast<unsigned>(time(0)));

Added static_cast<unsigned> to explicitly cast the time result to unsigned.

Loop Condition:

Original Code:

while (cin) {

Used cin as the condition for the loop.

Modified Code:

while (true) {

Replaced cin with true for an infinite loop and explicitly handle exit conditions within the loop.

Digit Vector Type:

Original Code:

vector<int> digits = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};

Used vector<int> for digits.

Modified Code:

vector<char> digits = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};

Changed vector<int> to vector<char> to match the type of elements used in the digit vector.

Code
Diff
  • #include <iostream>
    #include <cstdlib>
    #include <ctime>
    #include <vector>
    #include <algorithm>
    #include <cmath>
    #include <random>
    
    using namespace std;
    
    string generateRandomNumber() {
        vector<char> digits = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
    
        // Initialize random number generator
        random_device rd;  // Obtain a random number from hardware
        mt19937 g(rd());  // Seed the generator
    
        // Shuffle digits
        shuffle(digits.begin(), digits.end(), g);
    
        // Return the first 4 digits as a string
        return string(digits.begin(), digits.begin() + 4);
    }
    
    bool hasDuplicateDigits(const string &number) {
        vector<bool> seen(10, false);
        for (const auto c : number) {
            if (seen[c - '0']) return true;
            seen[c - '0'] = true;
        }
        return false;
    }
    
    bool isNumeric(const string &s) {
        return all_of(s.begin(), s.end(), [](char c){ return isdigit(c); });
    }
    
    string getUserGuess() {
        string guess;
        cout << "Enter your guess (a 4-digit number with non-repeating digits): ";
        cin >> guess;
        return guess;
    }
    
    pair<int, int> checkGuess(const string &randomNumber, const string &userGuess) {
        int correctNumbers = 0;
        int correctPosition = 0;
    
        for (int i = 0; i < 4; ++i) {
            if (randomNumber[i] == userGuess[i]) {
                correctNumbers++;
                correctPosition++;
            } else if (count(randomNumber.begin(), randomNumber.end(), userGuess[i])) {
                correctNumbers++;
            }
        }
    
        return make_pair(correctNumbers, correctPosition);
    }
    
    int _main() {
        // Seed rand since random_shuffle _probably_ uses it.
        srand(static_cast<unsigned>(time(0)));
    
        string randomNumber = generateRandomNumber();
    
        int attempts = 0;
    
        while (true) {
            string userGuess = getUserGuess();
            if (userGuess.length() != 4 || hasDuplicateDigits(userGuess) || !isNumeric(userGuess)) {
                cout << "Invalid input. Please enter a 4-digit number with non-repeating digits." << endl;
                continue;
            }
    
            auto [corNum, corPos] = checkGuess(randomNumber, userGuess);
            cout << "Correct numbers: " << corNum << " Correct position: " << corPos << endl;
    
            attempts++;
    
            if (corPos == 4) {
                cout << "Congratulations! You guessed the number " << randomNumber << " correctly in " << attempts << " attempts!" << endl;
                break;
            }
        }
    
        return 0;
    }
    
    • #include <iostream>
    • #include <cstdlib>
    • #include <ctime>
    • #include <vector>
    • #include <algorithm>
    • #include <cmath>
    • #include <random>
    • using namespace std;
    • string generateRandomNumber() {
    • vector<int> digits = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
    • random_shuffle(digits.begin(), digits.end());
    • vector<char> digits = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
    • // Initialize random number generator
    • random_device rd; // Obtain a random number from hardware
    • mt19937 g(rd()); // Seed the generator
    • // Shuffle digits
    • shuffle(digits.begin(), digits.end(), g);
    • // Return the first 4 digits as a string
    • return string(digits.begin(), digits.begin() + 4);
    • }
    • bool hasDuplicateDigits(const string &number) {
    • // limitation: invalid index if number isn't really numeric.
    • vector<bool> seen(10, false);
    • for (const auto c : number) {
    • if (seen[c - '0']) return true;
    • seen[c - '0'] = true;
    • if (seen[c - '0']) return true;
    • seen[c - '0'] = true;
    • }
    • return false;
    • }
    • bool isNumeric(const string &s) {
    • return all_of(s.begin(), s.end(), [](char c){return isdigit(c);});
    • return all_of(s.begin(), s.end(), [](char c){ return isdigit(c); });
    • }
    • string getUserGuess() {
    • string guess;
    • cout << "Enter your guess (a 4-digit number with non-repeating digits): ";
    • cin >> guess;
    • return guess;
    • }
    • pair<int, int> checkGuess(const string &randomNumber, const string &userGuess) {
    • int correctNumbers = 0;
    • int correctPosition = 0;
    • for (int i = 0; i < 4; ++i) {
    • if (randomNumber[i] == userGuess[i]) {
    • correctNumbers++;
    • correctPosition++;
    • } else if (count(randomNumber.begin(), randomNumber.end(), userGuess[i])) {
    • correctNumbers++;
    • }
    • }
    • return make_pair(correctNumbers, correctPosition);
    • }
    • int _main() {
    • // seed rand since random_shuffle _probably_ uses it.
    • srand(time(0));
    • // Seed rand since random_shuffle _probably_ uses it.
    • srand(static_cast<unsigned>(time(0)));
    • string randomNumber = generateRandomNumber();
    • int attempts = 0;
    • while (cin) {
    • while (true) {
    • string userGuess = getUserGuess();
    • if (userGuess.length() != 4 || hasDuplicateDigits(userGuess) || !isNumeric(userGuess)) {
    • cout << "Invalid input. Please enter a 4-digit number with non-repeating digits." << endl;
    • continue;
    • }
    • auto [corNum, corPos] = checkGuess(randomNumber, userGuess);
    • cout << "Correct numbers: " << corNum << " Correct position: " << corPos << endl;
    • attempts++;
    • if (corPos == 4) {
    • cout << "Congratulations! You guessed the number " << randomNumber << " correctly in " << attempts << " attempts!" << endl;
    • break;
    • }
    • }
    • return 0;
    • }

Here you will be making a game where you will be guessing a 4 digits game.

Where the number you will guess should have 4 characters and at every guess show how many number you got correct and their position telling you.Where you can't repeat numbers.

Example:

  "correct numbers: 2 correct position: 1" 
  "correct numbers: 3 correct position: 0"

Notice that the correct position of the number can't be greater than correct number vale you got and then the code should continue until it found the correct number by first by applying random 4 digits.

(At the moment I don't know how the codewars console works to interact with the user or have an AI that simulates it)

! To check your code works apply it on other compliers as VScode.

(AND PLEASE I WILL APPRECIATE ANY FORK WHO COULD HELP ME BIULD THIS KATA) 😊

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <vector>
#include <algorithm>
#include <cmath>

using namespace std;

int generateRandomNumber() {
    vector<int> digits = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
    random_shuffle(digits.begin(), digits.end());
    int number = 0;
    for (int i = 0; i < 4; ++i) {
        number = number * 10 + digits[i];
    }
    return number;
}

bool hasDuplicateDigits(int number) {
    vector<bool> seen(10, false);
    while (number > 0) {
        int digit = number % 10;
        if (seen[digit]) {
            return true;
        }
        seen[digit] = true;
        number /= 10;
    }
    return false;
}

int getUserGuess() {
    int guess;
    cout << "Enter your guess (a 4-digit number with non-repeating digits): ";
    cin >> guess;
    return guess;
}

pair<int, int> checkGuess(int randomNumber, int userGuess) {
    int correctNumbers = 0;
    int correctPosition = 0;

    string randomStr = to_string(randomNumber);
    string guessStr = to_string(userGuess);

    for (int i = 0; i < 4; ++i) {
        if (randomStr[i] == guessStr[i]) {
            correctNumbers++;
            correctPosition++;
        } else if (count(randomStr.begin(), randomStr.end(), guessStr[i]) > 0) {
            correctNumbers++;
        }
    }

    return make_pair(correctNumbers, correctPosition);
}

int main() {
    srand(time(0));

    int randomNumber = generateRandomNumber();
    while (hasDuplicateDigits(randomNumber)) {
        randomNumber = generateRandomNumber();
    }

    int attempts = 0;

    while (true) {
        int userGuess = getUserGuess();
        if (to_string(userGuess).length() != 4 || hasDuplicateDigits(userGuess)) {
            cout << "Invalid input. Please enter a 4-digit number with non-repeating digits." << endl;
            continue;
        }

        pair<int, int> result = checkGuess(randomNumber, userGuess);
        cout << "Correct numbers: " << result.first << " Correct position: " << result.second << endl;

        attempts++;

        if (result.second == 4) {
            cout << "Congratulations! You guessed the number " << randomNumber << " correctly in " << attempts << " attempts!" << endl;
            break;
        }
    }

    return 0;
}