samedi 28 avril 2018

Turning numbers into text and sorting for Card Deck

So I've done the majority of this project (unwrapping cards in ascending order, shuffling them, and dealing them). However, I now have to take what I have, which has been approved, and turn the numbers into text descriptions of the cards( Ace, Hearts, ect) and sort them. I basically have to finish out my showHand function. I'm having trouble figuring out where to start with this. I was thinking of using a struct, but don't know if that would be the correct approach. We have not learned classes and public, so I would want to keep with the approach I've already taken. Here is what I have so far:

#include <iostream>
#include <vector>
#include <algorithm>
#include <iomanip>
#include <string>

using namespace std;

using cards = unsigned short;

//Global constants
const int NUM_COLS=13;
const int NUM_ROWS=4;


//Create Function prototypes for card loading and shuffling deck of 
cards
void unwrap(vector<int> &);
void shuffle(vector<int> &);
void printCards(vector<int>);

//Function for dealing cards to players
void deal(vector<vector<int>>&, const vector<int>&);
void showHands(const vector<vector<int>>&);


int main() {

//Declare vector for the number of cards
vector <int> deck;



//Call function for showing cards in ascending order, and shuffling 
cards
cout << "The deck before shuffling: " << endl;
unwrap(deck);
printCards(deck);

cout << "The deck after shuffling: " << endl;
shuffle(deck);
printCards(deck);

//Declare 2d vector for dealing the cards to each player
vector<vector<int>> players(NUM_ROWS, vector<int>(NUM_COLS, 0));

cout << " Before cards have been dealt " << endl;
showHands(players);

cout << " After the cards have been dealt "<<endl;
deal(players, deck);
showHands(players);

return 0;
}

//Function definitions that load cards and randomly shuffles them
void unwrap(vector<int> &deck)
{
//Load vector with ints from 0 to 51
for (int i = 0; i <= 51; i++)
{
    deck.push_back(i);
}

}

// Randomize the cards in the deck
void shuffle(vector<int> &deck)
{
random_shuffle(deck.begin(), deck.end());

}
void printCards(vector<int> deck)
{
for(int j=0; j<deck.size(); j++)
{
    cout<< deck[j] << endl;
}
}
// deal one card to each player in order, repeat until the entire deck 
is dealt
void deal(vector<vector<int>>& players, const vector<int>& deck)
{
int card_count = 0;

for (int cols = 0; cols < NUM_COLS; cols++)
{
    for (int rows = 0; rows < NUM_ROWS; rows++)
    {
        players[rows][cols] = deck[card_count];
        card_count++;
    }
}
}
//Show hand after cards have been randomly dealt to each player
void showHands(const vector<vector<int>>& players)
{
for (int rows = 0; rows < NUM_ROWS; rows++)
{
    cout << "Player " << rows + 1 << ": ";
    for (int cols = 0; cols < NUM_COLS; cols++)
    {
        cout << players[rows][cols] << ' ';
    }
    cout << '\n';
}
}

Aucun commentaire:

Enregistrer un commentaire