-
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 : )
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("************************");
}
}
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;
}
}
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;
}
}
-
Are you having any compile problems? Any error messages? If so, best to post them in the original post.
for e.g.:
Code:
private Dice1 dice2; //??
-
I didn't find a game class. And there are other things that I find strange...
Code:
private Dice1 dice2;
I didnt find any Dice1 class
Code:
public int sumOfTwoDice()
I also can't find that the sumOfTwoDice() is ever called
Wondering ...
CJSL
-
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"
-
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
-
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!
-
Added the Dice1 Class now, sry guys. Hope this helps me and you ; )
-
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.
-
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.
-
why not just do this?
Code:
if(sumOfTwoDice() == 7) {
firstname.addCredits(50);
}
And a few things:
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:
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
-
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
-
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.
-
Indeed I create a LuckySeven object.
First I create a Player, then a LuckySeven object.
hmm
-
Have managed to get it to kinda work. . .
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;
}
}