Guest User

Untitled

a guest
Jun 25th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.75 KB | None | 0 0
  1.  
  2.  
  3. #include <iostream>
  4. #include <string>
  5. #include <sstream>
  6. #include <ctime>
  7. #include <cstdlib>
  8.  
  9. using namespace std;
  10.  
  11.  
  12. class Card {
  13.  
  14. private:
  15. int value;
  16. string suit;
  17.  
  18. public:
  19. int getValue();
  20. string getSuit();
  21. void setValue(int verdi);
  22. void setSuit(string streng);
  23. string getFace();
  24. };
  25.  
  26.  
  27. class CardDeck {
  28.  
  29. private:
  30. Card cards[52];
  31. int numberDealt;
  32.  
  33. public:
  34. void buildDeck();
  35. void shuffle();
  36. CardDeck();
  37. CardDeck(bool shouldShuffle);
  38. void dealHand(Card hand[], int numberOfCards);
  39.  
  40. };
  41.  
  42.  
  43.  
  44. int main (){
  45.  
  46. srand(time(NULL));
  47.  
  48. Card kort;
  49.  
  50. kort.setValue(12);
  51. kort.setSuit("H");
  52.  
  53. cout << kort.getFace() << endl;
  54.  
  55.  
  56.  
  57.  
  58. CardDeck myDeck(true);
  59.  
  60. const int HANDSIZE = 7;
  61.  
  62. Card myHand[HANDSIZE];
  63. myDeck.dealHand(myHand, HANDSIZE);
  64.  
  65. for(int i = 1; i < 8; i++){
  66.  
  67. myDeck.dealHand(myHand, HANDSIZE);
  68. cout << "Hånd " << i << ":" << endl;
  69.  
  70. for(int j = 0; j < HANDSIZE; j++)
  71. cout << myHand[j].getFace() << endl;
  72. cout << endl;
  73. }
  74.  
  75. return 0;
  76. }
  77.  
  78.  
  79.  
  80.  
  81.  
  82. int Card::getValue(){
  83. return value;
  84. }
  85.  
  86. string Card::getSuit(){
  87. return suit;
  88. }
  89.  
  90. void Card::setValue(int verdi){
  91. if (verdi >= 1 && verdi <= 13){
  92. value = verdi;
  93. }
  94. }
  95.  
  96. void Card::setSuit(string streng){
  97. if (streng[0] == 'S' || streng[0] == 'H' || streng[0] == 'C' || streng[0] == 'D'){
  98. suit = streng;
  99. }
  100. }
  101.  
  102. string Card::getFace(){
  103.  
  104. string numberAsString;
  105. stringstream ss;
  106. ss << value;
  107. numberAsString = ss.str();
  108.  
  109. string colorAndValue = suit + numberAsString;
  110.  
  111. return colorAndValue;
  112. }
  113.  
  114. void CardDeck::buildDeck(){
  115. string suits[4] = { "S", "H", "C", "D" };
  116. for ( int i = 0 ; i < 52 ; i++ ){
  117. cards[i].setValue((i % 13) + 1);
  118. cards[i].setSuit(suits[i / 13]);
  119. }
  120.  
  121. numberDealt = 0;
  122. }
  123.  
  124. void CardDeck::shuffle(){
  125.  
  126. int remain;
  127. buildDeck();
  128. Card temp;
  129.  
  130. for ( int i = 0 ; i < 51 ; i++){
  131. remain = (rand() % (52 - i));
  132. temp = cards[i];
  133. cards[i] = cards[i + remain];
  134. cards[i + remain] = temp;
  135.  
  136. }
  137.  
  138. numberDealt = 0;
  139. }
  140.  
  141.  
  142. CardDeck::CardDeck(){
  143. buildDeck();
  144. }
  145.  
  146. CardDeck::CardDeck(bool shouldShuffle){
  147. buildDeck();
  148. if (shouldShuffle){
  149. shuffle();
  150. }
  151. }
  152.  
  153.  
  154. void CardDeck::dealHand(Card hand[], int numberOfCards){
  155. if(numberDealt + numberOfCards > 52)
  156. cout << "Kan ikke trekke så mange kort!";
  157. return;
  158.  
  159. for(int i = 0; i < numberOfCards; i++)
  160. hand[i] = cards[numberDealt + i];
  161.  
  162. numberDealt += numberOfCards;
  163. }
Add Comment
Please, Sign In to add comment