In: Computer Science
Many games involve the use of dice. The singular form of dice is die. A standard die is a cube, i.e. a polyhedron with six sides. Each side of a die has a number of dots (or pips) on it; each side of a die is unique and contains a number of pips from 1 through 6. A die is rolled or tossed such that it comes to rest with one side randomly facing up. The die is said to have rolled a number equal to the number of pips on the side facing up.
Armed with this knowledge:
Part 1 (20%)
Implement a Java class called Die with the following features:
Part 2 (30%)
Implement a dice game called Guess wherein the player chooses a number between 2 and 12 - this is the range of possible values that can be achieved by summing the result of rolling a pair of dice. This dice game will be in its own class called Guess; it will not be in the Die class.
The game then creates two die objects and rolls them together up to three times. If the player's number comes up when summing the result of rolling the pair of dice, the player wins and the game ends. If, after three rolls of the pair of dice, the player's number has not come up, the computer wins and the game ends.
Create output charting the progress of the game and the result.
Part 3 (50%)
Implement a dice game called Matador. The Matador game will be in its own class called Matador; it will not be in the Die class or in the Guess class.
Here are the rules of Matador:
The first to reach a total score of 121 wins. Create output charting the progress of the game and the result.
Please submit:
ANSWER part 1 and 2
SOURCE CODE
import java.util.Random;
import java.util.Scanner;
public class Die{
private int value;
// DEFINING THE CONSTRUCTOR
public Die(){
Random rand = new Random();
value = rand.nextInt(6) + 1;
}
// thsi function assigns a radnom value to a dice
// between 1 and 6
public void roll(){
Random rand = new Random();
value = rand.nextInt(6) + 1;
}
// returns the value of the dice
public int getValue(){
return value;
}
}
// Guess class
class Guess{
// decalare 2 die
private Die dice1 = new Die();
private Die dice2 = new Die();
// it has only one method
// this method controls the entire game
public void playGame(){
// decalring a new scanner
Scanner sc = new Scanner(System.in);
// taking user's guess as input
System.out.print("Please Enter your guess(2-12): ");
int guess = sc.nextInt();
sc.close();
int diceRoll = 0, attempts = 0, flag = 0;
// loop to see who won
while(attempts < 3){
// roll the die
dice1.roll();
dice2.roll();
// get the net value
diceRoll = dice1.getValue() + dice2.getValue();
// print the net value
System.out.println("The result of rolling the die is " + diceRoll + "...");
// see if user has won
if(guess == diceRoll){
flag = 1;
break;
}
attempts++;
}
// if user has won
if(flag == 1)
System.out.println("Player has won!");
// if user has lost
else
System.out.println("Computer has won!");
}
}
// main class to test the above classes
class main_class{
public static void main(String[] args) {
Guess g = new Guess();
g.playGame();
}
}
OUTPUT
PART 3
SOURCE CODE
import java.util.Random;
import java.util.Scanner;
public class Die {
private int value;
// DEFINING THE CONSTRUCTOR
public Die() {
Random rand = new Random();
value = rand.nextInt(6) + 1;
}
// thsi function assigns a radnom value to a dice
// between 1 and 6
public void roll() {
Random rand = new Random();
value = rand.nextInt(6) + 1;
}
// returns the value of the dice
public int getValue() {
return value;
}
}
// The Matador class
class Matador{
// private data members
private Die dice1 = new Die();
private Die dice2 = new Die();
private int playerScore;
private int computerScore;
private int playerGuess;
private int computerGuess;
private boolean playersTurn;
// constructor
// sets the scores to 0
public Matador(){
playerScore = 0;
computerScore = 0;
}
// function to show the current score to screen
public void showScore(){
System.out.println("Player Score = " + playerScore + "\t\tComputer Score = " + computerScore);
}
// function to roll the die
private void rollDie(){
dice1.roll();
dice2.roll();
}
// function to set the score according to given conditions
private void setScore(){
int d1 = dice1.getValue(), d2 = dice2.getValue();
int rollValue = d1+d2;
// if one 1 has been generated
if((d1 == 1 && d2 != 1) || (d1 != 1 && d2 == 1)){
if(playersTurn){
playerScore += rollValue;
playersTurn = false;
}
else{
computerScore += rollValue;
playersTurn = true;
}
}
// if both die yield 1
else if(d1 == 1 && d2 == 1){
if(playersTurn){
playersTurn = false;
playerScore = 0;
}
else{
playersTurn = true;
computerScore = 0;
}
}
// if none of the die yield one
else{
// if the die and guess values are same for player
if(playersTurn && d1 == playerGuess && d2 == playerGuess)
playerScore = 121;
// if the die and guess values are not same for player
else if(playersTurn)
playerScore += rollValue;
// if the die and guess values are same for computer
else if(!playersTurn && d1 == computerGuess && d2 == computerGuess)
computerScore = 121;
// if the die and guess values are not same for computer
else
computerScore += rollValue;
}
}
// method to play the game
public void playGame(){
// the turn is set to the player
playersTurn = true;
Scanner sc = new Scanner(System.in);
// loop to see who wins
while(playerScore != 121 && computerScore != 121){
if(playersTurn){
System.out.println("Player's turn...");
System.out.print("Enter your guess(2-6): ");
// enter players guess
playerGuess = sc.nextInt();
System.out.println("Player has guessed " + playerGuess);
// roll the die and set the score
rollDie();
setScore();
// show what values have been yield by die
System.out.println("Dice 1 = " + dice1.getValue() + "\t\tDice 2 = " + dice2.getValue());
}
else{
// set the guess of computer
Random rand = new Random();
System.out.println("Computer's turn...");
computerGuess = rand.nextInt(5) + 2;
System.out.println("Computer has guessed " + computerGuess);
// roll the die and set the score
rollDie();
setScore();
// show what values have been yield by die
System.out.println("Dice 1 = " + dice1.getValue() + "\t\tDice 2 = " + dice2.getValue());
}
// show score
showScore();
}
sc.close();
// see who's won
if(playerScore == 121)
System.out.println("Player has won!");
else
System.out.println("Computer has won!");
}
}
// main class to test the above classes
class main_class {
public static void main(String[] args) {
Matador m = new Matador();
m.playGame();
}
}
OUTPUT