1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
#include <iostream> #include <vector> #include <cstdlib> #include <ctime> using namespace std; // Function to generate a random code vector<int> generateCode(int length, int maxDigit) { vector<int> code(length); for (int i = 0; i < length; ++i) { code[i] = rand() % maxDigit + 1; } return code; } // Function to get a guess from the player vector<int> getPlayerGuess(int length) { vector<int> guess(length); cout << "Enter your guess (separate digits with spaces): "; for (int i = 0; i < length; ++i) { cin >> guess[i]; } return guess; } // Function to compare the guess with the code and give feedback pair<int, int> compareGuess(const vector<int>& code, const vector<int>& guess) { int correctPosition = 0; int correctNumber = 0; vector<bool> codeChecked(code.size(), false); vector<bool> guessChecked(guess.size(), false); // Check for correct numbers in the correct position for (int i = 0; i < code.size(); ++i) { if (code[i] == guess[i]) { correctPosition++; codeChecked[i] = true; guessChecked[i] = true; } } // Check for correct numbers in the wrong position for (int i = 0; i < code.size(); ++i) { if (!codeChecked[i]) { for (int j = 0; j < guess.size(); ++j) { if (!guessChecked[j] && code[i] == guess[j]) { correctNumber++; guessChecked[j] = true; break; } } } } return {correctPosition, correctNumber}; } // Main function int main() { srand(time(0)); const int codeLength = 4; const int maxDigit = 6; const int maxAttempts = 10; vector<int> code = generateCode(codeLength, maxDigit); bool isGuessed = false; cout << "Welcome to the Code Breaker Game!" << endl; cout << "Guess the " << codeLength << "-digit code. Each digit is between 1 and " << maxDigit << "." << endl; for (int attempt = 1; attempt <= maxAttempts; ++attempt) { cout << "\nAttempt " << attempt << " of " << maxAttempts << ":" << endl; vector<int> guess = getPlayerGuess(codeLength); pair<int, int> feedback = compareGuess(code, guess); cout << "Correct numbers in the correct position: " << feedback.first << endl; cout << "Correct numbers in the wrong position: " << feedback.second << endl; if (feedback.first == codeLength) { isGuessed = true; break; } } if (isGuessed) { cout << "Congratulations! You guessed the code!" << endl; } else { cout << "Sorry, you've run out of attempts. The code was: "; for (int digit : code) { cout << digit << " "; } cout << endl; } return 0; } |
Explanation:
- Code Generation:
- The function
generateCode
creates a random sequence of digits of a specified length. Each digit ranges between 1 andmaxDigit
- The function
- Player Guess:
- The function
getPlayerGuess
prompts the player to input a guess. The guess is a sequence of digits of the same length as the secret code.
- The function
- Comparison Logic:
- The function
compareGuess
compares the player’s guess with the secret code:- It first checks how many digits are correct and in the correct position (
correctPosition
). - It then checks how many correct digits are in the wrong position (
correctNumber
).
- It first checks how many digits are correct and in the correct position (
- The function returns a pair of integers representing these two values.
- The function
- Game Loop:
- The main function controls the game loop, giving the player a fixed number of attempts (
maxAttempts
) to guess the secret code. - After each guess, the program provides feedback on the number of correct digits in the correct and wrong positions.
- If the player guesses the code correctly within the allowed attempts, they win. Otherwise, the correct code is revealed at the end.
- The main function controls the game loop, giving the player a fixed number of attempts (
- Randomization:
- The
srand(time(0))
function call ensures that the random code generated is different each time the game is played, by seeding the random number generator with the current time.
- The
Possible Enhancements:
- Difficulty Levels: Introduce different difficulty levels with varying code lengths and digit ranges.
- Improved Feedback: Enhance the feedback system to provide more detailed hints.
- Multiple Rounds: Implement a system to allow for multiple rounds, with the player’s score tracked across games.
- Graphical Interface: Create a graphical interface for better user interaction and game experience.