Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The game of Nim. This is a well-known game with a number of variants. We will co

ID: 3532227 • Letter: T

Question

The game of Nim. This is a well-known game with a number of variants. We will consider the following variants, which 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.

Write a program and create a Flow Chart in which 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 integer between 0 and 1 to decide whether the computer or the human takes the first turn. Generate a random integer between 0 and 1 to decide whether the computer plays smart or stupid. In stupid model, the computer simply takes a random legal value (between 1 and n/2) from the pile whenever it has a turn. In smart model the computer takes off enough marbles to make the size of the pile a power of 2 minus 1

Explanation / Answer

This code might help you


int height = (int)(Math.random() * 90 + 10);


PileOfMarbles pile = new PileOfMarbles(height);


System.out.println("Game starts with a pile of height: "

+ pile.report());


int number, currentHeight;


while (true) {


System.out.println("*** Computer moves.");


System.out.println("Pile of marbles of height: " + pile.report());


currentHeight = pile.report();


if (currentHeight == 1) {

number = 1;

} else {

number = (int)(Math.random() * (currentHeight / 2)) + 1;

}


System.out.println("Computer chooses to remove: " +

number + " marbles.");


pile.move(number, "Computer");


System.out.println("--------------------------");


System.out.println("*** Now " + user + " has to move.");


System.out.println("Pile of marbles of height: " + pile.report());


System.out.print(user +

", please enter number of marbles you want to take: ");


number = console.readInt();

pile.move(number, user);

System.out.println("--------------------------");

}



class Nim {

public static void main(String[] args) {


ConsoleReader console = new ConsoleReader(System.in);

System.out.println("Hello, and welcome to the game of Nim!");

System.out.print("What is your name: ");

String user = console.readLine();

int height = (int)(Math.random() * 90 + 10);

PileOfMarbles pile = new PileOfMarbles(height);

System.out.println("Game starts with a pile of height: "

+ pile.report());

int number, currentHeight;

while (true) {

System.out.println("*** Computer moves.");

System.out.println("Pile of marbles of height: " +

pile.report());

currentHeight = pile.report();

if (currentHeight == 1) {

number = 1;

} else {

number = (int)(Math.random() * (currentHeight / 2)) + 1;

}

System.out.println("Computer chooses to remove: " +

number + " marbles.");

pile.move(number, "Computer");

System.out.println("--------------------------");

System.out.println("*** Now " + user + " has to move.");

System.out.println("Pile of marbles of height: " +

pile.report());

System.out.print(user +

", please enter number of marbles you want to take: ");

number = console.readInt();

pile.move(number, user);

System.out.println("--------------------------");

}

}

}


class PileOfMarbles {

int height;

PileOfMarbles (int height) {

this.height = height;

}

int report() {

return this.height;

}

void move(int number, String user) {

System.out.println("***Removing " + number +

" marbles from the pile for: " + user);

if (number <= 0 || ((number > height / 2) && (number != 1))) {


System.out.println("***Bad move for " + user + ". " +

user + " loses.");


System.exit(0);

} else {

this.height -= number;

if (this.height == 0) {

System.out.println("***End of game. " +

user + " loses.");

System.exit(0);

}

}

System.out.println("Pile of marbles is now: " + this.report());

}

}



Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote