Advertisement
ZoriaRPG

ZScript: Video Poker v7

May 13th, 2017
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 21.20 KB | None | 0 0
  1. //! Video Poker in ZScript
  2.  
  3. //User Interface
  4. const int VPUI_FONT         = 0; //General UI font and colour.
  5. const int VPUI_HOLD_COLOUR      = 0x01; //Whitetru
  6. const int VPUI_HOLD_BG_COLOUR       = 0; //Red, box behind 'HOLD' text.
  7.  
  8. //Felt
  9. const int VPUI_BACKGROUND_COLOUR = 0x0F; //Should be green.
  10. const int VPUI_FELT_WIDTH = 250;
  11. const int VPUI_FELT_HEIGHT = 170;
  12. const int VPUI_FELT_X = 4;
  13. const int VPUI_FELT_Y = 4;
  14. const int VPUI_FELT_SCALE = 1;
  15.  
  16. //Backdrop behind felt.
  17. const int VPUI_COLOUR_BLACK = 0x0F;
  18. const int VPUI_BLACK_BG_SCALE = 1;
  19. const int VPUI_BLACK_BG_X = 0;
  20. const int VPUI_BLACK_BG_Y = 0;
  21. const int VPUI_BLACK_BG_WIDTH = 255;
  22. const int VPUI_BLACK_BG_HEIGHT = 175;
  23.  
  24. //Card Dimensions and Properties
  25. const int VP_CARD_HOLD_SPACING = 3; //Space between card bottom and 'HOLD' indicator.
  26.  
  27. const int VP_CARD_LAYER = 3; //Layer for drawing card objects to the screen.
  28. const int VP_CARD_WIDTH = 40;
  29. const int VP_CARD_HEIGHT = 60;
  30. const int VP_CARD_SPACING = 8; //Space between cards.
  31. const int VP_CARD_COLOUR = 0x01; //White
  32. const int VP_CARD_SHADOW = 0x0F; //Black
  33. const int VP_CARD_OPACITY = 128; //Opaque
  34. const int VP_CARD_SHADOW_OPACITY = 64; //Trans
  35.  
  36. const int VP_CARD_BASE_X = 10;
  37. const int VP_CARD_BASE_Y = 20;
  38.  
  39. const int VP_CARD_SHADOW_Y_OFFSET = 5;
  40. const int VP_CARD_SHADOW_X_OFFSET = 3;
  41. const int VP_CARD_SCALE = 1;
  42.  
  43. //Card Display (Text)
  44.     //Display of characters over the cards, for 2-10 and A-K-Q-J.
  45. const int VP_CARD_CHAR_X_OFFSET = 18;   //offset from card left/top edges
  46. const int VP_CARD_CHAR_Y_OFFSET = 24;
  47. const int VP_CARD_CHAR_FONT = 1;  //font for glyphs
  48. const int VP_CARD_CHAR_BG_COLOUR = 0; //background colour of glyphs. Normally 0.
  49. const int VP_CARD_CHAR_WIDTH = -1; //Glyph scale: This only works in 2.54+
  50. const int VP_CARD_CHAR_HEIGHT = -1;
  51. const int VP_CARD_CHAR_OPACITY = 128;
  52.  
  53. //Hand Types
  54. const int HT_ROYAL = 9;
  55. const int HT_STRAIGHTFLUSH = 8;
  56. const int HT_FLUSH = 7;
  57. const int HT_STRAIGHT = 6;
  58. const int HT_QUADS = 5;
  59. const int HT_FULLHOUSE = 4;
  60. const int HT_TRIPS = 3;
  61. const int HT_TWOPAIR = 2;
  62. const int HT_ONEPAIR = 1;
  63.  
  64. //Settings
  65. const int MAX_SHUFFLE_FRAMES = 600;
  66.    
  67. //String Display
  68. const int BETS_LAYER        = 4;
  69. const int BETS_X        = 5; //Where to display the number of bets on the UI.
  70. const int BETS_Y        = -16;
  71. const int BETS_FONT     = 1;
  72. const int BETS_COLOUR       = 1;
  73. const int BETS_BG_COLOUR    = 0;
  74. const int BETS_FORMAT       = 0;
  75.  
  76. //Suit Colours
  77. const int CARD_SUIT_HEARTS_COLOUR   = 1; //Red
  78. const int CARD_SUIT_DIAMONDS_COLOUR     = 1; //Blue
  79. const int CARD_SUIT_SPADES_COLOUR   = 1; //Black
  80. const int CARD_SUIT_CLUBS_COLOUR    = 1; //Green
  81.  
  82. const int VP_MIN_PAIR = 11; //Jacks.
  83. const int VP_SFX_WIN = 31;
  84. const int VP_SFX_LOSE = 0;
  85. const int VP_SFX_HOLD = 0;
  86. const int VP_SFX_DRAWCARDS = 0;
  87.  
  88. const int VP_SFX_BET = 0;
  89.  
  90. ffc script VideoPoker{
  91.     void run(int min_pair, int win_sfx, int lose_sfx, int holdcard_sfx, int draw_sfx, int counter, int bet_sfx){
  92.         //c[5] used for fault timer
  93.        
  94.         if ( counter < 1 ) counter = CR_RUPEES; //Default.
  95.         //Defaults
  96.         if ( min_pair < 1 ) min_pair = VP_MIN_PAIR;
  97.         if ( lose_sfx < 1 ) lose_sfx = VP_SFX_LOSE;
  98.         if ( win_sfx < 1 ) win_sfx = VP_SFX_WIN;
  99.         if ( holdcard_sfx < 1 ) holdcard_sfx = VP_SFX_HOLD;
  100.         if ( bet_sfx < 1 ) bet_sfx = VP_SFX_BET;
  101.        
  102.         min_pair = min_pair << 0; //Truncate for safety.
  103.        
  104.         //Numeric Values
  105.         int cards[52]={1,2,3,4,5,6,7,8,9,10,11,12,13,
  106.             1,2,3,4,5,6,7,8,9,10,11,12,13,
  107.             1,2,3,4,5,6,7,8,9,10,11,12,13,
  108.             1,2,3,4,5,6,7,8,9,10,11,12,13,};
  109.         //Suits
  110.         int suits[52]={ 1,1,1,1,1,1,1,1,1,1,1,1,1,
  111.                 2,2,2,2,2,2,2,2,2,2,2,2,2,
  112.                 3,3,3,3,3,3,3,3,3,3,3,3,3,
  113.                 4,4,4,4,4,4,4,4,4,4,4,4,4 };
  114.         //cards dealt.
  115.         bool dealt[52];
  116.         //five first cards, and five under them.
  117.         int deal[10] = { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1};
  118.         int counts[13]; //Pairs, pryles, quads.
  119.         int c[255];
  120.        
  121.         bool held[5]; //The cards that will be held.
  122.         int hand[5]; //The final five chards after dealing.
  123.         bool cardanims[5]; //holds what cards to 'flip'.
  124.         int handbuf[5]; //Buffer to use for initial hand checking, to do preliminary display
  125.         //if hand prior to discarding.
  126.        
  127.         //X and Y of each of the five cards.
  128.         int cardpositions[5] = {
  129.            
  130.             VP_CARD_BASE_X,
  131.             VP_CARD_BASE_X+((VP_CARD+WIDTH+VP_CARD_SPACING)*1),
  132.             VP_CARD_BASE_X+((VP_CARD+WIDTH+VP_CARD_SPACING)*2),
  133.             VP_CARD_BASE_X+((VP_CARD+WIDTH+VP_CARD_SPACING)*3),
  134.             VP_CARD_BASE_X+((VP_CARD+WIDTH+VP_CARD_SPACING)*4)};
  135.            
  136.  
  137.         int s_hand_rank[40]; //String buffer for hand rank.
  138.            
  139.         int coins; //present number of coins.
  140.         int betline = -1; //number of bets
  141.         int s_coind[7]; //string to hold ItoA for coins
  142.         int s_bets[2]; //String to hold ItoA for bets.
  143.        
  144.         bool draw; bool running;
  145.        
  146.         int handtype = doHandType(handbuf,cards,counts,min_pair); //Establish ther hand type.
  147.         //This needs a buffer so that we do not use 'hand'until we do a discard phase.
  148.        
  149.         //We will check handtype again after doDraw();
  150.        
  151.         c[3] = getHandRankString(s_hand_rank, handtype); //Put the hand rank string into the buffer.
  152.             //c[3] holds the buffer pointer.
  153.            
  154.         bool firstdeal = true;  
  155.            
  156.         while ( firstdeal ) {
  157.             //Initial bet entry.
  158.            
  159.             drawVPUI(VPUI_FELT_X, VPUI_FELT_Y, VPUI_FELT_WIDTH, VPUI_FELT_HEIGHT, VPUI_BACKGROUND_COLOUR, VPUI_FELT_SCALE);  
  160.  
  161.             displayBetline(); //display the present number of bets.
  162.            
  163.             if ( KeyPress[KEY_1] ) { betline = 1; setBets(betline, s_bets); }
  164.             if ( KeyPress[KEY_2] ) { betline = 2; setBets(betline, s_bets); }
  165.             if ( KeyPress[KEY_3] ) { betline = 3; setBets(betline, s_bets); }
  166.             if ( KeyPress[KEY_4] ) { betline = 4; setBets(betline, s_bets); }
  167.             if ( KeyPress[KEY_5] ) { betline = 5; setBets(betline, s_bets); }
  168.            
  169.             //Press b for next hand.
  170.             if ( KeyPress[KEY_B] ) { firstdeal = false; break; }
  171.            
  172.             Waitframe();
  173.         }
  174.        
  175.        
  176.         running = ( doBet(betline, counter, bet_sfx) && getCards(deck,hand) ); //Shuffle deck and load cards into hand[].
  177.        
  178.         while(running){
  179.             bool waiting = true;
  180.             //bool betting = true;
  181.            
  182.            
  183.             //DIsplay the cards.
  184.            
  185.             //Display the current hand rank.
  186.            
  187.             //! Keys
  188.             /// 1-5 hold or unhold card 1 to 5
  189.             /// b = set bet amount
  190.             /// d = draw
  191.            
  192.            
  193.             //!Display the UI
  194.             drawVPUI(VPUI_FELT_X, VPUI_FELT_Y, VPUI_FELT_WIDTH, VPUI_FELT_HEIGHT, VPUI_BACKGROUND_COLOUR, VPUI_FELT_SCALE);
  195.            
  196.             //DISPLAY THE CARDS
  197.             drawCards(cardpositions, hand, cards, suits); //Display the cards
  198.            
  199.             //DISPLAY WHICH ARE HELD
  200.             drawHeld(cardpositions, held);
  201.            
  202.             //! This might be better as an if statement.
  203.             while(waiting) { //Waiting on input
  204.                
  205.                 drawVPUI(VPUI_FELT_X, VPUI_FELT_Y, VPUI_FELT_WIDTH, VPUI_FELT_HEIGHT, VPUI_BACKGROUND_COLOUR, VPUI_FELT_SCALE);
  206.                
  207.                 displayBetline(); //display the present number of bets.
  208.                 drawCards(cardpositions, hand, cards, suits); //Display the cards.
  209.                
  210.                 //Allow player to hold cards.
  211.                
  212.                 if ( KeyPress[KEY_1] ) { if ( holdcard_sfx ) Game->PlaySound(holdcard_sfx); held[0] = !held[0]; cardanims[0] = held[0]; }
  213.                 if ( KeyPress[KEY_2] ) { if ( holdcard_sfx ) Game->PlaySound(holdcard_sfx); held[1] = !held[1]; cardanims[1] = held[1]; }
  214.                 if ( KeyPress[KEY_3] ) { if ( holdcard_sfx ) Game->PlaySound(holdcard_sfx); held[2] = !held[2]; cardanims[2] = held[2]; }
  215.                 if ( KeyPress[KEY_4] ) { if ( holdcard_sfx ) Game->PlaySound(holdcard_sfx); held[3] = !held[3]; cardanims[3] = held[3]; }
  216.                 if ( KeyPress[KEY_5] ) { if ( holdcard_sfx ) Game->PlaySound(holdcard_sfx); held[4] = !held[4]; cardanims[4] = held[4]; }
  217.                
  218.                 if ( KeyPress[KEY_D] ) { if ( draw_sfx) Game->PlaySound(draw_sfx); draw = true; waiting = false;  break; }
  219.                
  220.                 drawHeld(cardpositions, held); //Draw 'HOLD'string at card locations.
  221.                
  222.                 Waitframe();
  223.             }
  224.            
  225.             //Do the draw
  226.             //!Update cards
  227.            
  228.             //future location of fancy animation.
  229.            
  230.             //Check what cards are held, and replace un-held cards.
  231.                
  232.             if ( draw ) {
  233.                 draw = false;
  234.                 int display_timer = 300;
  235.                 bool wait_nextgame;
  236.                 doDraw(deal, hand, held); //Update the hand.
  237.                 //Update hand evaluations.
  238.                 handtype = doHandType(hand,cards,counts,min_pair);
  239.                 c[3] = getHandRankString(s_hand_rank, handtype);
  240.                
  241.                 //!do animation
  242.                 //bool cardanims[5]; //holds what cards to 'flip'.
  243.                 //flip animated cards to reveal new cards
  244.                 //clear cardanims[]
  245.                
  246.                 //Show payout.
  247.                
  248.                 int pay = doOdds(betline); //Calculates the payout.
  249.                
  250.                
  251.                 //Make sound.
  252.                
  253.                 if ( pay > 0 && win_sfx ) Game->PlaySound(win_sfx);
  254.                 if ( pay <= 0 && lose_sfx ) Game->PlaySound(lose_sfx);
  255.                
  256.                 //Award the money.
  257.                
  258.                 Game->DCounter[counter] += pay; //Might not want a counter for this.
  259.                
  260.                 while( display_timer-- ) {
  261.                    
  262.                     drawVPUI(VPUI_FELT_X, VPUI_FELT_Y, VPUI_FELT_WIDTH, VPUI_FELT_HEIGHT, VPUI_BACKGROUND_COLOUR, VPUI_FELT_SCALE);
  263.                
  264.                     displayBetline(); //display the present number of bets.
  265.                    
  266.                     //Show strings.
  267.                     Waitframe();
  268.                 }
  269.                
  270.                 //Pay out
  271.                
  272.                 wait_nextgame = true;
  273.                
  274.                 //Show Next Game Options
  275.                 while ( wait_nextgame ) {
  276.                     //Allow changing the bet amount, or exiting.
  277.                    
  278.                     if ( KeyPress[KEY_1] ) { betline = 1; setBets(betline, s_bets); }
  279.                     if ( KeyPress[KEY_2] ) { betline = 2; setBets(betline, s_bets); }
  280.                     if ( KeyPress[KEY_3] ) { betline = 3; setBets(betline, s_bets); }
  281.                     if ( KeyPress[KEY_4] ) { betline = 4; setBets(betline, s_bets); }
  282.                     if ( KeyPress[KEY_5] ) { betline = 5; setBets(betline, s_bets); }
  283.                    
  284.                    
  285.                     drawVPUI(VPUI_FELT_X, VPUI_FELT_Y, VPUI_FELT_WIDTH, VPUI_FELT_HEIGHT, VPUI_BACKGROUND_COLOUR, VPUI_FELT_SCALE);
  286.                    
  287.                     displayBetline(); //Show the present bet value.
  288.                    
  289.                     //Press b for next hand.
  290.                     if ( KeyPress[KEY_B] ) { wait_nextgame = false; break; }       
  291.                     //Press key to begin new game.
  292.                     Waitframe();
  293.                 }  
  294.                
  295.                 running = ( doBet(betline, counter, bet_sfx) && getCards(deck,hand) ); //Do next hand arrangement.
  296.                 //!! Lack of short circuiting may cause a problem doing it this way. -Z
  297.                 //!! MAY NEED: running = doBet(); then running = getCards() as separate instructions.
  298.                
  299.                 //fault = nextHand(hand, handbuf, count, held, deal, delt, cardanims, c); //Do all hand arrangements.
  300.                 //if ( fault >= MAX_SHUFFLE_FRAMES ) {
  301.                 //  //do machine error
  302.                 //}
  303.                
  304.             }
  305.            
  306.        
  307.             Waitframe();
  308.         }
  309.        
  310.                
  311.                
  312.        
  313.     }
  314.    
  315.     void drawVPUI(int felt_x, int felt_y, int feltwidth, int feltheight, int feltcolour, int feltscale){
  316.         //Draw a black field
  317.         Screen->Rectangle(1, VPUI_BLACK_BG_X, VPUI_BLACK_BG_Y, VPUI_BLACK_BG_X+VPUI_BLACK_BG_WIDTH,
  318.             VPUI_BLACK_BG_Y+VPUI_BLACK_BG_HEIGHT, VPUI_COLOUR_BLACK, VPUI_BLACK_BG_SCALE, 0,0,0,true, OP_OPAQUE);
  319.         //Draw the felt
  320.         Screen->Rectangle(1, felt_x, felt_y, felt_x+feltwidth, felt_y+feltheight, feltcolour, feltscale, 0,0,0,true, OP_OPAQUE);
  321.         //Text draws go here, later.
  322.        
  323.     }
  324.     //Processes a bet and starts the game.
  325.     bool doBet(int bet, int counter, int sfx) {
  326.         if ( Game->Counter[counter] >= bet ) {
  327.             Game->DCounter -= bet;
  328.             if ( sfx ) Game->PlaySound(sfx);
  329.             return true;
  330.         }
  331.         return false;
  332.     }
  333.            
  334.    
  335.     //Shuffles deck and populates ten-card hand.
  336.     //The hand is selected from a frame of ten cards from the shuffled pack,
  337.     //with the first entity on that frame randomised, and wrapping around if
  338.     //it reaches the array boundary.
  339.         //! This 10-card frame is an extra randomisation factor to minimise digital RNG favourability.
  340.     bool getCards(int deck, int hand){
  341.         int q = Rand(0,51); //Find the entry point of the frame.
  342.         int w = q+10; int h;
  343.         shuffle(deck);
  344.         for ( ; q < w; q++ ) { //Grab a frame of ten cards at random from the shuffled deck.
  345.             if ( q <= 51 ) { hand[h] = deck[q]; h++; } //Populate the hand.
  346.             if ( q >= 52 ) {
  347.                 q -= 52; h-= 52; //Wrap around
  348.                 hand[h] = deck[q]; h++; //Finish populating the hand.
  349.             }
  350.         }
  351.         return true; //boolean so that we can do:
  352.                 //  bool running = getCards(deck,hand);
  353.     }
  354.        
  355.     void drawCards(int cardpositions, int hand, int cards, int suits){
  356.         int q;
  357.         int markings[13]={'A', '2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K'};
  358.         int suitcolours[4] = { CARD_SUIT_SPADES_COLOUR, CARD_SUIT_HEARTS_COLOUR, CARD_SUIT_DIAMONDS_COLOUR, CARD_SUIT_CLUBS_COLOUR };
  359.         for ( q = 0; q < 5; q++ ) {
  360.             //Shadows
  361.             Screen->Rectangle(VP_CARD_LAYER, cardpositions[q]+VP_CARD_SHADOW_X_OFFSET, VP_CARD_BASE_Y+VP_CARD_SHADOW_Y_OFFSET,
  362.                 cardpositions[q]+VP_CARD_WIDTH+VP_CARD_SHADOW_X_OFFSET, VP_CARD_BASE_Y+VP_CARD_HEIGHT+VP_CARD_SHADOW_Y_OFFSET,
  363.                 VP_CARD_SHADOW, VP_CARD_SCALE, 0, 0, 0, true, VP_CARD_SHADOW_OPACITY);
  364.                 //Card background
  365.             Screen->Rectangle(VP_CARD_LAYER, cardpositions[q], VP_CARD_BASE_Y, VP_CARD_BASE_Y+VP_CARD_WIDTH,
  366.                     cardpositions[q+1]+VP_CARD_HEIGHT, VP_CARD_COLOUR, VP_CARD_SCALE, 0, 0, 0, true, VP_CARD_OPACITY);
  367.            
  368.            
  369.             //Card pips and markers.
  370.            
  371.             //Pips
  372.             //...to add.
  373.            
  374.             //Markers
  375.             Screen->DrawCharacter(VP_CARD_LAYER, cardpositions[q]+VP_CARD_CHAR_X_OFFSET, VP_CARD_BASE_Y+VP_CARD_CHAR_Y_OFFSET,
  376.                 VP_CARD_CHAR_FONT, suitcolours[ suits[ hand[q] ] ], VP_CARD_CHAR_BG_COLOUR, VP_CARD_CHAR_WIDTH,
  377.                 VP_CARD_CHAR_HEIGHT, markings[ cards[ hand[q] ] ], VP_CARD_CHAR_OPACITY);
  378.            
  379.         }
  380.     }
  381.  
  382.     void drawHeld(cardpositions, held){
  383.         int s_hold[]="HOLD";
  384.         for ( int q = 0; q < 10; q+= 2 ){
  385.             //carspositions q = x, q+1 = y
  386.             Screen->DrawString(VP_CARD_LAYER+1, cardpositions[q], cardpositions[q+1]+VP_CARD_HEIGHT+VP_CARD_HOLD_SPACING, VPUI_FONT, VPUI_HOLD_COLOUR,
  387.             VPUI_HOLD_BG_COLOUR, 0, s_hold, OP_OPAQUE);
  388.         }
  389.     }
  390.    
  391.     int nextHand(int hand, int handbuf, int count, bool held, int deal, bool delt, bool cardanims, int c){
  392.        
  393.         c[5] = 0; //fault timer.
  394.        
  395.         //wipe delt
  396.         for ( c[1] = 0; c[1] < 52; c[1]++ ) { delt[ c[1] = false; }
  397.        
  398.         //wipe the pack
  399.         for ( c[1] = 0; c[1] < 10; c[1]++ ) { deal[ c[1] ] = -1; }
  400.            
  401.         //clear the count
  402.         for ( c[1] = 0; c[1] < 13; c[1]++ ) { count[ c[1] ] = 0; }
  403.        
  404.         for ( c[1] = 0; c[1] < 5; c[1]++ ) {
  405.             held[ c[1] ] = false; //clear held cards
  406.             cardanims[ c[1] ] = false; //clear card anims
  407.         }
  408.        
  409.         //! New hand
  410.         //Populagte the cards.
  411.         for ( c[1] = 0; c[1] < 10; c[1]++ ) {
  412.             do {
  413.                 c[0] = Rand(0,51);
  414.                 c[5]++; //fault timer
  415.                
  416.                 //If we take too long to shuffe, raise a fault.
  417.                 if ( c[5] > MAX_SHUFFLE_FRAMES ) { return c[5]; }
  418.                
  419.             } while( dealt[ c[0] ] ); //If the card was dealt, choose another.
  420.             deal[ c[q] ] = c[0]; //Put the card in the deal pack.
  421.             dealt[ c[0] ] = true; //Mark the card dealt.
  422.         }
  423.    
  424.         //Populate the hand wih the first five cards.
  425.         for ( c[1] = 0; c[1] < 5; C[1] ++ ) {
  426.             hand[ c[1] ] = deal[ [c[1] ];
  427.         }
  428.        
  429.        
  430.         for ( c[1] = 0; c[1] < 5; c[1] ++ ) { handbuf[ c[1] ] = hand[ c[1] ]; //Copy to buffer.
  431.        
  432.  
  433.         } //refreshes the game for the next hand, retaining the betting values.
  434.         return 0;
  435.        
  436.        
  437.     }
  438.     int leaveGame() {
  439.         int s_leavegame[]="ร„re you sure that you wish to quit?";
  440.         int surety;
  441.         //Are you sure?
  442.        
  443.         if ( KeyPress[KEY_Y] ) surety = 1;
  444.         if ( KeyPress[KEY_N] ) surety = -1;
  445.         return surety;
  446.     }
  447.    
  448.     void setBets(int bets, int s_bets){
  449.         ItoA(bets,s_bets);
  450.     }
  451.        
  452.     void displayBetline(int bets, int s_bets){
  453.         Screen->DrawString(BETS_LAYER, BETS_X, BETS_Y, BETS_FONT, BETS_COLOUR, BETS_BG_COLOUR, BETS_FORMAT,
  454.         s_bets, OP_OPAQUE);
  455.     }
  456.    
  457.     //Fetch a string with the hand rank text into a buffer.
  458.     int getHandRankString(int buf, int handrank){
  459.         int s_nopair[]="High Card";
  460.         int s_onepair[]="One Pair";
  461.         int s_twopair[]="Two Pair";
  462.         int s_trips[]="Three of a Kind";
  463.         int s_straight[]="Straight";
  464.         int s_flush[]="Flush";
  465.         int s_fullhouse[]="Full House";
  466.         int s_quads[]="Four of a Kind";
  467.         int s_straightflush[]="Straight Flush";
  468.         int s_royal[]="Royal Flush";
  469.        
  470.         int strings[10]={   s_nopair, s_onepair, s_twopair, s_trips, s_straight,
  471.                     s_flush, s_fullhouse, s_quads, s_straightflush, s_royal};
  472.        
  473.         int ptr = strings[handrank];
  474.         int sz = SizeOfArray(ptr);
  475.         int q;
  476.         for ( q = 0; q < sz; q++ ) { buf[q] = ptr[q]; }
  477.         buf[q+1] = NULL; //Terminate the string.
  478.         return buf; //Return the pointer to the buffer on success.
  479.     }
  480.    
  481.     int doHandType(int hand, int cards, int counts, int min_pair){
  482.         bool pryle; int pairs; bool onepair; bool twopairs;
  483.         bool quads;  bool flush; bool straight;
  484.         bool broadway; bool fullhouse;
  485.         bool straightflush;
  486.         bool royal;
  487.        
  488.         int hand_type;
  489.  
  490.         flush = isFlush(hand,cards);
  491.         straight = isStraight(hand,cards);
  492.         broadway = isBroadway(hand,cards);
  493.         if ( straight & flush ) straightflush = true;
  494.         if ( broadway && flush ) royal = true;
  495.        
  496.         if ( !flush && !straight && !broadway ) {
  497.             hand_type = handRand(hand, cards, counts);
  498.             if ( hand_type == HT_ONEPAIR && min_pair > 0 ) {
  499.                 if ( !minPair(counts, min_pair) ) return 0;
  500.             }
  501.         }
  502.         if ( royal ) return HT_ROYAL;
  503.         if ( straightflush ) return HT_STRAIGHTFLUSH;
  504.         if ( flush ) return HT_FLUSH;
  505.         if ( straight ) return HT_STRAIGHT;
  506.        
  507.         else return hand_type; //Quads, full house, trips, two pair,
  508.                     //and one pair if ( the pair is > min_pair ) or
  509.                     //there is no min pair.
  510.        
  511.         //if ( quads ) return HT_QUADS;
  512.         //if ( fullhouse ) return HT_FULLHOUSE;
  513.         //if ( pryle ) return HT_TRIPS;
  514.         //if ( twopairs ) return HT_TWOPAIR;
  515.         //if ( onepair ) return HT_ONEPAIR;
  516.         return 0; //Dead hand.
  517.     }
  518.        
  519.     bool isStraight(int hand, int cards){
  520.  
  521.         int sorted[5];
  522.        
  523.         //store numeric values from cards[]
  524.         for ( int q = 0; q < 5; q++ ) {
  525.             sorted[q] = cards[ hand[q] ]; //The numeric values.
  526.         }
  527.  
  528.         //Now, sort the array in a descending manner.
  529.         sortArray(cards, 5);
  530.         //!
  531.            
  532.         bool match;
  533.        
  534.         for ( int q = 0; q < 4; q++ ) { //Not all five.
  535.             if ( sorted[q+1] == sorted[q] -1 ) { match = true };
  536.             else match = false;
  537.         }
  538.         return match;
  539.        
  540.     }
  541.     bool isBroadway(int hand, int cards) { //A-10 Straight
  542.         int sorted[5];
  543.        
  544.         //store numeric values from cards[]
  545.         for ( int q = 0; q < 5; q++ ) {
  546.             sorted[q] = cards[ hand[q] ]; //The numeric values.
  547.         }
  548.  
  549.         //Now, sort the array in a descending manner.
  550.         sortArray(cards, 5);
  551.         //!
  552.            
  553.         bool match;
  554.        
  555.         for ( int q = 0; q < 3; q++ ) { //Check the first four, and read ito the fourth only.
  556.             if ( sorted[q+1] == sorted[q] -1 ) { match = true };
  557.             else match = false;
  558.         }
  559.         if ( sorted[5] != 1 ) { match = false; } //it must be an Ace.
  560.         return match;
  561.        
  562.     }
  563.     bool isFlush(int hand, int suits) {
  564.         int suit;
  565.         bool match;
  566.         suit = suits[ hand[0] ];
  567.         for ( int q = 1; q < 5; q++ ) {
  568.             if ( suits[ hand[q] ] == suit ) { match = true; }
  569.             else match = false;
  570.         }
  571.         return match;
  572.        
  573.     }
  574.    
  575.     //Hand ranks using matched cards.
  576.     int handRank(int counts){
  577.         int pairs;
  578.         bool pryle;
  579.         bool quads;
  580.         bool fullhouse;
  581.         for ( int q = 0; q < 13; q++ ) {
  582.             if ( counts[q] == 4 ) quads = true;
  583.             if ( counts[q] == 3 ) pryle = true;
  584.             if ( counts[q] == 2 ) pairs++;
  585.         }
  586.         if ( pryle && pairs == 1 ) return HT_FULLHOUSE;
  587.         if ( pryle && pairs == 0 ) return HT_TRIPS;
  588.         if ( !pryle && pairs == 2 ) return HT_TWOPAIR;
  589.         if ( !pryle && pairs == 1 ) return HT_ONEPAIR;
  590.         return 0;
  591.     }
  592.    
  593.     //If only one pair, checks that the pair is at at least the minimum type.
  594.     bool minPair(int counts, int highcard){
  595.         int card;
  596.         for ( card = 0; card < 13; card++ ) {
  597.             if ( counts[q] == 2 ) { break; }
  598.         }
  599.         if ( card >= (highcard -1) ) return true;
  600.         return false;
  601.     }
  602.        
  603.     void doOdds(int handtype){
  604.         //int odds[] = {    ODDS_NOPAIR, ODDS_PAIR, ODDS_TWOPAIR, ODDS_TRIPS, ODDS_STRAIGHT, ODDS_FLUSH,
  605.         //      ODDS_FULLHOUSE, ODDS_QUADS, ODDS_STRAIGHTFLUSH, ODDS_ROYAL } ;
  606.        
  607.         int payout[] = { 0, 1, 2, 3, 4, 6, 9, 25, 50, 800 };
  608.         return payout[handtype];
  609.     }
  610.  
  611.     //Draw the cards. Populates the un-held slots in the present hand with the preselected cards.
  612.     void doDraw(int deal, int held, int hand ) {
  613.         for ( int q = 0; q < 5; q++ ) {
  614.             if ( !held[q] ) {
  615.                 hand[q] = deal[q+5];
  616.             }
  617.         }
  618.     }
  619.    
  620.     void sortArray(int array, int size) {
  621.         if (size < 2) {return;}
  622.  
  623.         for (int i = 1; i < size; ++i) {
  624.             int j = i;
  625.             int j_val = array[j];
  626.             while (j > 0) {
  627.                 if (array[j - 1] <= j_val) {break;}
  628.                 int temp = array[j - 1];
  629.                 array[j - 1] = array[j];
  630.                 array[j] = temp;
  631.                 --j;
  632.             }
  633.         }
  634.     }
  635.    
  636.     void doCounts(int cards, int hand, int counts) {
  637.         int pairs = 0;
  638.         for (int i = 0; i < 5; ++i) {
  639.             int rank = cards[hand[i]];
  640.             counts[rank - 1]++;
  641.             //if (counts[rank - 1] % 2 == 0) pairs++;
  642.         }
  643.         //return pairs;
  644.     }
  645.    
  646.     //Shuffles a 52 card array 'deck[]'.
  647.     void shuffle(int deck) {
  648.         int q; int w; int swap;
  649.         for ( q = 51; q > 1; q-- ) {
  650.             x = rand(q+1);
  651.             if ( q == w ) continue;
  652.             swap = deck[q];
  653.             deck[q] = deck[w];
  654.             deck[w] = swap;
  655.         }
  656.     }
  657.    
  658.    
  659. }
  660.  
  661. /*
  662. int countPairs(int cards, int hand) {
  663.  int counts[13];
  664.  int pairs = 0;
  665.  for (int i = 0; i < 5; ++i) {
  666.   int rank = cards[hand[i]];
  667.   counts[rank - 1]++;
  668.   if (counts[rank - 1] % 2 == 0) pairs++;
  669.  }
  670.  return pairs;
  671. }
  672.  
  673.  
  674. void shuffle(){
  675. for (int i = 51; i > 0; --i) {
  676.   int j = Rand(i + 1);
  677.   int x = list[i];
  678.   list[i] = list[j];
  679.   list[j] = x;
  680. }
  681. }
  682.  
  683. //Shuffles a 52 card array 'deck[]'.
  684.     void shuffle(int deck) {
  685.         int q; int w; int swap;
  686.         for ( q = 51; q > 1; q-- ) {
  687.         x = rand(q);
  688.         if ( q == w ) continue;
  689.         swap = deck[q];
  690.         deck[q] = deck[w];
  691.         deck[w] = swap;
  692.     }
  693.    
  694. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement