Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: 2 problems...

  1. #1
    Junior Member
    Join Date
    Nov 2010
    Location
    Boston, MA
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 2 problems...

    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();
                            }  
        }


  2. #2
    Member DavidFongs's Avatar
    Join Date
    Oct 2010
    Location
    Minneapolis, MN
    Posts
    107
    Thanks
    1
    Thanked 45 Times in 41 Posts

    Default Re: 2 problems...

    You have a marblePile member in both the Player class and the Pile class..... Which one isn't functioning the way you expect it to?

    It looks like you set the marblePile member of the Pile class in the Pile class using a scanner. Is the println after this set displaying the correct value?

    It also looks like you are referencing the marblePile member of the Player class, in the Player class, before you are initializing it (Were you expecting it to have the value that the member of the same name was initialized to in the Pile class? Because it doesn't work like that).

Similar Threads

  1. [SOLVED] Have a few odd problems.
    By javapenguin in forum What's Wrong With My Code?
    Replies: 18
    Last Post: October 19th, 2010, 06:08 PM
  2. rmi problems
    By deepthought in forum Java SE APIs
    Replies: 7
    Last Post: September 7th, 2010, 07:25 PM
  3. Exercise problems
    By Virgildonatifan in forum Java Theory & Questions
    Replies: 0
    Last Post: February 14th, 2010, 04:12 AM
  4. How to compile and run java program?
    By ebalari56 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 27th, 2009, 09:33 PM
  5. If you have any .NET problems
    By antony_t in forum The Cafe
    Replies: 1
    Last Post: August 26th, 2009, 10:49 AM