Thursday, August 27, 2015

Bridge hands problem in C++

Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. #include <iostream>
  2. #include <array>
  3. #include <vector>
  4. //#include <cstdio>
  5. //#include <cmath>
  6. #include <cstdlib>
  7. #include <ctime>
  8. //#include <iterator>
  9.  
  10.  
  11. //os x: clang++ -std=c++11 -stdlib=libc++ -g -Wall bridgehands.cpp -o bridgehands
  12. //linux: clang++ -std=c++11 -g -Wall bridgehands.cpp -o bridgehands.linux
  13.  
  14. std::array<int,52> shuffle_deck ()
  15. {
  16.   std::vector<int> unshuffled_deck (52,0);
  17.   //initialize unshuffled deck
  18.   for (int i = 0; i < 52; i++) unshuffled_deck[i] = i;
  19.  
  20.   srand (time (NULL));
  21.   std::array<int,52> shuffled_deck;
  22.   shuffled_deck.fill(-1);
  23.  
  24.   /* shuffle the deck by taking a random
  25.      card from the unshuffled deck then
  26.      deleting the card */
  27.   for (int i = 0; i < 52; i++) {
  28.     int random_card = rand() % (52 - i);
  29.     shuffled_deck[i] = unshuffled_deck[random_card];
  30.     unshuffled_deck.erase(unshuffled_deck.begin() + random_card);
  31.    
  32.  
  33.   }
  34.  
  35.  
  36.  
  37.  
  38.   return shuffled_deck;
  39.  
  40. }
  41.  
  42. template<typename C>
  43. void print_sequence(C& items) {
  44.   for (auto const& item : items) std::cout << item << ' ';
  45. }
  46.  
  47. std::string convert_card (int card)
  48. {
  49.   std::array<std::string,13> card_strings =
  50.   {{ "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"}};
  51.   return card_strings[card % 13];
  52.  
  53. }
  54.  
  55. template<typename C>
  56. void print_card_sequence(const C& cards) {
  57.   for (auto const& card : cards) std::cout << convert_card(card) << ' ';
  58. }
  59.  
  60. std::array<std::array<int, 13>, 4> deal_cards (std::array<int,52> shuffled_cards)
  61. {
  62.   std::array<std::array<int,13>, 4> dealt_cards;
  63.   for (int i = 0; i < 52; i++)
  64.     {
  65.       dealt_cards[i%4][i%13] = shuffled_cards[i];
  66.     }
  67.   return dealt_cards;
  68. }
  69.  
  70.  
  71. struct Hand
  72. {
  73.  
  74.   //store cards as an array of four vectors
  75.   std::array<std::vector<int>,4> cards;
  76.  
  77.   void add_cards(std::array<int,13> new_cards)
  78.   {
  79.     std::sort(new_cards.begin(), new_cards.end());
  80.     std::reverse(new_cards.begin(),new_cards.end());
  81.    
  82.     for (auto card : new_cards) {
  83.       cards[(card / 13)].push_back(card);
  84.     }
  85.   }
  86.  
  87.   std::array<std::string,4> suit_labels {{"C", "D", "H", "S"}};
  88.  
  89.   void print_hand(void)
  90.   {
  91.     for (int i = 3; i > -1; i--)
  92.       {
  93.     std::cout << suit_labels[i] << ": ";
  94.     print_card_sequence(cards[i]);
  95.     std::cout << "\n";
  96.       }
  97.   }
  98.  
  99. };
  100.  
  101.  
  102. void print_all_hands (std::array<std::array<int,13>, 4> dealt_cards)
  103. {
  104.   std::array<std::string,4> hand_labels = {{"NORTH", "EAST", "SOUTH", "WEST"}};
  105.   for (int i = 0; i < 4; i ++)
  106.     {
  107.       std::cout << hand_labels[i] << "\n";
  108.       Hand this_hand;
  109.       this_hand.add_cards(dealt_cards[i]);
  110.       this_hand.print_hand();
  111.       std::cout << "\n";
  112.     }
  113. }
  114.  
  115.  
  116.  
  117. int main(int argc, char* argv[])
  118. {
  119.   std::array<int,52> shuffled_deck = shuffle_deck();
  120.   std::array<std::array<int,13>, 4> dealt_cards = deal_cards(shuffled_deck);
  121.   //print_sequence(dealt_cards[0]);
  122.   //Hand north;
  123.   //north.add_cards(dealt_cards[0]);
  124.   //north.print_hand();
  125.  
  126.   print_all_hands(dealt_cards);
  127.  
  128.   return 0;
  129. }

No comments:

Post a Comment