大学C++课程作业:Rock Paper Scissors程序实现技术问询
Hey there! Let's walk through building this Rock Paper Scissors program exactly to your C++ assignment specs. I'll break it down with code and explanations so you can follow along easily.
Step 1: Define the Player Struct
First up, we need to create the required struct with all the specified member variables. Here's how to do it properly, with comments explaining each part:
#include <iostream> #include <cstring> #include <cstdlib> #include <ctime> #include <cctype> // Exact struct as required by your assignment struct Player { char name[41]; // Holds up to 40 characters + null terminator int points; // Tracks player's score bool master; // Flags if player has achieved "master" status char weapon[8]; // Stores "rock", "paper", or "scissors" (all fit with room for null terminator) };
Step 2: Implement the checkMastery Function
The assignment specifies a void function that takes a Player pointer. I'll assume the logic here is to set master to true when the player reaches a certain point threshold (common in such assignments—adjust the threshold if your prof gave a specific number):
// Checks if player qualifies as a "master" based on points void checkMastery(Player* player) { // Example rule: Master status unlocks at 10 points // Replace 10 with your assignment's required threshold player->master = (player->points >= 10); }
Step 3: Full Game Implementation
To make this a working program, we'll add helper functions for input validation, computer choice generation, and winner determination, plus a main function to run the game:
Helper Function: Get Valid Player Weapon
Ensures the player only enters one of the three allowed choices:
void getPlayerWeapon(Player* player) { std::cout << "Enter your choice (rock, paper, scissors): "; std::cin >> player->weapon; // Validate input to match required weapons while (strcmp(player->weapon, "rock") != 0 && strcmp(player->weapon, "paper") != 0 && strcmp(player->weapon, "scissors") != 0) { std::cout << "Invalid choice! Please enter rock, paper, or scissors: "; std::cin >> player->weapon; } }
Helper Function: Generate Computer's Random Weapon
Uses rand() to pick a random choice, with seed initialization to ensure different results each run:
void getComputerWeapon(char computerWeapon[]) { // Initialize random seed only once static bool seedSet = false; if (!seedSet) { srand(time(0)); seedSet = true; } int randChoice = rand() % 3; switch(randChoice) { case 0: strcpy(computerWeapon, "rock"); break; case 1: strcpy(computerWeapon, "paper"); break; case 2: strcpy(computerWeapon, "scissors"); break; } }
Helper Function: Determine Winner & Update Scores
Compares the player's and computer's choices, updates points, and calls checkMastery to update the player's status:
void determineWinner(Player* player, const char* computerWeapon) { std::cout << "\nYou chose: " << player->weapon << "\nComputer chose: " << computerWeapon << "\n"; // Check for tie if (strcmp(player->weapon, computerWeapon) == 0) { std::cout << "It's a tie!\n"; } // Check player win conditions else if ((strcmp(player->weapon, "rock") == 0 && strcmp(computerWeapon, "scissors") == 0) || (strcmp(player->weapon, "paper") == 0 && strcmp(computerWeapon, "rock") == 0) || (strcmp(player->weapon, "scissors") == 0 && strcmp(computerWeapon, "paper") == 0)) { std::cout << "You win this round!\n"; player->points++; } // Computer wins else { std::cout << "Computer wins this round!\n"; } // Update master status after points change checkMastery(player); }
Main Function: Run the Game
Ties everything together, initializes the player, and handles the game loop:
int main() { Player humanPlayer; char computerWeapon[8]; char playAgain; // Initialize player data std::cout << "Enter your name: "; std::cin.getline(humanPlayer.name, 41); // Clear any leftover newline from input buffer if (!std::cin) { std::cin.clear(); std::cin.ignore(1000, '\n'); } humanPlayer.points = 0; humanPlayer.master = false; std::cout << "\nWelcome to Rock Paper Scissors, " << humanPlayer.name << "!\n"; std::cout << "Earn 10 points to become a Rock Paper Scissors Master!\n\n"; // Game loop do { getPlayerWeapon(&humanPlayer); getComputerWeapon(computerWeapon); determineWinner(&humanPlayer, computerWeapon); // Display current status std::cout << "\nYour current points: " << humanPlayer.points << "\n"; if (humanPlayer.master) { std::cout << "🎉 Congratulations! You are now a Master! 🎉\n"; } // Ask to play again std::cout << "Play another round? (y/n): "; std::cin >> playAgain; std::cin.ignore(); // Clear newline for next input std::cout << "\n"; } while (tolower(playAgain) == 'y'); // Final goodbye std::cout << "Thanks for playing! Final score: " << humanPlayer.points << "\n"; if (humanPlayer.master) { std::cout << "You finished as a Rock Paper Scissors Master! Awesome job!\n"; } return 0; }
Key Notes for Your Assignment
- All struct members match exactly what your assignment requires (including array sizes)
- The
checkMasteryfunction uses a pointer to modify the player'smasterstatus directly - Input validation ensures the
weaponarray only holds valid values - Random seed initialization prevents the computer from making the same sequence of choices every run
内容的提问来源于stack exchange,提问作者pavlkara1

