fork download
  1. #include <iostream>
  2.  
  3. class Card
  4. {
  5. public:
  6. int suit;
  7. int rank;
  8. private:
  9. };
  10.  
  11. #include <vector>
  12. using namespace std;
  13. class Hand
  14. {
  15. public:
  16. void addCard(Card c);
  17. bool isFlush();
  18. private:
  19. vector<Card> myHand;
  20. int suit;
  21. };
  22.  
  23. using namespace std;
  24. class Deck
  25. {
  26. public:
  27. Deck();
  28. void shuffleDeck();
  29. Hand dealCards();
  30. private:
  31. vector<Card> myDeck;
  32. };
  33.  
  34. bool Hand::isFlush()
  35. {
  36. bool result;
  37. // Check for a flush (all the same suit)
  38. if(myHand[0].suit == myHand[1].suit && myHand[0].suit== myHand[2].suit && myHand[0].suit==myHand[3].suit && myHand[0].suit==myHand[4].suit)
  39. {
  40. result = true;
  41. }
  42. else
  43. {
  44. result = false;
  45. }
  46. return result;
  47. }
  48. void Hand::addCard(Card c)
  49. {
  50. myHand.push_back(c);
  51. }
  52.  
  53. #include<algorithm>
  54. Deck::Deck()
  55. {
  56. for(int i=0; i<52;i++)
  57. {
  58. Card c;
  59. c.suit=i/13;
  60. c.rank=(i%13)+2;
  61. myDeck.push_back(c);
  62. }
  63. }
  64. void Deck::shuffleDeck()
  65. {
  66. random_shuffle(myDeck.begin(), myDeck.end());
  67. }
  68. Hand Deck::dealCards()
  69. {
  70. Hand result;
  71. for(int i=0;i<5;i++)
  72. {
  73. result.addCard(myDeck[i]);
  74. }
  75. return result;
  76. }
  77.  
  78. int main()
  79. {
  80. srand(time(0));
  81. cout<<"Welcome to the Poker Experiment."<<endl;
  82. Deck deck;
  83. int flushCount=0;
  84. int numberOfHands=500000;
  85. for(int i=0; i<numberOfHands;i++)
  86. {
  87. deck.shuffleDeck();
  88. Hand hand=deck.dealCards();
  89. if(hand.isFlush())
  90. {
  91. flushCount++;
  92. deck.shuffleDeck();
  93. }
  94. else
  95. {
  96. deck.shuffleDeck();
  97. }
  98. hand = deck.dealCards();
  99. }
  100. cout<<"Flush percentage = "
  101. << ((double)flushCount/(double)numberOfHands)*100.0<<endl;
  102. cout << "Num flush: " << flushCount << endl;
  103. cout << "Num hands: " << numberOfHands;
  104. return 0;
  105. }
  106.  
Success #stdin #stdout 3.03s 3476KB
stdin
Standard input is empty
stdout
Welcome to the Poker Experiment.
Flush percentage = 0.1984
Num flush: 992
Num hands: 500000