Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please help me! Exercise Overview Implement a Java program that is based on the simulated roll of 3 dice. You are to develop the system

Please help me!

Exercise Overview

Implement a Java program that is based on the simulated roll of 3 dice.

You are to develop the system using object-oriented programming techniques (objects and classes).

  • Player scores points based on the combination of (1) the sum of the dice and (2) bonus points from rolling a pair, a triple, or a straight (3 numbers in a row).
  • Player rolls 3 dice in the first roll, and then decides for each die whether to roll again or lock the value of a die to improve the score hence, the name Lock N Roll.
  • At the beginning of the game, player will enter his/her name and the number of turns they wish to have in a single game. A turn is an initial roll plus the second roll after lock/reroll.
  • After all turns have been played, the program will print a history of the turns showing the players initial roll score, final roll score, and the improvement after the Lock N Roll action.
  • Player is prompted to play again or not.

Functional Requirements (how the code will work from the user perspective)

  • System displays the rules of the game on the console.
  • Player enters his/her name.
  • Player enters the number of turns to be taken in the game. A turn consists of the initial role and the second (final) roll.
  • System displays the initial roll including values of the 3 dice, sum of the 3 dice, bonus points, and total points.
  • System indicates progression to the Lock N Roll phase.
  • Player indicates Lock or Roll for each die. Player can lock 0 to 3 dice or roll 0 to 3 dice.
  • After Lock N Roll, die/dice designated for Roll are rolled for new value(s).
  • System analyzes the new set of dice to determine bonus points.
  • System displays the second roll including values of the 3 dice, sum of the 3 dice, bonus points, and total points. If all dice were locked, the values from the initial roll are the values for the second roll. Turn is completed.
  • If turn completion is not the last turn in the game, a new turn begins starting with the initial roll.
  • If turn completion marks the last turn in the game, system displays the turn history, including initial and final dice values, final sum, final bonus points, final total points, and points improved from initial to final roll for each turn in the game.
  • Player is prompted to indicate whether to play again or not, and responds with Y or N.

Additional Functional Requirements for Project 2

  • Rules must be printed at the beginning of the game and before the name prompt.
  • Entered values must be entered on the same line as the prompt (use System.out.print(); rather than System.out.println(); ).
    • For example: Enter your name: Name
  • System must accept entered letters, such as L, R, Y, N, in either uppercase or lowercase.
    • Use the String method, toUppercase(), e.g., yesNo = yesNo.toUpperCase();
  • Dice roll must be sorted for presentation and when choosing L or R. Use Arrays.sort();
  • Headers must be aligned over columns when presenting roll and point values.

Technical Requirements (how you must code it)

The system should include the following Java components:

  • System must consist of a Driver class and at least 2 object classes, such as Roll and Turn, or Roll, Turn, and Game.
  • System should be submitted in one .java file, with the Driver class first followed by object classes.
  • Name of your source code main class as follows: YourName_Exercise2.java
  • Object classes must be organized as follows:
    • Variables declared at the beginning of the class.
      • Exception is local vars, such as int i in a for loop, or temp vars in a method.
    • Constructors follow variables.
    • Methods follow constructors.
  • All data variables in the object classes have a visibility of private.
    • This will require you to use setter and getter methods for these variables.
  • All methods in the object classes have a visibility of public and are not static.
  • Use arrays for storing of dice values, e.g., int[] roll1 = new int[3];
  • while or for loop to execute the indicated number of turns.
  • Algorithm for generating the random die values using Random class.
  • System.out.printf() method for formatting of printing turn, roll, and game results.
  • String array to store the values associated with each turn.
  • do-while loop to enable continuous play with Y/N response.
  • for loops for various activities updating dice values, printing of turn history, etc. especially for those associated with arrays.

Additional Technical Requirements for Project 2

  • You are to start with your Exercise1 code base and refactor that Exercise1 code into an object-oriented programming design.
  • Alternatively, you can use the solution codebase provided for Exercise1 as your starting point, and refactor that into an object-oriented programming design.

Pervious code

import java.util.Scanner; import java.util.Random; public class DiceGame{ static int session[][][]; public static int rolldice(){ Random rand = new Random(); return rand.nextInt(6)+1; } public String readl(){ Scanner sc = new Scanner(System.in); return sc.nextLine(); } public static int getMin(int d1,int d2,int d3){ return Math.min(d1, Math.min(d2, d3)); } public static int getMax(int d1,int d2,int d3){ return Math.max(d1, Math.max(d2, d3)); } public static String checkBonus(int d1,int d2,int d3){ String bonus = "none"; if(d1==d2){ if(d2==d3){ return "triple"; } return "pair"; } else if(d1==d3 || d2==d3){ return "pair"; } else{ if((getMax(d1,d2,d3)-getMin(d1,d2,d3))==2) return "straight"; } return "none"; } public static int print_rolls(int d1,int d2,int d3,int roll,int turn){ Scanner sc = new Scanner(System.in); int points = d1+d2+d3; String res = checkBonus(d1,d2,d3); boolean triple = false; boolean pair = false; boolean straight = false; System.out.println("\t1\t2\t3\tSum\tPair\tTrip\tStrait\tPoints"); System.out.println("\t--\t--\t--\t---\t----\t----\t---\t---"); System.out.print("Roll-"+(roll+1)+"\t"+d1+"\t"+d2+"\t"+d3+"\t"+points+"\t"); String pair_point = "0"; String trip_point = "0"; String straight_point = "0"; if(res == "pair"){ pair_point = "2"; points += 2; pair = true; } System.out.print(""+pair_point+"\t"); if(res=="triple"){ trip_point = "30"; points +=30; triple = true; } System.out.print(""+trip_point+"\t"); if(res == "straight"){ straight_point = "10"; points +=10; straight = true; } System.out.println(""+straight_point+"\t"+points); session[turn][roll][0] = d1; session[turn][roll][1] = d2; session[turn][roll][2] = d3; session[turn][roll][3] = points; session[turn][roll][4] = Integer.parseInt(pair_point); session[turn][roll][5] = Integer.parseInt(trip_point); session[turn][roll][6] = Integer.parseInt(straight_point); return points; } public static void main(String arg[]){ Scanner sc = new Scanner(System.in); System.out.println("Enter your name to begin this fast-action game, and let's LOCK N ROLL!"); System.out.print("Name : "); String user = sc.nextLine(); boolean continue_ = true; while(continue_){ System.out.print("How many turns would you like in this game? "); int turns = sc.nextInt(); session = new int[turns][2][8]; String c = sc.nextLine();/* dummy*/ for(int i=0; i                        

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

SQL For Data Science Data Cleaning Wrangling And Analytics With Relational Databases

Authors: Antonio Badia

1st Edition

3030575918, 978-3030575915

More Books

Students also viewed these Databases questions