Advertisement
ZoriaRPG

ZScript: Video Poker v8

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