Home > Mobile >  Dice continuous sum game Java
Dice continuous sum game Java

Time:03-14

I'm new to programming and trying to make a dice game. The game consists of three dice and involves the sum of 12 (on any number of dice) in each round. Each dice can only be rolled once per round.

In each round, the player must be able to choose between: 1 to roll the dice 1, 2 to roll the dice 2, 3 to roll the dice 3, q to exit the game. The program must randomly find a value on the selected dice and then calculate the score. The program should also present the number of wins and the number of rounds lost. The program should continue until the user chooses to cancel the game. Regardless of the number of dice, and the definition of loss is a sum exceeding 12 after all three dice have been rolled. If the sum after three rolls is less than 12, there will be no profit or loss, but you go straight to the next round.

So currently my code works for 1 round, the problem begins when the second round begins. The new dice rolls are just the input I give it, and not a random value (1-6).

How can I make this work?

import java.util.Scanner;
import java.util.Random;

public class Main{
  //Declaring int variables for all 3 dice
  static int dice1;
  static int dice2;
  static int dice3;
  //Declaring true/false for which dice that is thrown, can't be thrown more than 1 time in each round.
  static boolean dice1Thrown = false;
  static boolean dice2Thrown = false;
  static boolean dice3Thrown = false;
  //Method for which dice is thrown - the input the user choses (1, 2, 3 or q)
  static char diceToThrow;
  //Method that checks what number 1-6 the dice roll. sum by default is 0
  static int sum = 0;
  //Method for keep track of # wins
  static int wins;
  //Method keep tracks of # losses
  static int losses;
  //If firstGame = true it will print "Welcome to the game 12" if false, it doesn't print that every round
  static boolean firstGame = true;
  //Int for keeping track of when dice thrown is = 3. Go to next round.
  static int rounds;
  

  public static void main(String[] args)
  
  {
    Scanner scan = new Scanner(System.in);
    if(firstGame)
    {
        System.out.println("Welcome to the game 12. You must roll 1-3 dice and try to get the sum 12 ... ");
        firstGame = false; 
    }
    System.out.printf("Enter which dice you want to roll [1,2,3] (exit with q): ");  

    //Calling method diceToThrow, chosing input 1,2,3 or q
    diceToThrow = scan.next().charAt(0);
    tärningsKast(diceToThrow);
    //sum = dice1   dice2   dice3
    calcSum();
    //If user gets 12 store int wins as  1, if lose store int loss as  1
    winOrLose();
    //Printing dices, if input from user is 1, it shows sum of first dice etc.
    System.out.printf(dice1   " "   dice2   " "   dice3   " sum: "   sum   " #win: "   wins   " #loss: "   losses  " ");
    //Calling method for round score.
    checkRounds();
    
    main(args);

   //Method created for sum calculating, called in Main method
  }static void calcSum(){
    sum = (dice1   dice2   dice3);
  }

   //Method created for dice1, dice2, dice3 and char q (as exit). Dice generates random number between 0-5 and always adds   1 (1-6)
  static void tärningsKast(char diceToThrow){
    Random rand = new Random();
   //If user input = 1, generate dice1
    if(diceToThrow == '1' && !dice1Thrown){
      dice1 = rand.nextInt(6)   1;
      dice1Thrown = true;
    }
  //If user input = 2, generate dice2
  else if(diceToThrow == '2' && !dice2Thrown){
  dice2 = rand.nextInt(6)  1;
  //If user input = 3, generate dice3
  }else if(diceToThrow == '3' && !dice3Thrown){
    dice3 = rand.nextInt(6)   1;
    dice3Thrown = true;
  //If user input = char q, Print "Game Over!" and exit game.
  }else if(diceToThrow == 'q'){
    System.out.println("Game Over!");
    System.exit(0);
  }
}
  
  static void winOrLose(){
      if(sum == 12){
          wins  ;
      }
      else if(sum > 12){
          losses  ;
      }
  }
  static void checkRounds(){
      rounds  ;
      if(rounds == 3)
      {
          System.out.println("\n"   "Next round!");
          dice1 = 0;
          dice2 = 0; 
          dice3 = 0; 
          rounds = 0;
        
      }
  }
}

CodePudding user response:

When you are going for the next round, the method tärningsKast() is only going to work again and again once each round passes if the dice is not thrown.

Meaning that once a round finishes and you are going to start another round, you have to set that the dices are not thrown, since the round is going to start right after.

The solution I found is to set the dice1Thrown, dice2Thrown and dice3Thrown to false on the if statement that prints that the next round is starting.

    static void checkRounds(){
    rounds  ;
    if(rounds == 3)
    {
        System.out.println("\n"   "Next round!");
        dice1 = 0;
        dice2 = 0;
        dice3 = 0;
        rounds = 0;
        dice1Thrown = false;
        dice2Thrown = false;
        dice3Thrown = false;

    }

After that the game seems to be working fine.

The steps I made to find that it, were:

  1. Set a break point to debug the line that prints "Next round".
  2. Once the debugger reachs that line, set a break point in the first line of the method tärningsKast(), then check that in each check of the if, else if and etc. the diceNThrown variables are all still setted to true, so none of the conditions will match.

CodePudding user response:

Regarding the question:

"You mean, if you wanted to stop the game in the round 2 ?" "Yeah exactly like that"

First I'd use you checkRounds() method to check if it were to stop the game, meaning if the user already played two rounds.

Second I would move the logic that move to the next round to a method that checks attemps, intead of rounds, since we will move to the next round every time the user already had three attempts.

And finally before moving to the next round I would be increasing the number of rounds, and right after checking if the number of rounds if the expected number of rounds to stop, that way you can play the game with the limit of rounds you desire to.


//Declaring int variables for all 3 dice
static int dice1;
static int dice2;
static int dice3;
//Declaring true/false for which dice that is thrown, can't be thrown more than 1 time in each round.
static boolean dice1Thrown = false;
static boolean dice2Thrown = false;
static boolean dice3Thrown = false;
//Method for which dice is thrown - the input the user choses (1, 2, 3 or q)
static char diceToThrow;
//Method that checks what number 1-6 the dice roll. sum by default is 0
static int sum = 0;
//Method for keep track of # wins
static int wins;
//Method keep tracks of # losses
static int losses;
//If firstGame = true it will print "Welcome to the game 12" if false, it doesn't print that every round
static boolean firstGame = true;
//Int for keeping track of when dice thrown is = 3. Go to next round.
static int attempts;
// keeping track of the number of rounds
static int rounds = 0;
// desired number of rounds to play
static final int LIMIT_OF_ROUNDS = 5;

public static void main(String[] args)

{
    Scanner scan = new Scanner(System.in);
    if(firstGame)
    {
        System.out.println("Welcome to the game 12. You must roll 1-3 dice and try to get the sum 12 ... ");
        firstGame = false;
    }
    System.out.printf("Enter which dice you want to roll [1,2,3] (exit with q): ");

    //Calling method diceToThrow, chosing input 1,2,3 or q
    diceToThrow = scan.next().charAt(0);
    tärningsKast(diceToThrow);
    //sum = dice1   dice2   dice3
    calcSum();
    //If user gets 12 store int wins as  1, if lose store int loss as  1
    winOrLose();
    //Printing dices, if input from user is 1, it shows sum of first dice etc.
    System.out.printf(dice1   " "   dice2   " "   dice3   " sum: "   sum   " #win: "   wins   " #loss: "   losses  " ");
    //Calling method for round score.
    checkAttempts();

    main(args);

    //Method created for sum calculating, called in Main method
}static void calcSum(){
    sum = (dice1   dice2   dice3);
}

//Method created for dice1, dice2, dice3 and char q (as exit). Dice generates random number between 0-5 and always adds   1 (1-6)
static void tärningsKast(char diceToThrow){
    Random rand = new Random();
    //If user input = 1, generate dice1
    if(diceToThrow == '1' && !dice1Thrown){
        dice1 = rand.nextInt(6)   1;
        dice1Thrown = true;
    }
    //If user input = 2, generate dice2
    else if(diceToThrow == '2' && !dice2Thrown){
        dice2 = rand.nextInt(6)  1;
        //If user input = 3, generate dice3
    }else if(diceToThrow == '3' && !dice3Thrown){
        dice3 = rand.nextInt(6)   1;
        dice3Thrown = true;
        //If user input = char q, Print "Game Over!" and exit game.
    }else if(diceToThrow == 'q'){
        System.out.println("Game Over!");
        System.exit(0);
    }
}

static void winOrLose(){
    if(sum == 12){
        wins  ;
    }
    else if(sum > 12){
        losses  ;
    }
}
static void checkAttempts(){
    attempts  ;

    if(attempts == 3)
    {
        rounds  ;
        checkRounds(rounds);
        System.out.println("\n"   "Next round!");
        dice1 = 0;
        dice2 = 0;
        dice3 = 0;
        attempts = 0;
        dice1Thrown = false;
        dice2Thrown = false;
        dice3Thrown = false;
    }
}

static void checkRounds(int rounds) {
    if (rounds == LIMIT_OF_ROUNDS) {
        tärningsKast('q');
    }
}
  • Related