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
|
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <vector>
using namespace std;
//global arrays
string r[13] ={
"Ace","Two","Three","Four",
"Five", "Six","Seven","Eight",
"Nine","Ten","Jack", "Queen", "King"
};
string s[4] = {"Spades","Hearts","Clubs","Diamonds"};
/* Assignment: Modify the Card class to include a private variable called value that will
* represent the value of the card in a game of blackjack.
*
* Create getter/setter functions for this variable.
*
* To assign the value, you can:
* 1.) Create a member function called assignValue() WITHIN the class
* that will determine the rank and assign the correct value of each card.
*
* or
*
* 2.) Assign the value of each card OUTSIDE the class
* */
//classes
class Card{
private:
int value;
string rank;
string suit;
public:
//setter functions
void set_rank(string r){rank = r;};
void set_suit(string s){suit = s;};
void set_value(int v){value = v;};
// getter functions
string get_rank(){ return rank; };
string get_suit(){ return suit; };
int get_value(){ return value; };
void display(){
cout << value << " - " <<rank << " of "<< suit << endl;
}
}; //end of Card class
//prototype
void shuffle(vector <Card> &d);
Card dealfrom(vector <Card> &d);
int main(){
srand(time(0));
Card temp;
vector <Card> deck(52);
vector <Card> hand(0);
int count;
bool playagain = true;
char again;
// assigning rank and suit and value to each Card object in vector deck
for (int i = 0; i < 52; i++) {
if(i % 13 == 0)
deck[i].set_value(11);
else if (i % 13 >= 9)
deck[i].set_value(10);
else
deck[i].set_value(i%13 + 1);
deck[i].set_rank(r[i % 13]);
deck[i].set_suit(s[i / 13]);
}
shuffle(deck);
//begin Black Jack Game
while(playagain){
cout << "Do you want to play another hand? (y/n)";
cin >> again;
if(again != 'y')
playagain = false;
}
/*/prints every card in the deck
count = 0;
for(Card c: hand){
cout << ++count<< " ";
c.display();
}cout << endl;
*/
shuffle(deck);
}// end of main()
//function definitions
void shuffle(vector <Card> &d){
Card temp;
for (int i=0; i < d.size(); i++){
int r = rand() % d.size();
temp = d[i];
d[i] = d[r];
d[r] = temp;
}
}
Card dealfrom(vector <Card> &d){
Card temp;
temp = d[d.size()-1];
d.pop_back();
return temp;
}
| |