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 |
#include <iostream> #include <vector> #include <cstdlib> #include <ctime> enum Choice { COOPERATE, DEFECT }; class Player { public: Player(std::string name) : name(name), score(0) {} void makeChoice(Choice choice) { this->choice = choice; } Choice getChoice() const { return choice; } void updateScore(int points) { score += points; } int getScore() const { return score; } std::string getName() const { return name; } private: std::string name; Choice choice; int score; }; class PrisonersDilemma { public: PrisonersDilemma(Player& player1, Player& player2) : player1(player1), player2(player2) {} void playRound() { int points1 = 0, points2 = 0; if (player1.getChoice() == COOPERATE && player2.getChoice() == COOPERATE) { points1 = points2 = 3; // Both cooperate } else if (player1.getChoice() == COOPERATE && player2.getChoice() == DEFECT) { points1 = 0; // Player 1 cooperates, Player 2 defects points2 = 5; } else if (player1.getChoice() == DEFECT && player2.getChoice() == COOPERATE) { points1 = 5; // Player 1 defects, Player 2 cooperates points2 = 0; } else { points1 = points2 = 1; // Both defect } player1.updateScore(points1); player2.updateScore(points2); printRoundResults(points1, points2); } void printScores() const { std::cout << player1.getName() << " Score: " << player1.getScore() << '\n'; std::cout << player2.getName() << " Score: " << player2.getScore() << '\n'; } private: Player& player1; Player& player2; void printRoundResults(int points1, int points2) const { std::cout << player1.getName() << " chose " << (player1.getChoice() == COOPERATE ? "Cooperate" : "Defect") << " and scored " << points1 << '\n'; std::cout << player2.getName() << " chose " << (player2.getChoice() == COOPERATE ? "Cooperate" : "Defect") << " and scored " << points2 << '\n'; } }; int main() { std::srand(static_cast<unsigned>(std::time(0))); Player player1("Player 1"); Player player2("Player 2"); // Random strategy for demonstration player1.makeChoice(static_cast<Choice>(std::rand() % 2)); player2.makeChoice(static_cast<Choice>(std::rand() % 2)); PrisonersDilemma game(player1, player2); game.playRound(); game.printScores(); return 0; } |
Explanation
- Class Definitions:
Player
Class:- Private Members:
name
: The name of the player.choice
: The choice made by the player (COOPERATE or DEFECT).score
: The score accumulated by the player.
- Public Methods:
Player(std::string name)
: Constructor initializes the player with a name.void makeChoice(Choice choice)
: Sets the player’s choice.Choice getChoice() const
: Retrieves the player’s choice.void updateScore(int points)
: Updates the player’s score.int getScore() const
: Retrieves the player’s score.std::string getName() const
: Retrieves the player’s name.
- Private Members:
PrisonersDilemma
Class:- Private Members:
player1
andplayer2
: References to the two players.
- Public Methods:
PrisonersDilemma(Player& player1, Player& player2)
: Constructor initializes the game with two players.void playRound()
: Simulates a round of the game based on the players’ choices.- Awards points according to the classic Prisoner’s Dilemma rules.
- Updates players’ scores and prints the round results.
void printScores() const
: Prints the current scores of both players.
- Private Members:
- Private Methods:
void printRoundResults(int points1, int points2) const
: Prints the choices and points for the round.
main
Function:- Initializes two players with names.
- Randomly assigns a strategy (COOPERATE or DEFECT) to each player.
- Creates a
PrisonersDilemma
game instance with the two players. - Plays one round of the game and prints the scores.