-
Notifications
You must be signed in to change notification settings - Fork 0
/
NIM.HPB
200 lines (200 loc) · 4.84 KB
/
NIM.HPB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
10 REM *** NIM
12 REM *** PEOPLE'S COMPUTER COMPANY, MENLO PARK, CA
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(I,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 DIM A$(72)
100 PRINT "DO YOU NEED INSTRUCTIONS (YES OR NO)";
102 INPUT A$
104PRINT
106 IF A$[1,1]="N" THEN 880
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 PLATER SET N=3 AND P(1)=2,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?
182 PRINT "DO YOU WANT TO MOVE FIRST (YES OR NO)";
184 INPUT A$
186 IF A$[1,1]="Y" THEN 202
188 REM PRINT THE PILES
190 GOSUB 960
192 GOTO 214
194 REM
196 REM MAIN BODY
198 REM
200 REM PRINT THE PILES
202 GOSUB 960
204 REM THE HUMAN MOVES
206 GOSUB 800
208 REM CHECK WHETHER THE HUMAN WON
210 GOSUB 830
212 IF T=0 THEN 850
214 REM THE COMPUTER MOVES
216 GOSUB 600
218 REM CHECK WHATHER THE COMPUTER WON
220 GOSUB 830
222 IF T=0 THEN 860
224 GOTO 202
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)=I
632 LET T=0
634 FOR I=5 TO 1 STEP -1
636 LET T=T+I*D(I)*(1-SGN(T))'MAY BE T*I NOT T+I
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 T ANY PILE I SUCH THAT B(I,T)=1
646 REM IF T=0 THEN REMOVE I FROM ANY NON-EMPTY PILE
648 IF T>0 THEN 662
650 LET S=INT(N*RND(0))+1
652 IF P(S)=0 THEN 650
654 LET T=1
656 GOTO 684
660 REM FIND I SO THAT B(I,J)=I
662 LKT 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 684
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
786 REM THE HUMAN MOVES
798 REM
800 PRINT "YOUR MOVE";
802 INPUT T,S
904 IF S=SGN(N+1-S)*SGN(S)+INT(S-1) THEN 810
806 PRINT "WHICH PILE DO YOU WANT";
807 INPUT S
808 GOTO 804
810 IF INT(T)*P(S)>0 THEN 816
812 PRINT "SORRY, BUT YOU CAN'T TAKE ";T;"'*'S FROM PILE ";S
814 GOTO 800
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 "CONGRADULATIONS!! YOU WON . . . "
854 GOTO 868
858 REM THE COMPUTER WON
860 PRINT
862 PRINT "I WON. BETTER LUCK NEXT TIME."
866 REM ANOTHER GAME?
868 PRINT "DO YOU WANT TO PLAY AGAIN (YES OR NO)";
870 INPUT A$
872 IF A$[1,1]="N" THEN 999
874 PRINT
876 REM SET UP THE PILES
878 REM
880 GOSUB 900
882 GOTO 182
894 REM
896 REM INPUT THE NUMBER OF PILES AND THE SIZE OF EACH PILE
898 REM
900 PRINT "HOW MANY PILES DO YOU WANT";
902 INPUT N
904 PRINT
906 LET N=INT(N)
908 IF N <= 0 THEN 999
910 IF N <= M THEN 918
912 PRINT "I CAN PLAY WITH AT MOST "'M;"PILES."
914 PRINT
916 GOTO 900
918 FOR I=1 TO N
920 PRINT "HOW MANY '*'S IN PILE ";I;
922 INPUT P(J)
924 LET P(I)=INT(P(I)*SGN(1+INT(P(I)))
926 IF P(I) <= 31 THEN 932
928 PRINT "I CAN PLAY WITH AT MOST 31 '*'S IN EACH PILE."
930 GOTO 920
932 NEXT I
934 REM CHECK THAT NOT ALL THE PILES ARE EMPTY
936 GOSUB 830
938 IF >0 THEN 946
940 PRINT "HOW CAN BE PLAY IF ALL THE PILES ARE EMPTY?"
942 PRINT
944 GOTO 900
946 PRINT
948 PRINT
950 RETURN
956 REM
958 REM PRINT THE PILES
960 FOR I=1 TO N
962 PRINT TAB(5);"PILE ";I;": ";
964 IF P(I)=0 THEN 972
966 FOR J=1 TO P(I)
968 PRINT "* ";
970 NEXT J
972 PRINT
974 NEXT I
976 PRINT
978 RETURN
999 END