Write a java program: Using only the main method, no other methods aloud. The ga
ID: 3865489 • Letter: W
Question
Write a java program: Using only the main method, no other methods aloud.
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 the number of remaining 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 human opponent. Generate a random integer, 0 or 1 to decide if the computer or the human takes the first turn. Then generate a random number in the range [10,100] to denote the initial size of the number of marbles. Generate another random integer [0,1] to decide whether the computer plays expert or novice. In novice mode, the computer simply takes a random legal number of marbles (between 1 and n/2) from the pile whenever it has a turn. In expert mode the computer takes off enough marbles to make the size of the remaining pile a power of two minus 1, that is 3, 5, 7, 15, 31 or 63. That is always a legal move, except when the size of the pile is currently one less than a power of two. In that case, the computer makes a random legal move.
Before the game starts, state how many marbles are in the original pile, who is going first and which mode the computer is using (expert or novice). After each move, state who made the move, how many marbles the player removed and how many marbles remain in the pile. When there is one marble left in the pile, print who won the game.
You will note that the computer cannot be beaten in expert mode when it has the first move, unless the initial pile size happens to be 15, 31, or 63. Of course, a human player who has the first turn and knows the winning strategy can win against the computer.
Explanation / Answer
import java.util.Scanner;
public class P4_22 {
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
int size_of_pile = (int) (Math.random() * 91) + 10;
int current_turn = (int) (Math.random() * 2);
int smart_or_stupid = (int) (Math.random() * 2);
while (size_of_pile > 0)
{
System.out.println("Current number of marlbes in pile: " + size_of_pile);
int marbles_to_remove = 0;
if (current_turn == 0)
{
if (smart_or_stupid == 1 || (size_of_pile == 1 || size_of_pile == 3
|| size_of_pile == 7 || size_of_pile == 15 || size_of_pile == 31
|| size_of_pile == 63))
{
marbles_to_remove = (int) (Math.random() * (size_of_pile / 2 + 1)) + 1;
}
else
{
if (size_of_pile > 63)
{
marbles_to_remove = size_of_pile - 63;
}
else if (size_of_pile > 31)
{
marbles_to_remove = size_of_pile - 31;
}
else if (size_of_pile > 15)
{
marbles_to_remove = size_of_pile - 15;
}
else if (size_of_pile > 7)
{
marbles_to_remove = size_of_pile - 7;
}
else if (size_of_pile > 3)
{
marbles_to_remove = size_of_pile - 3;
}
else
{
marbles_to_remove = size_of_pile - 1;
}
}
System.out.println("Computer removes " + marbles_to_remove + " marble" + ((marbles_to_remove > 1)? "s": ""));
current_turn = 1;
}
else
{
do
{
System.out.println("How many marbles do you want to remove: ");
marbles_to_remove = input.nextInt();
} while ((marbles_to_remove != 1) && (marbles_to_remove <= 0 || marbles_to_remove > size_of_pile / 2));
current_turn = 0;
}
size_of_pile -= marbles_to_remove;
}
input.close();
if (current_turn == 0)
{
System.out.println("Human took last marble, computer wins!");
}
else
{
System.out.println("Computer took the last marble, human wins!");
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.