Fill in the STARTER CODE with proper code. FIll in where comments say TODO. Comp
ID: 3728425 • Letter: F
Question
Fill in the STARTER CODE with proper code. FIll in where comments say TODO. Complete the starter program (Nim.java) in which the computer plays against a human opponent. You may NOT use try/catch statements. Generate a random integer between 10 and 100 to denote the initial size of the pile. Generate a random boolean to decide whether the computer or the human takes the first turn. Generate a random boolean to decide whether the computer plays smart or stupid. In stupid mode the computer simply takes a random legal value (between 1 and n/2) from the pile whenever it has a turn. In smart mode the computer takes off enough marbles to make the size of the pile a power of two minus 1—that is, 3, 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.
You will note that the computer cannot be beaten in smart mode when 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 and knows the winning strategy can win against the computer.
You must check that the user enters a valid number of marbles (between 1 and n/2). Examples:
STARTER CODE
import java.util.Scanner;
/**
* 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.
*
*
*
*/
public class Nim {
/** 2^1 */
public static final int TWO_ONE = 2;
/** 2^2 */
public static final int TWO_TWO = 4;
/** 2^3 */
public static final int TWO_THREE = 8;
/** 2^4 */
public static final int TWO_FOUR = 16;
/** 2^5 */
public static final int TWO_FIVE = 32;
/** 2^6 */
public static final int TWO_SIX = 64;
/**
* Starts the program
*
* @param args command line arguments
*/
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
// TODO: Create Random object named r
// TODO: Use r to set (int) pile to value random integer between 10 and 100 to
// denote the initial size of the pile
System.out.println("Start pile: " + pile);
// TODO: Use r to generate a random boolean to decide whether the computer or
// the human takes the first turn store as boolean humanTurn
System.out.println("Human first: " + humanTurn);
// Use r to generate a random boolean to decide whether the computer
// plays smart or stupid store as boolean smartComputer
System.out.println("Smart computer: " + smartComputer);
while (pile != 0) {// while there are still more marbles
int remove = 0;
if (humanTurn) {// equivalent to humanTurn==true
if (pile == 1) {
System.out.println("You (human) must remove the last marble.");
remove = 1;
} else {
// TODO: Prompt user for a valid number of marbles to take
// (between 1 and pile/2). Continually prompt if int outside
// of range or non-int value.
}
} else { // Computer's turn
if (smartComputer) { // smart computer mode
if (pile == TWO_ONE - 1 || pile == TWO_TWO - 1 || pile == TWO_THREE - 1
|| pile == TWO_FOUR - 1 || pile == TWO_FIVE - 1
|| pile == TWO_SIX - 1) {
// Random number
if (pile > 1) {
remove = r.nextInt(pile / 2) + 1;
} else {
remove = 1;
}
} else { // Smart guess
// TODO: In smart mode the computer takes off enough marbles
// to make the size of the pile a power of two minus
// 1—that is, 3, 7, 15, 31, or 63.
}
} else { // stupid computer mode
if (pile > 1) {
remove = r.nextInt(pile / 2) + 1;
} else {
remove = 1;
}
}
System.out.println("Computer removed " + remove + " marble(s).");
}
pile = pile - remove;
System.out.println("Current pile: " + pile);
humanTurn = !humanTurn;
}
if (humanTurn) {
System.out.println("Computer took last marble. Human won!");
} else {
System.out.println("Human took last marble. Computer won!");
}
}
}
Explanation / Answer
import java.util.Scanner;
/**
* 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.
*
*
*
*/
public class Nim {
/** 2^1 */
public static final int TWO_ONE = 2;
/** 2^2 */
public static final int TWO_TWO = 4;
/** 2^3 */
public static final int TWO_THREE = 8;
/** 2^4 */
public static final int TWO_FOUR = 16;
/** 2^5 */
public static final int TWO_FIVE = 32;
/** 2^6 */
public static final int TWO_SIX = 64;
/**
* Starts the program
*
* @param args command line arguments
*/
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
// TODO: Create Random object named r
Random r=new Random();
// TODO: Use r to set (int) pile to value random integer between 10 and 100 to
// denote the initial size of the pile
int min=10;
int max=100;
int pile=r.nextInt((max-min)+1)+min;
System.out.println("Start pile: " + pile);
// TODO: Use r to generate a random boolean to decide whether the computer or
// the human takes the first turn store as boolean humanTurn
boolean humanTurn=r.nextBoolean();
System.out.println("Human first: " + humanTurn);
// Use r to generate a random boolean to decide whether the computer
// plays smart or stupid store as boolean smartComputer
boolean smartComputer=r.nextBoolean();
System.out.println("Smart computer: " + smartComputer);
while (pile != 0) {// while there are still more marbles
int remove = 0;
if (humanTurn) {// equivalent to humanTurn==true
if (pile == 1) {
System.out.println("You (human) must remove the last marble.");
remove = 1;
} else {
// TODO: Prompt user for a valid number of marbles to take
// (between 1 and pile/2). Continually prompt if int outside
// of range or non-int value.
do
{
System.out.println("Please take any number of marbles between 1 and "+pile/2);
int p=console.nextInt();
if(p>=1 && p<=pile/2)
{
if(p%10==0)
{
remove=p;
break;
}
}
}while(1);
}
} else { // Computer's turn
if (smartComputer) { // smart computer mode
if (pile == TWO_ONE - 1 || pile == TWO_TWO - 1 || pile == TWO_THREE - 1
|| pile == TWO_FOUR - 1 || pile == TWO_FIVE - 1
|| pile == TWO_SIX - 1) {
// Random number
if (pile > 1) {
remove = r.nextInt(pile / 2) + 1;
} else {
remove = 1;
}
} else { // Smart guess
// TODO: In smart mode the computer takes off enough marbles
// to make the size of the pile a power of two minus
// 1—that is, 3, 7, 15, 31, or 63.
}
} else { // stupid computer mode
if (pile > 1) {
remove = r.nextInt(pile / 2) + 1;
} else {
remove = 1;
}
}
System.out.println("Computer removed " + remove + " marble(s).");
}
pile = pile - remove;
System.out.println("Current pile: " + pile);
humanTurn = !humanTurn;
}
if (humanTurn) {
System.out.println("Computer took last marble. Human won!");
} else {
System.out.println("Human took last marble. Computer won!");
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.