Silverbullets

Program 2

Dec 9th, 2014
750
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.92 KB | None | 0 0
  1. // Example program
  2. #include <iostream>
  3. #include <string>
  4. #include <ctime>
  5. #include <cstdlib>
  6.  
  7. using namespace std;
  8.  
  9. void shuffleDeck(int *&deck);
  10. int dealHand(int *deck);
  11. void checkHand(int *hand);
  12. void orderHand(int *hand);
  13.  
  14. int *newDeck = new int[52];
  15. string rankStrings[13] = {"one","two","three","four","five","six","seven","eight","nine","ten","jack","queen","king"};
  16. string suitStrings[4] = {"spades","diamonds","clubs","hearts"};
  17.  
  18. int main()
  19. {
  20.     //int *ranks = new int[52];
  21.     //int *suits = new int[52];
  22.     for(int i = 0; i < 52; i++) {
  23.         newDeck[i] = i;
  24.         //cout << "Ranks: " << ranks[i] << endl;
  25.         //cout << "Suits: " << suits[i] << endl;
  26.         //cout << "New Deck: " << newDeck[i] << endl;
  27.     }
  28.     shuffleDeck(newDeck);
  29.     for (int i = 0; i < 52; i++) {
  30.      //cout << newDeck[i] << endl;  
  31.     }
  32.     int *myHand = new int[5];
  33.     for (int j = 0; j < 50; j+=5) {
  34.         for (int i = j; i < j+5; i++ ) {
  35.         myHand[i%5] = newDeck[i];
  36.         cout << rankStrings[myHand[i%5]/4] << " of " << suitStrings[myHand[i%5]%4] << endl;
  37.         }
  38.             checkHand(myHand);
  39.             cout << "------------------------" << endl;
  40.     }
  41. }
  42.  
  43. void shuffleDeck(int *&deck) {
  44.     srand(time(NULL));
  45.     //do the swap here
  46.     for (int i = 0; i < 52; i++) {
  47.         int temp = deck[i];
  48.         int swap = rand() % (i+1);
  49.         deck[i] = deck[swap];
  50.         deck[swap] = temp;
  51.     }
  52. }
  53.  
  54. void orderHand(int *hand) {
  55.     for (int j = 0; j < 5; j++)
  56.     {
  57.         for (int l = 0; l < 5; l++){
  58.         if (hand[l] <= hand[l+1])
  59.         {
  60.         int tempCard = *&hand[l+1];
  61.         hand[l+1] = *&hand[l];
  62.         hand[l] = *&tempCard;
  63.         }
  64.     }
  65.   }
  66. }
  67.  
  68. void checkHand(int *hand) {
  69.     int *cards = new int[13];
  70.     int *counts = new int[5];
  71.     int *cloneHand = new int[5];
  72.     for (int i = 0; i < 5; i++)
  73.     {
  74.      cloneHand[i] = hand[i]/4;
  75.     }
  76.     for (int j = 0; j < 5; j++){
  77.      int card = cloneHand[j];
  78.      cards[card]++;
  79.     }
  80.     for (int k = 0; k < 13; k++) {
  81.      if (cards[k] == 0) {
  82.       counts[0]++;
  83.      } else if (cards[k] == 1) {
  84.          counts[1]++;
  85.      }
  86.          else if (cards[k] == 2) {
  87.              counts[2]++;
  88.          } else if (cards[k] == 3) {
  89.              counts[3]++;
  90.          } else if (counts[4]) {
  91.              counts[4]++;
  92.          }
  93.     }
  94.     if (counts[4] == 1) {
  95.         cout << "Four of a kind!" << endl;
  96.     } else if (counts[3] == 1) {
  97.         cout << "Three of a kind!" << endl;
  98.     } else if (counts[2] == 2) {
  99.         cout << "Two pairs!" << endl;
  100.     } else if (counts[2] == 1) {
  101.         cout << "Pair!" << endl;
  102.     } else {
  103.         cout << "No win!" << endl;
  104.     }
  105.    
  106.     //delete[] cards;
  107.     //delete[] counts;
  108.     //delete[] cloneHand;
  109. }
  110.  
  111. /*int dealHand(int *deck) {
  112.     int *myHand = new int[5];
  113.     for (int i = 0; i < 5; i++) {
  114.         myHand[i] = deck[i];
  115.     }
  116.     return *myHand;
  117. }*/
Advertisement
Add Comment
Please, Sign In to add comment