assign values to card
Feb 20, 2016 at 12:21pm UTC
this is my whole code but i have an issue in the card class with the get value member function how do i do assign values from 0-13 for each card of the deck?
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
#include <string>
#include <iostream>
#include <vector>
#include <ctime>
using namespace std;
class Card
{
private :
string face;
string suit;
int value;
public :
void setFace(string f) { face = f; }
void setSuit(string s) { suit = s; }
string getSuit() { return suit; }
string getFace() { return face; }
int getValue(){ return value; }
void show(){ cout << face << " of " << suit << endl; }
};
class StandardDeck
{
private :
vector<Card> deck;
public :
void make();
void show();
Card getRandomCard();
};
void StandardDeck::make()
{
deck.resize(52);
for (unsigned int i = 0; i<deck.size(); i++)
{
switch (i / 13){
case 0: deck[i].setSuit("Heart" ); break ;
case 1: deck[i].setSuit("Diamond" ); break ;
case 2: deck[i].setSuit("Club" ); break ;
case 3: deck[i].setSuit("Spade" ); break ;
}
switch (i % 13){
case 0: deck[i].setFace("Ace" ); break ;
case 1: deck[i].setFace("Two" ); break ;
case 2: deck[i].setFace("Three" ); break ;
case 3: deck[i].setFace("Four" ); break ;
case 4: deck[i].setFace("Five" ); break ;
case 5: deck[i].setFace("Six" ); break ;
case 6: deck[i].setFace("Seven" ); break ;
case 7: deck[i].setFace("Eight" ); break ;
case 8: deck[i].setFace("Nine" ); break ;
case 9: deck[i].setFace("Ten" ); break ;
case 10: deck[i].setFace("Jack" ); break ;
case 11: deck[i].setFace("Queen" ); break ;
case 12: deck[i].setFace("King" ); break ;
}
}
}
void StandardDeck::show()
{
for (unsigned int i = 0; i<deck.size(); i++)
cout << deck[i].getFace() << " of " << deck[i].getSuit() << endl;
}
Card StandardDeck::getRandomCard()
{
Card c;
int index;
index = rand() % deck.size();
c = deck[index];
deck.erase(deck.begin() + index);
return c;
}
class Player
{
private :
string name;
vector<Card> hand;
public :
void setName(string n) { name = n; }
string getName() { return name; }
Card getHand(int h) { return hand[h]; }
void setHand();
int handValue();
void addCard(Card c) {hand.push_back(c); }
};
int Player :: handValue()
{
int value;
value = 1024;
return value;
}
int main()
{
srand(time(NULL));
const int handSize = 3;
Player house, player1;
StandardDeck mydeck;
player1.setName("Player 1" );
house.setName("House" );
mydeck.make();
for (int i = 0; i < handSize; i++)
player1.addCard(mydeck.getRandomCard());
cout << endl << endl;
cout << player1.getName() << endl;
for (int i = 0; i<3; i++){
house.getHand(i).show();
}
cout << house.getName() << endl << endl;
//deck.getRandomCard().show();cout<<endl;
mydeck.show();
return 0;
}
Feb 20, 2016 at 12:22pm UTC
1 2 3 4 5 6 7
int Player :: handValue()
{
int value;
value = 1024;
return value;
}
i did this just so the program would run i think i need some if statements but how do i check each card and enter the value for each one is where im stuck at
Feb 20, 2016 at 3:50pm UTC
Why don't your classes have constructors that initialize their member variables?
Shouldn't the Player.handValue() look at the Card.values to determine the hand values?
Where is the Card.value initialized?
Topic archived. No new replies allowed.