Results 1 to 14 of 14
  1. #1
    kimmelim is offline Member
    Join Date
    Feb 2009
    Posts
    16
    Rep Power
    0

    Default Small Dice Program

    Having some problems, making a game called LuckySeven, if the two dices sum is seven, you get 50more credits. My problem is how to do this from the game class. Hope you can help me : ) Would be forever thankful : )

    Java Code:
    public class Player
    { 
    
    private String firstname;
    private String lastname;
    public int credits;
    
    /**
     * The constructor of the player class.
     * @param firstname the firstname of the player.
     * @param lastname the lastname of the player.
     */
    public Player(String firstname, String lastname){
        this.firstname = firstname;
        this.lastname = lastname;
        credits = 100;
    } 
    
    /**
     * Method that allows alteration of the firstname.
     * @param firstname the new firstname of the player.
     */
    public void setFirstname(String firstname){
        this.firstname = firstname;
    }
    
    /**
     * Method that allows alteration of the lastname.
     * @param lastname the new lastname of the player.
     */
    public void setLastname(String lastname){
        this.lastname = lastname;
    }
    
    /**
     * Method that adds additional credits.
     * @param sum the sum to be added.
     */
    public void addCredits(int sum){
        credits = credits + sum;
    }
    
    public void removeCredits(int removecredits)
        {
            credits -= removecredits;
        }
    
    /**
     * Method that returns the firstname.
     * @return firstname.
     */
    public String getFirstname(){
        return firstname;
    }
    
    /**
     * Method that returns the lastname.
     * @return lastname.
     */
    public String getLastname(){
        return lastname;
    }
    
    /**
     * Method that returns the credits.
     * @return credits.
     */
    public int getCredits(){
        return credits;
    }
    
    /**
     * Method that checks if a player has credits.
     * @return true/false.
     */
    public boolean validPlayer(){
    if(credits > 0) return true;
        else return false;
    }
    
    /**
     * Method that prints out info about the player.
     */
    public void print(){
        System.out.println("************************");
        System.out.println("Player: "+firstname+" "+lastname);
        System.out.println("Credits: "+credits+" kr.");
        System.out.println("************************");
    }
    
    
    }

    Java Code:
     public class LuckySeven
    { 
        public Player player1;
        private Dice dice1;
        private Dice dice2;
    
        public LuckySeven()
        {
            dice1 = new Dice();
            dice2 = new Dice();
            
        }
     // **********************************
        public int Play(Player player1) {
        System.out.println("************************");
        System.out.println("Welcom Player : " + player1.getFirstname()+ " " + player1.getLastname());
        System.out.println("Your currently credits are : "+ player1.getCredits() +" kr.");
        System.out.println("Each LuckySeven game cost 10credits, if you win you get 50credits : ) ");
        System.out.println("The game LuckySeven will now start!");
        System.out.println("************************");
            int x = dice1.getValue();
            int y = dice2.getValue();
            int c=y+x;
    
               if (c ==7){ 
                player1.addCredits(50); 
                 System.out.println("You rolled: " + getValue());
                 System.out.println("You win! your credits are now: " + player1.getCredits());
                 System.out.println("Please Start a new LuckySeven game, maybe you win again! : ");
                 System.out.println("************************");
    
            }
            else{
               player1.addCredits(-10);  
            System.out.println("You rolled: " + getValue());
            System.out.println("You loose! your credits are now: " +  player1.getCredits());
            System.out.println("Please Start a new LuckySeven game!"); 
         }
            return c;
    }
     public int getValue() {
            int x = dice1.getValue();
            int y = dice2.getValue();
            int c=y+x;
            return c;
     }
    
    
    }

    Java Code:
    import java.util.Random;
    
    public class Dice {
     
     protected int value;
     
     protected static Random random;
     
     public Dice() {
       if (random == null) {
         random = new Random();
       }
       this.trowDice();
     }
     
     public int getValue() {
       return this.value;
     }
     
     public int trowDice() {
       this.value = random.nextInt(6) + 1;
       return this.value;
     }
    }
    Last edited by kimmelim; 02-15-2009 at 01:02 PM. Reason: Code Tip added

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Are you having any compile problems? Any error messages? If so, best to post them in the original post.
    for e.g.:
    Java Code:
    private Dice1 dice2; //??

  3. #3
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default

    I didn't find a game class. And there are other things that I find strange...
    Java Code:
    private Dice1 dice2;
    I didnt find any Dice1 class
    Java Code:
    public int sumOfTwoDice()
    I also can't find that the sumOfTwoDice() is ever called

    Wondering ...
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    also I have a minor nitpick on the Player class. A "set" method is used to set the value of a field. Your setCredits method actually adds the parameter value to the credits field, and so this method should probably be called "addCredits"

  5. #5
    kimmelim is offline Member
    Join Date
    Feb 2009
    Posts
    16
    Rep Power
    0

    Default

    Some clairifications:

    - LuckySeven is the Game class
    - The Dice class does work, can roll two dices, get the result with sumOfTwoDice()
    - Yes, I agree with the naming of the method Fubarable, I will change it
    - The code you see here compiles great, but if I attempt to add more functions like:


    - When a new LuckySeven game is created by a player, the credits the player has, should be reduced by 5

    - If sum = 7 then addCredits, then it fails, because I do not know how to code it

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    If your code is compiling, then it's not the same code you posted here. As Chris and I have both pointed out, there is no Dice1 class. So either you have a class by this name (and you shouldn't have this) and haven't posted it or else the code you posted isn't the one that you compiled. This may seem like a minor nitpick to you, but it actually is a major point that needs to be clarified before we can progress. best of luck!

  7. #7
    kimmelim is offline Member
    Join Date
    Feb 2009
    Posts
    16
    Rep Power
    0

    Default

    Added the Dice1 Class now, sry guys. Hope this helps me and you ; )

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    No you don't want a Dice1 class which is nothing more than a copy of the Dice class, and this is not at all what we were suggesting.

    One of the goals of programming is to create re-usable code, and so creating code that does nothing more than duplicates an already existant class flies against this goal.

    In sum, you want only one class, Dice, and from that single class you'll declare two Dice objects, you can call them dice1 and dice2 if you'd like. Delete the Dice1 class and change your Dice1 declaration to be a Dice declaration.
    Last edited by Fubarable; 02-14-2009 at 05:39 AM.

  9. #9
    kimmelim is offline Member
    Join Date
    Feb 2009
    Posts
    16
    Rep Power
    0

    Default

    Indeed, only need one class. I see that now, can anybody help me on the way with:

    when the sum of dices is 7, give the Player 50more credits.

    Have tried with addCredits, but nothing changes, because I do not know how to implement this method in the LuckySeven class.

  10. #10
    MK12's Avatar
    MK12 is offline Senior Member
    Join Date
    Jan 2009
    Posts
    185
    Rep Power
    6

    Post

    why not just do this?
    Java Code:
    if(sumOfTwoDice() == 7) {
         firstname.addCredits(50);
    }
    And a few things:
    Java Code:
    public class LuckySeven
    { 
        [COLOR="red"]public Player firstname; //  why is it called firstname?
        // firstname is a field in the Player class. A better name would be
        // public Player player1;[/COLOR]
        private Dice dice1;
        private Dice dice2;
    
        public LuckySeven(Player firstname, int credits)
        {
            dice1 = new Dice();
            dice2 = new Dice();
    
            
        // return sum of two dice
    }
     [COLOR="Green"]// **********************************[/COLOR]
        public int sumOfTwoDice() {
            int x = dice1.getValue();
            int y = dice2.getValue();
            [COLOR="Red"]int e=7;   // Not needed[/COLOR]
            int c=y+x;
          [COLOR="DarkOrange"]// some changes added here:[/COLOR]
            if (c ==7){ 
                 System.out.println("You win");
                 player1.addCredits(50); [COLOR="DarkOrange"]// asumming you change firstname to player1[/COLOR]
            }
            return c;
    }
    [COLOR="green"]// *************************************[/COLOR]
    }
    Inbetween the commented green *'s, I suggest to change to:
    Java Code:
    public [COLOR="Red"]void[/COLOR] [COLOR="DarkOrange"]rollDice[/COLOR]() {
         if((dice1.getValue() + dice2.getValue()) == 7) {
              player1.addCredits(50);
              System.out.print("You win! your credits are now: "player1.getCredits());
         }
    }
    Hope this helps.
    -MK12
    Tell me if you want a cool Java logo avatar like mine and I'll make you one.

  11. #11
    kimmelim is offline Member
    Join Date
    Feb 2009
    Posts
    16
    Rep Power
    0

    Default

    It does help alot, thank you for your help so far : )

    Now I get:

    java.lang.NullPointerException
    at LuckySeven.sumOfTwoDice(LuckySeven.java:27)

    This occours each time I call sumOfTwoDice.

    Error example : player1.removeCredits(5);

    NullPointerException:
    Null
    Last edited by kimmelim; 02-14-2009 at 05:50 PM.

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    A NPE occurs when you try to use an object that has not yet been initialized. So look carefully at the LuckySeven object (you do create one, correct?) and see if you don't initialize it.

  13. #13
    kimmelim is offline Member
    Join Date
    Feb 2009
    Posts
    16
    Rep Power
    0

    Default

    Indeed I create a LuckySeven object.

    First I create a Player, then a LuckySeven object.

    hmm
    Last edited by kimmelim; 02-15-2009 at 01:30 AM.

  14. #14
    kimmelim is offline Member
    Join Date
    Feb 2009
    Posts
    16
    Rep Power
    0

    Default

    Have managed to get it to kinda work. . .

    Java Code:
      public class LuckySeven
    { 
        public Player player1;
        private Dice dice1;
        private Dice dice2;
    
        public LuckySeven()
        {
            dice1 = new Dice();
            dice2 = new Dice();
            
        }
     // **********************************
        public int Play(Player player1) {
        System.out.println("************************");
        System.out.println("Welcom Player : " + player1.getFirstname()+ " " + player1.getLastname());
        System.out.println("Your currently credits are : "+ player1.getCredits() +" kr.");
        System.out.println("Each LuckySeven game cost 10credits, if you win you get 50credits : ) ");
        System.out.println("The game LuckySeven will now start!");
        System.out.println("************************");
            int x = dice1.getValue();
            int y = dice2.getValue();
            int c=y+x;
    
               if (c ==7){ 
                player1.addCredits(50); 
                 System.out.println("You rolled: " + getValue());
                 System.out.println("You win! your credits are now: " + player1.getCredits());
                 System.out.println("Please Start a new LuckySeven game, maybe you win again! : ");
                 System.out.println("************************");
    
            }
            else{
               player1.addCredits(-10);  
            System.out.println("You rolled: " + getValue());
            System.out.println("You loose! your credits are now: " +  player1.getCredits());
            System.out.println("Please Start a new LuckySeven game!"); 
         }
            return c;
    }
     public int getValue() {
            int x = dice1.getValue();
            int y = dice2.getValue();
            int c=y+x;
            return c;
     }
    
    
    }
    Last edited by kimmelim; 02-15-2009 at 01:02 PM. Reason: new code

Similar Threads

  1. Proofreading this small Java program
    By almina in forum New To Java
    Replies: 5
    Last Post: 10-23-2009, 08:42 AM
  2. Please HELP Java small program
    By afrttoh in forum New To Java
    Replies: 14
    Last Post: 11-08-2008, 03:29 AM
  3. small issues with a program
    By jimJohnson in forum New To Java
    Replies: 6
    Last Post: 04-25-2008, 09:28 AM
  4. Defective code for dice, help please?
    By byron in forum New To Java
    Replies: 10
    Last Post: 04-01-2008, 07:22 AM
  5. Help debugging a dice game
    By Windoze in forum New To Java
    Replies: 7
    Last Post: 11-22-2007, 02:01 AM

Posting Permissions

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