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

DRA Overview In Lab 6 you created an object oriented version of your DoubleDrat

ID: 3711492 • Letter: D

Question

DRA Overview In Lab 6 you created an object oriented version of your DoubleDrat game. In this lab, you will rewrite your application using : file input/output, ArrayList, and inheritance. You will add code to your game to allow the user to choose from two games: DoubleDrat or TripleDrat Pre-Lab [5 points] Create the GameLauncher following the provided instructions and bring it to your lab session NOTES Each program should include comments that explain what each block of code is doing. Additionally, the programs should compile without errors, and run with the results described in the exercise. The following deductions will be made from each exercise if any of the following is incorrect or missing Lab (90 Points) Create the DoubleDrat and TripleDrat games using the design provided on the following pages. Your output should match the format shown on the last page, although your actual output will vary based on the the outcome of the game Proper formatting [5 points] Proper names for classes and variables [5 points Comments [5 points per class] Program doesn't compile[ 10 points] Source code (java file) missing [10 points] Executable (class file) missing [10 points] Missing array where an array was required [5 points each] Missing loop where a loop was required [5 points each] Missing class from the design provided [10 points each] Missing method from the design provided [ 5 points each] Submit the ava and class files via Canvas (as a single zip- file). Include a comment block at the top of each Java file that includes your name, student id number, and "Lab 6- Spring 2018". Also be sure to include the answers to the post-lab questions found in these instructions

Explanation / Answer

// Player.java

import java.util.Scanner;

public class Player {

                private String name;

                private int score;

                public Player(String name) {

                                this.name = name;

                                score = 0;

                }

                public String getName() {

                                return name;

                }

                public void setName(String name) {

                                this.name = name;

                }

                public void addPoints(int n) {

                                score += n;

                }

                public int getScore() {

                                return score;

                }

                public void setScore(int score) {

                                this.score = score;

                }

                /**

                * method to take one turn, and returns false when the turn is over

                *

                * @param cup

                *            - Cup object

                */

                public boolean takeTurn(Cup cup) {

                                Scanner scanner = new Scanner(System.in);

                                String choice = "R";

                                int roundScore = 0;

                                /**

                                * Looping until player chooses to roll, or until double drat is met

                                */

                                while (choice.equalsIgnoreCase("R")) {

                                                System.out.println(name + "'s turn. (" + roundScore

                                                                                + " points accumulated this turn.)");

                                                /**

                                                * Rolling the cup

                                                */

                                                cup.roll();

                                                System.out.println(cup);

                                                if (cup.getPointValue() == 0) {

                                                                System.out.println("!!! DOUBLE DRAT !!!");

                                                                System.out.println("TURN IS OVER");

                                                                return false;

                                                } else {

                                                                roundScore += cup.getPointValue();

                                                                System.out.println("Roll/Bank (R/B)? ");

                                                                choice = scanner.nextLine();

                                                                if (choice.equalsIgnoreCase("B")) {

                                                                                /**

                                                                                * Adding accumulated points

                                                                                */

                                                                                addPoints(roundScore);

                                                                }

                                                }

                                }

                                return false;

                }

                @Override

                public String toString() {

                                return name + " with score " + score;

                }

}

// Cup.java

public class Cup {

                private int pointValue;

                private Die dice;

                private int value1, value2, value3;

                private static final int DOUBLE_DRAT = 0, UNIQUE = 20, TRIPLE = 30;

                public Cup() {

                                pointValue = 0;

                                dice = new Die();

                }

                public void roll() {

                                /**

                                * rolling dice three times

                                */

                                value1 = dice.roll();

                                value2 = dice.roll();

                                value3 = dice.roll();

                                if (value1 == value2 && value1 == value3) {

                                                // triple

                                                pointValue = TRIPLE;

                                } else if (value1 == value2 || value1 == value3 || value2 == value3) {

                                                // double drat

                                                pointValue = DOUBLE_DRAT;

                                } else {

                                                // unique

                                                pointValue = UNIQUE;

                                }

                }

                public int getPointValue() {

                                return pointValue;

                }

                @Override

                public String toString() {

                                /**

                                * returning a properly formatted string

                                */

                                return " Rolled: " + value1 + " " + value2 + " " + value3 + " ("

                                                                + pointValue + " points)";

                }

}

// Die.java

import java.util.Random;

public class Die {

                private int faceValue;

                private Random random;

                public Die() {

                                //default value

                                faceValue=1;

                                //initializing random instance

                                random=new Random();

                }

                public int getFaceValue() {

                                return faceValue;

                }

                public void setFaceValue(int faceValue) {

                                this.faceValue = faceValue;

                }

                public int roll(){

                                /**

                                * setting a random value between 1 and 6 to the face value and returning it

                                */

                                faceValue=random.nextInt(6)+1;

                                return faceValue;

                }

                @Override

                public String toString() {

                                return ""+faceValue;

                }

}

// DoubleDrat.java

public class DoubleDrat {

                private static final int WIN_SCORE = 100; //winning score

                private Player[] players;

                private Cup cup;

                public DoubleDrat(Player players[]) {

                                this.players = players;

                                cup = new Cup();//initializing empty cup

                }

                /**

                * method to play the game

                */

                public void playGame() {

                                // displaying score board

                                displayScoreBoard();

                                boolean gameOver = false; // keeps the track of the game

                                /**

                                * loops until the game is over

                                */

                                while (!gameOver) {

                                                /**

                                                * looping through all players and their turns

                                                */

                                                for (int i = 0; i < players.length; i++) {

                                                                players[i].takeTurn(cup);

                                                               

                                                                displayScoreBoard();// displaying score board

                                                                /**

                                                                * checking if current player has won

                                                                */

                                                                if (players[i].getScore() >= WIN_SCORE) {

                                                                                gameOver = true;// end of the loop

                                                                                System.out

                                                                                                                .println("Player " + players[i].getName()

                                                                                                                                                + " won with " + players[i].getScore()

                                                                                                                                                + " points!");

                                                                                break;

                                                                }

                                                }

                                }

                }

                /**

                * method to display the score board

                */

                public void displayScoreBoard() {

                                System.out.println("----------------------------------");

                                System.out.println("SCOREBOARD");

                                for (Player p : players) {

                                                System.out.println(p);

                                }

                                System.out.println("----------------------------------");

                }

}

// GameLauncher.java

import java.util.Scanner;

public class GameLauncher {

                public static void main(String[] args) {

                                /**

                                * prompting and getting the number of players

                                */

                                Scanner scanner=new Scanner(System.in);

                                System.out.println("how many players: ");

                                int numPlayers = Integer.parseInt(scanner.nextLine());

                                /**

                                * Initializing the players array

                                */

                                Player[] players=new Player[numPlayers];

                                for (int i = 0; i < numPlayers; i++) {

                                                System.out.println("Player " + (i + 1) + " name:");

                                                players[i] = new Player(scanner.nextLine());

                                }

                                /**

                                * Creating a DoubleDrat object and playing the game

                                */

                                DoubleDrat doubleDrat=new DoubleDrat(players);

                                doubleDrat.playGame();

                }

}