Menu

(Solved) : Game Nim Well Known Game Number Variants Following Variant Interesting Winning Strategy Tw Q26781975 . . .

The game of Nim

This is a well-known game with a number of variants. Thefollowing variant has an interesting winning strategy. Two playersalternately take marbles from a pile. In each move, a playerchooses how many marbles to take. The player must take at least onebut at most half of the marbles. Then the other player takes aturn. The player who takes the last marble loses.

Write a program in which the computer plays against a humanopponent. Generate a random integer between 10 and 100 to denotethe initial size of the pile. Generate a random integer between 0and 1 to decide whether the computer or the human takes the firstturn. Generate a random integer between 0 and 1 to decide whetherthe computer plays smart or stupid. In stupid mode the computersimply takes a random legal value (between 1 and n / 2) from thepile whenever it has a turn. In smart mode the computer takes offenough marbles to make the size of the pile a power of two minus1—that is, 3, 7, 15, 31, or 63. That is always a legal move, exceptwhen the size of the pile is currently one less than a power oftwo. In that case, the computer makes a random legal move.

You will note that the computer cannot be beaten in smart modewhen it has the first move, unless the pile size happens to be 15,31, or 63. Of course, a human player who has the first turn andknows the winning strategy can win against the computer. Below isan example of a game run:

Example Output:

Current number of marbles in pile: 95

Computer removes 12 marble(s).

Current number of marbles in pile: 83

How many marbles do you want to remove: 12

Current number of marbles in pile: 71

Computer removes 17 marble(s).

Current number of marbles in pile: 54

How many marbles do you want to remove: 20

Current number of marbles in pile: 34

Computer removes 6 marble(s).

Current number of marbles in pile: 28

How many marbles do you want to remove: 28

How many marbles do you want to remove: 25

How many marbles do you want to remove: 20

How many marbles do you want to remove: 12

Current number of marbles in pile: 16

Computer removes 4 marble(s).

Current number of marbles in pile: 12

How many marbles do you want to remove: 12

How many marbles do you want to remove: 4

Current number of marbles in pile: 8

Computer removes 4 marble(s).

Current number of marbles in pile: 4

How many marbles do you want to remove: 6

How many marbles do you want to remove: 3

How many marbles do you want to remove: 2

Current number of marbles in pile: 2

Computer removes 1 marble(s).

Current number of marbles in pile: 1

How many marbles do you want to remove: 1

User took last marble. Computer wins!

Expert Answer


Answer to Game Nim Well Known Game Number Variants Following Variant Interesting Winning Strategy Tw Q26781975 . . .

OR