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 |
#include <iostream> #include <vector> #include <cstdlib> #include <ctime> using namespace std; // Struct to represent a color struct Color { string name; int id; }; // Function to generate a random color Color generateRandomColor(const vector<Color>& colors) { int index = rand() % colors.size(); return colors[index]; } // Function to get the player's guess int getPlayerGuess() { int guess; cout << "Enter the color ID you think it is: "; cin >> guess; return guess; } // Function to display the available colors void displayColors(const vector<Color>& colors) { cout << "Available colors:" << endl; for (const auto& color : colors) { cout << color.id << ": " << color.name << endl; } } // Main function int main() { srand(time(0)); // Define a set of colors vector<Color> colors = { {"Red", 1}, {"Green", 2}, {"Blue", 3}, {"Yellow", 4}, {"Orange", 5}, {"Purple", 6} }; cout << "Welcome to the Color Detection Game!" << endl; displayColors(colors); Color correctColor = generateRandomColor(colors); int attempts = 0; bool isCorrect = false; while (!isCorrect && attempts < 3) { // Limit the number of attempts to 3 int guess = getPlayerGuess(); attempts++; if (guess == correctColor.id) { isCorrect = true; cout << "Congratulations! You guessed the color correctly. It was " << correctColor.name << "!" << endl; } else { cout << "Incorrect guess. Try again!" << endl; } } if (!isCorrect) { cout << "Sorry, you're out of attempts. The correct color was " << correctColor.name << "." << endl; } return 0; } |
Explanation:
- Color Representation:
- The
Color
struct represents a color with aname
(e.g., “Red”) and anid
(a unique integer corresponding to that color).
- The
- Random Color Generation:
- The
generateRandomColor
function selects a random color from a predefined list of colors. This color is the one that the player needs to guess.
- The
- Player Guess:
- The
getPlayerGuess
function prompts the player to enter their guess by choosing the ID corresponding to the color they think was generated.
- The
- Color Display:
- The
displayColors
function prints out the available colors and their corresponding IDs to the console. This helps the player know which colors they can guess.
- The
- Game Loop:
- The game allows the player up to 3 attempts to guess the correct color. After each guess, the program checks if the player’s guess matches the randomly selected color:
- If correct, the game congratulates the player.
- If incorrect and attempts remain, the game prompts the player to try again.
- If all attempts are used without a correct guess, the game reveals the correct color.
- The game allows the player up to 3 attempts to guess the correct color. After each guess, the program checks if the player’s guess matches the randomly selected color:
Possible Enhancements:
- Difficulty Levels: Add more colors and increase the number of guesses for higher difficulty levels.
- Visual Representation: If developing in an environment that supports graphics, implement actual color display rather than using text names.
- Hint System: Introduce a hint system that narrows down the options after incorrect guesses.
- Score Tracking: Track the number of correct guesses over multiple rounds and provide a final score.