Results 1 to 4 of 4
Like Tree2Likes
  • 2 Post By pbrockway2

Thread: Pig Dice Game Help

  1. #1
    pvccstudent is offline Member
    Join Date
    Jul 2011
    Posts
    2
    Rep Power
    0

    Default Pig Dice Game Help

    Hello, I am a college student looking for help on a CSC project, "The Pig Game". I was wondering if someone would go over my code and help me with what I am doing wrong.

    (*Note I am using netbeans as my compiler. I also am loading an attachment which is a picture of the UML I am suppose to use.)

    Here is my code.

    Java Code:
     
    
    public class Course_Project2 {
    
        public static void main(String[] args)
        {
           PigGame project = new PigGame();
           project.play();
        }
    }
    
    
    
    public class Die
    {
       private final int MAX = 6;  // maximum face value
    
       private int faceValue;  // current value showing on the die
       //-----------------------------------------------------------------
       //  Constructor: Sets the initial face value.
       //-----------------------------------------------------------------
       public Die()
       {
          faceValue = 1;
       }
    
       //-----------------------------------------------------------------
       //  Rolls the die and returns the result.
       //-----------------------------------------------------------------
       public int roll()
       {
          faceValue = (int)(Math.random() * MAX) + 1;
    
          return faceValue;
       }
    
       //-----------------------------------------------------------------
       //  Face value mutator.
       //-----------------------------------------------------------------
       public void setFaceValue (int value)
       {
          faceValue = value;
       }
    
       //-----------------------------------------------------------------
       //  Face value accessor.
       //-----------------------------------------------------------------
       public int getFaceValue()
       {
          return faceValue;
       }
    
       //-----------------------------------------------------------------
       //  Returns a string representation of this die.
       //-----------------------------------------------------------------
       public String toString()
       {
          String result = Integer.toString(faceValue);
    
          return result;
       }
    }
    
    
    
    public class PairOfDice
    {
        // instance data
        private Die die1;
        private Die die2;
    
        //------------------------------------------
        // Constructor:  Intializes two dice.
        //------------------------------------------
        public void PairOfDice()
        {
    	die1 = new Die();
    	die2 = new Die();
        }
        
        //----------------------------------------------------------
        // Rolls the pair by rolling each die. The sum is returned.
        //----------------------------------------------------------
        public int roll()
        {
    	return die1.roll() + die2.roll();
        }
    
    
        //------------------------------------------------------
        // Returns a string representation of the pair of dice.
        //------------------------------------------------------
        @Override
        public String toString()
        {
    	return "[ " + die1.getFaceValue() + " : " + die2.getFaceValue() + " ]";
        }
    
        public void setDie1(int val)
        {
           die1.setFaceValue(val);
        }
        
        public void setDie2(int val)
        {
           die2.setFaceValue(val);
        }
        public int getDie1()
        {
            return die1.getFaceValue();
        }
        
        public int getDie2()
        {
           return die2.getFaceValue();
        }
        
    }        
    
    
    
    import java.lang.String;
    
    public abstract class Player 
    {
        private int roundPts;
        private int gamePts;
        private String name;
        
        public Player(String n)
        {
                    
        }        
        public void takeTurn(PigDice p)
        {
                    
        }
        @Override
        public String toString()
        {
          return ("Points earned this round "  + roundPts) +
                  ("Total is" + (roundPts + gamePts));
        }
        
        public void setGamePts(int Val)
        {
          gamePts += Val;    
        }
        
        public void setRoundPts(int Val)
        {
          roundPts += Val;   
        } 
        
        public int getGamePts()
        {
          return gamePts;
        }
        public int getRoundPts()
        {
          return roundPts;
        }
       
        public abstract boolean beAPig();
    }
    
    
    
    public class Computer extends Player
    {
        public Computer(String n)
        {
            if(Player.getRoundPts() < 20)
               
            else
                Player.takeTurn();
        }
    
    }
    
    
    
    import java.util.Scanner;
    
    
    public class Human extends Player
    {
       public Human(String n)
       {
          Scanner Scan = new Scanner(System.in);
          System.out.println("Would you like to roll again [y/n]? ");
          String response = Scan.nextLine();
          if (response.equals("y"))
                  
          else 
    
       }
    }
    
    
    
    
    import java.util.Scanner;
    
    
    public class PigGame 
    {
        private Player player1, player2;
        private PigDice dice;
        
        public PigGame(Player Human, Player Computer, PigDice pigDice)
        {
            System.out.println( "Welcome to the Pig Game");
            PigDice dice = new PigDice();
            player1 = new Human("Player 1");
            player2 = new Computer("Computer AI");
        }
        
        public void play()
        {
            Scanner Scan = new Scanner(System.in);
            CurPlayer = new
        }
        
        public String toString()
        {
            
        }
    }
    
    
    
    
    public class PigDice 
    {
        public PigDice()
        {
         super();   
        }
        
        public int roll()
       {
           int result = super.roll();
    
            if(die1.getFaceValue()==1||die2.getFaceValue()==1)
              {
                  result = 3;
              }
            if(die1.getFaceValue()==1&&die2.getFaceValue()==1)
              {
                  result = 2;
               }
               return result;
    }
    Attached Thumbnails Attached Thumbnails Pig Dice Game Help-project2.jpg  
    Last edited by pvccstudent; 07-10-2011 at 02:25 AM.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    It's a good idea to use the "code" tags: put [code] at the start of your code and [/code] at the end, that way it will be properly formatted when it appears here. You can edit your post to do this but realise that you may have to correct the indentation - spaces look better than tabs when displayed on a web page.

    -----

    I was wondering if someone would go over my code and help me with what I am doing wrong.
    Does your code compile? If it does not and you can't understand the compiler's message(s) post it (or them) here. Complete compiler messages are good and you should indicate which line(s) of your code they are referring to.

    Does your code compile but exhibit weird or undesirable runtime behaviour: eg give a runtime exception or do things that are "wrong" in terms of the rules of the game? In that case say what the program actually does as well as saying precisely what it should do.
    Last edited by pbrockway2; 07-10-2011 at 02:23 AM.

  3. #3
    pvccstudent is offline Member
    Join Date
    Jul 2011
    Posts
    2
    Rep Power
    0

    Default

    At the moment, only player, PairOfDice, and Die compile by themselves. The rest does not compile or says there are errors in the code. Right now I am working on implementing player class by using the human and computer. In the computer class, I am getting the error on the if statement (non-static method getRoundPts can not be referenced from a static content). The only other problem, I am having, is understanding what each class is suppose to do for this program.

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    Right now I am working on implementing player class by using the human and computer. In the computer class, I am getting the error on the if statement (non-static method getRoundPts can not be referenced from a static content).
    Java Code:
    public class Computer extends Player
    {
        public Computer(String n)
        {
            if([color=blue]Player.getRoundPts()[/color] < 20)
               
            else
                Player.takeTurn();
        }
    
    }
    The compiler doesn't like the expression "Player.getRoundPts()" and it is saying, in effect, "which player's getRoundPts() do you mean?!".

    The expression "static context" is a bit cryptic but it means that a method call like getRoundPts() only applies to a specific player. It is worth noting that your code has "instance data" and methods that apply to particular instances: ie there are no static or class wide data or methods. This is the usual state of affairs.

    So how do you fix the expression "Player.getRoundPts()"? Basically you have to realise that a computer IS-A player. So a particular instance of Computer has a getRoundPts() method that any method or constructor can use (likewise takeTurn()):

    Java Code:
    public class Computer extends Player
    {
        public Computer(String n)
        {
            if(getRoundPts() < 20) {
    
            } else {
                [color=blue]takeTurn();[/color]
            }
        }
    }
    You haven't said why you would want to call these methods when you are constructing an instance of Computer, but the code above is (almost) how you would do so: at any rate it calls the methods in a way that is not static. The reason for the reservation is that as it stands the statement "takeTurn();" does not make sense: the takeTurn() method needs an argument.

    -----

    The only other problem, I am having, is understanding what each class is suppose to do for this program.
    I completely sympathise, but this statement is a bit disturbing. You should begin by being 100% sure what each of the classes are and what each of their methods do. Only then should you write the first line of code. If you are not sure ask: your teacher, your classmates, here (but recognise that the diagram is rather small - I can't read it, eg) Asking specific questions about the overall outline of the program is more likely to be productive than expecting someone to walk you through the whole thing.

    Then start writing the code one small piece at a time. Compile every few lines. Address every compiler message as it appears. (And understand, or ask, why it appeared: just making it wrok is not enough).

    @Moderators: it doesn't worry me, but this is not an AWT/Swing question. Perhaps it could be moved.
    Last edited by pbrockway2; 07-10-2011 at 03:26 AM.
    Norm and Fubarable like this.

Similar Threads

  1. Pig dice game
    By dangyounoobs in forum New To Java
    Replies: 2
    Last Post: 12-09-2013, 05:21 AM
  2. Need help with AI in roll dice game
    By shazakala in forum New To Java
    Replies: 3
    Last Post: 04-04-2011, 12:49 PM
  3. Help with a dice game.
    By hero in forum AWT / Swing
    Replies: 14
    Last Post: 07-26-2009, 12:50 PM
  4. Help debugging a dice game
    By Windoze in forum New To Java
    Replies: 7
    Last Post: 11-22-2007, 02:01 AM
  5. help debugging a dice game
    By Windoze in forum Advanced Java
    Replies: 0
    Last Post: 11-16-2007, 11:28 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •