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

P6.6 The game of Nim. This is a well-known game with a number of variants. The f

ID: 3592301 • Letter: P

Question

P6.6 The game of Nim. This is a well-known game with a number of variants. The fol- lowing 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 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.

Explanation / Answer

import java.util.Random;

public class Game

{

public static void main(String args[]) {

//declare variables

Pile marbles = new Pile();

Random random = new Random();

Player player1;

Player player2;

int marblesToRemove = 0; //determine who goes first

if(random.nextInt(2) == 0)

{

System.out.println("User goes first ");

player1 = new Player(2);

player2 = new Player();

}

else

{

System.out.println("Computer goes first ");

player1 = new Player();

player2 = new Player(2);

}

//keep going until all the marbles are gone

while(marbles.getMarbles() > 0)

{

//check if player two wins

if(marbles.getMarbles() == 1)

{

System.out.println("Player 2 Wins!!!");

break;

}

//prompt/calculate number of marbles to remove

System.out.println("Number of marbles left: " + marbles.getMarbles());

System.out.println("Player 1's turn:");

marblesToRemove = player1.chooseNextMove(marbles.getMarbles());

System.out.println("Player 1 removed " + marblesToRemove);

System.out.println();

//remove the marbles

marbles.removeMarbles(marblesToRemove);

//check if player one wins

if(marbles.getMarbles() == 1)

{

System.out.println("Player 1 Wins!!!");

break;

}

//prompt/calculate number of marbles to remove

System.out.println("Number of marbles left: " + marbles.getMarbles());

System.out.println("Player 2's turn:");

marblesToRemove = player2.chooseNextMove(marbles.getMarbles());

System.out.println("Player 2 removed " + marblesToRemove);

System.out.println();

//remove marbles

marbles.removeMarbles(marblesToRemove);

}

}

}

import java.util.Random;

public class Pile

{

//fields

int numMarbles = 0;

//generate the number of marbles

public Pile()

{

Random random = new Random();

numMarbles = random.nextInt(89) + 11;

}

//get method

public int getMarbles()

{

return numMarbles;

}

//remove method

public void removeMarbles(int marblesToRemove)

{

numMarbles -= marblesToRemove;

}

}

import java.util.Random;

import java.util.Scanner;

public class Player

{

int playerType = 0;

// 0 dumb, 1 smart, 2 player

Random random = new Random();

Scanner input;

//constructor where player type is specified

public Player(int type)

{

if (type < 0 || type > 2)

playerType = 2;

else

playerType = type;

input = new Scanner(System.in);

}

//constructor where player type is specified

public Player()

{

playerType = random.nextInt(2);

input = new Scanner(System.in);

}

//pick next move

public int chooseNextMove(int marblesLeft)

{

//setup variables

int marblesToRemove = 0;

//remove the second to last marble

if(marblesLeft == 3)

return 1;

//if dumb, pick random number

if(playerType == 0)

{

marblesToRemove = random.nextInt((marblesLeft/2) - 1) + 1;

}

//if smart get to specific numbers

else if (playerType == 1)

{

if(marblesLeft > 63)

marblesToRemove = marblesLeft - 63;

else if(marblesLeft > 31)

marblesToRemove = marblesLeft - 31;

else if(marblesLeft > 15)

marblesToRemove = marblesLeft - 15;

else if(marblesLeft > 7)

marblesToRemove = marblesLeft - 7;

else if(marblesLeft > 3)

marblesToRemove = marblesLeft - 3;

else if(marblesLeft == 2)

marblesToRemove = 1;

//pick random number

else

marblesToRemove = random.nextInt((marblesLeft/2) - 1 ) + 1;

}

else

{

//prompt user

System.out.println("Please enter a number of marbles to remove between 1 and " + (marblesLeft / 2));

marblesToRemove = input.nextInt();

//error check

while(marblesToRemove > marblesLeft / 2 || marblesToRemove < 0)

{

System.out.println("That is not a valid entry, please try again (1 - " + (marblesLeft / 2) + ")");

marblesToRemove = input.nextInt();

}

}

//return values

return marblesToRemove;

}

}