Question 1 [35 marks] - The stone game

Write a program to play the following game where the computer is one of the players.

Two players take turns removing stones from a pile. In each move, the player decides how many stones to remove. The player must take at least one stone and at most half of the pile. Then the other player takes a turn. The player who removes the last stone loses.

Generate a random integer between 10 and 100 for the initial number of stones. (Use import java.util.Random;.) Generate a random integer between 0 and 1 to decide who goes first. Generate a random integer between 0 and 1 to decide whether the computer plays clever or stupid. In stupid mode, the computer simply takes a random legal number of stones from the pile, whenever it has a turn. In smart mode, the computer takes, off enough stones to make the size of the remaining pile a power of two minus one (i.e. 3,7,15,31 or 63), if that is a legal move. If it's not legal, the computer makes a random legal move.


View the source code

Back