109
2.cpp
Normal file
109
2.cpp
Normal file
@@ -0,0 +1,109 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <queue>
|
||||
#include <stack>
|
||||
#include <string>
|
||||
|
||||
// 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<Card> cards; // Vector of cards in the player's hand
|
||||
};
|
||||
|
||||
// Global variables
|
||||
std::vector<Hand> players; // Vector of player hands
|
||||
std::queue<Card> drawPile; // Queue of cards to be drawn
|
||||
std::stack<Card> 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;
|
||||
}
|
||||
Reference in New Issue
Block a user