From 1081fb06871f568aac76f46f7cb4dfdbdb8e7120 Mon Sep 17 00:00:00 2001 From: ChenKaiLiuG Date: Tue, 30 Dec 2025 06:04:19 +0000 Subject: [PATCH] Upload 2.cpp Signed-off-by: ChenKaiLiuG --- 2.cpp | 109 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 109 insertions(+) create mode 100644 2.cpp diff --git a/2.cpp b/2.cpp new file mode 100644 index 0000000..1ae93b8 --- /dev/null +++ b/2.cpp @@ -0,0 +1,109 @@ +#include +#include +#include +#include +#include + +// Structure to represent a card +struct Card { + int color; // Color of the card (red, green, blue) + int value; // Value of the card (1-7) +}; + +// Struct to represent a player's hand +struct Hand { + std::vector cards; // Vector of cards in the player's hand +}; + +// Global variables +std::vector players; // Vector of player hands +std::queue drawPile; // Queue of cards to be drawn +std::stack discardPile; // Stack of cards to be discarded +int currentPlayer = 0; // Current player index +int winner = -1; // Index of the winner (or -1 if no one won) + +// Function to shuffle the deck and deal cards to players +void dealCards() { + for (int i = 0; i < 4; i++) { + Hand &playerHand = players[i]; + playerHand.cards.clear(); // Reset the hand + for (int j = 0; j < 7; j++) { + Card card = {randomColor(), randomValue()}; // Draw a card from the deck + playerHand.cards.push_back(card); // Add the card to the hand + } + } +} + +// Function to draw cards and end the round +void endRound() { + for (int i = 0; i < 4; i++) { + Hand &playerHand = players[i]; + playerHand.cards.clear(); // Reset the hand + while (!drawPile.empty()) { + Card card = drawPile.front(); + drawPile.pop(); // Remove the top card from the draw pile + playerHand.cards.push_back(card); // Add the card to the hand + } + } +} + +// Function to discard cards and end the game +void endGame() { + for (int i = 0; i < 4; i++) { + Hand &playerHand = players[i]; + playerHand.cards.clear(); // Reset the hand + while (!discardPile.empty()) { + Card card = discardPile.front(); + discardPile.pop(); // Remove the top card from the discard pile + playerHand.cards.push_back(card); // Add the card to the hand + } + } + winner = players[0].cards[0].color; // Determine the winner based on the cards in each player's hand + for (int i = 0; i < 4; i++) { + std::cout << "Player " << i + 1 << " has " << players[i].cards.size() << " cards." << std::endl; + } +} + +// Function to handle player actions +void handleAction(int action) { + switch (action) { + case 0: // Draw a card + drawPile.push(Card{randomColor(), randomValue()}); + break; + case 1: // Discard a card + discardPile.push(players[currentPlayer].cards[0]); + players[currentPlayer].cards.erase(players[currentPlayer].cards.begin()); + break; + default: + std::cerr << "Invalid action!" << std::endl; + return; + } +} + +int main() { + // Initialize the game + dealCards(); + currentPlayer = 0; + winner = -1; + + // Loop until the game ends + while (currentPlayer < 4) { + // Handle player actions + handleAction(currentPlayer); + + // End the round and draw new cards + endRound(); + + // Check if the game is over + if (winner != -1) { + break; + } + } + + // Print the final scores + for (int i = 0; i < 4; i++) { + std::cout << "Player " << i + 1 << " has " << players[i].cards.size() << " cards." << std::endl; + } + + return 0; +}