Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <sstream>
- #include <ctime>
- #include <cstdlib>
- using namespace std;
- class Card {
- private:
- int value;
- string suit;
- public:
- int getValue();
- string getSuit();
- void setValue(int verdi);
- void setSuit(string streng);
- string getFace();
- };
- class CardDeck {
- private:
- Card cards[52];
- int numberDealt;
- public:
- void buildDeck();
- void shuffle();
- CardDeck();
- CardDeck(bool shouldShuffle);
- void dealHand(Card hand[], int numberOfCards);
- };
- int main (){
- srand(time(NULL));
- Card kort;
- kort.setValue(12);
- kort.setSuit("H");
- cout << kort.getFace() << endl;
- CardDeck myDeck(true);
- const int HANDSIZE = 7;
- Card myHand[HANDSIZE];
- myDeck.dealHand(myHand, HANDSIZE);
- for(int i = 1; i < 8; i++){
- myDeck.dealHand(myHand, HANDSIZE);
- cout << "Hånd " << i << ":" << endl;
- for(int j = 0; j < HANDSIZE; j++)
- cout << myHand[j].getFace() << endl;
- cout << endl;
- }
- return 0;
- }
- int Card::getValue(){
- return value;
- }
- string Card::getSuit(){
- return suit;
- }
- void Card::setValue(int verdi){
- if (verdi >= 1 && verdi <= 13){
- value = verdi;
- }
- }
- void Card::setSuit(string streng){
- if (streng[0] == 'S' || streng[0] == 'H' || streng[0] == 'C' || streng[0] == 'D'){
- suit = streng;
- }
- }
- string Card::getFace(){
- string numberAsString;
- stringstream ss;
- ss << value;
- numberAsString = ss.str();
- string colorAndValue = suit + numberAsString;
- return colorAndValue;
- }
- void CardDeck::buildDeck(){
- string suits[4] = { "S", "H", "C", "D" };
- for ( int i = 0 ; i < 52 ; i++ ){
- cards[i].setValue((i % 13) + 1);
- cards[i].setSuit(suits[i / 13]);
- }
- numberDealt = 0;
- }
- void CardDeck::shuffle(){
- int remain;
- buildDeck();
- Card temp;
- for ( int i = 0 ; i < 51 ; i++){
- remain = (rand() % (52 - i));
- temp = cards[i];
- cards[i] = cards[i + remain];
- cards[i + remain] = temp;
- }
- numberDealt = 0;
- }
- CardDeck::CardDeck(){
- buildDeck();
- }
- CardDeck::CardDeck(bool shouldShuffle){
- buildDeck();
- if (shouldShuffle){
- shuffle();
- }
- }
- void CardDeck::dealHand(Card hand[], int numberOfCards){
- if(numberDealt + numberOfCards > 52)
- cout << "Kan ikke trekke så mange kort!";
- return;
- for(int i = 0; i < numberOfCards; i++)
- hand[i] = cards[numberDealt + i];
- numberDealt += numberOfCards;
- }
Add Comment
Please, Sign In to add comment