Hey, I've been recently coding a java program for the High Low dice game. I'm absolutely sure of my logic in that I know what i have to do, it's just executing it. Methods and how to call them are my main crux. I'm stumped at the moment, and I'm aware that the experienced coders will probably cringe at the amateur mistakes I've made, but you gotta learn from those. All that happens with this code is it constantly finds whatever is put in as invalid. But you already knew that. :)

/**
* High-Low is a dice game played with two dice against a banker.
 * 
 * Players are identified by a name and have an initial amount of money. Before the game
 *  starts, players place their bets on oNE of three outcomes of the total of the two dice.
 * 
 * The possible outcomes are:
 * High if the total: 8,9,10,11 or 12
 * Low if the total: 2,3,4,5, or 6
 * Seven if the total is 7.
 * 
 * The dice are then rolled. The payoffs for a wining bet of High or Low is 1:1. The payoff
 * for a bet of seven is 4:1.
*/

import java.util.*;

public class diceGame
{
	static int rollOne;
	static int rollTwo;
	static int odds;
	static int total;
	static String bet;
	
	public static int rollOne(int rollOne, int rollTwo)
	{
		rollOne = (int)(Math.random()*6+1);
		rollTwo = (int)(Math.random()*6+1);
		
		return total = rollOne + rollTwo;
	}
	
	
	public static void main(String[] args)
	{
		int rollTotal = 0;
		
	Scanner in = new Scanner(System.in);
    System.out.print("Please enter your bet ('high', 'low' or 'seven'): ");
    String bet = in.nextLine();
    
    if (bet == "high")
    {
    	rollTotal = total;	
    }
    else if (bet == "low")
    {
    	
    }
    else if (bet == "seven")
    {
    	
    }
    else
    	System.out.println("That is not a valid bet (please enter 'high', 'low' or 'seven'):");
    }
	
}

Here's my pseudocode logic that I've written out:

roll1 = CALL roll die
roll2 = CALL roll die
	
	total = roll1 + roll2
	
	IF total > 7 and guess is high
			OR total < 7 and guess is low
			Display “Player wins”
			Set odds to 1
	ELSE IF total = 7 and guess is seven
			Display “Player wins”
			Set odds to 4
	ELSE
		Display “Player loses”
		
		Set odds to 0
		
	Return odds]

Help where to go next appreciated :)

Edited 5 Years Ago by NeoSyn: n/a

Hi NeoSyn,
I have a few hints/ comments.
Firstly here is the code to randomly choose the next winning number
Please use the

import java.util.Random;
public int WinnigNumber(){
        /*used to set the range for the numbers that we will be 
         * generating for the winning numbers
         */
        int START = 1;
        int END = 13;
        
        Random randomGenerator = new Random();
        long range = (long)END - (long)START + 1;
    
        // compute a fraction of the range, 0 <= frac < range
        long fraction = (long)(range * randomGenerator.nextDouble());
        int randomNumber =  (int)(fraction + START);
        return randomNumber;
    }

Next you will need a way to store and access player data like name, score, 1st roll etc...so i made u this player class.

public class Player {
    /*Variables
     */
    private int rollone;
    private int rolltwo;
    private String bet;
    private String playername;
    private float credits;
    private int wins;
    private int losses;
    private int gamesplayed;
    
    /*Class Constructor
     */
    Player(String Playername,float Credits){
       set_playername(Playername);
       set_credits(Credits);
       
    }
    /*Access Modifiers
     */
    public String get_player(){
        return playername;
    }
    public String get_bet(){
        return bet;
    }
    public int get_roll_one(){
        return rollone;
    }
    public int get_rolltwo(){
        return rolltwo;
    }
    public int total_rolls(){
        return rollone+rolltwo;
    }
    public float get_total_credits(){
        return credits;
    }
     public int get_wins(){
        return wins;
    }
      public int get_losses(){
        return losses;
    }
       public int get_gamesplayed(){
        return gamesplayed;
    }
    public void set_playername(String Playername){
        playername=Playername;
    }
     public void set_credits(float Credits){
        credits=Credits;
    }
     public void set_bet(String Bet){
         bet=Bet;
     }
      public void set_roll_one(int Rollone){
         rollone=Rollone;
    }
    public void set_rolltwo(int Rolltwo){
        rolltwo=Rolltwo;
    }
     public void Another_gameplayed(){
         gamesplayed=gamesplayed++;
    }
    public void isWinner(){
        wins=wins++;
    }
    public void isLoser(){
        losses=losses++;
    }
    
   
}

This is the basic building blocks for your game.
In the interest of helping u...not just coding it for you....you should create a main class that will run all of the logic for who's turn it is....when u can win...what to display etc.
Seriously this will aid you in your mission but i cant build the engine till i see you help yourself more.
I am here if you have any questions.
Cheers,
Corliss

and remember:

if (bet == "high")
{
rollTotal = total;
}
else if (bet == "low")
{

}
else if (bet == "seven")
{
 
}

is a big NO-NO. never compare objects with the == comparator, always use the .equals method.

Hi NeoSyn,
I have a few hints/ comments.
Firstly here is the code to randomly choose the next winning number
Please use the

import java.util.Random;
public int WinnigNumber(){
        /*used to set the range for the numbers that we will be 
         * generating for the winning numbers
         */
        int START = 1;
        int END = 13;
        
        Random randomGenerator = new Random();
        long range = (long)END - (long)START + 1;
    
        // compute a fraction of the range, 0 <= frac < range
        long fraction = (long)(range * randomGenerator.nextDouble());
        int randomNumber =  (int)(fraction + START);
        return randomNumber;
    }

Next you will need a way to store and access player data like name, score, 1st roll etc...so i made u this player class.

public class Player {
    /*Variables
     */
    private int rollone;
    private int rolltwo;
    private String bet;
    private String playername;
    private float credits;
    private int wins;
    private int losses;
    private int gamesplayed;
    
    /*Class Constructor
     */
    Player(String Playername,float Credits){
       set_playername(Playername);
       set_credits(Credits);
       
    }
    /*Access Modifiers
     */
    public String get_player(){
        return playername;
    }
    public String get_bet(){
        return bet;
    }
    public int get_roll_one(){
        return rollone;
    }
    public int get_rolltwo(){
        return rolltwo;
    }
    public int total_rolls(){
        return rollone+rolltwo;
    }
    public float get_total_credits(){
        return credits;
    }
     public int get_wins(){
        return wins;
    }
      public int get_losses(){
        return losses;
    }
       public int get_gamesplayed(){
        return gamesplayed;
    }
    public void set_playername(String Playername){
        playername=Playername;
    }
     public void set_credits(float Credits){
        credits=Credits;
    }
     public void set_bet(String Bet){
         bet=Bet;
     }
      public void set_roll_one(int Rollone){
         rollone=Rollone;
    }
    public void set_rolltwo(int Rolltwo){
        rolltwo=Rolltwo;
    }
     public void Another_gameplayed(){
         gamesplayed=gamesplayed++;
    }
    public void isWinner(){
        wins=wins++;
    }
    public void isLoser(){
        losses=losses++;
    }
    
   
}

This is the basic building blocks for your game.
In the interest of helping u...not just coding it for you....you should create a main class that will run all of the logic for who's turn it is....when u can win...what to display etc.
Seriously this will aid you in your mission but i cant build the engine till i see you help yourself more.
I am here if you have any questions.
Cheers,
Corliss

So basically I have to add the random method instead of my

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

code. And the main method I create has to call the sets and gets of the player class. How would I call them? (which is my original crux :(

Thanks stultuske, that was peliminary code, I had actually changed that int he mean time. Aprreiciated :)

Edited 5 Years Ago by NeoSyn: n/a

Firstly here is the code to randomly choose the next winning number
Please use the

import java.util.Random;
public int WinnigNumber(){
        /*used to set the range for the numbers that we will be 
         * generating for the winning numbers
         */
        int START = 1;
        int END = 13;
        
        Random randomGenerator = new Random();
        long range = (long)END - (long)START + 1;
    
        // compute a fraction of the range, 0 <= frac < range
        long fraction = (long)(range * randomGenerator.nextDouble());
        int randomNumber =  (int)(fraction + START);
        return randomNumber;
    }

Any particular reason for all that rather than:

return randomGenerator.nextInt(12)+1;

Edited 5 Years Ago by JamesCherrill: n/a

Any particular reason for all that rather than:

return randomGenerator.nextInt(12)+1;

So I can use that line instead of the entire random method?

I was asking corliss why he posted such a complicated method. Unless he has something cunning in mind I think the single line gives the desired answer (a random int between 1 and 12 inclusive)

ps: Oops - you can't throw a 1 with 2 dice! For 2 dice that should be 2-12, so here's the corrected version

randomGenerator.nextInt(11)+2;

EDIT:

I've reworked my code more akin to the pseudocode, but it still doesn't work!

import java.util.*;

public class diceGame

{

	static int rollOne;
	static int rollTwo;
	static int total = 0;
	static double credit = 0;
	
	Scanner in = new Scanner(System.in);

	public int rollDie()
	{
		rollOne = (int)(Math.random()*6+1);
		rollTwo = (int)(Math.random()*6+1);

		return rollOne;
	}

	public double credit() 
	{
		System.out.println("How much credit do you want to bet?. Enter ammount: ");
		double credit = in.nextDouble();
		return credit;	
	}
	
	public String bet()
	{
		System.out.print("Please enter your bet ('high', 'low' or 'seven'): \n ");
		String bet = in.nextLine();
		return bet;	
	}



	public void play(int roll, double credit, String bet)
	{

		credit = credit();
		total = rollOne + rollTwo;
		roll = rollDie();
		bet = bet();

		if (bet.equals("high"))
		{
			if (total > 7 && bet == "high" || total < 7 && bet == "low" )
			{
				System.out.println("Dice roll one: " +rollOne+ "\n Dice roll two: " +rollTwo+ "\n  Total: " +total+ "\n   You bet: " +credit+ "\n   Therefore you WIN!");
				credit = credit * 1; 
				System.out.println("Your winnings are: " +credit);
			}
			else
				System.out.println("Dice roll one: " +rollOne+ "\n Dice roll two: " +rollTwo+ "\n  Total: " +total+ "\n   You bet: " +credit+ "\n    Therefore you LOSE!");
			credit = credit * 1; 
			System.out.println("\n     You lost: " +credit);
		}

		else if (bet.equals("low"))
		{

			if (total > 7 && bet == "high" || total < 7 && bet == "low" )
			{
				System.out.println("Dice roll one: " +rollOne+ "\n Dice roll two: " +rollTwo+ "\n  Total: " +total+ "\n   You bet: " +credit+ "\n   Therefore you WIN!");
				credit = credit * 1; 
				System.out.println("Your winnings are: " +credit);
			}
			else
				System.out.println("Dice roll one: " +rollOne+ "\n Dice roll two: " +rollTwo+ "\n  Total: " +total+ "\n   You bet: " +credit+ "\n    Therefore you LOSE!");
			credit = credit * 1; 
			System.out.println("\n     You lost: " +credit);
		}

		else if (bet.equals("seven"))
		{
			if (total == 7 && bet == "seven")
			{
				System.out.println("Dice roll one: " +rollOne+ "\n Dice roll two: " +rollTwo+ "\n  Total: " +total+ "\n   You bet: " +credit+ "\n    Therefore you WIN!");
				credit = credit * 4; 
				System.out.println("Your winnings are: " +credit);
			}
			else
				System.out.println("Dice roll one: " +rollOne+ "\n Dice roll two: " +rollTwo+ "\n  Total: " +total+ "\n   You bet: " +credit+ "\n    Therefore you LOSE!");
			credit = credit * 1; 
			System.out.println("\n     You lost: " +credit);
		}

	}


	public static void main(String[] args)
	{
		diceGame play = new diceGame();
		play.play(0, 0, null);

	}
}

it still doesn't work!

Come on now, what kind of info is that? How can anyone diagnose a problem if you don't tell them what it is? You must post an exact description of any problem: error messages, actual vs expected output etc.

Anyway, Line 42 you overwrite the value of the parameter credit that was passed to you, that's not the credit variable you declared in your class. Then on line 43 you add roll1 and roll2 to get the total, but you don't set the values of roll1 and roll2 until the call on line 44. There are probably more things to fix, but that will do for now.

import java.util.*;

public class diceGame

{
	static int rollOne;
	static int rollTwo;
	static int roll;
	static int total = 0;
	static double credit = 0;
	static String bet;

	Scanner in = new Scanner(System.in);

	public int rollDie()
	{
		rollOne = (int) (Math.random() * 6 + 1);
		rollTwo = (int) (Math.random() * 6 + 1);

		return rollOne;
	}

	public double credit() {
		System.out
		.println("How much credit do you want to bet?. Enter ammount: ");
		double credit = in.nextDouble();
		return credit;
	}

	public String bet() {
		System.out
		.print("Please enter your bet ('high', 'low' or 'seven'): \n ");
		String bet = in.nextLine();
		return bet;
	}

	public void play() {

		credit = credit();
		roll = rollDie();
		total = rollOne + rollTwo;
		bet = bet();

		if (bet.equals("high")) {
			if (total > 7 && bet == "high" || total < 7 && bet == "low") {
				System.out.println("Dice roll one: " + rollOne
						+ "\n Dice roll two: " + rollTwo + "\n  Total: "
						+ total + "\n   You bet: " + credit
						+ "\n   Therefore you WIN!");
				credit = credit * 1;
				System.out.println("Your winnings are: " + credit);
			} else
				System.out.println("Dice roll one: " + rollOne
						+ "\n Dice roll two: " + rollTwo + "\n  Total: "
						+ total + "\n   You bet: " + credit
						+ "\n    Therefore you LOSE!");
			credit = credit * 1;
			System.out.println("\n     You lost: " + credit);
		}

		else if (bet.equals("low")) {

			if (total > 7 && bet == "high" || total < 7 && bet == "low") {
				System.out.println("Dice roll one: " + rollOne
						+ "\n Dice roll two: " + rollTwo + "\n  Total: "
						+ total + "\n   You bet: " + credit
						+ "\n   Therefore you WIN!");
				credit = credit * 1;
				System.out.println("Your winnings are: " + credit);
			} else
				System.out.println("Dice roll one: " + rollOne
						+ "\n Dice roll two: " + rollTwo + "\n  Total: "
						+ total + "\n   You bet: " + credit
						+ "\n    Therefore you LOSE!");
			credit = credit * 1;
			System.out.println("\n     You lost: " + credit);
		}

		else if (bet.equals("seven")) {
			if (total == 7 && bet == "seven") {
				System.out.println("Dice roll one: " + rollOne
						+ "\n Dice roll two: " + rollTwo + "\n  Total: "
						+ total + "\n   You bet: " + credit
						+ "\n    Therefore you WIN!");
				credit = credit * 4;
				System.out.println("Your winnings are: " + credit);
			} else
				System.out.println("Dice roll one: " + rollOne
						+ "\n Dice roll two: " + rollTwo + "\n  Total: "
						+ total + "\n   You bet: " + credit
						+ "\n    Therefore you LOSE!");
			credit = credit - credit * 1;
			System.out.println("\n     You lost: " + credit);
		} 
                 else
			System.out.print("Not a valid bet!");

	}

	public static void main(String[] args) {
		diceGame play = new diceGame();
		play.play();

	}
}

Apologies, I'll give more in depth problems from now on. Being an amateur, I just assumed it would be glaringly obvious what was wrong to you guys. I think I've fixed the things you've suggested above. The actual problem is the console(/code) will not allow me to input for the high, low, seven (and I've yet to add error handling for wrong strings). The output stops at:

CONSOLE:

How much credit do you want to bet?. Enter ammount: 
70
Please enter your bet ('high', 'low' or 'seven'):

It's meant to read in input in the form of a string and equate that to the if statements, it's not doing that.

EDIT:
Actually, I've not fixed your credit/bet yet. Give me a minute.

Edited 5 Years Ago by NeoSyn: n/a

Fixed it. The problem stemmed from the variables ' bet' and 'credit', as they did not correspond when I called them. So I moved the code they contained to the play() method itself, and changed up the logic of winning etc. Technically it works perfectly now, but it needs a bit more flesh, I was wondering how I'd go about adding a loop, I'm thinking a while, which will ask the user if they wanted to play again, and store the amount of money they have (after each game).
Anyway, here's my current code, working, in it's ugly form. :P

import java.util.*;

public class diceGame

{
	static int rollOne;
	static int rollTwo;
	static int roll;
	static int total = 0;
	static double credit = 0;
	static String bet;

	Scanner in = new Scanner(System.in);

	public int rollDie()
	{
		rollOne = (int) (Math.random() * 6 + 1);
		rollTwo = (int) (Math.random() * 6 + 1);

		return rollOne;
	}

	public void play() {
		System.out.print("Please enter your bet ('high', 'low' or 'seven'): \n ");
		String bet = in.nextLine();
		
		System.out.println("How much credit do you want to bet?. Enter ammount: ");
		double credit = in.nextDouble();
		
		roll = rollDie();
		total = rollOne + rollTwo;

		if (bet.equals("high")) {
			if (total > 7) 
			{
				System.out.println("Dice roll one: " + rollOne
						+ "\n Dice roll two: " + rollTwo
						+ "\n  Total: "+ total
						+ "\n   Total matches '"+ bet
						+ "' bet, Therefore you WIN! \n   You bet: €" +credit);
				credit = credit * 2;
				System.out.println("\n     You now have: €" + credit);
			}
			else if (total < 7)
			{
				System.out.println("Dice roll one: " + rollOne
						+ "\n Dice roll two: " + rollTwo
						+ "\n  Total: "+ total
						+ "\n   Total DOES NOT match '"+ bet
						+ "' bet, Therefore you LOSE! \n     You bet: €" +credit);
			credit = credit - credit * 2;
			System.out.println("\n     You now have: €" + credit);
			}
			else if (total == 7)
			{
			System.out.println("Dice roll one: " + rollOne
					+ "\n Dice roll two: " + rollTwo
					+ "\n  Total: "+ total
					+ "\n   Total DOES NOT match '"+ bet
					+ "' bet, Therefore you LOSE! \n     You bet: €" +credit);
			credit = credit - credit*2;
			System.out.println("\n     You now have: €" + credit);
			}
		}

		else if (bet.equals("low")) {

			if (total < 7)
			{
				System.out.println("Dice roll one: " + rollOne
						+ "\n Dice roll two: " + rollTwo
						+ "\n  Total: "+ total
						+ "\n   Total matches '"+ bet
						+ "' bet, Therefore you WIN! \n     You bet: €" +credit);
				credit = credit * 2;
				System.out.println("You now have: €" + credit);
			} 
			else if (total > 7)
			{
				System.out.println("Dice roll one: " + rollOne
						+ "\n Dice roll two: " + rollTwo
						+ "\n  Total: "+ total
						+ "\n   Total DOES NOT match '"+ bet
						+ "' bet, Therefore you LOSE! \n     You bet: " +credit);
			credit = credit - credit * 2;
			System.out.println("\n     You now have: €" + credit);
			}
			else if (total == 7)
			{
				System.out.println("Dice roll one: " + rollOne
						+ "\n Dice roll two: " + rollTwo
						+ "\n  Total: "+ total
						+ "\n   Total DOES NOT match '"+ bet
						+ "' bet, Therefore you LOSE! \n     You bet: " +credit);
			credit = credit - credit * 2;
			System.out.println("\n     You now have: €" + credit);
			}
		}

		else if (bet.equals("seven")) {
			if (total == 7)
			{
				System.out.println("Dice roll one: " + rollOne
						+ "\n Dice roll two: " + rollTwo
						+ "\n  Total: "+ total
						+ "\n   Total matches '"+ bet
						+ "' bet, Therefore you WIN! \n     You bet: " +credit);
				credit = credit *4;
				System.out.println("\n     You now have: €" + credit);
			} 
			else if (total != 7)
			{
		
				System.out.println("Dice roll one: " + rollOne
						+ "\n Dice roll two: " + rollTwo
						+ "\n  Total: "+ total
						+ "\n   Total DOES NOT match '"+ bet
						+ "' bet, Therefore you LOSE! \n     You bet: " +credit);
			credit = credit - credit * 4;
			System.out.println("\n     You now have: " + credit);
			}
		}
		else
			System.out.print("Not a valid bet!");

	}

	public static void main(String[] args) {
		diceGame play = new diceGame();
		play.play();

	}
}

Edited 5 Years Ago by NeoSyn: n/a

This article has been dead for over six months. Start a new discussion instead.