Results 1 to 5 of 5
  1. #1
    nwbaldi is offline Member
    Join Date
    Jul 2012
    Posts
    3
    Rep Power
    0

    Default Quick question about a game of Pig (Die game) that I'm writing Code for

    Hello, I'm writing code for a game of Pig and I can't seem to compile Player.java, something to do with the takeTurn method. If someone could look and see what's wrong, that would be fantastic. I'm also including the rest of my program, and if you want to look through that and offer any improvements/corrections that would be amazing.

    Thanks so much!

    Player Class:

    Java Code:
    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)
        {
    		takeTurn(dice);
        }
        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();
    }




    Die Class:

    Java Code:
    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;
       }
    }


    Pair of Dice:

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



    PigDice
    Java Code:
    public class PigDice extends PairOfDice
    {
        public PigDice()
        {
         super();
        }
    
        public int roll()
       {
           int result = super.roll();
    
            if(super.getDie1()==1||super.getDie2()==1)
              {
                  result = 3;
              }
            if(super.getDie1()==1&&super.getDie2()==1)
              {
                  result = 2;
               }
               return result;
    
    }
    }


    Pig Game:



    Java Code:
    import java.util.Scanner;
    
    
    public class PigGame
    {
        private Player player1, player2;
        private PigDice dice;
    
        public PigGame(Player p1, Player p2, PigDice pigDice)
        {
            System.out.println( "Welcome to the Pig Game");
            PigDice dice = pigDice();
            player1 = p1;
            player2 = p2;
        }
    
        public void playGame()
        {
            while (player1.getGamePts() < 100&&player2.getGamePts() < 100)
    
            	player1.takeTurn(dice);
            	 if (player1.getGamePts() < 100)
            	 	player2.takeTurn(dice);
    
        }
    
        public String toString()
        {
    		// GUI makes this unnecessary 
        }
    }

    Human Class

    Java Code:
    import java.util.Scanner;
    
    
    public class Human extends Player
    {
       public Human(String player)
       {
    	   	  super(player);
      }
    
      	public boolean beAPig()
      	{
          Scanner Scan = new Scanner(System.in);
          System.out.println("Would you like to roll again [y/n]? ");
          String response = Scan.nextLine();
          boolean result;
            if (response.equals("y"))
    		{
    		    result = true;
    		}
    
          	else
    
          		result = false;
    
          	return result;
    
        }
    }

    Computer Class:

    Java Code:
    public class Computer extends Player
    {
        public Computer(String player)
        {
    		super(player);
    
    	}
    
        public boolean sensible()
        {
            if(Player.getRoundPts() < 20)
    
            else
                Player.takeTurn();
        }
    
    }
    Last edited by JosAH; 07-07-2012 at 11:21 PM. Reason: added [code] ... [/code] tags

  2. #2
    nwbaldi is offline Member
    Join Date
    Jul 2012
    Posts
    3
    Rep Power
    0

    Default Re: Quick question about a game of Pig (Die game) that I'm writing Code for

    This is a bit hard to read, I wasn't sure how to attach it any better though.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,732
    Blog Entries
    7
    Rep Power
    21

    Default Re: Quick question about a game of Pig (Die game) that I'm writing Code for

    I fixed it for you.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    nwbaldi is offline Member
    Join Date
    Jul 2012
    Posts
    3
    Rep Power
    0

    Default Re: Quick question about a game of Pig (Die game) that I'm writing Code for

    Haha sorry I can't tell did you fix the readability as well as the actual code or just the readability?

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,732
    Blog Entries
    7
    Rep Power
    21

    Default Re: Quick question about a game of Pig (Die game) that I'm writing Code for

    Quote Originally Posted by nwbaldi View Post
    Haha sorry I can't tell did you fix the readability as well as the actual code or just the readability?
    I only fixed the readabilty issue (check your code and see that nothing has changed in it); your remark that it has "something to do with the takeTurn method" is too vague; you could at least have shown us the actual compiler error message.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. RPG game question
    By SwEeTAcTioN in forum New To Java
    Replies: 17
    Last Post: 07-08-2012, 06:56 AM
  2. Writing a Pong game - enhancing my default code.
    By DeagleFace in forum New To Java
    Replies: 6
    Last Post: 05-12-2012, 06:30 PM
  3. Replies: 1
    Last Post: 06-02-2011, 10:29 AM
  4. game code for any game
    By deathnote202 in forum Java Gaming
    Replies: 4
    Last Post: 06-10-2010, 09:06 AM
  5. Quick question about this simple code..
    By shroomiin in forum New To Java
    Replies: 2
    Last Post: 11-10-2009, 06:58 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
  •