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

This is a well­-known game with a number of variants. The following variant has

ID: 3820763 • Letter: T

Question

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.

Write a program 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 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 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 (i.e., uses stupid mode).

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.

Sample Output (user input shown in red)


run:
Current number of marbles in pile: 76
How many marbles do you want to remove:
12
Current number of marbles in pile: 64
Computer removes 1 marble(s).
Current number of marbles in pile: 63
How many marbles do you want to remove:
5
Current number of marbles in pile: 58
Computer removes 27 marble(s).
Current number of marbles in pile: 31
How many marbles do you want to remove:
15
Current number of marbles in pile: 16
Computer removes 1 marble(s).
Current number of marbles in pile: 15
How many marbles do you want to remove:
4
Current number of marbles in pile: 11
Computer removes 4 marble(s).
Current number of marbles in pile: 7
How many marbles do you want to remove:
3
Current number of marbles in pile: 4
Computer removes 1 marble(s).
Current number of marbles in pile: 3
How many marbles do you want to remove:
1
Current number of marbles in pile: 2
Computer removes 1 marble(s).
Current number of marbles in pile: 1
How many marbles do you want to remove:
1
Human took last marble, human loses.
BUILD SUCCESSFUL (total time: 6 minutes 4 seconds)

Explanation / Answer

Ans: I explained the code with comments. Thank you.

Program::

import java.util.Scanner; // util.scanner

// Game

public class Game {

// main

public static void main(String[] args) {

// scanner

Scanner input = new Scanner(System.in);

// size_pile , math.rondom() is used

int size_of_pile = (int) (Math.random() * 91) + 10;

// current_turn

int current_turn = (int) (Math.random() * 2);

// smart_oor_smart

int smart_or_stupid = (int) (Math.random() * 2);

// while(>0)

while (size_of_pile > 0) {

//sop- print

System.out.println("Current no. of marlbes in the pile is: " + size_of_pile);

// marbls_to_remve

int marbles_to_remove = 0;

// current_turn=0

if (current_turn == 0) {

// size of pile - 1,3,7,15,31,63

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)) {

// to remove

marbles_to_remove = (int) (Math.random() * (size_of_pile / 2 + 1)) + 1;

}

// els

else {

// if >63

if (size_of_pile > 63) {

// 63

marbles_to_remove = size_of_pile - 63;

}

// else if > 31

else if (size_of_pile > 31) {

// 31

marbles_to_remove = size_of_pile - 31;

}

// else if >15

else if (size_of_pile > 15) {

// 15

marbles_to_remove = size_of_pile - 15;

}

// else if >7

else if (size_of_pile > 7) {

// 7

marbles_to_remove = size_of_pile - 7;

}

// else if >3

else if (size_of_pile > 3) {

// 3

marbles_to_remove = size_of_pile - 3;

}

elsee

else {

// 1

marbles_to_remove = size_of_pile - 1;

}

}

// print

System.out.println("Computer_removes " + marble's_to_remov + " marble" + ((marbles_to_remove > 1)? "s": ""));

// current turn 1

current_turn = 1;

}

// elsee

else {

// do

do {

// ask how many marbles to remove

System.out.println("How many of __MARBLES you want to remove: ");

// to remove,, scanner

marbles_to_remove = input.nextInt();

// while marbles to rem not equal to 1 and <= 0 and > size of pile / 2

} while ((marbles_to_remove != 1) && (marbles_to_remove <= 0 || marbles_to_remove > size_of_pile / 2));

// curr turn = 0

current_turn = 0;

}

// size of pile = size of pile - marbles to remove

size_of_pile -= marbles_to_remove;

}

// close

input.close();

// if curr turn = 0

if (current_turn == 0) {

// computr wins

System.out.println("Human has took the last__marble and computer wins!!!");

}   

// else

else {

// human wins

System.out.println("Computr has took last__marble and human wins!!!");

}

}

}

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