Solve the problem with java code The game of Nim. This is a well-known game with
ID: 3916192 • Letter: S
Question
Solve the problem with java code
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 kes a turn. The player who takes the last marble loses. Write a program in which the computer plays against a human opponent. Generate a ta 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 er 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.*;
public class Nim
{
public static void main(String[] args)
{
startGame();
}
public static void startGame()
{
Scanner in = new Scanner(System.in);
Random generator = new Random();
int numPlayers = 1;
Boolean smartComputer = true;
Boolean twoPlayer = false;
System.out.println("Nim");
System.out.println("---");
System.out.println("How many players:");
System.out.println("a). One Player against Smart Computer,");
System.out.println("b). One Player against Dumb Computer, or");
System.out.println("c). Two Players?");
String choice = in.nextLine();
if (choice.toLowerCase().equals("a")) {
System.out.println("Playing with a Smart Computer.");
smartComputer = true;
} else if (choice.toLowerCase().equals("b")) {
System.out.println("Playing against a Dumb Computer.");
smartComputer = false;
} else if (choice.toLowerCase().equals("c")) {
numPlayers = 2;
System.out.println("Starting with two players.");
twoPlayer = true;
} else {
System.out.println("Error - please enter a, b, or c.");
Nim.startGame();
}
int numMarbles = generator.nextInt(90) + 10;
System.out.println("Playing with " + numMarbles + " marbles.");
if (twoPlayer) {
twoPlayerGame(numMarbles);
} else {
aiGame(smartComputer, numMarbles);
}
}
public static void twoPlayerGame(int marbles)
{
Random generator = new Random();
Scanner in = new Scanner(System.in);
Boolean playerOneGoesFirst;
Boolean playerOneWins = true;
Player playerOne = new Player();
Player playerTwo = new Player();
Pile thePile = new Pile(marbles);
if (generator.nextInt(2) == 1) {
System.out.println("Player one goes first.");
playerOneGoesFirst = true;
} else {
System.out.println("Player two goes first.");
playerOneGoesFirst = false;
}
while (thePile.getMarbles() != 0) {
if (playerOneGoesFirst) {
playerOneWins = false;
System.out.println("Player One's Turn:");
thePile.removeMarbles(playerOne.promptPlayer(thePile.getMarbles()));
if (thePile.getMarbles() != 0) {
System.out.println("Player Two's Turn:");
thePile.removeMarbles(playerTwo.promptPlayer(thePile.getMarbles()));
} else {
playerOneWins = true;
}
} else {
playerOneWins = true;
System.out.println("Player Two's Turn:");
thePile.removeMarbles(playerTwo.promptPlayer(thePile.getMarbles()));
if (thePile.getMarbles() != 0) {
System.out.println("Player One's Turn:");
thePile.removeMarbles(playerOne.promptPlayer(thePile.getMarbles()));
} else {
playerOneWins = false;
}
}
}
if (playerOneWins) {
System.out.println("Player One Wins!");
} else {
System.out.println("Player Two Wins!");
}
gameOverPrompt();
}
public static void aiGame(Boolean smartComputer, int marbles)
{
Random generator = new Random();
Scanner in = new Scanner(System.in);
Boolean playerGoesFirst;
Boolean youWin = true;
Player playerOne = new Player();
Pile thePile = new Pile(marbles);
AI theComputer = new AI(smartComputer);
if (generator.nextInt(2) == 1) {
System.out.println("Computer goes first.");
playerGoesFirst = false;
} else {
System.out.println("You go first.");
playerGoesFirst = true;
}
while (thePile.getMarbles() != 0) {
if (playerGoesFirst) {
youWin = false;
thePile.removeMarbles(playerOne.promptPlayer(thePile.getMarbles()));
if (thePile.getMarbles() != 0) {
thePile.removeMarbles(theComputer.takeTurn(thePile.getMarbles()));
} else {
youWin = true;
}
} else {
youWin = true;
thePile.removeMarbles(theComputer.takeTurn(thePile.getMarbles()));
if (thePile.getMarbles() != 0) {
thePile.removeMarbles(playerOne.promptPlayer(thePile.getMarbles()));
} else {
youWin = false;
}
}
}
if (youWin) {
System.out.println("You won!");
} else {
System.out.println("You lost.");
}
gameOverPrompt();
}
public static void gameOverPrompt()
{
Scanner in = new Scanner(System.in);
System.out.println("Play again? [Y/N]");
String choice = in.nextLine();
if (choice.toLowerCase().equals("y")) {
System.out.println("Restarting game...");
startGame();
} else if (choice.toLowerCase().equals("n")) {
System.out.println("Bye!");
} else {
System.out.println("Invalid choice, please try again.");
gameOverPrompt();
}
}
}
class AI
{
private Boolean amISmart;
public AI(Boolean smartComputer)
{
if (smartComputer) {
amISmart = true;
} else {
amISmart = false;
}
}
public int takeTurn(int numMarbles)
{
int takeAmount = 1;
Random generator = new Random();
if (amISmart) {
for (int i = 1; i <= (numMarbles / 2); i++) {
int t = numMarbles - i;
if (t == 3 || t == 7 || t == 15 || t == 31 || t == 63) {
takeAmount = i;
}
}
if (takeAmount == 0) {
takeAmount = generator.nextInt((numMarbles / 2) - 1) + 1;
}
} else {
takeAmount = generator.nextInt((numMarbles / 2) - 1) + 1;
}
System.out.println("The computer took " + takeAmount + " marbles.");
return takeAmount;
}
}
class Pile
{
private int numMarbles;
public Pile(int marbles)
{
numMarbles = marbles;
}
public int getMarbles()
{
return numMarbles;
}
public void removeMarbles(int marbles)
{
numMarbles -= marbles;
}
}
class Player
{
public int promptPlayer(int numMarbles)
{
Scanner in = new Scanner(System.in);
Boolean success = false;
int takeAmount = 0;
System.out.println("There are " + numMarbles + " marbles left.");
while (!success) {
System.out.print("> ");
takeAmount = in.nextInt();
if (takeAmount <= 0) {
System.out.println("Nice try. Please enter an amount above zero.");
} else if ((takeAmount > (numMarbles / 2)) && (numMarbles != 1)) {
System.out.println("There are only " + numMarbles + " marbles in the pile.");
System.out.println("You can only take up to half of the pile.");
} else {
System.out.println("Okay, " + takeAmount + " marbles were taken from the pile.");
success = true;
}
}
return takeAmount;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.