//DieGame.java import java.util.*; public class DieGame { private final int TOTA
ID: 3800379 • Letter: #
Question
//DieGame.java
import java.util.*;
public class DieGame
{
private final int TOTAL_SIDES = 6;
private int diceValue;
DieGame()
{
rollDice();
}
public void rollDice()
{
Random r = new Random();
diceValue = r.nextInt(TOTAL_SIDES) + 1;
}
public int getdiceValue()
{
return diceValue;
}
public static int getRolldiceValue()
{
DieGame d = new DieGame();
int roll1diceValue = d.getdiceValue();
d.rollDice();
int roll2diceValue = d.getdiceValue();
return (roll1diceValue + roll2diceValue);
}
public static boolean checkLimit(int diceValue)
{
if (diceValue > 21)
return false;
else
return true;
}
public static boolean againPlay()
{
Scanner scan = new Scanner(System.in);
System.out.print("Roll the dice? (y/n) : ");
String userResponse = scan.nextLine();
char choice = userResponse.charAt(0);
if (choice == 'Y' || choice == 'y')
return true;
else
return false;
}
public static void printResults(int c_poinys, int p_points)
{
System.out.println(" Game Over ");
System.out.println("Player Points: " + p_points);
System.out.println("Computer Points: " + c_poinys);
if (p_points > c_poinys && checkLimit(p_points))
{
System.out.println("You win!");
} else if (checkLimit(p_points)
&& !checkLimit(c_poinys))
{
System.out.println("You win!");
} else if (p_points == 21 && c_poinys != 21)
{
System.out.println("You win!");
} else if (p_points == c_poinys)
{
System.out.println("Game Tie !");
} else if (!checkLimit(p_points)&& !checkLimit(c_poinys))
{
System.out.println("No Winner");
} else
{
System.out.println("Computer wins!");
}
}
public static void main(String[] args)
{
int comp_Points = 0;
int player_Points = 0;
while (againPlay() == true)
{
comp_Points = comp_Points + getRolldiceValue();
player_Points = player_Points + getRolldiceValue();
if (!checkLimit(player_Points)|| !checkLimit(comp_Points))
{
break;
}
System.out.println("Player Points: " + player_Points);
}
if (player_Points == 0 && comp_Points == 0)
System.out.println("No play, No win!!!");
else
printResults(comp_Points, player_Points);
}
}
HOW DO YOU SEPERATE THIS SOURCE CODE TO MAKE TWO CODE. ONE WITH DIE AND DIEDEMO SHOWING THE RELATIONSHIP OF BOTH ONLY ONE SHOULD OUTPUT USING BLUE J
Explanation / Answer
HI, Please find answer.
Please let me know in case of any issue.
######################## Die.java ###############
import java.util.*;
public class Die
{
private final int TOTAL_SIDES = 6;
private int diceValue;
Die()
{
rollDice();
}
public void rollDice()
{
Random r = new Random();
diceValue = r.nextInt(TOTAL_SIDES) + 1;
}
public int getdiceValue()
{
return diceValue;
}
}
############### DieDemo.java ##############
import java.util.Scanner;
public class DieDemo {
public static int getRolldiceValue(Die d)
{
int roll1diceValue = d.getdiceValue();
d.rollDice();
int roll2diceValue = d.getdiceValue();
return (roll1diceValue + roll2diceValue);
}
public static boolean checkLimit(int diceValue)
{
if (diceValue > 21)
return false;
else
return true;
}
public static boolean againPlay()
{
Scanner scan = new Scanner(System.in);
System.out.print("Roll the dice? (y/n) : ");
String userResponse = scan.nextLine();
char choice = userResponse.charAt(0);
if (choice == 'Y' || choice == 'y')
return true;
else
return false;
}
public static void printResults(int c_poinys, int p_points)
{
System.out.println(" Game Over ");
System.out.println("Player Points: " + p_points);
System.out.println("Computer Points: " + c_poinys);
if (p_points > c_poinys && checkLimit(p_points))
{
System.out.println("You win!");
} else if (checkLimit(p_points)
&& !checkLimit(c_poinys))
{
System.out.println("You win!");
} else if (p_points == 21 && c_poinys != 21)
{
System.out.println("You win!");
} else if (p_points == c_poinys)
{
System.out.println("Game Tie !");
} else if (!checkLimit(p_points)&& !checkLimit(c_poinys))
{
System.out.println("No Winner");
} else
{
System.out.println("Computer wins!");
}
}
public static void main(String[] args)
{
int comp_Points = 0;
int player_Points = 0;
Die d = new Die(); // creating object of Die
while (againPlay() == true)
{
comp_Points = comp_Points + getRolldiceValue(d);
player_Points = player_Points + getRolldiceValue(d);
if (!checkLimit(player_Points)|| !checkLimit(comp_Points))
{
break;
}
System.out.println("Player Points: " + player_Points);
}
if (player_Points == 0 && comp_Points == 0)
System.out.println("No play, No win!!!");
else
printResults(comp_Points, player_Points);
}
}
/*
Sample run:
Roll the dice? (y/n) : y
Player Points: 10
Roll the dice? (y/n) : y
Player Points: 15
Roll the dice? (y/n) : n
Game Over
Player Points: 15
Computer Points: 17
Computer wins!
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.