In: Computer Science
Write a program in Basic to play the game of Nim with acomputer.
ANSWER :
The traditional game of Nim is played in the following
manner:
There are four rows of stones arranged as shown:
0 (1)
0 0 0 (3)
0 0 0 0 0 (5)
0 0 0 0 0 0 0 (7)
Let A and B be the two players. The players play according to the 3
rules below:
1) On any given turn only objects from one row may be removed.
There is no restriction on which row or on how many objects you
remove.
2) You cannot skip a move or remove zero objects or remove more
objects than there are in a row.
3) Opponents take turns removing objects until there are none
left.
The win option should be specified in the program. The program
below allows the number, of piles to be variable, the pile sizes to
be variable and also the win option to be either taking the last
stone (s) or being left with the last stone(s).
10 PRINT "THE GAME OP NIM"
20 DIM A (100), B (100,10), D (2)
30 PRINT "ENTER WIN OPTION ‒ 1 TO TAKE LAST,
40 2 TO AVOID LAST";
50 INPUT W
60 IF W = 1 THEN 80
70 IF W <> 2 THEN 30
80 PRINT "ENTER NUMBER OF PILES";
90 INPUT N
100 IF N > 100 THEN 80
110 IF N < 1 THEN 80
120 IF N <> INT (N) THEN 80
130 PRINT "ENTER PILE SIZES"
140 FOR I = 1 TO N
150 PRINT I;
160 INPUT A (I)
170 IF A (I) > 2000 THEN 150
180 IF A (I) <> INT(A (I)) THEN 150
190 NEXT I
200 PRINT "DO YOU WANT TO MOVE FIRST?";
210 INPUT A$
220 IF A$ = "YES" GOTO 1000
230 IF A$ = "NO" GOTO 250
235 PRINT "PLEASE, YES OR NO";
240 GOTO 210
250 IF W = 1 THEN 490
260 LET C = 0
270 FOR I = 1 TO N
280 IF A (I) = 0 THEN 320
290 LET C = C + 1
300 IF C = 3 THEN 390
310 LET D (C) = I
320 NEXT I
330 IF C = 2 THEN 470
340 IF A (D(1)) > 1 THEN 370
350 PRINT "MACHINE LOSES"
360 GOTO 1190
370 PRINT "MACHINE WINS"
380 GOTO 1190
390 LET C = 0
400 FOR I = 1 TO N
410 IF A (I) > 1 THEN 490
420 IF A (I) = 0 THEN 440
430 LET C = C + 1
440 NEXT I
450 IF C/2 <> INT (C/2) THEN 350
460 GOTO 490
470 IF A (D (1)) = 1 THEN 370
480 IF A (D (2)) = 1 THEN 370
490 FOR I = 1 TO N
500 LET E = A (I)
510 FOR J = 0 TO 10
520 LET F = E/2
530 LET B (I, J) = 2*(F ‒ INT(F) )
540 LET E = INT (F)
550 NEXT J
560 NEXT I
570 FOR J = 10 TO 0 STEP ‒ 1
580 LET C = 0
590 LET H = 0
600 FOR I = 1 TO N
610 IF B (I, J) = 0 THEN 660
620 LET C = C + 1
630 IF A (I) < = H THEN 660
640 LET H = A (I)
650 LET G = I
660 NEXT I
670 IF C/2 <> INT (C/2) THEN 740
680 NEXT J
690 LET E = INT (N*RND (1) + 1)
700 IF A(E) = 0 THEN 690
710 LET F = INT (A (E)*RND (1) + 1)
720 LET A (E) = A (E) ‒ F
730 GOTO 930
740 LET A (G) = 0
750 FOR J = 0 TO 10
760 LET B (G, J) = 0
770 LET C = 0
780 FOR I = 1 TO N
790 IF B (I, J) = 0 THEN 810
800 LET C = C + 1
810 NEXT I
820 LET A (G) = A (G) + 2*(C/2 ‒ INT (C/2) )*2 ↑ J
830 NEXT J
840 IF W = l THEN 930
850 LET C = 0
860 FOR I = 1 TO N
870 IF A (I) > 1 THEN 930
880 IF A (I) = 0 THEN 900
890 LET C = C + 1
900 NEXT I
910 IF C/2 <> INT (C/2) THEN 930
920 LET A(G) = 1 ‒ A (G)
930 PRINT "PILE SIZE"
940 FOR I = 1 TO N
950 PRINT I; A (I)
960 NEXT I
970 IF W = 2 THEN 1000
980 GOSUB 1120
990 IF Z = 1 THEN 370
1000 PRINT "YOUR MOVE ‒ PILE, NUMBER TO BE REMOVED";
1010 INPUT X,Y
1020 IF X > N THEN 1000
1030 IF X < 1 THEN 1000
1040 IF X <> INT (X) THEN 1000
1050 IF Y > A (X) THEN 1000
1060 IF Y < 1 THEN 1000
1070 IF Y <> INT (Y) THEN 1000
1080 LET A (X) = A (X) ‒ Y
1090 GOSUB 1120
1100 IF Z = 1 THEN 350
1110 GOTO 250
1120 LET Z = 0
1130 FOR I = 1 TO N
1140 IF A (I) =0 THEN 1160
1150 RETURN
1160 NEXT I
1170 LET Z = 1
1180 RETURN
1190 PRINT "DO YOU WANT TO PLAY ANOTHER GAME"
1200 INPUT A$
1210 IF A$ = "YES" THEN 1240
1220 IF A$ = "NO" THEN 1250
1230 GOTO 1200
1240 GOTO 30
1250 END.