Jump to content

Two classes aren't working together

- - - - -

  • Please log in to reply
11 replies to this topic

#1
An Alien

An Alien

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 260 posts
Okay, so the game craps is what I'm creating.
If you don't know the rules of craps, I'll explain it here.
1.First you need to set a bankroll.
2.From the bankroll, you bet a certain amount of money. If you win, that bet is doubled and added to your bankroll, if you loose, then that bet is subtracted from your bank roll.
3. You play the game by rolling two dice.
4. The total of a dice must be 7 or 11 to win.
5. If the total of the dice is 2, 3, or 12, then you loose.
6. There is another way of winning or loosing.
7.If you roll any other number other than those numbers above
8.For example, you roll a 5, you MUST roll a 5 again in order to win or you loose if you roll ANY OTHER number.

When I was coding this game, I forgot rule number 8 completely. But I did manage to put in half or rule# 8. I put made it so that if you roll the same number twice, you win. But I can't make it so that if you don't roll that same number twice, you will loose.

I tried, but I ran into errors and it became too complicated for me.

My code is below, I will post two different version of it. One version is where I am right now with only half of step eight done and the other half not. And the other version is my failed attempt of adding. Maybe you guys can look at my attempt and get an idea.

The dice class:

public class PairOfDice {

    

    private int die1;   // Number showing on the first die.

    private int die2;   // Number showing on the second die.

    

    public PairOfDice() {

            // Constructor.  Rolls the dice, so that they initially

            // show some random values.

        roll();  // Call the roll() method to roll the dice.

    }

    

    public void roll() {

            // Roll the dice by setting each of the dice to be

            // a random number between 1 and 6.

        die1 = (int)(Math.random()*6) + 1;

        die2 = (int)(Math.random()*6) + 1;

    }

             

    public int getDie1() {

          // Return the number showing on the first die.

       return die1;

    }

    

    public int getDie2() {

          // Return the number showing on the second die.

       return die2;

    }

    

    public int getTotal() {

          // Return the total showing on the two dice.

       return die1 + die2;

    }

    

    public void resetTotal() {

    	//Reset dice and total

    	die1 = 0;

    	die2 = 0;

    }

    

    

    

    

 }  

Version 1 of my code that is stable and with only half of step 8:
import java.util.Scanner;

public class CrapsFA {

    public static void main(String[] args) {

    	Scanner scanner = new Scanner(System.in);

    	PairOfDice dice;          

    	int bankroll = 0;

    	int bet = 0;

    	String input;

    	int rollCount = 0;    // Number of times the dice have been rolled

    	int bankrolltemp = 0;

    	int repeat = 0;

    	

    	dice = new PairOfDice();  // Create the PairOfDice object.

        rollCount = 0;

        

        //start getting bankroll and bet amount:

        do{ //get bankroll and make sure it's greater than 20. 

	        System.out.print("How much money is in your bank roll" +

	        		"(Must be more than 20)?: ");

	        bankroll = scanner.nextInt();

        } while(bankroll < 20);

        

        do{ //get amount of bet and not less than 10 and greater than bank roll

	        System.out.print("how much would you like to bet" +

	        		"(Cannot be greater than bankroll or less than 10): ");

	        bet = scanner.nextInt();

        } while(bet < 10 || bet > bankroll);

        

        while (bankroll > 0) {

        	do {

        		System.out.println(" ");

                System.out.println("Press R to roll dice");

                System.out.print("Or enter X to exit: ");

                input = scanner.next();

                

                if (input.equalsIgnoreCase("R")){

                repeat = dice.getTotal();

                dice.roll();

                System.out.println("The dice come up " + dice.getDie1() 

                        + " and " + dice.getDie2() + " = " + dice.getTotal());

                

                rollCount++;}

            } 

        	while (dice.getTotal() != 2 && dice.getTotal() != 3 &&

            		dice.getTotal() != 12 && dice.getTotal() != 7 &&

            		dice.getTotal() != 11);

        	

        	if (input.equalsIgnoreCase("X")){

        		dice.resetTotal();

        		System.out.print("A small fee is charged for quitting " +

        				"Bankroll -$2.00 ");

        		bankrolltemp = bankroll;

        		bankroll = 0;	

        	}

        	

        	

        	if (dice.getTotal() == 7 || dice.getTotal() == 11)

        	{

        		System.out.print("You win!" +

        				" You rolled:" + dice.getTotal());

        		

        		System.out.println(". Your bet:" + bet + " has double" +

        				" and been added to your bankroll.");

        		bankroll += (bet * 2);

        		System.out.println(" Your bankroll is now:" + bankroll);

        	}

        	

        	if (dice.getTotal() == 2 || dice.getTotal() == 3 || dice.getTotal() == 12){

        		System.out.print("You lost! " +

        				"You rolled:" + dice.getTotal());

        		System.out.println(". Your bet:" + bet + " is lost" +

        				" and been subtracted from your bankroll.");

        		bankroll -= bet;

        		System.out.println(" Your bankroll is now:" + bankroll);

        	}

        	

        	else if (repeat == dice.getTotal()){

        		System.out.print("You win!" +

        				" You rolled:" + dice.getTotal() + " twice");

        		

        		System.out.println(". Your bet:" + bet + " has double" +

        				" and been added to your bankroll.");

        		bankroll += (bet * 2);

        		System.out.println(" Your bankroll is now:" + bankroll);

        	}

        	

        }

        bankroll = bankrolltemp;

        bankroll -= 2;

    	System.out.print("Your final bankroll is:" + bankroll);


    

    }

}

My failed attempt (you guys don't have to look it if you guys already have an idea):
import java.util.Scanner;

public class CrapsFA {

    public static void main(String[] args) {

    	Scanner scanner = new Scanner(System.in);

    	PairOfDice dice;          

    	int bankroll = 0;

    	int bet = 0;

    	String input;

    	int rollCount = 0;    // Number of times the dice have been rolled

    	int bankrolltemp = 0;

    	int repeat = 0;

    	boolean other = false;

    	

    	dice = new PairOfDice();  // Create the PairOfDice object.

        rollCount = 0;

        

        //start getting bankroll and bet amount:

        do{ //get bankroll and make sure it's greater than 20. 

	        System.out.print("How much money is in your bank roll" +

	        		"(Must be more than 20)?: ");

	        bankroll = scanner.nextInt();

        } while(bankroll < 20);

        

        do{ //get amount of bet and not less than 10 and greater than bank roll

	        System.out.print("how much would you like to bet" +

	        		"(Cannot be greater than bankroll or less than 10): ");

	        bet = scanner.nextInt();

        } while(bet < 10 || bet > bankroll);

        

        while (bankroll > 0) {

        	do {

        		System.out.println(" ");

                System.out.println("Press R to roll dice");

                System.out.print("Or enter X to exit: ");

                input = scanner.next();

                

                if (input.equalsIgnoreCase("R")){

                dice.resetTotal();

                repeat = dice.getTotal();

                dice.roll();

                System.out.println("The dice come up " + dice.getDie1() 

                        + " and " + dice.getDie2() + " = " + dice.getTotal());

                

                rollCount++;}

            } 

        	while (dice.getTotal() != 2 && dice.getTotal() != 3 &&

            		dice.getTotal() != 12 && dice.getTotal() != 7 &&

            		dice.getTotal() != 11);

        	

        	if (input.equalsIgnoreCase("X")){

        		dice.resetTotal();

        		System.out.print("A small fee is charged for quitting " +

        				"Bankroll -$2.00 ");

        		bankrolltemp = bankroll;

        		bankroll = 0;	

        	}

        	

        	if (other == true || repeat != dice.getTotal()){

        		System.out.print("You lost! " +

        				"You rolled:" + dice.getTotal() + " and not");

        		System.out.println(". Your bet:" + bet + " is lost" +

        				" and been subtracted from your bankroll.");

        		bankroll -= bet;

        		System.out.println(" Your bankroll is now:" + bankroll);

        	}

        	

        	

        	if (dice.getTotal() == 7 || dice.getTotal() == 11)

        	{

        		System.out.print("You win!" +

        				" You rolled:" + dice.getTotal());

        		

        		System.out.println(". Your bet:" + bet + " has double" +

        				" and been added to your bankroll.");

        		bankroll += (bet * 2);

        		System.out.println(" Your bankroll is now:" + bankroll);

        	}

        	

        	if (dice.getTotal() == 2 || dice.getTotal() == 3 || dice.getTotal() == 12){

        		System.out.print("You lost! " +

        				"You rolled:" + dice.getTotal());

        		System.out.println(". Your bet:" + bet + " is lost" +

        				" and been subtracted from your bankroll.");

        		bankroll -= bet;

        		System.out.println(" Your bankroll is now:" + bankroll);

        	}

        	

        	else if (dice.getTotal() == 4 && dice.getTotal() == 5 && 

        			dice.getTotal() == 6 && dice.getTotal() == 8 &&

        			dice.getTotal() == 9 && dice.getTotal() == 10){

        		other = true;

        	}

        	

        	else if (repeat == dice.getTotal()){

        		

        		System.out.print("You win!" +

        				" You rolled:" + dice.getTotal() + " twice");

        		

        		System.out.println(". Your bet:" + bet + " has double" +

        				" and added to your bankroll.");

        		bankroll += (bet * 2);

        		System.out.println(" Your bankroll is now:" + bankroll);

        	}

        	

        	

        	

        }

        bankroll = bankrolltemp;

        bankroll -= 2;

    	System.out.print("Your final bankroll is:" + bankroll);


    

    }

}

Edited by An Alien, 11 February 2011 - 11:36 AM.


#2
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
if (input == "R" || input == "r"){


                dice.roll();

                System.out.println("The dice come up " + dice.getDie1() 

                        + " and " + dice.getDie2());

            

                rollCount++;

            } 
This if statement won't compare the two strings for you, it's instead checking if the address values between the two string objects are equal (eg. they are the same instance). You should use the equals method, if (input.equals("R") || input.equals("r")) instead. I also wouldn't use two equality string comparisons here, something like if (input.toLowerCase().equals("r")) instead.
Wow I changed my sig!

#3
An Alien

An Alien

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 260 posts
Is there a equal method that's the opposite to the equals() for checking if it's not equal so I can replace the !='s. Because, if I get an 11, it still stays in the loop.

#4
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
That's easy, just use the not (!) operator:
if (!input.toLowerCase().equals("r"))
This will now be true if input does NOT equal "r".
Wow I changed my sig!

#5
An Alien

An Alien

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 260 posts
If you look after that if loop in the do while, that's where I want to use the not equal to method. If I do what you told me above, then I get a message "Cannot invoke equals(int) on the primitive type int.

#6
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
You only use equals( ) on String and other Objects. Not on primitive types.
Primitive types are the "classes" that don't start with a capitalised letter, like:
int,
double,
byte,
boolean,
float,
...

For these, just use '=='

#7
An Alien

An Alien

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 260 posts
But if you try to run my program, you can't get out of the do while loop because the not equal (!=) to is not working.

dice.getTotal() != 3 ||

dice.getTotal() != 12 || dice.getTotal() != 7 ||

dice.getTotal() != 11);


#8
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
You are exiting that while loop, but only if you roll 4,5,6,8,9,10
in the next IF-statement you do:

if (dice.getTotal() == 2 || dice.getTotal() == 3 || dice.getTotal() == 12)

So after exciting the inner while loop( total == 4,5,6,8,9,10)
You will never go inside the if-statement, nothing happens and the outer while loop will continue while (bankroll > 0) {

#9
An Alien

An Alien

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 260 posts
Okay, I figured out all of my first questions, now I have a new problem. See the first post. I need to add step 8 to my code. All info is in the first post edited. If you guys need any other info, please ask.

#10
An Alien

An Alien

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 260 posts
Nevermind, I've figured it out by myself.

This thread can be locked now.

#11
so1i

so1i

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 312 posts

An Alien said:

Nevermind, I've figured it out by myself.

This thread can be locked now.

A little thanks for the help they gave you might be nice!
My Company - My Homepage - My Twitter - My Google+ - My LinkedIn

"Things don’t have to change the world to be important.” - Steve Jobs

#12
An Alien

An Alien

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 260 posts
Sorry, I usually do give out thanks, but this time I forgot.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users