Question
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
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.
The Nimatron (Links to an external site.) from 1940 is considered to be one of the first computer games.
Make sure your program tells the user whether the computer played in smart or stupid mode and provides other useful communication as the game is played.
Your program will need to use both decisions (if statements) and loops. Do not use anything that has not been introduced in chapters 1 - 4 of the text.
Remember, the Java Math.random() (Links to an external site.) method returns a double value >= 0.0 and < 1.0. To get a random integer between two integer values (Min and Max) use:
Min + (int)(Math.random() * ((Max - Min) + 1))
This is for java
Here is my code that I have I need some help:
// Scanner is in the class import java.util.Scanner;
// Math library is in the class import java.lang.Math;
public class GameOfNim { // Main method public static void main(String args[]) { // Min and Max to see who goes first if it is 0 then the user goes //if it is a 1 then the computer goes final double MIN = 0.0; final double MAX = 1.0; // Scanner in the class Scanner in = new Scanner (System.in); int takenOutOfPile; int userNumber; System.out.println("Hello welcome to The Game Of Nim."); System.out.println("What's your name?"); // Getting the user's name String name = in.nextLine(); System.out.println("Would you like the computer to play in smart mode or normal mode? Please pick by typing in Smart or typing in Normal."); // Getting the level the user wants to play on. String difficult = in.next(); // Making the input case senseitive. difficult = difficult.trim().toLowerCase(); System.out.println("Hello " + name + ", you have chose to face me in " + difficult + " mode. Good luck!"); System.out.println("We are going to pick to see who goes first if the number is 0 then you go if the number is 1 then I will go."); // Making the random number to see who goes first. int selectRandom = (int)(Math.random()*((MAX-MIN)+1)); System.out.println("The random number is: " + selectRandom); System.out.println("Now I will determine how many marbles we will start out with by picking a random number from 10 to 100."); int marbleSize = (int)(Math.random()*((100-10)+10)); System.out.println("There are " + marbleSize + " marbles in the pile"); while(marbleSize > 0) { if(difficult.equals("smart") && selectRandom == 1) { System.out.println("It is my turn."); } }
} }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started