-
Notifications
You must be signed in to change notification settings - Fork 0
/
NIM.BAS
157 lines (157 loc) · 4.13 KB
/
NIM.BAS
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
50 PRINT CHR$(26):WIDTH 80
100 PRINT TAB(33);"NIM":PRINT
110 PRINT TAB(15);"CREATIVE COMPUTING MORRISTOWN, NEW JERSEY"
120 PRINT
210 DIM A(100),B(100,10),D(2)
220 PRINT "THIS IS THE GAME OF NIM.":PRINT
230 PRINT "DO YOU WANT INSTRUCTIONS";
240 INPUT Z$
250 IF LEFT$(Z$,1)="N" THEN 440
270 IF LEFT$(Z$,1)="Y" THEN 310
290 PRINT "PLEASE ANSWER YES OR NO." :PRINT
300 GOTO 230
310 PRINT:PRINT " THE GAME IS PLAYED WITH A NUMBER OF PILES OF OBJECTS."
320 PRINT "ANY NUMBER OF OBJECTS ARE REMOVED FROM ONE PILE BY YOU AND"
330 PRINT "THE MACHINE ALTERNATELY. ON YOUR TURN, YOU MAY TAKE"
340 PRINT "ALL THE OBJECTS THAT REMAIN IN ANY PILE, BUT YOU MUST"
350 PRINT "TAKE AT LEAST ONE OBJECT, AND YOU MAY TAKE OBJECTS FROM"
360 PRINT "ONLY ONE PILE ON A SINGLE TURN. YOU MUST SPECIFY WHETHER"
370 PRINT "WINNING IS DEFINED AS TAKING OR NOT TAKING THE LAST OBJECT,"
380 PRINT "THE NUMBER OF PILES IN THE GAME, AND HOW MANY OBJECTS ARE"
390 PRINT "ORIGINALLY IN EACH PILE. EACH PILE MAY CONTAIN A"
400 PRINT "DIFFERENT NUMBER OF OBJECTS.":PRINT
410 PRINT " THE MACHINE WILL SHOW ITS MOVE BY LISTING EACH PILE AND THE"
420 PRINT "NUMBER OF OBJECTS REMAINING IN THE PILES AFTER EACH OF ITS"
430 PRINT "MOVES."
440 PRINT
450 PRINT "ENTER WIN OPTION - 1 TO TAKE LAST, 2 TO AVOID LAST";
460 INPUT W
465 PRINT
470 IF W=1 THEN 490
480 IF W<>2 THEN 450
490 PRINT "ENTER NUMBER OF PILES";
500 INPUT N
505 PRINT
510 IF N>100 THEN 490
520 IF N<1 THEN 490
530 IF N<>INT(N) THEN 490
540 PRINT "ENTER PILE SIZES"
550 FOR I=1 TO N
560 PRINT I;
570 INPUT A(I)
580 IF A(I)>2000 THEN 560
590 IF A(I)<1 THEN 560
600 IF A(I)<>INT(A(I)) THEN 560
610 NEXT I
620 PRINT:PRINT "DO YOU WANT TO MOVE FIRST";
630 INPUT Q9$
635 PRINT
640 IF LEFT$(Q9$,1)="Y" THEN 1450
660 IF LEFT$(Q9$,1)="N" THEN 700
680 PRINT "PLEASE ANSWER YES OR NO."
690 GOTO 630
700 IF W=1 THEN 940
710 LET C=0
720 FOR I=1 TO N
730 IF A(I)=0 THEN 770
740 LET C=C+1
750 IF C=3 THEN 840
760 LET D(C)=I
770 NEXT I
780 IF C=2 THEN 920
790 IF A(D(1))>1 THEN 820
800 PRINT:PRINT CHR$(22);" COMPUTER LOSES. ";CHR$(22):PRINT
810 GOTO 1640
820 PRINT:PRINT CHR$(22);" COMPUTER WINS. ";CHR$(22):PRINT
830 GOTO 1640
840 LET C=0
850 FOR I=1 TO N
860 IF A(I)>1 THEN 940
870 IF A(I)=0 THEN 890
880 LET C=C+1
890 NEXT I
900 IF C/2<>INT(C/2) THEN 800
910 GOTO 940
920 IF A(D(1))=1 THEN 820
930 IF A(D(2))=1 THEN 820
940 FOR I=1 TO N
950 LET E=A(I)
960 FOR J=0 TO 10
970 LET F=E/2
980 LET B(I,J)=2*(F-INT(F))
990 LET E=INT(F)
1000 NEXT J
1010 NEXT I
1020 FOR J=10 TO 0 STEP -1
1030 LET C=0
1040 LET H=0
1050 FOR I=1 TO N
1060 IF B(I,J)=0 THEN 1110
1070 LET C=C+1
1080 IF A(I)<=H THEN 1110
1090 LET H=A(I)
1100 LET G=I
1110 NEXT I
1120 IF C/2<>INT(C/2) THEN 1190
1130 NEXT J
1140 LET E=INT(N*RND(1)+1)
1150 IF A(E)=0 THEN 1140
1160 LET F=INT(A(E)*RND(1)+1)
1170 LET A(E)=A(E)-F
1180 GOTO 1380
1190 LET A(G)=0
1200 FOR J=0 TO 10
1210 LET B(G,J)=0
1220 LET C=0
1230 FOR I=1 TO N
1240 IF B(I,J)=0 THEN 1260
1250 LET C=C+1
1260 NEXT I
1270 LET A(G)=A(G)+2*(C/2-INT(C/2))*2^J
1280 NEXT J
1290 IF W=1 THEN 1380
1300 LET C=0
1310 FOR I=1 TO N
1320 IF A(I)>1 THEN 1380
1330 IF A(I)=0 THEN 1350
1340 LET C=C+1
1350 NEXT I
1360 IF C/2<>INT(C/2) THEN 1380
1370 LET A(G)=1-A(G)
1380 PRINT:PRINT "PILE SIZE"
1385 PRINT "---- ----"
1390 FOR I=1 TO N
1400 PRINT USING " ## ###";I,A(I)
1410 NEXT I
1420 IF W=2 THEN 1450
1430 GOSUB 1570
1440 IF Z=1 THEN 820
1450 PRINT:PRINT "YOUR MOVE - PILE, NUMBER TO BE REMOVED";
1460 INPUT X,Y
1470 IF X>N THEN 1450
1480 IF X<1 THEN 1450
1490 IF X<>INT(X) THEN 1450
1500 IF Y>A(X) THEN 1450
1510 IF Y<1 THEN 1450
1520 IF Y<>INT(Y) THEN 1450
1530 LET A(X)=A(X)-Y
1540 GOSUB 1570
1550 IF Z=1 THEN 800
1560 GOTO 700
1570 LET Z=0
1580 FOR I=1 TO N
1590 IF A(I)=0 THEN 1610
1600 RETURN
1610 NEXT I
1620 LET Z=1
1630 RETURN
1640 PRINT "DO YOU WANT TO PLAY ANOTHER GAME";
1650 INPUT Q9$
1660 IF LEFT$(Q9$,1)="Y" GOTO 1720
1680 IF LEFT$(Q9$,1)="N" GOTO 1730
1690 IF Q9$="no" GOTO 1730
1700 PRINT "PLEASE ANSWER YES OR NO."
1710 GOTO 1650
1720 GOTO 440
1730 RUN "MENU"