Search code examples
c++playing-cards

Rounds in a card game


The game has 4 players.Each player takes turns being the card czar after the round ends. I've stored cards assigned to each player in vectors. How would i create rounds in this game? Would i create a loop in the main function to run 4 times and clear the vectors and call my functions within the loop? Here is my main function:

int main()
{   

vector<string> player;
vector<string> blackCards;
vector<string> whiteCards;
vector<string> CardCzar;
vector<string> player2;
vector<string> player3;
vector<string> player4;
int sz = 0;
int i = 0;
string n;

        for(i=0;i<4;i++)
        {   cout<<"Enter name for player"<<" "<< i+1 << " : "<<" ";
         cin>> n;
         player.push_back(n);
    }

BlackCards(blackCards);
WhiteCards(whiteCards);
order(player);
sz = player.size();
cout<<" "<<endl;
cout<<"*"<<player[sz - 4]<<" , you are the card czar for this round *"  <<endl;

assign_bcards(blackCards , CardCzar, player);
assign_wcards(whiteCards, player2, player3, player4, player);





    return 0;
}

Solution

  • To make rounds you could have a loop with a circular indicator that hits every player in order. An easy numerical way to do this is

    int currentCzar = 0;
    while(gameIsNotOver)
    {
      *Do game stuff*
      currentCzar = (currentCzar+1) % numPlayers;
    }
    

    Since currentCzar is always less than numPlayers

    (currentCzar+1) % numPlayers;
    

    will always return a values c where

    0 <= c <= numPlayers-1
    

    Then just have some other flag tell you when to stop playing like a win condition. Also numPlayers would be the size of the array.

    EDIT: You might also want to change the way you structre your data. Maybe by making a player a class or struct so that you can have something like

    class Player{
       vector<string> whiteCards;
       vector<string> wonBlackCardsl
       //Boilerplate class code
    }
    vector<Player> players;
    *assign cards and create players*
    int currentCzar = 0;
    while(gameIsNotOver)
    {
       *Do game stuff*
        //Have players[currentCzar] choose a black card
        //Have every other player put down a white card
        currentCzar = (currentCzar+1) % numPlayers;
    }
    

    Then you could couple the players to their hands in a much more direct way