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 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
|
#include <algorithm> // std::random_shuffle
#include <ctime> // std::time
#include <cstdlib>
#include <stdlib.h> /* srand, rand */
//My class files
#include "playerClass.h"
//standard namespace
using namespace std;
//Funtion prototypes
void addPlayers(unsigned int, vector <Player>&);
//adds players to the vector
//@parm unsigned int - the number of players to add.
//@parm vector <Player>& - they vector of all the players
void dealCards(unsigned int, vector<Player>&, vector <Card>&);
//deals x cards to specific player
//@parm unsigned int - the number of cards to deal
//@parm vector <Player>& - they vector of the player
//@parm vector <Card>& - our working deck
void displayDeck(const vector <Card>&);
//Displays the whole deck and card positions
//@parm const vector <Card>& - Cards in vector (deck)
void fillDeck(vector <Card>&);
//fills the deck with suits and values
//@parm vector <Card>& - the vector to be filled
void showHands(vector <Player>&);
//shows the hands of all players
//@parm vector <Player>& - the vector of all the players
int main() {
int hold; //only used to keep the consol open
int randomize; // for the shuffle
srand(unsigned(std::time(0)));//without this random_shuffle will always give the same result
//create my card and player vectors
vector <Card> cards;
vector <Player> players;
//add playsers - fill deck - randomize "shuffle" deck - deals the cards - clear the screen
//- shows the player hands - displays number of cards left in deck
addPlayers(2, players);
fillDeck(cards);
randomize = rand() % 100 + 1;
random_shuffle(cards.begin(), cards.end());
dealCards(10, players, cards);
system("CLS");
showHands(players);
cout << "Cards left in the deck: " << cards.size();
cin >> hold;
return 0;
}
void addPlayers(unsigned int amount, vector <Player>& players) {
string name;
int score (0);
for (unsigned int i = 0; i < amount; i++) {
cout << "Enter name of player " << i + 1 << ":";
cin >> name;
vector <Card> playerHand;
Player newPlayer(name, score, playerHand);
players.push_back(newPlayer);
}
}
void dealCards(unsigned int amount, vector<Player>& player, vector <Card>& deck) {
for (unsigned int i = 0; i < amount; i++) {
for (unsigned int j = 0; j < player.size(); j++) {
Card newCard;
newCard.setSuit(deck[0].getSuit());
newCard.setValue(deck[0].getValue());
player[j].addCard(newCard);
deck.erase(deck.begin());
}
}
}
void displayDeck(const vector <Card>& cards) {
unsigned int size = cards.size();
for (unsigned int i = 0; i < size; i++) {
cout << "Card " << i + 1 << " is: " << cards[i].getValue() << cards[i].getSuit() << endl;
}
}
void fillDeck(vector <Card>& card) {
//first loop to run every card second loop to place 13 cards per suit
for (int i = 0; i <13; i++) {
//suit value var - Always will be 1 value higher than i
int value = i + 1;
//select the suit and add the card to the vector
for (int j = 1; j <= 4; j++) {
if (j == 1) {
Card newCard(value, 'H');
card.push_back(newCard);
}
else if (j == 2) {
Card newCard(value, 'D');
card.push_back(newCard);
}
else if (j == 3) {
Card newCard(value, 'S');
card.push_back(newCard);
}
else {
Card newCard(value, 'C');
card.push_back(newCard);
}
}
}
}
void showHands(vector <Player>& players) {
for (unsigned int i = 0; i < players.size(); i++) {
cout << players[i].getName() << "'s hand: \n";
for (unsigned int j = 0; j < players[i].getHand().size();j++) {
cout << "card " << j + 1 << ": " << players[i].getHand()[j].getValue()
<< players[i].getHand()[j].getSuit() << endl;
}
cout << "\n\n";
}
}
|