Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I wanna add a function where once the sum of total reaches more than 41 even bef

ID: 3645472 • Letter: I

Question

I wanna add a function where once the sum of total reaches more than 41 even before 6th roll it automatically ends the game and announce that the player is the winner while it reaches 10, 20, 30, 40 do the opposite, ends game and announce the player lost before 6th roll.
and allow the player to have one more roll if it has two same numbers on each die(except 10,20,30,40).
Now what the program does is it plays 6th roll no matter what.
Can someone please help me out?


import java.util.Scanner;

public class FortyOne{
public static void main(String [] args){
Scanner scanIn = new Scanner(System.in);
boolean Continue = true;
while (Continue)
{
Die die1, die2;

//Create a pair of dice
die1 = new Die();
die2 = new Die(6);

//Rolls dice
die1.roll();
die2.roll();
int rollCount;
rollCount = 0;
rollCount++;


//Initiate game
System.out.println("Let's Play 41");
System.out.print("Roll #"+ rollCount+" ");
rollCount++;

// After the roll, prints out the value of die1 & 2.
System.out.print(die1.getUpValue() + " ");
System.out.print(die2.getUpValue() + " ");

//if you get doubles
if (die1.getUpValue() == die2.getUpValue())
System.out.print("YOU GOT DOUBLES ");
rollCount += 1;

//Gives turn total
int turnTotal = die1.getUpValue()+die2.getUpValue();

//prints out sum of die1 and die2
System.out.print("Your total is: " + turnTotal+ " ");

//Adds turnTotal to the next ?????
int playerTotal = 0;


//Limits the amount of rolls to 6.
if(rollCount >=7)
System.out.println("YOU LOSE!!");

//Tells player they lose if their roll amount or total equals 10,20,30,or 40
if(turnTotal == 10 || turnTotal == 20 || turnTotal == 30 || turnTotal == 40){
System.out.println(" " +" You lose...");}
else if(playerTotal == 10 || playerTotal == 20 || playerTotal == 30 || playerTotal == 40){
System.out.println(" " +" You lose...");}


else

do
{
System.out.print(" " + "Roll #"+ rollCount+" ");
rollCount++;
die1.roll();
die2.roll();
int pTotal = die1.getUpValue() + die2.getUpValue()+ playerTotal;
playerTotal = pTotal;
System.out.print(die1.getUpValue() + " ");
System.out.print(die2.getUpValue() + " ");
System.out.print("Your total is: " + (playerTotal)+ " ");


}


while (rollCount<=6);
rollCount++;
if(playerTotal >= 41)
{
System.out.println(" " + " You are the winner!");
}
else if(playerTotal <= 41)
{
System.out.println(" " + "You lose...");
}



System.out.print(" " + "Do You Want to Continue?(Type Yes or No): ");

String Continue1 = scanIn.nextLine();


if ( Continue1.equalsIgnoreCase("yes"))
{
Continue = true;
}
else
{
Continue = false;
}


}
}
}




Explanation / Answer

Class Forty One

import java.util.Scanner;

public class FortyOne {

      public static void main(String[] args) {

            FortyOne obj=new FortyOne();

            obj.playFortyOne();

      }

      private void playFortyOne() {

            Scanner scanIn = new Scanner(System.in);

            boolean playAgain = true;

            do {

                  // Create a pair of dice

                  Die die1 = new Die();

                  Die die2 = new Die(6);

                  int rollCount = 0;

                  int doublesCount = 0;

                  int turnTotal = 0;

                  int playerTotal = 0;

                  System.out.println("Let's Play 41");

                  // subtracting the doubleCount from rollCount (This gives extra

                  // chance)

                  while ((rollCount - doublesCount < 6)) {

                        // Rolls dice

                        die1.roll();

                        die2.roll();

                        rollCount++;

                        System.out.print("Roll #" + rollCount + " ");

                        // After the roll, prints out the value of die1 & 2.

                        System.out.print(die1.getUpValue() + " ");

                        System.out.print(die2.getUpValue() + " ");

                        turnTotal = die1.getUpValue() + die2.getUpValue();

                        playerTotal = playerTotal + turnTotal;

                        System.out.println(" Total" + " " + playerTotal);

                        // if you get 41

                        if (playerTotal > 41) {

                              System.out.println(" " + " You are the winner!");

                              System.out.println("your actual rolls are "

                                          + (rollCount - doublesCount));

                              break;

                        } else if (playerTotal == 10 || playerTotal == 20

                                    || playerTotal == 30 || playerTotal == 40) {

                              System.out.println("YOU LOSE");

                              break;

                        }

// if you get doubles

                        if (die1.getUpValue() == die2.getUpValue()) {

                              if (turnTotal == 10) {

                                    System.out.println("YOU DONT GET AN EXTRA CHANCe!!");

                                    break;

                              } else {

                                    System.out.println("YOU GOT DOUBLES !! YOU GET AN EXTRA CHANCE");

                                    doublesCount++;

                                    // Increasing the double count so that he gets another

                                    // extra chance

                              }

                        }

                  }// end of while loop

                  // if control comes out of the loop after exceeding the maximum

                  // number of rolls we should check the playerTotal

                  if ((rollCount - doublesCount >= 6) && (playerTotal <= 41)) {

                        if((playerTotal != 10 && playerTotal!= 20&&playerTotal!= 30&&playerTotal!= 40))//This is needed to avoid duplicate message

                        System.out.println("YOU LOSE");

                  }

                  System.out.println(" "+ "Do You Want to Continue?(Type Yes or No): ");

                  String playAgain1 = scanIn.nextLine();

                  if (playAgain1.equalsIgnoreCase("yes")) {

                        playAgain = true;

                  } else {

                        playAgain = false;

                  }

            } while (playAgain);

      }

}

Class Die:

public class Die {

int upValue;

Die(int upValue)

{

      setUpValue(upValue);

}

Die()

{

this.upValue=1;

}

void roll()

{

upValue=(int) (Math.random()*10%6+1);

setUpValue(upValue);

}

public int getUpValue() {

      return upValue;

}

public void setUpValue(int upValue) {

      this.upValue = upValue;

}

}



Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote