Advertisement
ZoriaRPG

ZScript: Video Poker v4

May 12th, 2017
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 14.01 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. const int MAX_SHUFFLE_FRAMES = 600;
  12.        
  13. //String Display
  14.  
  15. const int BETS_LAYER        = 2;
  16. const int BETS_X        = 5;
  17. const int BETS_Y        = -16;
  18. const int BETS_FONT     = 1;
  19. const int BETS_COLOUR       = 1;
  20. const int BETS_BG_COLOUR    = 0;
  21. const int BETS_FORMAT       = 0;
  22.  
  23. //Card Display (Text)
  24.  
  25. //Suit Colours
  26.  
  27. const int CARD_SUIT_HEARTS_COLOUR   = 1;
  28. const int CARD_SUIT_DIAMONDS_COLOUR     = 1;
  29. const int CARD_SUIT_SPADES_COLOUR   = 1;
  30. const int CARD_SUIT_CLUBS_COLOUR    = 1;
  31.  
  32. const int VP_CARD_LAYER         = 5;
  33. const int VPUI_FONT         = 0; //General UI font and colour.
  34. const int VPUI_HOLD_COLOUR      = 1;
  35. const int VPUI_HOLD_BG_COLOUR       = 0;
  36.  
  37.  
  38.  
  39. ffc script VideoPoker{
  40.     void run(int min_pair, int win_sfx, int lose_sfx, int holdvcard_sfx, int draw_sfx, int counter){
  41.         //c[5] used for fault timer
  42.        
  43.         int fault; //If the shuffle becomes stuck.
  44.        
  45.         //Numeric Values
  46.         int cards[52]={1,2,3,4,5,6,7,8,9,10,11,12,13,
  47.             1,2,3,4,5,6,7,8,9,10,11,12,13,
  48.             1,2,3,4,5,6,7,8,9,10,11,12,13,
  49.             1,2,3,4,5,6,7,8,9,10,11,12,13,};
  50.         //Suits
  51.         int suits[52]={ 1,1,1,1,1,1,1,1,1,1,1,1,1,
  52.                 2,2,2,2,2,2,2,2,2,2,2,2,2,
  53.                 3,3,3,3,3,3,3,3,3,3,3,3,3,
  54.                 4,4,4,4,4,4,4,4,4,4,4,4,4 };
  55.         //cards dealt.
  56.         bool dealt[52];
  57.         //five first cards, and five under them.
  58.         int deal[10] = { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1};
  59.         int counts[13]; //Pairs, pryles, quads.
  60.         int c[255];
  61.        
  62.         bool held[5]; //The cards that will be held.
  63.         int hand[5]; //The final five chards after dealing.
  64.         bool cardanims[5]; //holds what cards to 'flip'.
  65.         int handbuf[5]; //Buffer to use for initial hand checking, to do preliminary display
  66.         //if hand prior to discarding.
  67.        
  68.        
  69.         int cardpositions[10]; //X and Y of each of the five cards.
  70.        
  71.         //dealCards();
  72.         fault = nextHand(hand, handbuf, count, held, deal, delt, cardanims, c); //Do all hand arrangements.
  73.        
  74.         if ( fault >= MAX_SHUFFLE_FRAMES ) {
  75.             //do machine error
  76.         }
  77.        
  78.         int handtype = doHandType(handbuf,cards,counts,min_pair); //Establish ther hand type.
  79.         //This needs a buffer so that we do not use 'hand'until we do a discard phase.
  80.        
  81.         //We will check handtype again after doDraw();
  82.        
  83.         bool running = true;
  84.         int s_hand_rank[40]; //String buffer for hand rank.
  85.            
  86.         int coins; //present number of coins.
  87.         int betline; //number of bets
  88.         int s_coind[7]; //string to hold ItoA for coins
  89.         int s_bets[2]; //String to hold ItoA for bets.
  90.            
  91.         c[3] = getHandRankString(s_hand_rank, handtype); //Put the hand rank string into the buffer.
  92.             //c[3] holds the buffer pointer.
  93.        
  94.         bool draw;  
  95.        
  96.        
  97.    
  98.         while(running){
  99.             bool waiting = true;
  100.             //bool betting = true;
  101.            
  102.            
  103.             //DIsplay the cards.
  104.            
  105.             //Display the current hand rank.
  106.            
  107.             //! Keys
  108.             /// 1-5 hold or unhold card 1 to 5
  109.             /// b = set bet amount
  110.             /// d = draw
  111.            
  112.            
  113.             //!
  114.            
  115.             //DISPLAY THE CARDS
  116.            
  117.             //DISPLAY WHICH ARE HELD
  118.            
  119.             //! This might be better as an if statement.
  120.             while(waiting) { //Waitinf on input
  121.                
  122.                 displayBetline(); //display the present number of bets.
  123.                
  124.                 //Allow player to hold cards.
  125.                
  126.                 if ( KeyPress[KEY_1] ) { if ( holdcard_sfx ) Game->PlaySound(holdcard_sfx); held[0] = !held[0]; cardanims[0] = held[0]; }
  127.                 if ( KeyPress[KEY_2] ) { if ( holdcard_sfx ) Game->PlaySound(holdcard_sfx); held[1] = !held[1]; cardanims[1] = held[1]; }
  128.                 if ( KeyPress[KEY_3] ) { if ( holdcard_sfx ) Game->PlaySound(holdcard_sfx); held[2] = !held[2]; cardanims[2] = held[2]; }
  129.                 if ( KeyPress[KEY_4] ) { if ( holdcard_sfx ) Game->PlaySound(holdcard_sfx); held[3] = !held[3]; cardanims[3] = held[3]; }
  130.                 if ( KeyPress[KEY_5] ) { if ( holdcard_sfx ) Game->PlaySound(holdcard_sfx); held[4] = !held[4]; cardanims[4] = held[4]; }
  131.                
  132.                 if ( KeyPress[KEY_D] ) { if ( draw_sfx) Game->PlaySound(draw_sfx); draw = true; waiting = false;  break; }
  133.                
  134.                 drawHeld(cardpositions, held); //Draw 'HOLD'string at card locations.
  135.                
  136.                 Waitframe();
  137.             }
  138.            
  139.             //Do the draw
  140.            
  141.             //Check what cards are held, and replace un-held cards.
  142.                
  143.             if ( draw ) {
  144.                 draw = false;
  145.                 int display_timer = 300;
  146.                 bool wait_nextgame;
  147.                 doDraw(deal, hand, held); //Update the hand.
  148.                 //Update hand evaluations.
  149.                 handtype = doHandType(hand,cards,counts,min_pair);
  150.                 c[3] = getHandRankString(s_hand_rank, handtype);
  151.                
  152.                 //!do animation
  153.                 //bool cardanims[5]; //holds what cards to 'flip'.
  154.                 //flip animated cards to reveal new cards
  155.                 //clear cardanims[]
  156.                
  157.                 //Show payout.
  158.                
  159.                 int pay = doOdds(betline); //Calculates the payout.
  160.                
  161.                
  162.                 //Make sound.
  163.                
  164.                 if ( pay > 0 && win_sfx ) Game->PlaySound(win_sfx);
  165.                 if ( pay <= 0 && lose_sfx ) Game->PlaySound(lose_sfx);
  166.                
  167.                 //Award the money.
  168.                
  169.                 Game->DCounter[counter] += pay; //Might not want a counter for this.
  170.                
  171.                 while( display_timer-- ) {
  172.                     //Show strings.
  173.                     Waitframe();
  174.                 }
  175.                
  176.                 //Pay out
  177.                
  178.                 wait_nextgame = true;
  179.                
  180.                 //Show Next Game Options
  181.                 while ( wait_nextgame ) {
  182.                     //Allow changing the bet amount, or exiting.
  183.                    
  184.                     if ( KeyPress[KEY_1] ) { betline = 1; setBets(betline, s_bets); }
  185.                     if ( KeyPress[KEY_2] ) { betline = 2; setBets(betline, s_bets); }
  186.                     if ( KeyPress[KEY_3] ) { betline = 3; setBets(betline, s_bets); }
  187.                     if ( KeyPress[KEY_4] ) { betline = 4; setBets(betline, s_bets); }
  188.                     if ( KeyPress[KEY_5] ) { betline = 5; setBets(betline, s_bets); }
  189.                    
  190.                    
  191.                    
  192.                     displayBetline(); //Show the present bet value.
  193.                    
  194.                     //Press key to begin new game.
  195.                     Waitframe();
  196.                 }  
  197.                 fault = nextHand(hand, handbuf, count, held, deal, delt, cardanims, c); //Do all hand arrangements.
  198.        
  199.                 if ( fault >= MAX_SHUFFLE_FRAMES ) {
  200.                     //do machine error
  201.                 }
  202.                
  203.             }
  204.            
  205.        
  206.             Waitframe();
  207.         }
  208.        
  209.                
  210.                
  211.        
  212.     }
  213.    
  214.     void drawHeld(cardpositions, held){
  215.         int s_hold[]="HOLD";
  216.         for ( int q = 0; q < 10; q+= 2 ){
  217.             //carspositions q = x, q+1 = y
  218.             Screen->DrawString(VP_CARD_LAYER+1, cardpositions[q], cardpositions[q+1], VPUI_FONT, VPUI_HOLD_COLOUR,
  219.             VPUI_HOLD_BG_COLOUR, 0, s_hold, OP_OPAQUE);
  220.         }
  221.     }
  222.    
  223.     int nextHand(int hand, int handbuf, int count, bool held, int deal, bool delt, bool cardanims, int c){
  224.        
  225.         c[5] = 0; //fault timer.
  226.        
  227.         //wipe delt
  228.         for ( c[1] = 0; c[1] < 52; c[1]++ ) { delt[ c[1] = false; }
  229.        
  230.         //wipe the pack
  231.         for ( c[1] = 0; c[1] < 10; c[1]++ ) { deal[ c[1] ] = -1;
  232.            
  233.         //clear the count
  234.         for ( c[1] = 0; c[1] < 13; c[1]++ ) { count[ c[1] ] = 0; }
  235.        
  236.         for ( c[1] = 0; c[1] < 5; c[1]++ ) {
  237.             held[ c[1] ] = false; //clear held cards
  238.             cardanims[ c[1] ] = false; //clear card anims
  239.         }
  240.        
  241.         //! New hand
  242.         //Populagte the cards.
  243.         for ( c[1] = 0; c[1] < 10; c[1]++ ) {
  244.             do {
  245.                 c[0] = Rand(0,51);
  246.                 c[5]++; //fault timer
  247.                
  248.                 //If we take too long to shuffe, raise a fault.
  249.                 if ( c[5] > MAX_SHUFFLE_FRAMES ) { return c[5]; }
  250.                
  251.             } while( dealt[ c[0] ] ); //If the card was dealt, choose another.
  252.             deal[ c[q] ] = c[0]; //Put the card in the deal pack.
  253.             dealt[ c[0] ] = true; //Mark the card dealt.
  254.         }
  255.    
  256.         //Populate the hand wih the first five cards.
  257.         for ( c[1] = 0; c[1] < 5; C[1] ++ ) {
  258.             hand[ c[1] ] = deal[ [c[1] ];
  259.         }
  260.        
  261.        
  262.         for ( c[1] = 0; c[1] < 5; c[1] ++ ) { handbuf[ c[1] ] = hand[ c[1] ]; //Copy to buffer.
  263.        
  264.  
  265.         } //refreshes the game for the next hand, retaining the betting values.
  266.         return 0;
  267.        
  268.        
  269.     }
  270.     int leaveGame() {
  271.         int s_leavegame[]="Äre you sure that you wish to quit?";
  272.         int surety;
  273.         //Are you sure?
  274.        
  275.         if ( KeyPress[KEY_Y] ) surety = 1;
  276.         if ( KeyPress[KEY_N] ) surety = -1;
  277.         return surety;
  278.     }
  279.    
  280.     void setBets(int bets, int s_bets){
  281.         ItoA(bets,s_bets);
  282.     }
  283.        
  284.     void displayBetline(int bets, int s_bets){
  285.         Screen->DrawString(BETS_LAYER, BETS_X, BETS_Y, BETS_FONT, BETS_COLOUR, BETS_BG_COLOUR, BETS_FORMAT,
  286.         s_bets, OP_OPAQUE);
  287.     }
  288.    
  289.     //Fetch a string with the hand rank text into a buffer.
  290.     int getHandRankString(int buf, int handrank){
  291.         int s_nopair[]="High Card";
  292.         int s_onepair[]="One Pair";
  293.         int s_twopair[]="Two Pair";
  294.         int s_trips[]="Three of a Kind";
  295.         int s_straight[]="Straight";
  296.         int s_flush[]="Flush";
  297.         int s_fullhouse[]="Full House";
  298.         int s_quads[]="Four of a Kind";
  299.         int s_straightflush[]="Straight Flush";
  300.         int s_royal[]="Royal Flush";
  301.        
  302.         int strings[10]={   s_nopair, s_onepair, s_twopair, s_trips, s_straight,
  303.                     s_flush, s_fullhouse, s_quads, s_straightflush, s_royal};
  304.        
  305.         int ptr = strings[handrank];
  306.         int sz = SizeOfArray(ptr);
  307.         int q;
  308.         for ( q = 0; q < sz; q++ ) { buf[q] = ptr[q]; }
  309.         buf[q+1] = NULL; //Terminate the string.
  310.         return buf; //Return the pointer to the buffer on success.
  311.     }
  312.    
  313.     int doHandType(int hand, int cards, int counts, int min_pair){
  314.         bool pryle; int pairs; bool onepair; bool twopairs;
  315.         bool quads;  bool flush; bool straight;
  316.         bool broadway; bool fullhouse;
  317.         bool straightflush;
  318.         bool royal;
  319.        
  320.         int hand_type;
  321.  
  322.         flush = isFlush(hand,cards);
  323.         straight = isStraight(hand,cards);
  324.         broadway = isBroadway(hand,cards);
  325.         if ( straight & flush ) straightflush = true;
  326.         if ( broadway && flush ) royal = true;
  327.        
  328.         if ( !flush && !straight && !broadway ) {
  329.             hand_type = handRand(hand, cards, counts);
  330.             if ( hand_type == HT_ONEPAIR && min_pair > 0 ) {
  331.                 if ( !minPair(counts, min_pair) ) return 0;
  332.             }
  333.         }
  334.         if ( royal ) return HT_ROYAL;
  335.         if ( straightflush ) return HT_STRAIGHTFLUSH;
  336.         if ( flush ) return HT_FLUSH;
  337.         if ( straight ) return HT_STRAIGHT;
  338.        
  339.         else return hand_type; //Quads, full house, trips, two pair,
  340.                     //and one pair if ( the pair is > min_pair ) or
  341.                     //there is no min pair.
  342.        
  343.         //if ( quads ) return HT_QUADS;
  344.         //if ( fullhouse ) return HT_FULLHOUSE;
  345.         //if ( pryle ) return HT_TRIPS;
  346.         //if ( twopairs ) return HT_TWOPAIR;
  347.         //if ( onepair ) return HT_ONEPAIR;
  348.         return 0; //Dead hand.
  349.     }
  350.        
  351.     bool isStraight(int hand, int cards){
  352.  
  353.         int sorted[5];
  354.        
  355.         //store numeric values from cards[]
  356.         for ( int q = 0; q < 5; q++ ) {
  357.             sorted[q] = cards[ hand[q] ]; //The numeric values.
  358.         }
  359.  
  360.         //Now, sort the array in a descending manner.
  361.         sortArray(cards, 5);
  362.         //!
  363.            
  364.         bool match;
  365.        
  366.         for ( int q = 0; q < 4; q++ ) { //Not all five.
  367.             if ( sorted[q+1] == sorted[q] -1 ) { match = true };
  368.             else match = false;
  369.         }
  370.         return match;
  371.        
  372.     }
  373.     bool isBroadway(int hand, int cards) { //A-10 Straight
  374.         int sorted[5];
  375.        
  376.         //store numeric values from cards[]
  377.         for ( int q = 0; q < 5; q++ ) {
  378.             sorted[q] = cards[ hand[q] ]; //The numeric values.
  379.         }
  380.  
  381.         //Now, sort the array in a descending manner.
  382.         sortArray(cards, 5);
  383.         //!
  384.            
  385.         bool match;
  386.        
  387.         for ( int q = 0; q < 3; q++ ) { //Check the first four, and read ito the fourth only.
  388.             if ( sorted[q+1] == sorted[q] -1 ) { match = true };
  389.             else match = false;
  390.         }
  391.         if ( sorted[5] != 1 ) { match = false; } //it must be an Ace.
  392.         return match;
  393.        
  394.     }
  395.     bool isFlush(int hand, int suits) {
  396.         int suit;
  397.         bool match;
  398.         suit = suits[ hand[0] ];
  399.         for ( int q = 1; q < 5; q++ ) {
  400.             if ( suits[ hand[q] ] == suit ) { match = true; }
  401.             else match = false;
  402.         }
  403.         return match;
  404.        
  405.     }
  406.    
  407.     //Hand ranks using matched cards.
  408.     int handRank(int counts){
  409.         int pairs;
  410.         bool pryle;
  411.         bool quads;
  412.         bool fullhouse;
  413.         for ( int q = 0; q < 13; q++ ) {
  414.             if ( counts[q] == 4 ) quads = true;
  415.             if ( counts[q] == 3 ) pryle = true;
  416.             if ( counts[q] == 2 ) pairs++;
  417.         }
  418.         if ( pryle && pairs == 1 ) return HT_FULLHOUSE;
  419.         if ( pryle && pairs == 0 ) return HT_TRIPS;
  420.         if ( !pryle && pairs == 2 ) return HT_TWOPAIR;
  421.         if ( !pryle && pairs == 1 ) return HT_ONEPAIR;
  422.         return 0;
  423.     }
  424.    
  425.     //If only one pair, checks that the pair is at at least the minimum type.
  426.     bool minPair(int counts, int highcard){
  427.         int card;
  428.         for ( card = 0; card < 13; card++ ) {
  429.             if ( counts[q] == 2 ) { break; }
  430.         }
  431.         if ( card >= (highcard -1) ) return true;
  432.         return false;
  433.     }
  434.        
  435.     void doOdds(int handtype){
  436.         //int odds[] = {    ODDS_NOPAIR, ODDS_PAIR, ODDS_TWOPAIR, ODDS_TRIPS, ODDS_STRAIGHT, ODDS_FLUSH,
  437.         //      ODDS_FULLHOUSE, ODDS_QUADS, ODDS_STRAIGHTFLUSH, ODDS_ROYAL } ;
  438.        
  439.         int payout[] = { 0, 1, 2, 3, 4, 6, 9, 25, 50, 800 };
  440.         return payout[handtype];
  441.     }
  442.  
  443.     //Draw the cards. Populates the un-held slots in the present hand with the preselected cards.
  444.     void doDraw(int deal, int held, int hand ) {
  445.         for ( int q = 0; q < 5; q++ ) {
  446.             if ( !held[q] ) {
  447.                 hand[q] = deal[q+5];
  448.             }
  449.         }
  450.     }
  451.    
  452.     void sortArray(int array, int size) {
  453.         if (size < 2) {return;}
  454.  
  455.         for (int i = 1; i < size; ++i) {
  456.             int j = i;
  457.             int j_val = array[j];
  458.             while (j > 0) {
  459.                 if (array[j - 1] <= j_val) {break;}
  460.                 int temp = array[j - 1];
  461.                 array[j - 1] = array[j];
  462.                 array[j] = temp;
  463.                 --j;
  464.             }
  465.         }
  466.     }
  467.    
  468.     void doCounts(int cards, int hand, int counts) {
  469.         int pairs = 0;
  470.         for (int i = 0; i < 5; ++i) {
  471.             int rank = cards[hand[i]];
  472.             counts[rank - 1]++;
  473.             //if (counts[rank - 1] % 2 == 0) pairs++;
  474.         }
  475.         //return pairs;
  476.     }
  477.    
  478.     //Shuffles a 52 card array 'deck[]'.
  479.     void shuffle(int deck) {
  480.         int q; int w; int swap;
  481.         for ( q = 51; q > 1; q-- ) {
  482.         x = rand(q+1);
  483.         if ( q == w ) continue;
  484.         swap = deck[q];
  485.         deck[q] = deck[w];
  486.         deck[w] = swap;
  487.     }
  488. }
  489.    
  490.    
  491. }
  492.  
  493. /*
  494. int countPairs(int cards, int hand) {
  495.  int counts[13];
  496.  int pairs = 0;
  497.  for (int i = 0; i < 5; ++i) {
  498.   int rank = cards[hand[i]];
  499.   counts[rank - 1]++;
  500.   if (counts[rank - 1] % 2 == 0) pairs++;
  501.  }
  502.  return pairs;
  503. }
  504.  
  505.  
  506. void shuffle(){
  507. for (int i = 51; i > 0; --i) {
  508.   int j = Rand(i + 1);
  509.   int x = list[i];
  510.   list[i] = list[j];
  511.   list[j] = x;
  512. }
  513. }
  514.  
  515. //Shuffles a 52 card array 'deck[]'.
  516.     void shuffle(int deck) {
  517.         int q; int w; int swap;
  518.         for ( q = 51; q > 1; q-- ) {
  519.         x = rand(q);
  520.         if ( q == w ) continue;
  521.         swap = deck[q];
  522.         deck[q] = deck[w];
  523.         deck[w] = swap;
  524.     }
  525.    
  526. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement