Advertisement
ZoriaRPG

ZScript: Video Poker v2

May 12th, 2017
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.90 KB | None | 0 0
  1. const int HT_ROYAL = 9;
  2. const int HT_STRAIGHTFLUSH = 8;
  3. const int HT_FLUSH = 7;
  4. const int HT_STRAIGHT = 6;
  5. const int HT_QUADS = 5;
  6. const int HT_FULLHOUSE = 4;
  7. const int HT_TRIPS = 3;
  8. const int HT_TWOPAIR = 2;
  9. const int HT_ONEPAIR = 1;
  10.        
  11.  
  12. ffc script VideoPoker{
  13.     void run(){
  14.        
  15.         //Numeric Values
  16.         int cards[52]={1,2,3,4,5,6,7,8,9,10,11,12,13,
  17.             1,2,3,4,5,6,7,8,9,10,11,12,13,
  18.             1,2,3,4,5,6,7,8,9,10,11,12,13,
  19.             1,2,3,4,5,6,7,8,9,10,11,12,13,};
  20.         //Suits
  21.         int suits[52]={ 1,1,1,1,1,1,1,1,1,1,1,1,1,
  22.                 2,2,2,2,2,2,2,2,2,2,2,2,2,
  23.                 3,3,3,3,3,3,3,3,3,3,3,3,3,
  24.                 4,4,4,4,4,4,4,4,4,4,4,4,4 };
  25.         //cards dealt.
  26.         bool dealt[52];
  27.         //five first cards, and five under them.
  28.         int deal[10] = { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1};
  29.         int counts[13]; //Pairs, pryles, quads.
  30.         int c[255];
  31.        
  32.        
  33.        
  34.         //Populagte the cards.
  35.         for ( c[1] = 0; c[1] < 10; c[1]++ ) {
  36.             do {
  37.                 c[0] = Rand(0,51);
  38.                
  39.             } while( dealt[ c[0] ] ); //If the card was dealt, choose another.
  40.             deal[ c[q] ] = c[0]; //Put the card in the deal pack.
  41.             dealt[ c[0] ] = true; //Mark the card dealt.
  42.         }
  43.        
  44.         bool held[5]; //The cards that will be held.
  45.         int hand[5]; //The final five chards after dealing.
  46.        
  47.         //Populate the hand wih the first five cards.
  48.         for ( c[1] = 0; c[1] < 5; C[1] ++ ) {
  49.             hand[ c[1] ] = deal[ [c[1] ];
  50.         }
  51.        
  52.         int handbuf[5]; //Buffer to use for initial hand checking, to do preliminary display
  53.         //if hand prior to discarding.
  54.        
  55.         for ( c[1] = 0; c[1] < 5; c[1] ++ ) { handbuf[ c[1] ] = hand[ c[1] ]; //Copy to buffer.
  56.            
  57.         int handtype = doHandType(handbuf,cards); //Establish ther hand type.
  58.         //This needs a buffer so that we do not use 'hand'until we do a discard phase.
  59.        
  60.         //We will heck handtype again after doDraw();
  61.            
  62.        
  63.                
  64.                
  65.        
  66.     }
  67.    
  68.     int doHandType(int hand, int cards, int counts, int min_pair){
  69.         bool pryle; int pairs; bool onepair; bool twopairs;
  70.         bool quads;  bool flush; bool straight;
  71.         bool broadway; bool fullhouse;
  72.         bool straightflush;
  73.         bool royal;
  74.        
  75.         int hand_type;
  76.  
  77.         flush = isFlush(hand,cards);
  78.         straight = isStraight(hand,cards);
  79.         broadway = isBroadway(hand,cards);
  80.         if ( straight & flush ) straightflush = true;
  81.         if ( broadway && flush ) royal = true;
  82.        
  83.         if ( !flush && !straight && !broadway ) {
  84.             hand_type = handRand(hand, cards, counts);
  85.             if ( hand_type == HT_ONEPAIR && min_pair > 0 ) {
  86.                 if ( !minPair(counts, min_pair) ) return 0;
  87.             }
  88.         }
  89.         if ( royal ) return HT_ROYAL;
  90.         if ( straightflush ) return HT_STRAIGHTFLUSH;
  91.         if ( flush ) return HT_FLUSH;
  92.         if ( straight ) return HT_STRAIGHT;
  93.        
  94.         else return hand_type; //Quads, full house, trips, two pair,
  95.                     //and one pair if ( the pair is > min_pair ) or
  96.                     //there is no min pair.
  97.        
  98.         //if ( quads ) return HT_QUADS;
  99.         //if ( fullhouse ) return HT_FULLHOUSE;
  100.         //if ( pryle ) return HT_TRIPS;
  101.         //if ( twopairs ) return HT_TWOPAIR;
  102.         //if ( onepair ) return HT_ONEPAIR;
  103.         return 0; //Dead hand.
  104.     }
  105.        
  106.     bool isStraight(int hand, int cards){
  107.  
  108.         int sorted[5];
  109.        
  110.         //store numeric values from cards[]
  111.         for ( int q = 0; q < 5; q++ ) {
  112.             sorted[q] = cards[ hand[q] ]; //The numeric values.
  113.         }
  114.  
  115.         //Now, sort the array in a descending manner.
  116.         sortArray(cards, 5);
  117.         //!
  118.            
  119.         bool match;
  120.        
  121.         for ( int q = 0; q < 4; q++ ) { //Not all five.
  122.             if ( sorted[q+1] == sorted[q] -1 ) { match = true };
  123.             else match = false;
  124.         }
  125.         return match;
  126.        
  127.     }
  128.     bool isBroadway(int hand, int cards) { //A-10 Straight
  129.         int sorted[5];
  130.        
  131.         //store numeric values from cards[]
  132.         for ( int q = 0; q < 5; q++ ) {
  133.             sorted[q] = cards[ hand[q] ]; //The numeric values.
  134.         }
  135.  
  136.         //Now, sort the array in a descending manner.
  137.         sortArray(cards, 5);
  138.         //!
  139.            
  140.         bool match;
  141.        
  142.         for ( int q = 0; q < 3; q++ ) { //Check the first four, and read ito the fourth only.
  143.             if ( sorted[q+1] == sorted[q] -1 ) { match = true };
  144.             else match = false;
  145.         }
  146.         if ( sorted[5] != 1 ) { match = false; } //it must be an Ace.
  147.         return match;
  148.        
  149.     }
  150.     bool isFlush(int hand, int suits) {
  151.         int suit;
  152.         bool match;
  153.         suit = suits[ hand[0] ];
  154.         for ( int q = 1; q < 5; q++ ) {
  155.             if ( suits[ hand[q] ] == suit ) { match = true; }
  156.             else match = false;
  157.         }
  158.         return match;
  159.        
  160.     }
  161.    
  162.     //Hand ranks using matched cards.
  163.     int handRank(int counts){
  164.         int pairs;
  165.         bool pryle;
  166.         bool quads;
  167.         bool fullhouse;
  168.         for ( int q = 0; q < 13; q++ ) {
  169.             if ( counts[q] == 4 ) quads = true;
  170.             if ( counts[q] == 3 ) pryle = true;
  171.             if ( counts[q] == 2 ) pairs++;
  172.         }
  173.         if ( pryle && pairs == 1 ) return HT_FULLHOUSE;
  174.         if ( pryle && pairs == 0 ) return HT_TRIPS;
  175.         if ( !pryle && pairs == 2 ) return HT_TWOPAIR;
  176.         if ( !pryle && pairs == 1 ) return HT_ONEPAIR;
  177.         return 0;
  178.     }
  179.    
  180.     //If only one pair, checks that the pair is at at least the minimum type.
  181.     bool minPair(int counts, int highcard){
  182.         int card;
  183.         for ( card = 0; card < 13; card++ ) {
  184.             if ( counts[q] == 2 ) { break; }
  185.         }
  186.         if ( card >= (highcard -1) ) return true;
  187.         return false;
  188.     }
  189.        
  190.     /*
  191.     bool isQuads(int counts){
  192.         for ( int q = 0; q < 13; q++ ) {
  193.             if ( counts[q] == 4 ) return true;
  194.         }
  195.         return false;
  196.     }
  197.    
  198.     bool isFullHouse(int counts){
  199.         bool pair;
  200.         bool pryle;
  201.         for ( int q = 0; q < 13; q++ ) {
  202.             if ( counts[q] == 2 ) pair = true;
  203.             if ( counts[q] == 3 ) pryle = true;
  204.         }
  205.         if ( pair && pryle ) return true;
  206.         return false;
  207.     }
  208.    
  209.     bool isPryle(int counts){
  210.         for ( int q = 0;
  211.        
  212.        
  213.     //It is a straight flush if both isStraight and isFluah return true, and isBroadway does not.
  214.     //it is a royal flush if both isBroadway and isFlush return true.
  215.     bool isQuads(int hand, int cards) { //Four cards have the same numeric value
  216.         int sorted[5]; int matched; bool m;
  217.        
  218.         //store numeric values from cards[]
  219.         for ( int q = 0; q < 5; q++ ) {
  220.             sorted[q] = cards[ hand[q] ]; //The numeric values.
  221.         }
  222.  
  223.         //Now, sort the array in a descending manner.
  224.         sortArray(cards, 5);
  225.         //!
  226.        
  227.        
  228.         //! Match the five cards against one-another.
  229.        
  230.         //Chect the first two cards to see if they match.
  231.         if ( cards[ hand[0] ] == cards[ hand[1] ] ) { m = true; }//Try matching from the front.
  232.         //else {  } //match fron the back.
  233.  
  234.         if ( m ) {
  235.             for ( int q = 0; q < 4; q++ ) {
  236.                 if ( cards[ hand[q] ] == cards[ hand[q+1] ] ) { match++; }
  237.             }
  238.         }
  239.         else {
  240.             for ( int q = 4; q >= 0; q-- ) {
  241.                 if ( cards[ hand[q] ] == cards[ hand[q-1] ] ) { match++; }
  242.             }
  243.         }
  244.        
  245.         return ( match == 4 );
  246.        
  247.     }
  248.        
  249.     //Three cards have the same numeric value
  250.     bool isPryle() {
  251.         int sorted[5]; int matched; bool m;
  252.        
  253.         //store numeric values from cards[]
  254.         for ( int q = 0; q < 5; q++ ) {
  255.             sorted[q] = cards[ hand[q] ]; //The numeric values.
  256.         }
  257.  
  258.         //Now, sort the array in a descending manner.
  259.         sortArray(cards, 5);
  260.         //!
  261.        
  262.        
  263.         //! Match the five cards against one-another.
  264.        
  265.         //Chect the first two cards to see if they match.
  266.         if ( cards[ hand[0] ] == cards[ hand[1] ] ) { m = true; cardval = cards[ hand[0] ]; }//Try matching from the front.
  267.         else { cardval = cards[hand[4]]; } //match fron the back.
  268.  
  269.         if ( m ) {
  270.             for ( int q = 0; q < 4; q++ ) {
  271.                 if ( cards[ hand[q] ] == cards[ hand[q+1] ] ) { match++; }
  272.             }
  273.         }
  274.         else {
  275.             for ( int q = 4; q >= 0; q-- ) {
  276.                 if ( cards[ hand[q] ] == cards[ hand[q-1] ] ) { match++; }
  277.             }
  278.         }
  279.        
  280.         for ( int q = 0; q < 5; q++ ) {
  281.             if ( cards[ hand[q] ] == cardval ) { hand[q] = -1; } //Mark the cards used to form the pryle
  282.             //so that they are not counted in pair checking.
  283.         }
  284.         return ( match == 3 );
  285.        
  286.     }
  287.        
  288.     //if !isQuads && !isPryle Two cards have the same numeric value
  289.     int isPair(int hand, int cards) {
  290.         int pairs;
  291.         int sorted[5];
  292.         int matched; bool m; int q;
  293.        
  294.         //store numeric values from cards[]
  295.         for ( q = 0; q < 5; q++ ) {
  296.             sorted[q] = cards[ hand[q] ]; //The numeric values.
  297.         }
  298.  
  299.         //Now, sort the array in a descending manner.
  300.         sortArray(cards, 5);
  301.         //!
  302.        
  303.         //Check the cards against one another.
  304.         for ( q = 0; q < 5; q++ ) {
  305.             for ( int w = 1; w < 5; w++ ) {
  306.                 if ( hand[q] != -1 ) { //&& hand[w] != -1 ) { //If we have not checked thse cards.
  307.                     if ( cards[ hand[q] ] == cards[ hand[w] ] ) {
  308.                         matched++;
  309.                         hand[q] = -1; //Flag abd remove the matched pair.
  310.                         hand[w] = -1;
  311.                         continue;
  312.                     }
  313.                 }
  314.             }
  315.         }
  316.         return pairs;
  317.        
  318.     }
  319.    
  320.     //isPair returns the number of pairs, so isTwoPair is superfluous.
  321.     bool isTwoPair() { } //If isPair for two numeric values and not isQuads.
  322.     //It is a full house if both isPryle and isPair return true for two values and isQuads retrns false.
  323.    
  324.     */
  325.    
  326.    
  327.     void doOdds(int handtype){
  328.         //int odds[] = {    ODDS_NOPAIR, ODDS_PAIR, ODDS_TWOPAIR, ODDS_TRIPS, ODDS_STRAIGHT, ODDS_FLUSH,
  329.         //      ODDS_FULLHOUSE, ODDS_QUADS, ODDS_STRAIGHTFLUSH, ODDS_ROYAL } ;
  330.        
  331.         int payout[] = { 0, 1, 2, 3, 4, 6, 9, 25, 50, 800 };
  332.         return payout[handtype];
  333.     }
  334.  
  335.     //Draw the cards. Populates the un-held slots in the present hand with the preselected cards.
  336.     void draw(int deal, int held, int hand ) {
  337.         for ( int q = 0; q < 5; q++ ) {
  338.             if ( !held[q] ) {
  339.                 hand[q] = deal[q+5];
  340.             }
  341.         }
  342.     }
  343.    
  344.     void sortArray(int array, int size) {
  345.         if (size < 2) {return;}
  346.  
  347.         for (int i = 1; i < size; ++i) {
  348.             int j = i;
  349.             int j_val = array[j];
  350.             while (j > 0) {
  351.                 if (array[j - 1] <= j_val) {break;}
  352.                 int temp = array[j - 1];
  353.                 array[j - 1] = array[j];
  354.                 array[j] = temp;
  355.                 --j;
  356.             }
  357.         }
  358.     }
  359.    
  360.     void doCounts(int cards, int hand, int counts) {
  361.         int pairs = 0;
  362.         for (int i = 0; i < 5; ++i) {
  363.             int rank = cards[hand[i]];
  364.             counts[rank - 1]++;
  365.             //if (counts[rank - 1] % 2 == 0) pairs++;
  366.         }
  367.         //return pairs;
  368.     }
  369.    
  370.    
  371. }
  372.  
  373. int countPairs(int cards, int hand) {
  374.  int counts[13];
  375.  int pairs = 0;
  376.  for (int i = 0; i < 5; ++i) {
  377.   int rank = cards[hand[i]];
  378.   counts[rank - 1]++;
  379.   if (counts[rank - 1] % 2 == 0) pairs++;
  380.  }
  381.  return pairs;
  382. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement