Advertisement
ZoriaRPG

ZScript: Video Poker

May 12th, 2017
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.70 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){
  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.         flush = isFlush(hand,cards);
  76.         straight = isStraight(hand,cards);
  77.         broadway = isBroadway(hand,cards);
  78.         if ( straight & flush ) straightflush = true;
  79.         if ( broadway && flush ) royal = true;
  80.        
  81.         if ( !flush && !straight && !broadway ) {
  82.             quads = isQuads(hand,cards);
  83.             if ( !quads ) {
  84.                 pryle = isPryle(hand,cards);
  85.                 //we need to mark the three matching cards in the pryle so that they are not kater counted as pairs.
  86.                 pairs = isPair(hand,cards);
  87.                 if ( pryle && pairs == 1 ) { fullhouse = true; }
  88.                 if ( !pryle ) {
  89.                     if ( pairs == 2 ) { twopair = true; }
  90.                     else if ( pairs == 1 ) { onepair = true; }
  91.                 }
  92.             }
  93.         }
  94.         if ( royal ) return HT_ROYAL;
  95.         if ( straightflush ) return HT_STRAIGHTFLUSH;
  96.         if ( flush ) return HT_FLUSH;
  97.         if ( straight ) return HT_STRAIGHT;
  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;
  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.     //It is a straight flush if both isStraight and isFluah return true, and isBroadway does not.
  162.     //it is a royal flush if both isBroadway and isFlush return true.
  163.     bool isQuads(int hand, int cards) { //Four cards have the same numeric value
  164.         int sorted[5]; int matched; bool m;
  165.        
  166.         //store numeric values from cards[]
  167.         for ( int q = 0; q < 5; q++ ) {
  168.             sorted[q] = cards[ hand[q] ]; //The numeric values.
  169.         }
  170.  
  171.         //Now, sort the array in a descending manner.
  172.         sortArray(cards, 5);
  173.         //!
  174.        
  175.        
  176.         //! Match the five cards against one-another.
  177.        
  178.         //Chect the first two cards to see if they match.
  179.         if ( cards[ hand[0] ] == cards[ hand[1] ] ) { m = true; }//Try matching from the front.
  180.         //else {  } //match fron the back.
  181.  
  182.         if ( m ) {
  183.             for ( int q = 0; q < 4; q++ ) {
  184.                 if ( cards[ hand[q] ] == cards[ hand[q+1] ] ) { match++; }
  185.             }
  186.         }
  187.         else {
  188.             for ( int q = 4; q >= 0; q-- ) {
  189.                 if ( cards[ hand[q] ] == cards[ hand[q-1] ] ) { match++; }
  190.             }
  191.         }
  192.        
  193.         return ( match == 4 );
  194.        
  195.     }
  196.        
  197.     //Three cards have the same numeric value
  198.     bool isPryle() {
  199.         int sorted[5]; int matched; bool m;
  200.        
  201.         //store numeric values from cards[]
  202.         for ( int q = 0; q < 5; q++ ) {
  203.             sorted[q] = cards[ hand[q] ]; //The numeric values.
  204.         }
  205.  
  206.         //Now, sort the array in a descending manner.
  207.         sortArray(cards, 5);
  208.         //!
  209.        
  210.        
  211.         //! Match the five cards against one-another.
  212.        
  213.         //Chect the first two cards to see if they match.
  214.         if ( cards[ hand[0] ] == cards[ hand[1] ] ) { m = true; cardval = cards[ hand[0] ]; }//Try matching from the front.
  215.         else { cardval = cards[hand[4]]; } //match fron the back.
  216.  
  217.         if ( m ) {
  218.             for ( int q = 0; q < 4; q++ ) {
  219.                 if ( cards[ hand[q] ] == cards[ hand[q+1] ] ) { match++; }
  220.             }
  221.         }
  222.         else {
  223.             for ( int q = 4; q >= 0; q-- ) {
  224.                 if ( cards[ hand[q] ] == cards[ hand[q-1] ] ) { match++; }
  225.             }
  226.         }
  227.        
  228.         for ( int q = 0; q < 5; q++ ) {
  229.             if ( cards[ hand[q] ] == cardval ) { hand[q] = -1; } //Mark the cards used to form the pryle
  230.             //so that they are not counted in pair checking.
  231.         }
  232.         return ( match == 3 );
  233.        
  234.     }
  235.        
  236.     //if !isQuads && !isPryle Two cards have the same numeric value
  237.     int isPair(int hand, int cards) {
  238.         int pairs;
  239.         int sorted[5];
  240.         int matched; bool m; int q;
  241.        
  242.         //store numeric values from cards[]
  243.         for ( q = 0; q < 5; q++ ) {
  244.             sorted[q] = cards[ hand[q] ]; //The numeric values.
  245.         }
  246.  
  247.         //Now, sort the array in a descending manner.
  248.         sortArray(cards, 5);
  249.         //!
  250.        
  251.         //Check the cards against one another.
  252.         for ( q = 0; q < 5; q++ ) {
  253.             for ( int w = 1; w < 5; w++ ) {
  254.                 if ( hand[q] != -1 ) { //&& hand[w] != -1 ) { //If we have not checked thse cards.
  255.                     if ( cards[ hand[q] ] == cards[ hand[w] ] ) {
  256.                         matched++;
  257.                         hand[q] = -1; //Flag abd remove the matched pair.
  258.                         hand[w] = -1;
  259.                         continue;
  260.                     }
  261.                 }
  262.             }
  263.         }
  264.         return pairs;
  265.        
  266.     }
  267.    
  268.     //isPair returns the number of pairs, so isTwoPair is superfluous.
  269.     bool isTwoPair() { } //If isPair for two numeric values and not isQuads.
  270.     //It is a full house if both isPryle and isPair return true for two values and isQuads retrns false.
  271.    
  272.     void doOdds(int handtype){
  273.         //int odds[] = {    ODDS_NOPAIR, ODDS_PAIR, ODDS_TWOPAIR, ODDS_TRIPS, ODDS_STRAIGHT, ODDS_FLUSH,
  274.         //      ODDS_FULLHOUSE, ODDS_QUADS, ODDS_STRAIGHTFLUSH, ODDS_ROYAL } ;
  275.        
  276.         int payout[] = { 0, 1, 2, 3, 4, 6, 9, 25, 50, 800 };
  277.         return payout[handtype];
  278.     }
  279.  
  280.     //Draw the cards. Populates the un-held slots in the present hand with the preselected cards.
  281.     void draw(int deal, int held, int hand ) {
  282.         for ( int q = 0; q < 5; q++ ) {
  283.             if ( !held[q] ) {
  284.                 hand[q] = deal[q+5];
  285.             }
  286.         }
  287.     }
  288.    
  289.     void sortArray(int array, int size) {
  290.         if (size < 2) {return;}
  291.  
  292.         for (int i = 1; i < size; ++i) {
  293.             int j = i;
  294.             int j_val = array[j];
  295.             while (j > 0) {
  296.                 if (array[j - 1] <= j_val) {break;}
  297.                 int temp = array[j - 1];
  298.                 array[j - 1] = array[j];
  299.                 array[j] = temp;
  300.                 --j;
  301.             }
  302.         }
  303.     }
  304.    
  305.     void doCounts(int cards, int hand, int counts) {
  306.         int pairs = 0;
  307.         for (int i = 0; i < 5; ++i) {
  308.             int rank = cards[hand[i]];
  309.             counts[rank - 1]++;
  310.             //if (counts[rank - 1] % 2 == 0) pairs++;
  311.         }
  312.         //return pairs;
  313.     }
  314.    
  315.    
  316. }
  317.  
  318. int countPairs(int cards, int hand) {
  319.  int counts[13];
  320.  int pairs = 0;
  321.  for (int i = 0; i < 5; ++i) {
  322.   int rank = cards[hand[i]];
  323.   counts[rank - 1]++;
  324.   if (counts[rank - 1] % 2 == 0) pairs++;
  325.  }
  326.  return pairs;
  327. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement