Advertisement
ZoriaRPG

ZScript: Video Poker v3

May 12th, 2017
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 11.06 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(int min_pair, int win_sfx, int lose_sfx, int holdvcard_sfx, int draw_sfx, int counter){
  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.         bool held[5]; //The cards that will be held.
  33.         int hand[5]; //The final five chards after dealing.
  34.         bool cardanims[5]; //holds what cards to 'flip'.
  35.         int handbuf[5]; //Buffer to use for initial hand checking, to do preliminary display
  36.         //if hand prior to discarding.
  37.        
  38.         //dealCards();
  39.         nextHand(hand, handbuf, count, held, deal, delt, cardanims, c); //Do all hand arrangements.
  40.        
  41.         int handtype = doHandType(handbuf,cards,counts,min_pair); //Establish ther hand type.
  42.         //This needs a buffer so that we do not use 'hand'until we do a discard phase.
  43.        
  44.         //We will check handtype again after doDraw();
  45.        
  46.         bool running = true;
  47.         int s_hand_rank[40]; //String buffer for hand rank.
  48.            
  49.         int coins; //present number of coins.
  50.         int betline; //number of bets
  51.         int s_coind[7]; //string to hold ItoA for coins
  52.         int s_bets[2]; //String to hold ItoA for bets.
  53.            
  54.         c[3] = getHandRankString(s_hand_rank, handtype); //Put the hand rank string into the buffer.
  55.             //c[3] holds the buffer pointer.
  56.        
  57.         bool draw;  
  58.            
  59.         while(running){
  60.             bool waiting = true;
  61.             //bool betting = true;
  62.            
  63.            
  64.             //DIsplay the cards.
  65.            
  66.             //Display the current hand rank.
  67.            
  68.             //! Keys
  69.             /// 1-5 hold or unhold card 1 to 5
  70.             /// b = set bet amount
  71.             /// d = draw
  72.             while(waiting) { //Waitinf on input
  73.                
  74.                 displayBetline();
  75.                
  76.                 //Allow player to hold cards.
  77.                
  78.                 if ( KeyPress[KEY_1] ) { if ( holdcard_sfx ) Game->PlaySound(holdcard_sfx); held[0] = !held[0]; cardanims[0] = held[0]; }
  79.                 if ( KeyPress[KEY_2] ) { if ( holdcard_sfx ) Game->PlaySound(holdcard_sfx); held[1] = !held[1]; cardanims[1] = held[1]; }
  80.                 if ( KeyPress[KEY_3] ) { if ( holdcard_sfx ) Game->PlaySound(holdcard_sfx); held[2] = !held[2]; cardanims[2] = held[2]; }
  81.                 if ( KeyPress[KEY_4] ) { if ( holdcard_sfx ) Game->PlaySound(holdcard_sfx); held[3] = !held[3]; cardanims[3] = held[3]; }
  82.                 if ( KeyPress[KEY_5] ) { if ( holdcard_sfx ) Game->PlaySound(holdcard_sfx); held[4] = !held[4]; cardanims[4] = held[4]; }
  83.                
  84.                 if ( KeyPress[KEY_D] ) { if ( draw_sfx) Game->PlaySound(draw_sfx); draw = true; waiting = false;  break; }
  85.                
  86.                 Waitframe();
  87.             }
  88.            
  89.             //Do the draw
  90.            
  91.             //Check what cards are held, and replace un-held cards.
  92.                
  93.             if ( draw ) {
  94.                 draw = false;
  95.                 int display_timer = 300;
  96.                 bool wait_nextgame;
  97.                 doDraw(deal, hand, held); //Update the hand.
  98.                 //Update hand evaluations.
  99.                 handtype = doHandType(hand,cards,counts,min_pair);
  100.                 c[3] = getHandRankString(s_hand_rank, handtype);
  101.                
  102.                 //!do animation
  103.                 //bool cardanims[5]; //holds what cards to 'flip'.
  104.                 //flip animated cards to reveal new cards
  105.                 //clear cardanims[]
  106.                
  107.                 //Show payout.
  108.                
  109.                 int pay = doOdds(betline); //Calcus gthe payout.
  110.                
  111.                
  112.                 //Make sound.
  113.                
  114.                 if ( pay > 0 && win_sfx ) Game->PlaySound(win_sfx);
  115.                 if ( pay <= 0 && lose_sfx ) Game->PlaySound(lose_sfx);
  116.                
  117.                 //Award the money.
  118.                
  119.                 Game->DCounter[counter] += pay; //Might not want a counter for this.
  120.                
  121.                 while( display_timer-- ) {
  122.                     //Show strings.
  123.                     Waitframe();
  124.                 }
  125.                
  126.                 //Pay out
  127.                
  128.                 wait_nextgame = true;
  129.                
  130.                 //Show Next Game Options
  131.                 while ( wait_nextgame ) {
  132.                     //Allow changing the bet amount, or exiting.
  133.                    
  134.                     if ( KeyPress[KEY_1] ) betline = 1;
  135.                     if ( KeyPresss[KEY_2] ) betline = 2;
  136.                     if ( KeyPress[KEY_3] ) betline = 3;
  137.                     if ( KeyPress[KEY_4] ) betline = 4;
  138.                     if ( KeyPress[KEY_5] ) betline = 5;
  139.                    
  140.                     displayBetline(); //Show the present bet value.
  141.                    
  142.                     //Press key to begin new game.
  143.                     Waitframe();
  144.                 }  
  145.                
  146.             }
  147.            
  148.        
  149.             Waitframe();
  150.         }
  151.        
  152.                
  153.                
  154.        
  155.     }
  156.    
  157.     void nextHand(int hand, int handbuf, int count, bool held, int deal, bool delt, bool cardanims, int c){
  158.        
  159.         //wipe delt
  160.         for ( c[1] = 0; c[1] < 52; c[1]++ ) { delt[ c[1] = false; }
  161.        
  162.         //wipe the pack
  163.         for ( c[1] = 0; c[1] < 10; c[1]++ ) { deal[ c[1] ] = -1;
  164.            
  165.         //clear the count
  166.         for ( c[1] = 0; c[1] < 13; c[1]++ ) { count[ c[1] ] = 0; }
  167.        
  168.         for ( c[1] = 0; c[1] < 5; c[1]++ ) {
  169.             held[ c[1] ] = false; //clear held cards
  170.             cardanims[ c[1] ] = false; //clear card anims
  171.         }
  172.        
  173.         //! New hand
  174.         //Populagte the cards.
  175.         for ( c[1] = 0; c[1] < 10; c[1]++ ) {
  176.             do {
  177.                 c[0] = Rand(0,51);
  178.                
  179.             } while( dealt[ c[0] ] ); //If the card was dealt, choose another.
  180.             deal[ c[q] ] = c[0]; //Put the card in the deal pack.
  181.             dealt[ c[0] ] = true; //Mark the card dealt.
  182.         }
  183.    
  184.         //Populate the hand wih the first five cards.
  185.         for ( c[1] = 0; c[1] < 5; C[1] ++ ) {
  186.             hand[ c[1] ] = deal[ [c[1] ];
  187.         }
  188.        
  189.        
  190.         for ( c[1] = 0; c[1] < 5; c[1] ++ ) { handbuf[ c[1] ] = hand[ c[1] ]; //Copy to buffer.
  191.        
  192.  
  193.         } //refreshes the game for the next hand, retaining the betting values.
  194.        
  195.        
  196.        
  197.     }
  198.     void leaveGame() { } //Exits.
  199.    
  200.     //Fetch a string with the hand rank text into a buffer.
  201.     int getHandRankString(int buf, int handrank){
  202.         int s_nopair[]="High Card";
  203.         int s_onepair[]="One Pair";
  204.         int s_twopair[]="Two Pair";
  205.         int s_trips[]="Three of a Kind";
  206.         int s_straight[]="Straight";
  207.         int s_flush[]="Flush";
  208.         int s_fullhouse[]="Full House";
  209.         int s_quads[]="Four of a Kind";
  210.         int s_straightflush[]="Straight Flush";
  211.         int s_royal[]="Royal Flush";
  212.        
  213.         int strings[10]={   s_nopair, s_onepair, s_twopair, s_trips, s_straight,
  214.                     s_flush, s_fullhouse, s_quads, s_straightflush, s_royal};
  215.        
  216.         int ptr = strings[handrank];
  217.         int sz = SizeOfArray(ptr);
  218.         int q;
  219.         for ( q = 0; q < sz; q++ ) { buf[q] = ptr[q]; }
  220.         buf[q+1] = NULL; //Terminate the string.
  221.         return buf; //Return the pointer to the buffer on success.
  222.     }
  223.    
  224.     int doHandType(int hand, int cards, int counts, int min_pair){
  225.         bool pryle; int pairs; bool onepair; bool twopairs;
  226.         bool quads;  bool flush; bool straight;
  227.         bool broadway; bool fullhouse;
  228.         bool straightflush;
  229.         bool royal;
  230.        
  231.         int hand_type;
  232.  
  233.         flush = isFlush(hand,cards);
  234.         straight = isStraight(hand,cards);
  235.         broadway = isBroadway(hand,cards);
  236.         if ( straight & flush ) straightflush = true;
  237.         if ( broadway && flush ) royal = true;
  238.        
  239.         if ( !flush && !straight && !broadway ) {
  240.             hand_type = handRand(hand, cards, counts);
  241.             if ( hand_type == HT_ONEPAIR && min_pair > 0 ) {
  242.                 if ( !minPair(counts, min_pair) ) return 0;
  243.             }
  244.         }
  245.         if ( royal ) return HT_ROYAL;
  246.         if ( straightflush ) return HT_STRAIGHTFLUSH;
  247.         if ( flush ) return HT_FLUSH;
  248.         if ( straight ) return HT_STRAIGHT;
  249.        
  250.         else return hand_type; //Quads, full house, trips, two pair,
  251.                     //and one pair if ( the pair is > min_pair ) or
  252.                     //there is no min pair.
  253.        
  254.         //if ( quads ) return HT_QUADS;
  255.         //if ( fullhouse ) return HT_FULLHOUSE;
  256.         //if ( pryle ) return HT_TRIPS;
  257.         //if ( twopairs ) return HT_TWOPAIR;
  258.         //if ( onepair ) return HT_ONEPAIR;
  259.         return 0; //Dead hand.
  260.     }
  261.        
  262.     bool isStraight(int hand, int cards){
  263.  
  264.         int sorted[5];
  265.        
  266.         //store numeric values from cards[]
  267.         for ( int q = 0; q < 5; q++ ) {
  268.             sorted[q] = cards[ hand[q] ]; //The numeric values.
  269.         }
  270.  
  271.         //Now, sort the array in a descending manner.
  272.         sortArray(cards, 5);
  273.         //!
  274.            
  275.         bool match;
  276.        
  277.         for ( int q = 0; q < 4; q++ ) { //Not all five.
  278.             if ( sorted[q+1] == sorted[q] -1 ) { match = true };
  279.             else match = false;
  280.         }
  281.         return match;
  282.        
  283.     }
  284.     bool isBroadway(int hand, int cards) { //A-10 Straight
  285.         int sorted[5];
  286.        
  287.         //store numeric values from cards[]
  288.         for ( int q = 0; q < 5; q++ ) {
  289.             sorted[q] = cards[ hand[q] ]; //The numeric values.
  290.         }
  291.  
  292.         //Now, sort the array in a descending manner.
  293.         sortArray(cards, 5);
  294.         //!
  295.            
  296.         bool match;
  297.        
  298.         for ( int q = 0; q < 3; q++ ) { //Check the first four, and read ito the fourth only.
  299.             if ( sorted[q+1] == sorted[q] -1 ) { match = true };
  300.             else match = false;
  301.         }
  302.         if ( sorted[5] != 1 ) { match = false; } //it must be an Ace.
  303.         return match;
  304.        
  305.     }
  306.     bool isFlush(int hand, int suits) {
  307.         int suit;
  308.         bool match;
  309.         suit = suits[ hand[0] ];
  310.         for ( int q = 1; q < 5; q++ ) {
  311.             if ( suits[ hand[q] ] == suit ) { match = true; }
  312.             else match = false;
  313.         }
  314.         return match;
  315.        
  316.     }
  317.    
  318.     //Hand ranks using matched cards.
  319.     int handRank(int counts){
  320.         int pairs;
  321.         bool pryle;
  322.         bool quads;
  323.         bool fullhouse;
  324.         for ( int q = 0; q < 13; q++ ) {
  325.             if ( counts[q] == 4 ) quads = true;
  326.             if ( counts[q] == 3 ) pryle = true;
  327.             if ( counts[q] == 2 ) pairs++;
  328.         }
  329.         if ( pryle && pairs == 1 ) return HT_FULLHOUSE;
  330.         if ( pryle && pairs == 0 ) return HT_TRIPS;
  331.         if ( !pryle && pairs == 2 ) return HT_TWOPAIR;
  332.         if ( !pryle && pairs == 1 ) return HT_ONEPAIR;
  333.         return 0;
  334.     }
  335.    
  336.     //If only one pair, checks that the pair is at at least the minimum type.
  337.     bool minPair(int counts, int highcard){
  338.         int card;
  339.         for ( card = 0; card < 13; card++ ) {
  340.             if ( counts[q] == 2 ) { break; }
  341.         }
  342.         if ( card >= (highcard -1) ) return true;
  343.         return false;
  344.     }
  345.        
  346.     void doOdds(int handtype){
  347.         //int odds[] = {    ODDS_NOPAIR, ODDS_PAIR, ODDS_TWOPAIR, ODDS_TRIPS, ODDS_STRAIGHT, ODDS_FLUSH,
  348.         //      ODDS_FULLHOUSE, ODDS_QUADS, ODDS_STRAIGHTFLUSH, ODDS_ROYAL } ;
  349.        
  350.         int payout[] = { 0, 1, 2, 3, 4, 6, 9, 25, 50, 800 };
  351.         return payout[handtype];
  352.     }
  353.  
  354.     //Draw the cards. Populates the un-held slots in the present hand with the preselected cards.
  355.     void doDraw(int deal, int held, int hand ) {
  356.         for ( int q = 0; q < 5; q++ ) {
  357.             if ( !held[q] ) {
  358.                 hand[q] = deal[q+5];
  359.             }
  360.         }
  361.     }
  362.    
  363.     void sortArray(int array, int size) {
  364.         if (size < 2) {return;}
  365.  
  366.         for (int i = 1; i < size; ++i) {
  367.             int j = i;
  368.             int j_val = array[j];
  369.             while (j > 0) {
  370.                 if (array[j - 1] <= j_val) {break;}
  371.                 int temp = array[j - 1];
  372.                 array[j - 1] = array[j];
  373.                 array[j] = temp;
  374.                 --j;
  375.             }
  376.         }
  377.     }
  378.    
  379.     void doCounts(int cards, int hand, int counts) {
  380.         int pairs = 0;
  381.         for (int i = 0; i < 5; ++i) {
  382.             int rank = cards[hand[i]];
  383.             counts[rank - 1]++;
  384.             //if (counts[rank - 1] % 2 == 0) pairs++;
  385.         }
  386.         //return pairs;
  387.     }
  388.    
  389.    
  390. }
  391. /*
  392. int countPairs(int cards, int hand) {
  393.  int counts[13];
  394.  int pairs = 0;
  395.  for (int i = 0; i < 5; ++i) {
  396.   int rank = cards[hand[i]];
  397.   counts[rank - 1]++;
  398.   if (counts[rank - 1] % 2 == 0) pairs++;
  399.  }
  400.  return pairs;
  401. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement