Question

In: Computer Science

Write a program in Basic to play the game of Nim with acom­puter.

Write a program in Basic to play the game of Nim with acom­puter.

Solutions

Expert Solution

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.


Related Solutions

Write a program where a user of this program will play a game in which he/she...
Write a program where a user of this program will play a game in which he/she needs to guess a target number, which is a number that the program has randomly picked in the range that the user chooses. The program will repeatedly prompt for the guessed number and provide a clue whether the guessed number is bigger or smaller than the target number, until the guessed number equals the target number.
Nim Game Java, PegClass One variation of the game of Nim, described in Chapter 5 Exercise...
Nim Game Java, PegClass One variation of the game of Nim, described in Chapter 5 Exercise 12, is played with four piles of stones. Initially, the piles contain 1, 2, 3, and 4 stones. On each turn, a player may take 1, 2, or 3 stones from a single pile. The player who takes the last stone loses. a) Write a program that allows two players to play Nim. Be sure to allow only legal moves. The program should be...
write on eclipse java Write a program named lab5 that will play a game of Blackjack...
write on eclipse java Write a program named lab5 that will play a game of Blackjack between the user and the computer. Create a second class named Card with the following: Define the following private instance variables: cardValue (int) & cardSuite (String) Write a constructor with no parameters that will Set cardValue to a random number between 1 and 13 Generate a second random number between 0 and 3 and assign cardSuite a string based on its value (0 –...
IN C++ Write a program to play the Card Guessing Game. Your program must give the...
IN C++ Write a program to play the Card Guessing Game. Your program must give the user the following choices: - Guess only the face value of the card. -Guess only the suit of the card. -Guess both the face value and suit of the card. Before the start of the game, create a deck of cards. Before each guess, use the function random_shuffle to randomly shuffle the deck.
Write a program with at least 2 functions that play the game of “guess the number”...
Write a program with at least 2 functions that play the game of “guess the number” as follows: Your program chooses the number to be guessed by selecting an integer at random in the range 1 to 1000. The program then displays the following: I have a number between 1 and 1000. Can you guess my number? Please type in your first guess. The player then types the first guess. The program then responds with one of the following: 1.      ...
Write a program to allow a user to play the game Hangman. DO NOT USE AN...
Write a program to allow a user to play the game Hangman. DO NOT USE AN ARRAY The program will generate a random number (between 1 and 4581) to pick a word from the file - this is the word you then have to guess. Note: if the random number you generate is 42, you need the 42nd word - so loop 41 times picking up the word from the file and not doing anything with it, it is the...
Rock, Paper, Scissors Game Write a Python program rps.py that lets the user play the game...
Rock, Paper, Scissors Game Write a Python program rps.py that lets the user play the game of Rock, Paper, Scissors against the computer. The program should work as follows: You can set these constant global variables at the top outside of your main function definition: COMPUTER_WINS = 1 PLAYER_WINS = 2 TIE = 0 INVALID = 3 ROCK = 1 PAPER = 2 SCISSORS = 3 For this program 1 represents rock, 2 represents paper, and 3 represents scissors. In...
The game of Nim. This is a well-known game with a number of variants. The following...
The game of Nim. This is a well-known game with a number of variants. The following variant has an interesting winning strategy. Two players alternately take marbles from a pile. In each move, a player chooses how many marbles to take. The player must take at least one but at most half of the marbles. Then the other player takes a turn. The player who takes the last marble loses. Write a program in which the computer plays against a...
Write a Java program that lets the user play a game where they must guess a...
Write a Java program that lets the user play a game where they must guess a number between zero and one hundred (0-100). The program should generate a random number between 0 and 100 and then the user will try to guess the number. The program should help the user guess the number (see details below). The program must allow the user five attempts to guess the number. Further Instruction: (a) Declare a variable diff and assign to it the...
Write a program using Python that allows the user to play a guessing game (please provide...
Write a program using Python that allows the user to play a guessing game (please provide a picture of code so it is easier to read). The game will choose a "secret number", a positive integer less than 10000. The user has 10 tries to guess the number. Requirements: Normally, we would have the program select a random number as the "secret number". However, for the purpose of testing your program (as well as grading it), simply use an assignment...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT