So, there are two problems with the code that I'm trying to compile.
1. The program simply isn't setting the variable marblePile to the value that I input in the Scanner class.
2. The program just ends as soon as it's the computer's turn to play.
The program is a simulation of the game of Nim, where the object is to take the last marble. However it's just a ton of sloppy code that won't properly compile.
import java.util.Random; import java.util.Scanner; /** * Write a description of class Player here. * * @author Daisy * @version 0.01 */ public class Player { // instance variables - replace the example below with your own private int cpuTakeNum; private int difficulty; private int turnOrder; private int takeNum; private int marblePile; private int marblesRemaining; /** * Constructor for objects of class Player */ public Player() { } /** * Sets the difficulty level of the Computer player. * * @param difficulty The difficulty of the computer * @return (Return type is void.) */ public void setCpuDifficulty(){ Random coinToss = new Random(); difficulty = coinToss.nextInt(2); System.out.println ("The computer's difficulty has been set to difficult."); } public void gameFlow(){while (marblesRemaining != 0) if (turnOrder == 1){playerTake(); if (difficulty == 1) {easyCpuTake();} else if (difficulty == 0) {hardCpuTake();} if (turnOrder == 0) { if (difficulty == 1){easyCpuTake();} else if (difficulty == 0) {hardCpuTake(); playerTake();}} } } /** * Takes a given number of marbles. * * @param takeNum The number of marbles being taken. * @return (Return type is void.) */ public void playerTake() { Scanner marbleTake = new Scanner(System.in); System.out.println("Please enter a number of marbles to take:"); takeNum = marbleTake.nextInt(); marblePile = marblePile - takeNum; System.out.println ( takeNum + " marble(s) have been taken from the pile."); System.out.println ("There are now " + marblePile + " marbles remaining."); if (marblePile == 0) {System.out.println("You have taken the last marble. You Win.");} } public void easyCpuTake(){ Random easyCpu = new Random(marblePile/2); cpuTakeNum = easyCpu.nextInt(); marblePile = marblePile - cpuTakeNum; } /** * Sets the turn order of the game. * * @param turnOrder The order of the turns in the game. * @return (Return type is void.) */ public void setTurnOrder(){ Random coinToss = new Random(); turnOrder = coinToss.nextInt(2); if (turnOrder == 1) System.out.println("The Player will Start."); else if (turnOrder == 0) System.out.println("The Computer will Start."); } /** * Take method for easy computer difficulty. Takes a random number of marbles up to half of the pile. * * @param turnOrder The order of the turns in the game. * @return (Return type is void.) */ /** * Take method for hard computer difficulty. Takes the number of marbles required to make the remaining number 1 less than a square number. * * @param turnOrder The order of the turns in the game. * @return (Return type is void.) */ public void hardCpuTake(){ if (marblePile > 63) {marblePile = 63 - marblePile;} else if (marblePile > 48) {marblePile = 48 - marblePile;} else if (marblePile > 35) {marblePile = 35 - marblePile;} else if (marblePile > 24) {marblePile = 24 - marblePile;} else if (marblePile > 15) {marblePile = 15 - marblePile;} else if (marblePile > 8) {marblePile = 8 - marblePile;} else if (marblePile > 3) {marblePile = 3 - marblePile;} else if (marblePile == 1) {marblePile = 0;} System.out.println("The computer has taken the last marble."); System.out.println("You lose."); } public String giveUp(){ return "End program."; } }
import java.util.Random; import java.util.Scanner; public class Pile { private int marblePile; private int takeNum; private int cpuTakeNum; private int turnOrder; private int difficulty; private int marblesRemaining; /** * Creates the pile * * @param marblePile The amount of marbles remaining in the pile. * @return (Return type is void.) */ public void createPile() { Scanner pileGen = new Scanner(System.in); System.out.println("Please select a number of marbles to play with."); marblePile = pileGen.nextInt(); System.out.println("The number of marbles in the pile has been set to " + marblePile + "."); } public void setTurnOrder(){ Random coinToss = new Random(); turnOrder = coinToss.nextInt(2); if (turnOrder == 1) System.out.println("The Player will Start."); else if (turnOrder == 0) System.out.println("The Computer will Start."); } public void setCpuDifficulty(){ Random coinToss = new Random(); difficulty = coinToss.nextInt(2); System.out.println ("The computer's difficulty has been set to difficult."); } /** * Takes a given number of marbles. * * @param takeNum The number of marbles being taken. * @return (Return type is void.) */ public void playerTake() { Scanner marbleTake = new Scanner(System.in); System.out.println("Please enter a number of marbles to take:"); takeNum = marbleTake.nextInt(); marblePile = marblePile - takeNum; System.out.println ( takeNum + " marble(s) have been taken from the pile."); System.out.println ("There are now " + marblePile + " marbles remaining."); if (marblePile == 0) {System.out.println("You have taken the last marble. You Win."); giveUp();} } public void easyCpuTake(){ Random easyCpu = new Random(marblePile/2); cpuTakeNum = easyCpu.nextInt(); marblePile = marblePile - cpuTakeNum; } public void gameFlow(){while (marblesRemaining != 0) if (turnOrder == 1){playerTake(); if (difficulty == 1) {easyCpuTake();} else if (difficulty == 0) {hardCpuTake();} if (turnOrder == 0) { if (difficulty == 1){easyCpuTake();} else if (difficulty == 0) {hardCpuTake(); playerTake();}} } } public void getMarbles(){ System.out.println("There are presently " + marblePile + "marbles remaining in the pile."); } public void hardCpuTake(){ if (marblePile > 63) {marblePile = 63 - marblePile;} else if (marblePile > 48) {marblePile = 48 - marblePile;} else if (marblePile > 35) {marblePile = 35 - marblePile;} else if (marblePile > 24) {marblePile = 24 - marblePile;} else if (marblePile > 15) {marblePile = 15 - marblePile;} else if (marblePile > 8) {marblePile = 8 - marblePile;} else if (marblePile > 3) {marblePile = 3 - marblePile;} else if (marblePile == 1) {marblePile = 0;} System.out.println("The computer has taken the last marble."); System.out.println("You lose."); } public String giveUp(){ return "End program."; } }
public class Game { public static void main (String args[]) { Player player = new Player(); Player cpu = new Player(); Pile gamePile = new Pile(); gamePile.createPile(); cpu.setTurnOrder(); cpu.setCpuDifficulty(); gamePile.gameFlow(); } }