Untitled
unknown
plain_text
a year ago
5.0 kB
3
Indexable
/**
* Nim.java
* This program will let you play a 2 player game called "Nim".
* Filip Ostrowski
* Nov 19, 2024
*/
import java.util.Random;
import java.util.Scanner;
public class Nim {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Random rand = new Random();
int stones = rand.nextInt(16) + 15; //Generate random # between 15-30
int userInput, computerInput; //Get user input + the computer generated input
System.out.println("Welcome to the game of Nim!");
while (stones > 0) { //Begin the game by starting the loop, and the loop only ends if there are no stones left
//Players turn
userInput = userTurn(stones); //First method, userTurn
stones -= userInput; //Total stones subracted by the users amount
if (stones == 0) {
System.out.println("The computer beats you!"); //If stones = 0 during the players turn the computer wins.
break;
}
//Computers turn
computerInput = computerTurn(stones); //Second method, computerTurn
stones -= computerInput; //Total stones after users turn subracted by the computer generated amount
System.out.println("You took " + userInput + " stones and the computer takes " + computerInput + " stones. There are " + stones + " stones left.");
if (stones == 0) {
System.out.println("The player beats the computer!"); //If stones = 0 during the computers turn the player wins.
break;
}
}
}
/**
* userTurn
* Pre : The # of stones remaining and the Scanner for user input
* Post : Returns the valid number of stones the user wants to take
*/
public static int userTurn(int stonesLeft) {
Scanner input = new Scanner(System.in); // Create the Scanner object inside the method
int userInput;
System.out.println("There are " + stonesLeft + " stones. How many would you like?"); //Ask how many stones the user wants to take
userInput = input.nextInt(); //Take user input
while (userInput < 1 || userInput > 3 || userInput > stonesLeft) { //Check if they picked numbers 1, 2, or 3. If not repeat loop until they do
System.out.println("Invalid choice. Please pick 1, 2, or 3.");
userInput = input.nextInt();
}
return userInput; //When they pick valid choice return the users #
}
/**
* computerTurn
* Pre : The number of stones remaining
* Post : Returns a random valid number of stones for the computer to take
*/
public static int computerTurn(int stonesLeft) {
int computerInput;
//Artifical Intelligence
if (stonesLeft==8) { //8 stones on P turn, C takes 3
computerInput=3;
//Difficulty + Remove if you want ez. /Comment out
}else if (stonesLeft==15) { //7 stones on P turn, C takes 2
computerInput=3;
}else if (stonesLeft==14) { //7 stones on P turn, C takes 2
computerInput=2;
}else if (stonesLeft==13) { //7 stones on P turn, C takes 2
computerInput=1;
}else if (stonesLeft==11) { //7 stones on P turn, C takes 2
computerInput=1;
}else if (stonesLeft==10) { //7 stones on P turn, C takes 2
computerInput=3;
}else if (stonesLeft==9) { //7 stones on P turn, C takes 2
computerInput=1;
}else if (stonesLeft==7) { //7 stones on P turn, C takes 2
computerInput=2;
}else if (stonesLeft==6) { //6 stones on P turn, C takes 1
computerInput=1;
}else if (stonesLeft==5) { //5 stones on P turn, C takes 1
computerInput=1;
}else if (stonesLeft==4) { //4 stones on P turn, C takes 3
computerInput=3;
}else if (stonesLeft==3) { //3 stones on P turn, C takes 2
computerInput=2;
}else if (stonesLeft==2) { //2 stones on P turn, C takes 1
computerInput=1;
}else if (stonesLeft==1) {
computerInput=1;
}else { //If there are more then 8 stones, C picks random #
computerInput=drawStones(stonesLeft);
}
return computerInput;
}
/**
* isValidEntry
* Pre : The user's input and the number of stones remaining
* Post : Returns true if the input is valid
*/
public static boolean isValidEntry(int take, int stonesLeft) {
return take >= 1 && take <= 3 && take <= stonesLeft; //Checks if trying to take less than 1, more than 3, or more than the amount left in the game
}
/**
* drawStones
* Pre : The number of stones remaining
* Post : Returns a random valid number of stones for the computer to take
*/
public static int drawStones(int stonesLeft) {
Random rand = new Random();
int stonesTaken = rand.nextInt(3) + 1;
return stonesTaken;
}
}
Editor is loading...
Leave a Comment