
L2 holds the deck.
  L2(1..3) is the widow.
  L2(4..6) is P1's hand.
  L2(3I+4..3I+6) is the hand of computer player #I.
L1 is scratch space and an argument to ZCMEVAL and ZCMMV.
L3 is an argument to ZCMMV.
Y1(X) computes the face value of the card X.
Y2(X) computes the suit of the card X.
A,B,C are scratch space for ZCMMV, ZCMEVAL, and ZCRD.
N tells who's kNocked.
P holds the number of computer Players.
R holds the current dealer Rotation.


0\->\Xmin:1\->\\Delta\X                      /* Setup code. */
\(-)\62\->\Ymin:1\->\\Delta\Y
AxesOff
PlotsOff :Full
"int(.5+13fPart(X/13\->\Y\1\
"int(X/13\->\Y\2\
FnOff 
"ACEPIPTEXVAL\->\Str1
seq(P,P,0,51\->\L\2\
Disp "NUMBER OF AI
"PLAYERS (2-4)?_                             /* Get number of players. */
Repeat 2\<=\P and P\<=\4
  Input Ans,P
  "ENTER 2,3, or 4.
End

randInt(0,P\->\R
9\->\N                                       /* preserve invariant for Lbl O */

While 1
  rand(52\->\L\1\
  SortA(L\1\,L\2\                            /* Shuffle the deck. */
  ClrDraw
  Lbl O                                      /* Lbl O: Display the widow (and sometimes */
  L\2\\->\L\1\:3\->\dim(L\1\                 /* P1's hand, also). */
  7\->\B
  Goto D:Lbl 1                               /* Lbl 1: (return from D) */
  seq(L\2\(I),I,4,6\->\L\1\
  43\->\B
  Lbl D                                      /* Lbl D: Display widow or P1's hand. */
  DelVar AL\1\(1\->\C
  prgmZCRD
  17\->\A:L\1\(2\->\C
  prgmZCRD
  34\->\A:L\1\(3\->\C
  prgmZCRD
  If Rnot(N:Goto P                           /* P: (return to CP dealer's exchange) */
  If B=7:Goto 1
  If not(N:Goto Q

  Vertical 53                                /* Draw the rest of the game screen. */
  Line(54,\(-)\55,94,\(-)\55
  DelVar NDelVar WText(56,64,"KNOCK
  If R:Then                                  /* R>0 means it's one of the CPs' deal. */
    L\2\\->\L\1\:3\->\dim(L\1\               /* Exchange only if the widow is better */
    prgmZCMEVAL                              /* than the CP dealer's hand. */
    A\->\C:B\->\D
    seq(L\2\(3R+I),I,4,6\->\L\1\
    prgmZCMEVAL
    If C>A or C=A and D>B:Then
      Text(0,0,sub(Str1,3R-2,3)," EXCHANGES
      For(I,1,3
        L\2\(I\->\L\1\(3R+3+I
        L\1\(I\->\L\2\(I
      End
      Goto O:Lbl P                           /* Display the new widow. */
    End
  Else
    Text(0,0,"CHOOSE A HAND                  /* else it's P1's deal */
    Text(32,20,"][                           /* draw a double-headed arrow */
    Pt-On(22,\(-)\38,3
    Pxl-Off(37,22
    Pt-On(22,\(-)\32,3
    Pxl-Off(33,22
    Repeat max(Ans={21,25,34:getKey:End
    If Ans=25:Then                           /* up-arrow means swap hands */
      L\2\\->\L\1\
      For(I,1,3
        L\2\(3+I\->\L\2\(I
        L\1\(I\->\L\2\(3+I
      End
      Goto O:Lbl Q                           /* redisplay the widow and P1's hand */
    End
  End
  "_____
  Text(29,20,Ans                             /* erase the double-headed arrow */
  Text(35,20,Ans
  For(I,0,12:Text(0,4I,Ans:End

  (R+1)(P\!=\R\->\R                          /* advance the deal for next round */
  If Ans:Then                                /* if we start with a CP's turn... */
    For(I,R,9                                /* ...then handle each CP starting with */
      If I\<=\P:Goto R                       /* that one. Note 9>P. */
    End
  End

  While N\!=\9                               /* N.B.: N may be 9 already! */
    If N\!=\8:Then                           /* N=8 if P1 has already knocked. */
      3\->\C
      While 0:Lbl K:End                      /* Lbl K: target of "Clear" from within widow */
      Text(0,0,"YOUR TURN
      Repeat K=21                            /* Get a card, or "Knock". */
        Pt-On(17C-8,\(-)\59,2
        Repeat Ans:getKey\->\K:End
        Pt-Off(17C-8,\(-)\59,2
        C+(Ans=26)-(Ans=24
        Ans+4(not(Ans)-(Ans=5\->\C
      End
      If C<4:Then                            /* If it was a card selected... */
        Text(0,0,"EXCHANGE WITH?
        C\->\D
        Repeat 12=abs(K-33                   /* Get a card in the widow, or Clear. */
          Pt-On(17Ans-8,\(-)\23,2
          Repeat Ans:getKey\->\K:End
          Pt-Off(17D-8,\(-)\23,2
          D+(Ans=26)-(Ans=24
          Ans+3(not(Ans)-(Ans=4\->\D
        End
        For(I,0,12:Text(0,4I,"____           /* blank top line: "EXCHANGE WITH" */
        End
        If K=45:Goto K                       /* K: get a different card C */
        17C-17\->\A:43\->\B
        L\2\(C+3\->\E                        /* swap cards C (P1's hand) and D (widow) */
        L\2\(D\->\L\2\(C+3:Ans\->\C
        prgmZCRD
        17D-17\->\A:7\->\B
        E\->\L\2\(D:Ans\->\C
        prgmZCRD
        Text(5+7W,56,"YOU TRADE
      Else
        9-not(N\->\N                         /* N>0 ==> N=9, N=0 ==> N=8 */
        Text(5+7W,56,"YOU KNOCK
        For(I,0,9:Text(0,4I,"____            /* blank top line: "YOUR TURN" */
        End
      End
      W+1\->\W                               /* we used one line of display text */
    End
    For(I,1,P(N\!=\9                         /* If N<9, then for each CP in turn */
      Lbl R
      If I\!=\N:Then                         /* if CP #I hasn't knocked yet... */
        seq(L\2\(3I+C),C,4,6\->\L\1\         /* L1: CP's hand */
        L\2\\->\L\3\:3\->\dim(L\3\           /* L3: the widow */
        prgmZCMMV                            /* C=CP's card, D=widow's card */
        If Ans:Then                          /* Ans>0 means trade */
          4\->\K:4\->\L
          3+3I+C\->\C                        /* trade the cards */
          L\2\(D\->\E
          L\2\(C\->\L\2\(D
          E\->\L\2\(C
          17D-17\->\A:7\->\B:L\2\(D\->\C     /* update the widow */
          prgmZCRD
          Y\1\(L\2\(D                        /* display the move in the text window */
          sub("A23456789XJQK",Ans+1,1
          If Ans="X
          DelVar K"10
          Text(5+7W,56,sub(Str1,3I-2,1),": ",Ans
          sub("A23456789XJQK",1+Y\1\(E),1
          If Ans="X
          DelVar L"10
          Text(5+7W,81-K,Ans
          Y\2\(E\->\C
          Y\2\(L\2\(D\->\D
          "{([({ /                           /* Todo: collapse these 2 sections into 1? */
          If not(D:"\e\Q[Q\e\ /
          If 2=D:"'QAQ' /
          If 3=D:"\e\([(\e\ /
          For(D,1,7
            Text(5+7W,70+D-K,sub(Ans,D,1
          End
          "{([({__
          If not(C:"\e\Q[Q\e\__
          If 2=C:"'QAQ'__
          If 3=C:"\e\([(\e\__
          For(C,1,7
            Text(5+7W,88+C-K-L,sub(Ans,C,1
          End
        Else                                 /* not(Ans) from prgmZCMMV means knock */
          Text(5+7W,56,sub(Str1,3I-2,3)," KNOCKS
          If N:9\->\I                        /* N>0 ==> N=9 and I>P */
          If N:Pause                         /* N=0 ==> N=I and I unchanged */
          I\->\N
        End

        W+1\->\W                             /* used another line of display text */
                                             /* If W>=6 and I=P, or W>=7 and I<P,
        If I=P\>=\7-W and N\!=\9:Then        /* but not if N=9... */
          DelVar WPause                      /* clear the text window */
          For(C,56,91
            Line(C,\(-)\6,C,\(-)\52,0
          End
        End

    End:End
  End                                        /* end "While N\!=\9" */

  For(I,56,91                                /* clear the text window */
    Line(I,\(-)\6,I,\(-)\52,0
  End

  DelVar BFor(I,1,P                          /* show the CPs' hands */
    Text(13I-8,56,sub(Str1,3I-2,3),":
    69\->\A:\(-)\1-L\2\(3I+4\->\C
    prgmZCRD
    76\->\A:\(-)\1-L\2\(3I+5\->\C
    prgmZCRD
    83\->\A:\(-)\1-L\2\(3I+6\->\C
    prgmZCRD
    B+13\->\B
  End
  DelVar DDelVar Emax(R-1,Pnot(R\->\J        /* (R-1) mod P+1 */
  Ans\->\W
  For(I,0,P                                  /* pick the best hand */
    seq(L\2\(X),X,3J+4,3J+6\->\L\1\          
    prgmZCMEVAL                              /* J=R-1 always wins ties; then J=R, */
    If A=D and B=E:1\->\T                    /* J=R+1,... J=0, J=1,... J=R-2.     */
    If A>D or A=D and B>E:Then               /* T indicates a tie. */
      DelVar TJ\->\W
      A\->\D:B\->\E
    End
    (J\!=\P)(J+1\->\J
  End
  "YOUR_
  If W:sub(Str1,3W-2,3)+"'S_
  If not(D:Ans+"POSITION
  If D and D\<=\2:Ans+"POINT
  If D=3:Ans+"SEQUENCE
  If D=4:Ans+"TRICON
  Text(28,1,Ans
  "HAND
  If T:"TIE
  Text(34,1,"WINS THE ",Ans
  Pause 
End

