1 REM  @@@ Salvaged from old Valpo Data General MV/8000 by RAK.
2 REM  @@@ Apple II version RTK, 01/93.
3 REM  asm START_CHECK_GAMES  <- what is this?
5 REM  RANDOMIZE  ( UNNECESSARY ON APPLE )
10 REM      *** NIM
15 REM 
50 REM       N     =>  NUMBER OF PILES
52 REM       P(I)  =>  NUMBER OF OBJECTS IN PILE I
54 REM       B(I,J)=>  P(I) IN BASE 2
56 REM       D(J)  =>  B(1,J) + ... + B(N,J) IN BASE 2
58 REM       M     =>  MAXIMUM NUMBER OF PILES
60 REM       THE MAXIMUM PILE SIZE IS 31
62 REM 
70 LET M=10
72 DIM P(10),B(10,5),D(5)
74 REM  DIM A$*72 ( UNNECESSARY ON APPLE )
102 INPUT "DO YOU NEED INSTRUCTIONS (YES OR NO)? ";A$
104 PRINT 
106 IF MID$ (A$,1,1)="N" THEN 881
108 PRINT "NIM IS PLAYED WITH PILES OF STARS (YOU DECIDE"
110 PRINT "HOW MANY PILES AND HOW MANY STARS IN EACH PILE)."
112 PRINT 
113 PRINT "WHEN IT IS YOUR TURN, YOU MAY TAKE AS MANY *'S ";
114 PRINT "AS YOU LIKE, BUT FROM ONE PILE ONLY."
116 PRINT "THE PLAYER WHO TAKES THE LAST '*' WINS."
118 PRINT 
120 PRINT "FOR EXAMPLE, SUPPOSE THE PILES ARE:"
122 PRINT 
124 PRINT TAB( 5);"PILE 1:  * *"
126 PRINT TAB( 5);"PILE 2:  * * *"
128 PRINT TAB( 5);"PILE 3:  * *"
130 PRINT 
132 PRINT "TO TAKE THREE *'S FROM PILE 2, YOU MOVE 3,2.  THAT LEAVES"
134 PRINT 
136 PRINT TAB( 5);"PILE 1:  * *"
138 PRINT TAB( 5);"PILE 2:"
140 PRINT TAB( 5);"PILE 3:  * *"
142 PRINT 
144 PRINT "NOW IF I MOVE  2,3  (TAKING 2 *'S FROM PILE 3)"
146 PRINT "YOU WIN BY MOVING  2,1  (2 *'S FROM PILE 1)!!"
148 PRINT 
150 PRINT "HERE WE GO ..."
152 PRINT 
154 PRINT 
156 REM 
158 REM       FOR A NEW PLAYER SET N=3 AND P(1)=3,P(2)=5,P(3)=7"
160 REM 
162 LET N=3
164 LET P(1)=3
166 LET P(2)=5
168 LET P(3)=7
180 REM       WHO MOVES FIRST?
184 INPUT "DO YOU WANT TO GO FIRST (YES OR NO)? ";A$
186 IF LEFT$(A$,1)="Y" THEN 202
188 REM       PRINT THE PILES
190 GOSUB 00960
192 GOTO 00214
194 REM 
196 REM       MAIN BODY
198 REM 
200 REM       PRINT THE PILES
202 GOSUB 00960
204 REM       THE HUMAN MOVES
206 GOSUB 00802
208 REM       CHECK WHETHER THE HUMAN WON
210 GOSUB 00830
212 IF T=0 THEN 00850
214 REM       THE COMPUTER MOVES
216 GOSUB 00600
218 REM       CHECK WHETHER THE COMPUTER WON
220 GOSUB 00830
222 IF T=0 THEN 00860
224 GOTO 00202
592 REM 
594 REM       THE COMPUTER MOVES
596 REM 
598 REM       INITIALIZE
600 FOR J=1 TO 5
602 LET D(J)=0
604 NEXT J
612 REM       ADD  P(1) + ... + P(N) IN BASE 2
614 FOR I=1 TO N
616 LET T=P(I)
618 FOR J=5 TO 1 STEP -1
620 LET B(I,J)=INT(T/2^(J-1))
622 LET D(J)=ABS(D(J)-B(I,J))
624 LET T=T-B(I,J)*2^(J-1)
626 NEXT J
628 NEXT I
630 REM       FIND THE LARGEST I SUCH THAT D(I)=1
632 LET T=0
634 FOR I=5 TO 1 STEP -1
636 LET T=T+I*D(I)*(1-SGN(T))
638 NEXT I
640 REM       IF T>0 THEN THE WINNING MOVE IS
642 REM       D(T)*2^(T-1) + D(T-1)*2^(T-2)*SGN(B(S,T-1)-.5) + ...
643 REM       + D(1)*SGN(B(S,1)-.5)
644 REM       FROM ANY PILE I SUCH THAT B(I,T)=1
646 REM       IF T=0 THEN REMOVE 1 FROM ANY NON-EMPTY PILE
648 IF T>0 THEN 00662
650 LET S=INT(N*RND)+1
652 IF P(S)=0 THEN 00650
654 LET T=1
656 GOTO 00684
660 REM       FIND I SO THAT B(I,J)=1
662 LET S=0
664 FOR I=1 TO N
666 LET S=S+I*B(I,T)*(1-SGN(S))
668 NEXT I
670 IF T=1 THEN 00684
672 LET W=0
674 FOR J=T-1 TO 1 STEP -1
676 LET W=W+D(J)*2^(J-1)*SGN(B(S,J)-.5)
678 NEXT J
680 LET T=2^(T-1)+W
682 REM       THE COMPUTER MOVES
684 PRINT "I MOVE";T;",";S
686 PRINT 
688 LET P(S)=P(S)-T
690 RETURN 
794 REM 
796 REM       THE HUMAN MOVES
798 REM 
802 INPUT "YOUR MOVE: ";T,S
804 IF S=SGN(N+1-S)*SGN(S)+INT(S-1) THEN 00810
807 INPUT "WHICH PILE TO YOU WANT? ";S
808 GOTO 00804
810 IF INT(T)*P(S)>0 THEN 00816
812 PRINT "SORRY, BUT YOU CAN'T TAKE ";T;"'*'S FROM PILE ";S
814 GOTO 00802
816 REM       VALID INPUT
818 LET P(S)=(P(S)-INT(T))*SGN(1+SGN(P(S)-INT(T)))
820 RETURN 
824 REM 
826 REM       CHECK FOR A WINNER
828 REM 
830 LET T=0
832 FOR I=1 TO N
834 LET T=T+P(I)
836 NEXT I
838 RETURN 
848 REM       THE HUMAN WON
850 PRINT 
852 PRINT "CONGRATULATIONS!  YOU WON  . . . "
854 GOTO 00870
858 REM       THE COMPUTER WON
860 PRINT 
862 PRINT "I WON.  BETTER LUCK NEXT TIME."
866 REM       ANOTHER GAME?
870 INPUT "DO YOU WANT TO PLAY AGAIN (YES OR NO)? ";A$
872 IF LEFT$(A$,1)="N" THEN 1000
874 PRINT 
876 REM       SET UP THE PILES
878 REM 
881 GOSUB 00902
882 GOTO 00184
894 REM 
896 REM       INPUT THE NUMBER OF PILES AND THE SIZE OF EACH PILE
898 REM 
902 INPUT "HOW MANY PILES DO YOU WANT? ";N
904 PRINT 
906 LET N=INT(N)
908 IF N<=0 THEN GOTO 01000
910 IF N<=M THEN 00918
912 PRINT "I CAN PLAY WITH AT MOST ";M;"PILES."
914 PRINT 
916 GOTO 00902
918 FOR I=1 TO N
920 PRINT "HOW MANY '*'S IN PILE ";I;
922 INPUT P(I)
924 LET P(I)=INT(P(I))*SGN(1+INT(P(I)))
926 IF P(I)<=31 THEN 00932
928 PRINT "I CAN PLAY WITH AT MOST 31 '*'S IN EACH PILE."
930 GOTO 00920
932 NEXT I
934 REM       CHECK THAT NOT ALL THE PILES ARE EMPTY
936 GOSUB 00830
938 IF T>0 THEN 00946
940 PRINT "HOW CAN WE PLAY IF ALL THE PILES ARE EMPTY?"
942 PRINT 
944 GOTO 00902
946 PRINT 
948 PRINT 
950 RETURN 
956 REM 
958 REM       PRINT THE PILES
959 REM 
960 FOR I=1 TO N
962 PRINT TAB( 5);"PILE ";I;": ";
964 IF P(I)=0 THEN 00972
966 FOR J=1 TO P(I)
968 PRINT "* ";
970 NEXT J
972 PRINT 
974 NEXT I
976 PRINT 
978 RETURN