home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / OL.LZH / PROGS.LZH / SOLIT.ICN < prev    next >
Text File  |  1991-09-05  |  27KB  |  957 lines

  1. ############################################################################
  2. #
  3. #  Name:    solit.icn
  4. #
  5. #  Title:   Play the game of solitaire
  6. #
  7. #  Author:  Jerry Nowlin, with changes by Phillip L. Thomas and Ralph Griswold
  8. #
  9. #  Date:    June 3, 1991
  10. #
  11. ############################################################################
  12. #
  13. #     This program was inspired by a solitaire game that was written
  14. #  by Allyn Wade and copyrighted by him in 1985.  His game was
  15. #  designed for the IBM PC/XT/PCjr with a color or monochrome moni-
  16. #  tor.
  17. #
  18. #     I didn't follow his design exactly because I didn't want to
  19. #  restrict myself to a specific machine.  This program has the
  20. #  correct escape sequences programmed into it to handle several
  21. #  common terminals and PC's.  It's commented well enough that most
  22. #  people can modify the source to work for their hardware.
  23. #
  24. #     These variables must be defined with the correct escape
  25. #  sequences to:
  26. #
  27. #          CLEAR  -  clear the screen
  28. #          CLREOL -  clear to the end of line
  29. #          NORMAL -  turn on normal video for foreground characters
  30. #          RED    -  make the foreground color for characters red
  31. #          BLACK  -  make the foreground color for characters black
  32. #
  33. #  If there is no way to use red and black, the escape sequences
  34. #  should at least make RED and BLACK have different video attri-
  35. #  butes; for example red could have inverse video while black has
  36. #  normal video.
  37. #
  38. #     There are two other places where the code is device dependent.
  39. #  One is in the face() procedure.  The characters used to display
  40. #  the suites of cards can be modified there.  For example, the IBM
  41. #  PC can display actual card face characters while all other
  42. #  machines currently use HDSC for hearts, diamonds, spades and
  43. #  clubs respectively.
  44. #
  45. #     The last, and probably trickiest place is in the movecursor()
  46. #  procedure.  This procedure must me modified to output the correct
  47. #  escape sequence to directly position the cursor on the screen.
  48. #  The comments and 3 examples already in the procedure will help.
  49. #
  50. #     So as not to cast dispersions on Allyn Wade's program, I
  51. #  incorporated the commands that will let you cheat.  They didn't
  52. #  exist in his program.  I also incorporated the auto pilot command
  53. #  that will let the game take over from you at your request and try
  54. #  to win.  I've run some tests, and the auto pilot can win about
  55. #  10% of the games it's started from scratch.  Not great but not
  56. #  too bad.  I can't do much better myself without cheating.  This
  57. #  program is about as totally commented as you can get so the logic
  58. #  behind the auto pilot is fairly easy to understand and modify.
  59. #  It's up to you to make the auto pilot smarter.
  60. #
  61. ############################################################################
  62. #
  63. #  Note:
  64. #
  65. #     The command-line argument, which defaults to support for the VT100,
  66. #  determines the screen driver.  For MS-DOS computers, the ANSI.SYS driver
  67. #  is needed.
  68. #
  69. ############################################################################
  70. #
  71. #  Requires:  keyboard functions
  72. #
  73. ############################################################################
  74.  
  75. global   VERSION, CLEAR, CLREOL, NORMAL, RED, BLACK
  76.  
  77. global   whitespace, amode, seed, deck, over, hidden, run, ace, kbhit
  78.  
  79. procedure main(args)
  80.    local a, p, c, r, s, cnt, cheat, cmd, act, from, dest
  81.  
  82.    VERSION := (!args == ("Atari ST" | "hp2621" | "IBM PC" | "vt100"))
  83.  
  84. #  if keyboard functions are not available, disable ability to
  85. #  get out of auto mode.
  86.  
  87.    if not(&features == "keyboard functions") then kbhit := 2
  88.  
  89.    case VERSION of {
  90.  
  91.       "Atari ST": {
  92.          CLEAR  := "\eE"
  93.          CLREOL := "\eK"
  94.          NORMAL := "\eb3"
  95.          RED    := "\eb1"
  96.          BLACK  := "\eb2"
  97.       }
  98.  
  99.       "hp2621": {
  100.          CLEAR  := "\eH\eJ"
  101.          CLREOL := "\eK"
  102.          NORMAL := "\e&d@"
  103.          RED    := "\e&dJ"
  104.          BLACK  := "\e&d@"
  105.       }
  106.  
  107.       "IBM PC" | "vt100": {
  108.          CLEAR  := "\e[H\e[2J"
  109.          CLREOL := "\e[0K"
  110.          NORMAL := "\e[0m"
  111.          RED    := "\e[0;31;47m"
  112.          BLACK  := "\e[1;30;47m"
  113.       }
  114.  
  115.       default: {  # same as IBM PC and vt100
  116.          CLEAR  := "\e[H\e[2J"
  117.          CLREOL := "\e[0K"
  118.          NORMAL := "\e[0m"
  119.          RED    := "\e[0;31;47m"
  120.          BLACK  := "\e[1;30;47m"
  121.       }
  122.    }
  123.  
  124.    # white space is blanks or tabs
  125.    whitespace := ' \t'
  126.  
  127.    # clear the auto pilot mode flag
  128.    amode := 0
  129.  
  130.    # if a command line argument started with "seed" use the rest of
  131.    # the argument for the random number generator seed value
  132.    if (a := !args)[1:5] == "seed" then seed := integer(a[5:0])
  133.  
  134.    # initialize the data structures
  135.    deck   := shuffle()
  136.    over   := []
  137.    hidden := [[],[],[],[],[],[],[]]
  138.    run    := [[],[],[],[],[],[],[]]
  139.    ace    := [[],[],[],[]]
  140.  
  141.    # lay down the 7 piles of cards
  142.    every p := 1 to 7 do every c := p to 7 do put(hidden[c],get(deck))
  143.  
  144.    # turn over the top of each pile to start a run
  145.    every r := 1 to 7 do put(run[r],get(hidden[r]))
  146.  
  147.    # check for aces in the runs and move them to the ace piles
  148.    every r := 1 to 7 do while getvalue(run[r][1]) = 1 do {
  149.       s := getsuite(!run[r])
  150.       push(ace[s],get(run[r]))
  151.       put(run[r],get(hidden[r]))
  152.    }
  153.  
  154.    # initialize the command and cheat counts
  155.    cnt := cheat := 0
  156.  
  157.    # clear the screen and display the initial layout
  158.    writes(CLEAR)
  159.    display()
  160.  
  161.    # if a command line argument was "auto" let the auto pilot take over
  162.    if !args == "auto" then autopilot(cheat)
  163.  
  164.    # loop reading commands
  165.    repeat {
  166.  
  167.       # increment the command count
  168.       cnt +:= 1
  169.  
  170.       # prompt for a command
  171.       movecursor(15,0)
  172.       writes("cmd:",cnt,"> ",CLREOL)
  173.  
  174.       # scan the command line
  175.       (cmd := read() | exit()) ? {
  176.  
  177.          # parse the one character action
  178.          tab(many(whitespace))
  179.          act := (move(1) | "")
  180.          tab(many(whitespace))
  181.  
  182.          # switch on the action
  183.          case map(act) of {
  184.  
  185.          # turn on the automatic pilot
  186.          "a": autopilot(cheat)
  187.  
  188.          # move a card or run of cards
  189.          "m": {
  190.             if {from := move(1)
  191.                tab(many(whitespace))
  192.                dest := move(1)
  193.                }                                    # Keep failure of parsing
  194.             then {                                  #   from movecard();
  195.                if not movecard(from,dest) then  {   # otherwise, program
  196.                   whoops(cmd)                       #   aborts.
  197.                   next                              # Exit from wrong
  198.                   }                                 #   instruction.
  199.                else if cardsleft() = 0 then
  200.                   finish(cheat)
  201.                      else &null
  202.                }
  203.            else {                                   # Exit from incomplete
  204.                whoops(cmd)                          #  command.
  205.                next
  206.                }
  207.            }
  208.  
  209.          # thumb the deck
  210.          "t" | "": thumb()
  211.  
  212.          # print some help
  213.          "h" | "?": disphelp()
  214.  
  215.          # print the rules of the game
  216.          "r": disprules()
  217.  
  218.          # give up without winning
  219.          "q": break
  220.  
  221.          # shuffle the deck (cheat!)
  222.          "s": {
  223.             deck |||:= over
  224.             over := []
  225.             deck := shuffle(deck)
  226.             display(["deck"])
  227.             cheat +:= 1
  228.          }
  229.  
  230.          # put hidden cards in the deck (cheat!)
  231.          "p": {
  232.             from := move(1) | whoops(cmd)
  233.             if integer(from) &
  234.                from >= 2 & from <= 7 &
  235.                *hidden[from] > 0 then {
  236.                deck |||:= hidden[from]
  237.                hidden[from] := []
  238.                display(["hide","deck"])
  239.                cheat +:= 1
  240.             } else {
  241.                whoops(cmd)
  242.             }
  243.          }
  244.  
  245.          # print the contents of the deck (cheat!)
  246.          "d": {
  247.             movecursor(17,0)
  248.             write(*deck + *over," card", plural(*deck + *over),
  249.                      " in deck:")
  250.             every writes(face(deck[*deck to 1 by -1])," ")
  251.             every writes(face(!over)," ")
  252.             writes("\nHit RETURN")
  253.             read()
  254.             movecursor(17,0)
  255.             every 1 to 4 do write(CLREOL)
  256.             cheat +:= 1
  257.          }
  258.  
  259.          # print the contents of a hidden pile (cheat!)
  260.          "2" | "3" | "4" | "5" | "6" | "7": {
  261.             movecursor(17,0)
  262.             write(*hidden[act]," cards hidden under run ",
  263.                act)
  264.             every writes(face(!hidden[act])," ")
  265.             writes("\nHit RETURN")
  266.             read()
  267.             movecursor(17,0)
  268.             every 1 to 4 do write(CLREOL)
  269.             cheat +:= 1
  270.          }
  271.  
  272.          # they gave an invalid command
  273.          default: whoops(cmd)
  274.  
  275.          } # end of action case
  276.  
  277.       } # end of scan line
  278.  
  279.    } # end of command loop
  280.  
  281.    # a quit command breaks the loop
  282.    movecursor(16,0)
  283.    writes(CLREOL,"I see you gave up")
  284.    if cheat > 0 then
  285.       write("...even after you cheated ",cheat," time", plural(cheat), "!")
  286.    else
  287.       write("...but at least you didn't cheat...congratulations!")
  288.  
  289.    exit(1)
  290.  
  291. end
  292.  
  293. # this procedure moves cards from one place to another
  294.  
  295. procedure movecard(from,dest,limitmove)
  296.  
  297.    # if from and dest are the same fail
  298.    if from == dest then fail
  299.  
  300.    # move a card from the deck
  301.    if from == "d" then {
  302.  
  303.       # to one of the aces piles
  304.       if dest == "a" then {
  305.          return deck2ace()
  306.  
  307.       # to one of the 7 run piles
  308.       } else if integer(dest) & dest >= 1 & dest <= 7 then {
  309.          return deck2run(dest)
  310.       }
  311.  
  312.    # from one of the 7 run piles
  313.    } else if integer(from) & from >= 1 & from <= 7 then {
  314.  
  315.       # to one of the aces piles
  316.       if dest == "a" then {
  317.          return run2ace(from)
  318.  
  319.  
  320.       # to another of the 7 run piles
  321.       } else if integer(dest) & dest >= 1 & dest <= 7 then {
  322.          return run2run(from,dest,limitmove)
  323.       }
  324.    }
  325.  
  326.    # if none of the correct move combinations were found fail
  327.    fail
  328.  
  329. end
  330.  
  331. procedure deck2run(dest)
  332.    local fcard, dcard, s
  333.  
  334.    # set fcard to the top of the overturned pile or fail
  335.    fcard := (over[1] | fail)
  336.  
  337.    # set dcard to the low card of the run or to null if there are no
  338.    # cards in the run
  339.    dcard := (run[dest][-1] | &null)
  340.  
  341.    # check to see if the move is legal
  342.    if chk2run(fcard,dcard) then {
  343.  
  344.       # move the card and update the display
  345.       put(run[dest],get(over))
  346.       display(["deck",dest])
  347.  
  348.       # while there are aces on the top of the overturned pile
  349.       # move them to the aces piles
  350.       while getvalue(over[1]) = 1 do {
  351.          s := getsuite(over[1])
  352.          push(ace[s],get(over))
  353.          display(["deck","ace"])
  354.       }
  355.       return
  356.    }
  357.  
  358. end
  359.  
  360. procedure deck2ace()
  361.    local fcard, a, s
  362.  
  363.    # set fcard to the top of the overturned pile or fail
  364.    fcard := (over[1] | fail)
  365.  
  366.    # for every ace pile
  367.    every a := !ace do {
  368.  
  369.       # if the top of the ace pile is one less than the from card
  370.       # they are in the same suit and in sequence
  371.       if a[-1] + 1 = fcard then {
  372.  
  373.          # move the card and update the display
  374.          put(a,get(over))
  375.          display(["deck","ace"])
  376.  
  377.          # while there are aces on the top of the overturned
  378.          # pile move them to the aces piles
  379.          while getvalue(over[1]) = 1 do {
  380.             s := getsuite(!over)
  381.             push(ace[s],get(over))
  382.             display(["deck","ace"])
  383.          }
  384.          return
  385.       }
  386.    }
  387.  
  388. end
  389.  
  390. procedure run2ace(from)
  391.    local fcard, a, s
  392.  
  393.    # set fcard to the low card of the run or fail if there are no
  394.    # cards in the run
  395.    fcard := (run[from][-1] | fail)
  396.  
  397.    # for every ace pile
  398.    every a := !ace do {
  399.  
  400.       # if the top of the ace pile is one less than the from card
  401.       # they are in the same suit and in sequence
  402.       if a[-1] + 1 = fcard then {
  403.  
  404.          # move the card and update the display
  405.          put(a,pull(run[from]))
  406.          display([from,"ace"])
  407.  
  408.          # if the from run is now empty and there are hidden
  409.          # cards to expose
  410.          if *run[from] = 0 & *hidden[from] > 0 then {
  411.  
  412.             # while there are aces on the top of the
  413.             # hidden pile move them to the aces piles
  414.             while getvalue(hidden[from][1]) = 1 do {
  415.                s := getsuite(hidden[from][1])
  416.                push(ace[s],get(hidden[from]))
  417.                display(["ace"])
  418.             }
  419.  
  420.             # put the top hidden card in the empty run
  421.             # and display the hidden counts
  422.             put(run[from],get(hidden[from]))
  423.             display(["hide"])
  424.          }
  425.  
  426.          # update the from run display
  427.          display([from])
  428.          return
  429.       }
  430.    }
  431.  
  432. end
  433.  
  434. procedure run2run(from,dest,limitmove)
  435.    local fcard, dcard, s
  436.  
  437.    # set fcard to the high card of the run or fail if there are no
  438.    # cards in the run
  439.    fcard := (run[from][1] | fail)
  440.  
  441.    # set dcard to the low card of the run or null if there are no
  442.    # cards in the run
  443.    dcard := (run[dest][-1] | &null)
  444.  
  445.    # avoid king thrashing in automatic mode (there's no point in
  446.    # moving a king high run to an empty run if there are no hidden
  447.    # cards under the king high run to be exposed)
  448.    if amode > 0 & /dcard & getvalue(fcard) = 13 & *hidden[from] = 0 then
  449.       fail
  450.  
  451.    # avoid wasted movement if the limit move parameter was passed
  452.    # (there's no point in moving a pile if there are no hidden cards
  453.    # under it unless you have a king in the deck)
  454.    if amode > 0 & \limitmove & *hidden[from] = 0 then fail
  455.  
  456.    # check to see if the move is legal
  457.    if chk2run(fcard,dcard) then {
  458.  
  459.       # add the from run to the dest run
  460.       run[dest] |||:= run[from]
  461.  
  462.       # empty the from run
  463.       run[from] := []
  464.  
  465.       # display the updated runs
  466.       display([from,dest])
  467.  
  468.       # if there are hidden cards to expose
  469.       if *hidden[from] > 0 then {
  470.  
  471.          # while there are aces on the top of the hidden
  472.          # pile move them to the aces piles
  473.          while getvalue(hidden[from][1]) = 1 do {
  474.             s := getsuite(hidden[from][1])
  475.             push(ace[s],get(hidden[from]))
  476.             display(["ace"])
  477.          }
  478.  
  479.          # put the top hidden card in the empty run and
  480.          # display the hidden counts
  481.          put(run[from],get(hidden[from]))
  482.          display(["hide"])
  483.       }
  484.  
  485.       # update the from run display
  486.       display([from])
  487.       return
  488.    }
  489.  
  490. end
  491.  
  492. procedure chk2run(fcard,dcard)
  493.  
  494.    # if dcard is null the from card must be a king or
  495.    if ( /dcard & (getvalue(fcard) = 13 | fail) ) |
  496.  
  497.    # if the value of dcard is one more than fcard and
  498.       ( getvalue(dcard) - 1 = getvalue(fcard) &
  499.  
  500.    # their colors are different they can be moved
  501.         getcolor(dcard) ~= getcolor(fcard) ) then return
  502.  
  503. end
  504.  
  505. # this procedure finishes a game where there are no hidden cards left and the
  506. # deck is empty
  507.  
  508. procedure finish(cheat)
  509.  
  510.    movecursor(16,0)
  511.    writes("\007I'll finish for you now...\007")
  512.  
  513.    # finish moving the runs to the aces piles
  514.    while movecard(!"7654321","a")
  515.  
  516.    movecursor(16,0)
  517.    writes(CLREOL,"\007You WIN\007")
  518.  
  519.    if cheat > 0 then
  520.       write("...but you cheated ", cheat, " time", plural(cheat), "!")
  521.    else
  522.       write("...and without cheating...congratulations!")
  523.  
  524.    exit(0)
  525.  
  526. end
  527.  
  528. # this procedure takes over and plays the game for you
  529.  
  530. procedure autopilot(cheat)
  531.    local tseq, totdeck
  532.  
  533.    movecursor(16,0)
  534.    writes("Going into automatic mode...")
  535.    if proc(kbhit) then writes( " [Press any key to return.]")
  536.    writes(CLREOL)
  537.  
  538.    # set auto pilot mode
  539.    amode := 1
  540.  
  541.    # while there are cards that aren't in runs or the aces piles
  542.    while (cardsleft()) > 0 do {
  543.  
  544.       # try to make any run to run plays that will uncover
  545.       # hidden cards
  546.       while movecard(!"7654321",!"1234567","hidden")
  547.  
  548.       # try for a move that will leave an empty spot
  549.       if movecard(!"7654321",!"1234567") then next
  550.  
  551.       # if there's no overturned card thumb the deck
  552.       if *over = 0 then thumb()
  553.  
  554.       # initialize the thumbed sequence set
  555.       tseq := set()
  556.  
  557.       # try thumbing the deck for a play
  558.       totdeck := *deck + *over
  559.       every 1 to totdeck do {
  560.          if movecard("d",!"1234567a") then break
  561.  
  562.          if kbhit() then {
  563.             movecursor(16,0)
  564.             write("Now in manual mode ...", CLREOL)
  565.             amode := 0
  566.             return
  567.             }
  568.          insert(tseq,over[1])
  569.          thumb()
  570.       }
  571.  
  572.       # if we made a deck to somewhere move continue
  573.       if totdeck > *deck + *over then next
  574.  
  575.       # try for a run to ace play
  576.       if movecard(!"7654321","a") then next
  577.  
  578.       # if we got this far and couldn't play give up
  579.       break
  580.    }
  581.  
  582.    # position the cursor for the news
  583.    movecursor(16,30)
  584.  
  585.    # if all the cards are in runs or the aces piles
  586.    if cardsleft() = 0 then {
  587.  
  588.       writes("\007YEA...\007", CLREOL)
  589.  
  590.       # finish moving the runs to the aces piles
  591.       while movecard(!"7654321","a")
  592.  
  593.       movecursor(16,37)
  594.       write("I won!!!!!")
  595.       if cheat > 0 then write("But you cheated ", cheat, " time",
  596.             plural(cheat), ".")
  597.  
  598.       exit(0)
  599.  
  600.    } else {
  601.  
  602.       writes("I couldn't win this time.", CLREOL)
  603.       if cheat > 0 then writes(" But you cheated ", cheat, " time",
  604.                plural(cheat), ".")
  605.  
  606.       # print the information needed to verify that the
  607.       # program couldn't win
  608.  
  609.       movecursor(17,0)
  610.       writes(*deck + *over," card", plural(*deck + *over),
  611.                " in deck.")
  612.       if *tseq > 0 then {
  613.          write("  Final thumbing sequence:")
  614.          every writes(" ",face(!tseq))
  615.       }
  616.       write()
  617.  
  618.       exit(1)
  619.  
  620.    }
  621.  
  622. end
  623.  
  624. # this procedure updates the display
  625.  
  626. procedure display(parts)
  627.    local r, a, h, c, part, l
  628.  
  629.    static   long  # a list with the length of each run
  630.  
  631.    initial {
  632.       long := [1,1,1,1,1,1,1]
  633.    }
  634.  
  635.    # if the argument list is empty or contains "all" update all parts
  636.    # of the screen
  637.    if /parts | !parts == "all" then {
  638.       long  := [1,1,1,1,1,1,1]
  639.       parts := [  "label","hide","ace","deck",
  640.             "1","2","3","4","5","6","7" ]
  641.    }
  642.  
  643.    # for every part in the argument list
  644.    every part := !parts do case part of {
  645.  
  646.       # display the run number, aces and deck labels
  647.       "label" : {
  648.          every r := 1 to 7 do {
  649.             movecursor(1,7+(r-1)*5)
  650.             writes(r)
  651.          }
  652.          movecursor(1,56)
  653.          writes("ACES")
  654.          movecursor(6,56)
  655.          writes("DECK")
  656.       }
  657.  
  658.       # display the hidden card counts
  659.       "hide" : {
  660.          every r := 1 to 7 do {
  661.             movecursor(1,9+(r-1)*5)
  662.             writes(0 < *hidden[r] | " ")
  663.          }
  664.       }
  665.  
  666.       # display the aces piles
  667.       "ace" : {
  668.          movecursor(3,49)
  669.          every a := 1 to 4 do
  670.             writes(face(ace[a][-1]) | "---","  ")
  671.       }
  672.  
  673.       # display the deck and overturned piles
  674.       "deck" : {
  675.          movecursor(8,54)
  676.          writes((*deck > 0 , " # ") | "   ","  ")
  677.          writes(face(!over) | "   ","  ")
  678.       }
  679.  
  680.       # display the runs piles
  681.       "1" | "2" | "3" | "4" | "5" | "6" | "7" : {
  682.          l := ((long[part] > *run[part]) | long[part])
  683.          h := ((long[part] < *run[part]) | long[part])
  684.          l <:= 1
  685.          every c := l to h do {
  686.             movecursor(c+1,7+(part-1)*5)
  687.             writes(face(run[part][c]) | "   ")
  688.          }
  689.          long[part] := *run[part]
  690.       }
  691.    }
  692.  
  693.    return
  694.  
  695. end
  696.  
  697. # A correction to my corrections for solit.icn.
  698. # The zero case never happens in solit.icn, but this
  699. #     procedure is more general. From Phillip L. Thomas:
  700.  
  701. # Return "s" for values equal to 0 or greater than 1, e.g.,
  702. #     0 horses, 1 horse, 2 horses.
  703.  
  704. procedure plural(n)
  705.    /n := 0                             # Handle &null values.
  706.    if n = 1 then return ""
  707.    else return "s"
  708. end
  709.  
  710. # this procedure thumbs the deck 3 cards at a time
  711.  
  712. procedure thumb()
  713.    local s
  714.  
  715.    # if the deck is all thumbed
  716.    if *deck = 0 then {
  717.  
  718.       # if there are no cards in the overturned pile either return
  719.       if *over = 0 then return
  720.  
  721.       # turn the overturned pile back over
  722.       while put(deck,pull(over))
  723.    }
  724.  
  725.    # turn over 3 cards or at least what's left
  726.    every 1 to 3 do if *deck > 0 then push(over,get(deck))
  727.  
  728.    display(["deck"])
  729.  
  730.    # while there are aces on top of the overturned pile move them to
  731.    # the aces pile
  732.    while getvalue(over[1]) = 1 do {
  733.       s := getsuite(over[1])
  734.       push(ace[s],get(over))
  735.       display(["deck","ace"])
  736.    }
  737.  
  738.    # if the overturned pile is empty again and there are still cards
  739.    # in the deck thumb again (this will only happen if the top three
  740.    # cards in the deck were aces...not likely but)
  741.    if *over = 0 & *deck > 0 then thumb()
  742.  
  743.    return
  744.  
  745. end
  746.  
  747. # this procedure shuffles a deck of cards
  748.  
  749. procedure shuffle(cards)
  750.  
  751.    static   fulldeck # the default shuffle is a full deck of cards
  752.  
  753.    initial {
  754.       # set up a full deck of cards
  755.       fulldeck := []
  756.       every put(fulldeck,1 to 52)
  757.  
  758.       # if seed isn't already set use the time to set it
  759.       if /seed then seed := integer(&clock[1:3] ||
  760.                      &clock[4:6] ||
  761.                      &clock[7:0])
  762.  
  763.       # seed the random number generator for the first time
  764.       &random := seed
  765.    }
  766.  
  767.    # if no cards were passed use the full deck
  768.    /cards := fulldeck
  769.  
  770.    # copy the cards (shuffling is destructive)
  771.    deck := copy(cards)
  772.  
  773.    # shuffle the deck
  774.    every !deck :=: ?deck
  775.  
  776.    return deck
  777.  
  778. end
  779.  
  780. procedure face(card)
  781.  
  782.    static   cstr, # the list of card color escape sequences
  783.       vstr, # the list of card value labels
  784.       sstr  # the list of card suite labels
  785.  
  786.    initial {
  787.       cstr := [RED,BLACK]
  788.       vstr := ["A",2,3,4,5,6,7,8,9,10,"J","Q","K"]
  789.       if \VERSION == "IBM PC" then
  790.          sstr := ["\003","\004","\005","\006"]
  791.       else
  792.          sstr := ["H","D","S","C"]
  793.    }
  794.  
  795.    # return a string containing the correct color change escape sequence,
  796.    # the value and suite labels right justified in 3 characters,
  797.    # and the back to normal escape sequence
  798.    return   cstr[getcolor(card)] ||
  799.       right(vstr[getvalue(card)] || sstr[getsuite(card)],3) ||
  800.       NORMAL
  801.  
  802. end
  803.  
  804. # a deck of cards is made up of 4 suites of 13 values; 1-13, 14-26, etc.
  805.  
  806. procedure getvalue(card)
  807.  
  808.    return (card-1) % 13 + 1
  809.  
  810. end
  811.  
  812. # each suite of cards is made up of ace - king (1-13)
  813.  
  814. procedure getsuite(card)
  815.  
  816.    return (card-1) / 13 + 1
  817.  
  818. end
  819.  
  820. # the first two suites are hearts and diamonds so all cards 1-26 are red
  821. # and all cards 27-52 are black.
  822.  
  823. procedure getcolor(card)
  824.  
  825.    return (card-1) / 26 + 1
  826.  
  827. end
  828.  
  829. # this procedure counts cards that aren't in runs or the aces piles
  830.  
  831. procedure cardsleft()
  832.    local totleft
  833.  
  834.    # count the cards left in the deck and the overturned pile
  835.    totleft := *deck + *over
  836.  
  837.    # add in the hidden cards
  838.    every totleft +:= *!hidden
  839.  
  840.    return totleft
  841.  
  842. end
  843.  
  844. # this procedure implements a device dependent cursor positioning scheme
  845.  
  846. procedure movecursor(line,col)
  847.  
  848.    if \VERSION == "Atari ST" then
  849.       writes("\eY",&ascii[33+line],&ascii[33+col])
  850.  
  851.    else if \VERSION == "hp2621" then
  852.       writes("\e&a",col,"c",line,"Y")
  853.  
  854.    else
  855.       writes("\e[",line,";",col,"H")
  856.  
  857. end
  858.  
  859. # all invalid commands call this procedure
  860.  
  861. procedure whoops(cmd)
  862.    local i, j
  863.  
  864.    movecursor(15,0)
  865.    writes("\007Invalid Command: '",cmd,"'\007")
  866.  
  867.    # this delay loop can be diddled for different machines
  868.    every i := 1 to 500 do j := i
  869.  
  870.    movecursor(15,0)
  871.    writes("\007",CLREOL,"\007")
  872.  
  873.    return
  874.  
  875. end
  876.  
  877. # display the help message
  878.  
  879. procedure disphelp()
  880.  
  881.    static   help
  882.  
  883.    initial {
  884.       help := [
  885. "Commands: t or RETURN     : thumb the deck 3 cards at a time",
  886. "          m [d1-7] [1-7a] : move cards or runs",
  887. "          a               : turn on the auto pilot (in case you get stuck)",
  888. "          s               : shuffle the deck (cheat!)",
  889. "          p [2-7]         : put a hidden pile into the deck (cheat!)",
  890. "          d               : print the cards in the deck (cheat!)",
  891. "          [2-7]           : print the cards in a hidden pile (cheat!)",
  892. "          h or ?          : print this command summary",
  893. "          r               : print the rules of the game",
  894. "          q               : quit",
  895. "",
  896. "Moving:   1-7, 'd', or 'a' select the source and destination for a move. ",
  897. "          Valid moves are from a run to a run, from the deck to a run,",
  898. "          from a run to an ace pile, and from the deck to an ace pile.",
  899. "",
  900. "Cheating: Commands that allow cheating are available but they will count",
  901. "          against you in your next life!"
  902.       ]
  903.    }
  904.  
  905.    writes(CLEAR)
  906.    every write(!help)
  907.    writes("Hit RETURN")
  908.    read()
  909.    writes(CLEAR)
  910.    display()
  911.    return
  912.  
  913. end
  914.  
  915. # display the rules message
  916.  
  917. procedure disprules()
  918.  
  919.    static   rules
  920.  
  921.    initial {
  922.       rules := [
  923. "Object:   The object of this game is to get all of the cards in each suit",
  924. "          in order on the proper ace pile.",
  925. "                                        ",
  926. "Rules:    Cards are played on the ace piles in ascending order: A,2,...,K. ",
  927. "          All aces are automatically placed in the correct aces pile as",
  928. "          they're found in the deck or in a pile of hidden cards.  Once a",
  929. "          card is placed in an ace pile it can't be removed.",
  930. "",
  931. "          Cards must be played in descending order: K,Q,..,2, on the seven",
  932. "          runs which are initially dealt.  They must always be played on a",
  933. "          card of the opposite color.  Runs must always be moved as a",
  934. "          whole, unless you're moving the lowest card on a run to the",
  935. "          correct ace pile.",
  936. "",
  937. "          Whenever a whole run is moved, the top hidden card is turned",
  938. "          over, thus becoming the beginning of a new run.  If there are no",
  939. "          hidden cards left, a space is created which can only be filled by",
  940. "          a king.",
  941. "",
  942. "          The rest of the deck is thumbed 3 cards at a time, until you spot",
  943. "          a valid move.  Whenever the bottom of the deck is reached, the",
  944. "          cards are turned over and you can continue thumbing."
  945.       ]
  946.    }
  947.  
  948.    writes(CLEAR)
  949.    every write(!rules)
  950.    writes("Hit RETURN")
  951.    read()
  952.    writes(CLEAR)
  953.    display()
  954.    return
  955.  
  956. end
  957.