Complete the starter program (Nim.java) in which the computer plays against a hu
ID: 3727774 • Letter: C
Question
Complete the starter program (Nim.java) in which the computer plays against a human opponent. 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
package nim;
import java.util.Random;
import java.util.Scanner;
public class Nim {
public static void main(String[] args) {
int pile;
int marbles;
int num=0;
boolean turn,smart;
//Generate a random integer between 10 and 100 to denote the initial size of the pile.
Random r = new Random();
int Low = 10;
int High = 100;
pile = r.nextInt(High-Low) + Low;
System.out.println("Start pile: "+pile);
//Generate a random boolean to decide whether the computer or the human takes the first turn.
Random b = new Random();
turn=b.nextBoolean();
if(turn==true)
System.out.println("Human first: "+"true");
else
System.out.println("Human first: "+"false");
//Generate a random boolean to decide whether the computer plays smart or stupid.
b = new Random();
smart=b.nextBoolean();
if(smart==true)
System.out.println("Smart computer: "+"true");
else
System.out.println("Smart computer: "+"false");
Scanner input=new Scanner(System.in);
marbles=pile/2;
while(true){
if(smart==false)//stupid mode
r = new Random();
Low = 1;
High = marbles;
num = r.nextInt(High-Low) + Low;
System.out.println("How many marbles (between 1 and "+marbles+") do you want to remove: "+num);
r = new Random();
Low = 1;
High = marbles;
num = r.nextInt(High-Low) + Low;
System.out.println("Current pile: "+pile);
System.out.println("Computer removed "+num+ "marble(s).");
if(pile==0)
{
if(turn==true)
System.out.println("Computer took last marble. Human won!");
else
System.out.println("Human took last marble. Computer won!");
break;
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.