home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 5 / 05.iso / a / a044 / 3.ddi / POKER / POKER.PRG < prev   
Encoding:
Text File  |  1993-08-31  |  12.3 KB  |  595 lines

  1. ************************************************************************
  2. *  Poker.PRG
  3. *
  4. *  Casino-Style Draw Poker
  5. *
  6. *  Copyright  1992 Computer Associates
  7. *  All rights reserved.
  8. *
  9. ************************************************************************
  10. set procedure to poker
  11.  
  12. #DEFINE WINNING_AMT 100000
  13.  
  14. declare HandString[10]
  15. declare Payoff[10]
  16. declare counts[13]
  17. declare suits[4]
  18. declare groups[4]
  19. declare PDeck[52]
  20. declare flipped[5]
  21.  
  22. HandString[1]  =  "nothing"
  23. Payoff[1]      = -1
  24. HandString[2]  =  "Jacks or better       2 to 1"
  25. Payoff[2]      =  2
  26. HandString[3]  =  "Two pair              3 to 1"
  27. Payoff[3]      =  3
  28. HandString[4]  =  "three of a kind       5 to 1"
  29. Payoff[4]      =  5
  30. HandString[5]  =  "Straight             10 to 1"
  31. Payoff[5]      =  10
  32. HandString[6]  =  "Flush                20 to 1"
  33. Payoff[6]      =  20
  34. HandString[7]  =  "Full house           50 to 1"
  35. Payoff[7]      =  50
  36. HandString[8]  =  "Four of a kind      100 to 1"
  37. Payoff[8]      =  100
  38. HandString[9]  =  "Straight flush      200 to 1"
  39. Payoff[9]      =  200
  40. HandString[10] =  "Royal flush         500 to 1"
  41. Payoff[10]     =  500
  42. CFlag          =  0
  43. score          =  1000
  44. bet            =  10
  45. sBet           = '10'
  46. lastTitle      =  0
  47. PokerMode      =  1
  48. card1          = ''
  49. card2          = ''
  50. card3          = ''
  51. card4          = ''
  52. card5          = ''
  53. cardback       = ''
  54. afill(@flipped, 0)
  55.  
  56. for i = 10 to 12
  57.    Random(seconds()/(9*i*day(date())))
  58. next
  59.  
  60. create window "DrawPoker" from 1,1 to 24,78
  61. set window title to "Draw Poker"
  62.  
  63. **********************
  64. * set up color display
  65. **********************
  66. store sayvideo() to savsay, svsav
  67. store bitand(savsay,240) to bascolor
  68. store bascolor + 4 to color | Red
  69. set say video to color
  70. center("dBFast Casino -- Draw Poker",0,0,78,11)
  71. set say video to svsav
  72.  
  73. do DrawTitles
  74. create Button " Quit " at 21,63
  75. create button " Deal " at 21,6
  76. create control editbox "BET" at 21,40 size 1,8 save to sBet
  77. @ 21,20 say "Cash: $ " + str(score, 7, 0)
  78.  
  79. declare cards[5]
  80. declare cardname[52]
  81.  
  82. **********************
  83. * Load card bmps
  84. **********************
  85. for i = 1 to 52
  86.    cardname[i] = "card" + alltrim(str(i)) + ".bmp"
  87. next
  88. load bitmap back.bmp into cardback
  89.  
  90. sAction = '?'
  91.  
  92. DO while .not. empty(sAction)
  93.  
  94.    nEvent  = GetEvent()
  95.    sAction = TranslateEvent(nEvent)
  96.  
  97.    DO CASE
  98.  
  99.       CASE sAction = ' Deal '
  100.          If OKBet() 
  101.              DO playhand
  102.          endif
  103.  
  104.       CASE sAction = ' Draw '
  105.          if OKBet()
  106.              DO ProcessMode2 with 1
  107.          endif
  108.  
  109.       CASE sAction = ' Quit '
  110. *: Set sAction to empty to EXIT the program
  111.          sAction = ''
  112.  
  113.       CASE sAction = '1st'
  114.          IF PokerMode = 1
  115.             do ProcessMode1 with 11
  116.          ELSE
  117.             do ProcessMode2 with 11
  118.          ENDIF
  119.  
  120.       CASE sAction = '2nd'
  121.          IF PokerMode = 1
  122.             do ProcessMode1 with 12
  123.          ELSE
  124.             do ProcessMode2 with 12
  125.          ENDIF
  126.  
  127.       CASE sAction = '3rd'
  128.          IF PokerMode = 1
  129.             do ProcessMode1 with 13
  130.          ELSE
  131.             do ProcessMode2 with 13
  132.          ENDIF
  133.  
  134.       CASE sAction = '4th'
  135.          IF PokerMode = 1
  136.             do ProcessMode1 with 14
  137.          ELSE
  138.             do ProcessMode2 with 14
  139.          ENDIF
  140.  
  141.       CASE sAction = '5th'
  142.          IF PokerMode = 1
  143.             do ProcessMode1 with 15
  144.          ELSE
  145.             do ProcessMode2 with 15
  146.          ENDIF
  147.  
  148.       OTHERWISE
  149.          sAction = 'unknown'
  150.          
  151.    ENDCASE
  152.  
  153. ENDDO
  154.  
  155. release all
  156. clear gets
  157. ERASE
  158.  
  159. RETURN
  160.  
  161. ************************
  162. FUNCTION GetEvent()
  163. * Get event from Windows
  164. ************************
  165. PRIVATE nTheEvent
  166.  
  167.    nTheEvent = -1
  168.    DO WHILE nTheEvent = -1
  169.       nTheEvent = CHKEVENT()
  170.    ENDDO
  171. *:
  172.    nTheMenu   = HMENU()
  173.    nTheOption = VMENU()
  174.    nTheKey    = LASTKEY()
  175.    sTheWindow = WINDOW()
  176.    sTheButton = BUTTON()
  177.  
  178. RETURN(nTheEvent)
  179.  
  180. **********************************************
  181. FUNCTION TranslateEvent()
  182. * Translate event number and set return code
  183. * see POKER.INC for event number definition
  184. **********************************************
  185. PARAMETER nTheEvent
  186. PRIVATE sTheAction
  187.  
  188.    DO CASE
  189.       CASE nTheEvent = eKybd
  190.          nTheKey    = LASTKEY()
  191.  
  192.          DO CASE
  193.  
  194.             CASE nTheKey = 1553     | Ctrl+F4
  195.                sTheAction = 'Exit'
  196.             otherwise
  197.                   If OKBet()
  198.                       do PlayHand
  199.                       PokerMode = 2
  200.                   endif
  201.                sTheAction = '?'
  202.  
  203.          ENDCASE
  204.  
  205.       CASE nTheEvent = eButton
  206.          sTheAction = button()
  207.  
  208.       CASE nTheEvent = eMClick     |Mouse clicked on the screen
  209.          row = MROW()
  210.          col = MCOL()
  211.          sTheAction = '?'
  212.  
  213.          if col < 15 .AND. col > 5 .AND. row > 11 .AND. row < 18
  214.             sTheAction = '1st'
  215.          endif
  216.          if col < 29 .AND. col > 19 .AND. row > 11 .AND. row < 18
  217.             sTheAction = '2nd'
  218.          endif
  219.          if col < 43 .AND. col > 33 .AND. row > 11 .AND. row < 18
  220.             sTheAction = '3rd'
  221.          endif
  222.          if col < 57 .AND. col > 47 .AND. row > 11 .AND. row < 18
  223.             sTheAction = '4th'
  224.          endif
  225.          if col < 71 .AND. col > 61 .AND. row > 11 .AND. row < 18
  226.             sTheAction = '5th'
  227.          endif
  228.  
  229.       OTHERWISE   
  230.          sTheAction = ""
  231.          
  232.    ENDCASE
  233.  
  234. RETURN(sTheAction)
  235.  
  236. **********************************************
  237. Function HandCompute
  238. **********************************************
  239. private i, straight, flush, retv, ai
  240. private suits, counts, groups, high1, low1, high2, low2
  241.  
  242.    declare counts[13]
  243.    declare suits[4]
  244.    declare groups[4]
  245.  
  246.    afill(@counts, 0)
  247.    afill(@suits, 0)
  248.    afill(@groups, 0)
  249.  
  250.    low1     = 15
  251.    high1    = 0
  252.    low2     = 15
  253.    high2    = 0
  254.    highpair = 0
  255.    straight = 0
  256.    ai       = 0
  257.    i        = 0
  258.    
  259.    FOR i = 1 to 5
  260.       val = int(mod((cards[i] - 1), 13)) + 1
  261.       suit = int((cards[i] - 1)/13) + 1
  262.       counts[val] = counts[val] + 1
  263.       suits[suit] = suits[suit] + 1
  264.       low1        = Min(low1, val)
  265.       high1       = Max(high1, val)
  266.       IF val      = 1
  267.          ai       = 14
  268.       else
  269.          ai       = val
  270.       endif
  271.       low2        = Min(low2, ai)
  272.       high2       = Max(high2, ai)
  273.    NEXT
  274.    ai = 0
  275.  
  276.    FOR i = 1 to 13
  277.       val = counts[i]
  278.       IF val > 0
  279.          groups[val] = groups[val] + 1
  280.          IF val = 2
  281.             if i = 1
  282.                ai = 14
  283.             else
  284.                ai = i
  285.             endif
  286.             highpair = Max(highpair, ai)
  287.          ENDIF
  288.       endif
  289.    NEXT
  290.  
  291.    if ((high1 - low1) = 4) .AND. (groups[1] = 5)
  292.       straight = 1
  293.    endif
  294.    if ((high2 - low2) = 4) .AND. (groups[1] = 5)
  295.       straight = 1
  296.    endif
  297.  
  298.    flush = 0
  299.    retv = 1
  300.  
  301.    FOR i = 1 to 4
  302.       IF suits[i] = 5
  303.          flush = 1
  304.       ENDIF
  305.    NEXT
  306.  
  307.    IF straight = 1 .AND. flush = 1
  308.       IF low2 = 10
  309.          RETV = 10
  310.       ELSE
  311.          RETV = 9
  312.       ENDIF
  313.    ENDIF
  314.  
  315.    IF groups[4] = 1
  316.       RETV = 8
  317.    ENDIF
  318.  
  319.    IF groups[3] = 1
  320.       RETV = 4
  321.    ENDIF
  322.  
  323.    IF groups[2] = 1 .AND. highpair > 10
  324.       RETV = 2
  325.    ENDIF
  326.  
  327.    IF groups[3] = 1 .AND. groups[2] = 1
  328.       RETV = 7
  329.    ENDIF
  330.  
  331.    IF flush = 1
  332.       RETV = 6
  333.    ENDIF
  334.  
  335.    IF straight = 1
  336.       RETV = 5
  337.    ENDIF
  338.  
  339.    IF groups[2] = 2
  340.       RETV = 3
  341.    ENDIF
  342.  
  343. RETURN RETV
  344.  
  345. **********************************************
  346. Procedure ResetCards
  347. **********************************************
  348.  
  349. afill(@PDeck, 0)
  350. afill(@flipped, 0)
  351.  
  352. IF CFlag = -1
  353.    PDeck[1]  = 49
  354.    PDeck[2]  = 52
  355.    PDeck[3]  = 15
  356.    PDeck[4]  = 33
  357.    PDeck[5]  = 50
  358.    PDeck[6]  = 40
  359.    PDeck[7]  = 51
  360.    PDeck[8]  = 19
  361.    PDeck[9]  = 37
  362.    PDeck[10] =  8
  363.    CFlag = 1
  364. ENDIF
  365.  
  366. Return
  367.  
  368. **********************************************
  369. Function RandomCard
  370. **********************************************
  371. private i, stop
  372.  
  373.    IF CFlag > 0
  374.       CFlag = CFlag + 1
  375.       RETURN PDeck[CFlag - 1]
  376.    ENDIF
  377.  
  378.    stop = .t.
  379.  
  380.    do while stop
  381.       i = int(seconds()*Random() / 32726 * 52)
  382.       i=mod(i,52)+1
  383.       IF PDeck[i] = 0
  384.          PDeck[i] = 1
  385.          stop = .f.
  386.       ENDIF
  387.    ENDDO
  388.  
  389. RETURN i
  390.  
  391. **********************************************
  392. Procedure DrawCard
  393. **********************************************
  394. parameter i
  395.  
  396. cardmap = cardname[cards[i]]
  397. if i = 1
  398.    load bitmap &cardmap into card1
  399.    @ 12,6 say card1
  400. endif
  401. if i = 2
  402.    load bitmap &cardmap into card2
  403.    @ 12,20 say card2
  404. endif
  405. if i = 3
  406.    load bitmap &cardmap into card3
  407.    @ 12,34 say card3
  408. endif
  409. if i = 4
  410.    load bitmap &cardmap into card4
  411.    @ 12,48 say card4
  412. endif
  413. if i = 5
  414.    load bitmap &cardmap into card5
  415.    @ 12,62 say card5
  416. endif
  417. Return
  418.  
  419. **********************************************
  420. Procedure DrawCardBack
  421. **********************************************
  422. parameter i
  423. @ 12,14*i-8 say cardback
  424. Return
  425.  
  426. **********************************************
  427. Procedure UpdateTitles
  428. **********************************************
  429. parameter n
  430.  
  431. IF n = LastTitle
  432.    return
  433. ENDIF
  434.  
  435. * Set title to black
  436.  
  437. IF LastTitle > 1
  438.    set say video to svsav
  439.    @ LastTitle + 3 - int((LastTitle-1)/6)*5, 6 + int((LastTitle-1)/6)*37 say Handstring[LastTitle]
  440. ENDIF
  441.  
  442. * Set winning title to Red
  443.  
  444. IF n > 1
  445.    store bascolor + 4 to color | Red
  446.    set say video to color
  447.    @ n + 3 - int((n-1)/6)*5, 6 + int((n-1)/6)*37 say Handstring[n]
  448.    set say video to svsav
  449. ENDIF
  450.  
  451. LastTitle = n
  452.  
  453. Return
  454.  
  455. **********************************************
  456. Procedure DrawTitles
  457. **********************************************
  458.  
  459. for i = 2 to 10
  460.    @ i + 3 - int((i-1)/6)*5, 6 + int((i-1)/6)*37 say Handstring[i]
  461. next
  462.  
  463. Return
  464.  
  465. **********************************************
  466. Procedure PlayHand
  467. **********************************************
  468. private i, sel, c, h
  469.  
  470. do UpdateTitles with 0
  471.  
  472. FOR i = 1 to 5
  473.    do DrawCardBack with i
  474. NEXT
  475.  
  476. do ResetCards
  477.  
  478. @ 12,6 clear to 17,76
  479.  
  480. FOR i = 1 to 5
  481.    cards[i] = RandomCard()
  482.    do DrawCard with i
  483. NEXT
  484.  
  485. h = HandCompute()
  486. do UpdateTitles with h
  487.  
  488. close button  " Deal "
  489. create button " Draw " at 21,6
  490. DISABLE CONTROL "BET"
  491.  
  492. @ 2,30 clear to 3,50                && for win/lose message
  493.  
  494. pokerMode = 2
  495.  
  496. Return
  497.  
  498. **********************************************
  499. Procedure ProcessMode2
  500. **********************************************
  501. parameter sel
  502. private i, c, h
  503.  
  504. DO CASE
  505.    CASE sel = 1        |Draw
  506.       FOR i = 1 to 5
  507.          IF flipped[i] = 1
  508.             cards[i] = RandomCard()
  509.             do DrawCard with i
  510.          ENDIF
  511.       NEXT i
  512.       h = HandCompute()
  513.       do UpdateTitles with h
  514.       afill(@PDeck, 0)
  515.  
  516.       score = score + Payoff[h] * val(sBet)
  517.       store sayvideo() to SaveSay
  518.       If Payoff[h]=-1
  519.         set say video to 15*16+12
  520.           @ 2,30 say "You Lose" font 15
  521.       else
  522.         set say video to 15*16+2
  523.         @ 2,30 say "You Win" font 15
  524.       endif
  525.       set say video to SaveSay
  526.       @ 21,20 clear to 21,27
  527.       @ 21,20 say "Cash: $ " + str(score, 7, 0)
  528.       If WinOrLose()
  529.           quit
  530.       endif
  531.       close  button " Draw "
  532.       create button " Deal " at 21,6
  533.       ENABLE CONTROL "BET"
  534.  
  535.       IF CFlag > 1
  536.          CFlag = 0
  537.       ENDIF
  538.  
  539.       PokerMode = 1
  540.  
  541.    CASE sel > 10 .and. sel < 16    | Cards
  542.       c = sel - 10
  543.       IF flipped[c] = 1
  544.          do DrawCard with c
  545.          flipped[c] = 0
  546.       ELSE
  547.          do DrawCardBack with c
  548.          flipped[c] = 1
  549.       ENDIF
  550.  
  551. ENDCASE
  552.  
  553. Return
  554.  
  555. **********************************************
  556. Procedure ProcessMode1
  557. **********************************************
  558. parameter sel
  559.  
  560. DO CASE
  561.    CASE sel = 1       |Deal
  562.         IF OKBet()
  563.             do PlayHand
  564.             PokerMode=2
  565.         endif
  566. ENDCASE
  567.  
  568. Return
  569.  
  570. **************
  571. Function OKBet
  572. **************
  573. If val(sBet)>score
  574.     warning("ERROR","You cannot bet more than you have.",384+1)
  575.     SELECT CONTROL "BET"
  576.     return(.f.)
  577. elseif val(sBet)<1
  578.     warning("ERROR","You must bet at least 1 coin.",384+1)
  579.     SELECT CONTROL "BET"
  580.     return(.f.)
  581. endif
  582. return(.t.)
  583.  
  584. ******************
  585. Function WinOrLose
  586. ******************
  587. If Score <=0 
  588.     warning("YOU LOSE","You have lost all of your money.  Better luck next time.",384+1)
  589.     return(.t.)
  590. elseif Score>=WINNING_AMT
  591.     warning("YOU WIN!!!","Congratulations! You have broken the bank!",384+3)
  592.     return(.t.)
  593. endif
  594. return(.f.)
  595.