project2(1).pdf Grand Valley State University School of Computing CIS 162 Project 2 - Farkle (a dice game) Due Date See due date for your section on Blackboard. Suggested guidelines for time required...

1 answer below »
Programming project


project2(1).pdf Grand Valley State University School of Computing CIS 162 Project 2 - Farkle (a dice game) Due Date See due date for your section on Blackboard. Suggested guidelines for time required for each phase:  Phase 1 – One – two days  Phase 2 – One week  Phase 3 – One week Before Starting the Project  Read chapter 8  Watch the provided video to learn how to play the Farkle game implemented in this project.  Read this entire project description before starting Learning Objectives After completing this project, you should be able to:  write methods to meet specific requirements  write code using arrays  write conditional statements with boolean expressions  describe the difference between public and private methods  read and understand source code for a Graphical User Interface (GUI)  write looping constructs using for loops Game Rules There are numerous variations of the Farkle game rules. For this project, we are choosing the most used rules with changes to the scoring categories. The Farkle game supports multiple players using six standard 6-sided dice. Players take turns playing the game. A player rolls all six dice and checks to see if they have rolled any scoring dice or combinations. (See scoring categories below). Any dice that score may be set aside and then the player may choose to roll all the remaining dice. The player must set aside at least one scoring die of their choice if possible but is not required to set aside all scoring dice. For example, if a player rolled 1-2-2-5-5-6 on their turn, they could set aside the 1 and the two 5's for scoring, or they could choose to set aside only the 1. Any scoring dice that are not set aside may be rerolled along with the non-scoring dice. CIS 162 Project 2 – Fall 2021 Page 1 of 12 If all six dice have been set aside for scoring (known as having “hot dice”), the player can choose to roll all six dice again and continue adding to their accumulated score or they can bank their points, end their turn, and pass the dice to the next player. A player’s turn continues until either they decide to stop (at which point they bank the points for the turn) or until they fail to roll any scoring dice on a throw (losing the points for the turn). If a player scores no points on a roll, this is known as a “Farkle”. All their points gained so far during that turn are lost. At the end of a player’s turn, the dice are passed to the next player. Scoring Categories Single die Points 1 100 points 5 50 points Combinations of dice 1-2-3-4-5-6 (straight) 3,000 points 3 Pairs 1,500 points (Including 4-of-a-kind and a pair) Four of any number 1,300 points Five of any number 2,000 points Six of any number 3,000 points Three 1's 1,000 points Three 2's 200 points Three 3's 300 points Three 4's 400 points Three 5's 500 points Three 6's 600 points Notes about scoring:  Note that scoring combinations only count when made with a single throw. (Example: If a player rolls a 1 and sets it aside and then rolls two 1’s on their next throw, they only score 300 points, not 1000.)  The 1’s and 5’s that are part of any scoring that use combination of dice, won’t be part of the single die score. Example: if the player throws a straight, the score will be 3,000 not 3.150. In other words, the 1’s and 5’s can only count for one category. CIS 162 Project 2 – Fall 2021 Page 2 of 12  The goal is to use the high scoring category. Example: if a player rolled 6-1- 1-4-1-1, and sets aside the four 1’s, the score should be four of any number (1,300 points), not three 1’s + 1 individual 1. (1,000 + 100). Winning The 9rst player to score a total of 10,000 or more points wins. Project Speci9cations. Create a project called Project2 GVdie class. Rather than writing your own Die class, we are providing a completed class for you. Download the GVdie.java to the folder where you saved your project 2. It should compile with no errors. Do not make any changes to this code. An object of the GVdie class can be in one of three states: 1) available to roll (white color). Dice start as available to roll 2) selected (pink color). Dice are selected by the player clicking on them. The color changes automatically from white to pink 2) scored (gray color). The Farkle game converts selected dice to scored dice after the player clicks the Roll button. The color change from pink to gray NOTES:  Only available dice can be rolled.  If players change their mind, they can click selected dice to make them available again.  Dice cannot be rolled or selected once they have been scored. Example: scored selected available You only need to use the following methods from the GVdie class, but you are encouraged to read the source code to understand how it works. GVdie d1 = new GVdie(); // declare and instantiate a GVdie object d1.roll(); // roll the die int val = d1.getValue(); // return die value d1.setBlank(); // set face to blank CIS 162 Project 2 – Fall 2021 scored Page 3 of 12 d1.setSelected(true); // mark die as selected (or not) d1.setScored(true); // mark die as scored (or not) if(d1.isSelected()) // check if die is selected if(d1.isScored()) // check if die is scored CIS 162 Project 2 – Fall 2021 Page 4 of 12 Coding Style (10 pts) Good programming practice includes writing elegant source code for the human reader. Follow the GVSU Java Style Guide. Phase 1 (10 pts) Important note: Exact spelling is required for the name of the Player and Farkle classes and the headers of all the methods within these classes. Do not make any changes to the following requirements. If you do so, the automated tests that we provide will likely fail. Create a Player class The class maintains four pieces of information for a game player: name (String), score (int), subtotal (int) and number of turns taken (int). Constructor  public Player (String n) – sets the player’s name to the value of the input parameter and sets the remaining values to zero. Getter Methods  public String getName() - returns player’s name.  public int getScore() - returns player’s score.  public int getSubtotal() - returns player’s subtotal.  public int getTurns() - returns player’s number of turns. Setter Methods  public void setName(String n) - set player’s name.  public void setScore(int s) - set player’s score.  public void setSubtotal(int s) - set player’s subtotal.  public void setTurns(int t) - set player’s number of turns. Mutator Methods  public void addToSubtotal(int amt) – increase the player’s subtotal by the parameter amt.  public void updateScore() – increase the player’s score by the subtotal and then set subtotal to zero. Increment number of turns by one.  public void newGame() – reset the player’s score, subtotal and number of turns to zero. Test Phase 1 using the automated JUnit class Download the MyFarkleTestPhase1.java to the folder where you saved your project 2 and test your solution. See instructions from project 1 to know how to run a JUnit class. Note: You do not need to create your own class to test the Player class. CIS 162 Project 2 – Fall 2021 Page 5 of 12 Phase 2 (40 pts) Create a class called Farkle For this phase, there is only one player. You will write the functionality of the game, but you will not write code to avoid any cheating at this time. Instance Variables Provide appropriate names and data types for each of the private instance variables:  an object of type Player that tracks the score and other information for the current player  an array of GVdie objects named dice  an array of integers, named tally. This array is used to tally the number of 1s, 2s, 3s, 4s, 5s, 6s  declare constants for the scoring categories. For example: private static final int WINNING_SCORE = 10000; private static final int STRAIGHT = 3000; private static final int THREE_PAIRS = 1500; private static final int FOUR_OF_A_KIND = 1300; Visual representation of the dice array Array of six GVdie objects Conten t Referenc e to a GVdie object Referenc e to a GVdie object Referenc e to a GVdie object Referenc e to a GVdie object Referenc e to a GVdie object Referenc e to a GVdie object index 0 1 2 3 4 5 Visual representation of the tally array Important note: Index zero of the array is not used int counts Not used count of 1’s count of 2’s count of 3’s count of 4’s count of 5’s count of 6’s index 0 1 2 3 4 5 6 Constructor  public Farkle() o instantiate the array of six GVdie objects dice = new GVdie [6]; o use a loop to fill the array created above with six GVdie objects. Remember to instantiate each GVdie before you put it in the array. o Instantiate the array of seven integers. tally = new int[7]; o Instantiate the current player object with your name . o Invoke the private resetGame() method. You will be writing the resetGame() CIS 162 Project 2 – Fall 2021 Page 6 of 12 method later. (See the instructions to write this method below) Getter/Accessor Methods  public Player getActivePlayer() – return the current player object (one line).  publc boolean gameOver() - return true if the game is over because the current player achieved at least 10,000 points. Otherwise, return false.  public GVdie[] getDice () - return the array of GVdie objects. This method is only one line
Answered 2 days AfterNov 02, 2021

Answer To: project2(1).pdf Grand Valley State University School of Computing CIS 162 Project 2 - Farkle (a dice...

Kondalarao answered on Nov 05 2021
117 Votes
Farkle.java
Farkle.java
/*****************************************************************
 * Farkle Game object representation
 * 
 * 
 * @author Jennifer G
 * @version Novmber 2021
 *****************************************************************/
public class Farkle {
    /** constants for scoring categories */
    private static final int STRAIGHT = 3000;
    private static final int THREE_PAIRS = 1500;
    private static final int FOUR_OF_A_KIND = 1300;
    private static final int FIVE_OF_A_KIND = 2000;
    private static final int SIX_OF_A_KIND = 3000;
    private static final int THREE_ONES = 1000;
    private static final int ONES = 100;
    private static final int FIVES = 50;
    private static final int WINNING_SCORE = 10000;
    /** active player of the game */
    private Player activePlayer;
    /** the game dice */
    private GVdie[] dice;
    /** tally 1s, 2s, 3s, 4s, 5s, 6s count */
    private int[] tally;
    /** players of the game */
    private Player[] players;
    /** is the di
ce roll first or not */
    private boolean isFirstRoll = false;
    /** is the dice ok to pass or not */
    private boolean isOkToPass = false;
    /** is the dice ok to roll or not */
    private boolean isOkToRoll = false;

    /****************************************************************
     * Default constructor creates the Farkle game with 6 dice 
     * and 3 players
     ***************************************************************/
    public Farkle() {
        dice = new GVdie[6];
        for (int i = 0; i < dice.length; i++) {
            dice[i] = new GVdie();
        }
        tally = new int[7];
        String[] names = { "Ada Lovelace", "Grace Hooper", "Alan Turing" };
        players = new Player[3];
        for (int i = 0; i < 3; i++)
            players[i] = new Player(names[i]);
        resetGame();
    }

    /****************************************************************
     * Reset the game
     ***************************************************************/
    public void resetGame() {
        for (int i = 0; i < players.length; i++) {
            players[i].newGame();
        }
        nextTurn();
        setActivePlayer(1);
    }

    /****************************************************************
     *  Check for each scoring category and update 
     *  the player’s subtotal when appropriate.
     ***************************************************************/
    public void scoreSelectedDice() {
        tallySelectedDice();
        if (hasStraight()) {
            activePlayer.addToSubtotal(STRAIGHT);
        } else if (hasThreePairs()) {
            activePlayer.addToSubtotal(THREE_PAIRS);
        } else if (hasMultiples(6)) {
            activePlayer.addToSubtotal(SIX_OF_A_KIND);
        } else if (hasMultiples(5)) {
            activePlayer.addToSubtotal(FIVE_OF_A_KIND);
            addOnes();
            addFives();
        } else if (hasMultiples(4)) {
            activePlayer.addToSubtotal(FOUR_OF_A_KIND);
            addOnes();
            addFives();
        } else if (hasMultiples(3)) {
            for (int i = 1; i < tally.length; i++) {
                if (tally[i] == 3) {
                    if (i == 1) {
                        activePlayer.addToSubtotal(THREE_ONES);
                    } else {
                        activePlayer.addToSubtotal(i * 100);
                    }
                } else if (i == 1) {
                    addOnes();
                } else if (i == 5) {
                    addFives();
                }
            }
        } else {
            addOnes();
            addFives();
        }
        diceSelectedToScored();
    }

    /****************************************************************
     *  Adds fives scores to player’s subtotal
     ***************************************************************/
    private void addFives() {
        if (tally[5]>0 && tally[5] <= 2) {
            activePlayer.addToSubtotal(tally[5] * FIVES);
        }
    }


    /****************************************************************
     *  Adds ones scores to player’s subtotal
     ***************************************************************/
    private void addOnes() {
        if (tally[1]>0 && tally[1] <= 2) {
            activePlayer.addToSubtotal(tally[1] * ONES);
        }
    }

    /****************************************************************
     *  This method will be called when Roll button clicked from GUI
     ***************************************************************/
    public void rollDice() {
        scoreSelectedDice();
        if (allDiceScroed()) {
            for (int i = 0; i < dice.length; i++) {
                dice[i].setSelected(false);
                dice[i].setScored(false);
                dice[i].roll();
            }
        } else {
            for (int i = 0; i < dice.length; i++) {
                if (!dice[i].isScored()) {
                    dice[i].roll();
                }
            }
        }
        if (isFirstRoll || wereDiceSelected()) {
            isFirstRoll = false;
            isOkToPass = true;
        }
        if (playerFarkled()) {
            activePlayer.setSubtotal(0);
            isOkToRoll = false;
        }
    }

    /****************************************************************
     *  This method will be called when Pass button clicked from GUI
     ***************************************************************/
    public void passDice() {
        scoreSelectedDice();
        activePlayer.updateScore();
        nextTurn();
    }

    /****************************************************************
     * Get the active player of the game
     * 
     * @return the active player of the game
     ***************************************************************/
    public Player getActivePlayer() {
        return activePlayer;
    }

    /****************************************************************
     * Is the game is over?
     * 
     * @return true if the game is over. Otherwise, false.
     ***************************************************************/
    public boolean gameOver() {
        if (activePlayer.getScore() >= WINNING_SCORE) {
            return true;
        } else {
            return false;
        }
    }

    /****************************************************************
     * Get the dice of the game
     * 
     * @return the dice of the game
     ***************************************************************/
    public GVdie[] getDice() {
        return dice;
    }

    /****************************************************************
     * Tally the values selected from dice
     ***************************************************************/
    void tallySelectedDice() {
        for (int i = 1; i < tally.length; i++) {
            tally[i] = 0;
        }
        for (int i = 0; i < dice.length; i++) {
            if (dice[i].isSelected()) {
                tally[dice[i].getValue()]++;
            }
        }
    }

    /****************************************************************
     * Tally the values unscored dice
     ***************************************************************/
    private void tallyUnscoredDice() {
        for (int i = 1; i < tally.length; i++) {
            tally[i] = 0;
        }
        for (int i = 0; i < dice.length; i++) {
            if (!dice[i].isScored()) {
                tally[dice[i].getValue()]++;
            }
        }
    }

    /****************************************************************
     * Is the any die is selected?
     * 
     * @return true if the any die is selected. Otherwise, false.
     ***************************************************************/
    private boolean wereDiceSelected() {
        for (int i = 0; i < dice.length; i++) {
            if (dice[i].isSelected()) {
                return true;
            }
        }
        return false;
    }

    /****************************************************************
     * Is the all dice is scored?
     * 
     * @return true if the all dice is scored. Otherwise, false.
     ***************************************************************/
    private boolean allDiceScroed() {
        for (int i = 0; i < dice.length; i++) {
            if (!dice[i].isScored()) {
                return false;
            }
        }
        return true;
    }

    /****************************************************************
     * Is the six dice contain a straight 
     * (includes all the values from 1 to 6)?
     * 
     * @return true if the six dice contain a straight 
     * (includes all the values from 1 to 6). Otherwise, false.
     ***************************************************************/
    private boolean hasStraight() {
        for (int i = 1; i < tally.length; i++) {
            if (tally[i] == 0) {
                return false;
            }
        }
        return true;
    }

    /****************************************************************
     * Is the six dice contain three pairs or 
     * four-of-a-kind and a pair?
     * 
     * @return true if the six dice contain three pairs or 
     * four-of-a-kind and a pair. Otherwise, false.
     ***************************************************************/
    private boolean hasThreePairs() {
        int pairCount = 0;

        for (int i = 1; i < tally.length; i++) {
            if (tally[i] == 2) {
                pairCount += 1;
            } else if (tally[i] == 4) {
                pairCount += 2;
            }
        }
        if (pairCount == 3) {
            return true;
        }
        return false;
    }

    /****************************************************************
     * Is the six dice contain any die value multiple times?
     * 
     * @param numberTimes the number of times
     * 
     * @return true if the six dice contain 
     * any die value multiple times. Otherwise, false.
     ***************************************************************/
    private boolean hasMultiples(int ...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here