Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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. 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 1that 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:

Start pile: 81 Human first: true Smart computer: true How many marbles (between 1 and 40) do you want to remove: 18 Current pile: 63 Computer removed 6 marble(s). Current pile: 57 How many marbles (between 1 and 28) do you want to remove: 26 Current pile: 31 Computer removed 13 marble(s). Current pile: 18 How many marbles (between 1 and 9) do you want to remove: 3 Current pile: 15 Computer removed 5 marble(s). Current pile: 10 How many marbles (between 1 and 5) do you want to remove: 3 Current pile: 7 Computer removed 2 marble(s). Current pile: 5 How many marbles (between 1 and 2) do you want to remove: 2 Current pile: 3 Computer removed 1 marble(s). Current pile: 2 How many marbles (between 1 and 1) do you want to remove: 1 Current pile: 1 Computer removed 1 marble(s). Current pile: 0 Computer took last marble. Human won! 
Start pile: 30 Human first: false Smart computer: true Computer removed 15 marble(s). Current pile: 15 How many marbles (between 1 and 7) do you want to remove: 7 Current pile: 8 Computer removed 1 marble(s). Current pile: 7 How many marbles (between 1 and 3) do you want to remove: 2 Current pile: 5 Computer removed 2 marble(s). Current pile: 3 How many marbles (between 1 and 1) do you want to remove: 1 Current pile: 2 Computer removed 1 marble(s). Current pile: 1 You (human) must remove the last marble. Current pile: 0 Human took last marble. Computer won! 

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

// 1that 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!");

}

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2010 Barcelona Spain September 2010 Proceedings Part 1 Lnai 6321

Authors: Jose L. Balcazar ,Francesco Bonchi ,Aristides Gionis ,Michele Sebag

2010th Edition

364215879X, 978-3642158797

More Books

Students also viewed these Databases questions