Code Breaker Game Gaming Project in C++
|
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 and maxDigit. This sequence represents the secret code that the player must guess. Player Guess: The function getPlayerGuess prompts the player to input a guess. The guess is a sequence of digits of …
