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

Create this game below using Java. The game of SKUNK presents middle-grade stude

ID: 3852917 • Letter: C

Question

Create this game below using Java.

The game of SKUNK presents middle-grade students with an experience that clearly involves both choice and chance. SKUNK is a variation on a dice game also known as "pig" or "hold'em." The object of SKUNK is to accumulate points by rolling dice. Points are accumulated by making several "good" rolls in a row but choosing to stop before a "bad" roll comes and wipes out all the points. SKUNK can be played by groups, by the whole class at once, or by individuals.

The Game of SKUNK

To start the game each player makes a score sheet like this:

Each letter of SKUNK represents a different round of the game; play begins with the "S" column and continue through the "K" column. The object of SKUNK is to accumulate the greatest possible point total over five rounds. The rules for play are the same for each of the five rounds.

- At the beginning of each round, every player stands. Then, a pair of dice is rolled. (Everyone playing uses that roll of the dice; unlike other games, players do not roll the dice for just themselves.)
- A player gets the total of the dice and records it in his or her column, unless a "one" comes up.
- If a "one" comes up, play is over for that round and all the player's points in that column are wiped out.
- If "double ones" come up, all points accumulated in prior columns are wiped out as well.
- If a "one" doesn't occur, the player may choose either to try for more points on the next roll (by continuing to stand) or to stop and keep what he or she has accumulated (by sitting down).

Note: If a "one" or "double ones" occur on the very first roll of a round, then that round is over and each player must take the consequences.

Here is the website if you want further information: http://illuminations.nctm.org/Lesson.aspx?id=956

Follow the rules above and create the required classes for a good rate.

Score Record 10

Explanation / Answer

Here is the complete code for the SKUNK game. Please post a comment in case of any issues/ doubts, I shall respond. If happy with the answer, please don't forget to rate it.

Die.java

import java.util.Random;

public class Die {

   private static final Random random = new Random(System.currentTimeMillis()); //random number generator

  

   public int roll()

   {

       return random.nextInt(6) + 1; //random number in range 1-6

   }

}

import java.util.Arrays;

public class Player {

   private String name;

   private boolean isStanding;

   private int scores[]; //scores for 5 rounds of SKUNK game

  

   public Player(String name)

   {

       this.name = name;

       isStanding = true;

       scores = new int[5];

   }

  

   public String getName()

   {

       return name;

   }

  

   public boolean isStanding()

   {

       return isStanding;

   }

  

   public void sit()

   {

       isStanding = false;

   }

  

   public void stand()

   {

       isStanding = true;

   }

  

   //accumulate the roll of 2 dice if the player is standing. If player is not standing , will not

   // accumulate

   public void accumulate(int round, int die1, int die2)

   {

       if(!isStanding) //player should not accumulate score if not standing

            return;

      

       boolean doubleOne = (die1 == 1 && die2 == 1); //find if its a double 1

       if(doubleOne)

       {

           //wipe of scores of all prior rounds as well

           for(int i = 0 ; i <= round; i++)

           {

               scores[i] = 0;

           }

       }

       else if (die1 == 1 || die2 == 1) // one of them is 1

       {

           //wipe off current round score

           scores[round] = 0;

       }

       else

       {

           //accumulate score

           scores[round] += die1 + die2;

       }

   }

  

   public String toString()

   {

       return name + Arrays.toString(scores) + " : Total (" + getTotalScore() + ")";

   }

  

   public int getTotalScore()

   {

       int total = 0;

       for(int i = 0; i < 5; i++)

           total += scores[i];

       return total;

   }

   public int getRoundScore(int round)

   {

       return scores[round];

   }

}

SkunkGame.java

import java.awt.DisplayMode;

import java.util.ArrayList;

import java.util.Scanner;

public class SkunkGame {

   private ArrayList<Player> players;

   private Die die1, die2;

   public SkunkGame()

   {

       players = new ArrayList<Player>();

       die1 = new Die();

       die2 = new Die();

   }

  

   public void addPlayer(Player p)

   {

       if(p != null)

           players.add(p);

   }

  

   public int getNumPlayers()

   {

       return players.size();

   }

  

   public void start(Scanner input)

   {

      

       int n1, n2;

       char rounds[] = {'S','K','U','N', 'K'};

       for(int i = 0; i < 5; i++) // 5 rounds

       {

           System.out.println("***** Start Round '" + rounds[i] + "' *****");

           //make all players stand in beginning of round

           for(Player p: players)

               p.stand();

          

           while(true) //keep rolling die until a 1 is rolled

           {

              

               n1 = die1.roll();

               n2 = die2.roll();

              

               System.out.println("Die1: " + n1 + " Die2: " + n2);

               for(Player p:players)

                   p.accumulate(i, n1, n2); //accumulate checks if player is standing and then accumulate

              

               if(n1 == 1 || n2 ==1) //

               {

                   System.out.println("***** End Round '" + rounds[i] + "' *****");

                   displayScores();

                   break; //end of this round

               }

              

               //after accumulating score, ask if any players want to sit, show a list of standing players to choose who should sit

               askPlayerWantToSit(input);

              

           }

       }

       declareWinner();//declare winner at the end of game

   }

  

   private void askPlayerWantToSit(Scanner input)

   {

       boolean standing = false;

       //check if any one is standing still

       for(Player p : players)

           if(p.isStanding())

           {

               standing = true;

               break;

           }

      

       if(!standing) //if nobody is standing just return

           return;

      

       displayStandingPlayers();

       System.out.println("Does anyone want to sit? y/n: ");

       String ans = input.nextLine().trim().toLowerCase();

       if(ans.equals("y"))

       {

          

           System.out.print(" Enter the sl no. of players who wish to sit : ");

           String line;

           while(true)

           {

               line = input.nextLine().trim();

               if(!line.equals(""))

                   break;

           }

           Scanner lineScanner = new Scanner(line);

           int pno;

           int count = 0;

           while(lineScanner.hasNextInt())

           {

               pno = lineScanner.nextInt();

               //System.out.println("pno = " + pno);

               if(pno >0 && pno <=players.size())

               {

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

                   {

                       if(players.get(i).isStanding())

                       {

                           //System.out.println("pno = " + pno + " count = "+ count);

                           count++;

                           if(count == pno)

                           {

                               players.get(i).sit();

                               break;

                           }

                       }

                          

                   }

               }

           }

           lineScanner.close();

       }

   }

   private void displayStandingPlayers()

   {

  

       int i = 0 ;

       System.out.println("List of standing players");

       for(Player p : players)

       {

      

           if(p.isStanding())

           {

               i++;

               System.out.println(i + ". " + p);

              

           }

       }

   }

  

   public void displayScores()

   {

       System.out.println("Scores of players is as follows: ");

       for(Player p : players)

           System.out.println(p.toString());

   }

  

   private void declareWinner()

   {

       int maxScore = 0;

       ArrayList<Player> winners = new ArrayList<Player>();

         

      

       //find player with maximum score

       for(Player p : players)

       {

           if(p.getTotalScore() > maxScore)

           {

               maxScore = p.getTotalScore();

               winners.clear(); //clear previously found winners

               winners.add(p);

           }

           else if(p.getTotalScore() == maxScore) //another player with same score

           {

               winners.add(p);

           }

       }

      

       if(winners.isEmpty())

           System.out.println("Nobody wins!!!");

       else

       {

           System.out.print(" ***** Winner is ");

           for(Player p : winners)

               System.out.print("[" +p.getName() + "] ");

          

           System.out.print(" *****");

       }

   }

}

SkunkDriver.java

import java.util.Scanner;

public class SkunkDriver {

   public static void main(String[] args) {

       Scanner input = new Scanner(System.in);

       int n;

       String name;

       SkunkGame game = new SkunkGame();

       //ask for number of players

       System.out.print(" How many players? ");

       n = input.nextInt();

      

       input.nextLine(); //flush out newline before using nextline()

      

       //get player names

       for(int i = 0; i < n; i++)

       {

           System.out.print(" Enter Player " + (i+1) + " Name: ");

           name = input.nextLine().trim();

           game.addPlayer(new Player(name));

       }

      

       //start the game

       game.start(input);

       input.close();

   }

}

output

How many players? 3

Enter Player 1 Name: Kerry

Enter Player 2 Name: Lisa

Enter Player 3 Name: Nelson

***** Start Round 'S' *****

Die1: 3 Die2: 4

List of standing players

1. Kerry[7, 0, 0, 0, 0] : Total (7)

2. Lisa[7, 0, 0, 0, 0] : Total (7)

3. Nelson[7, 0, 0, 0, 0] : Total (7)

Does anyone want to sit? y/n:

n

Die1: 5 Die2: 5

List of standing players

1. Kerry[17, 0, 0, 0, 0] : Total (17)

2. Lisa[17, 0, 0, 0, 0] : Total (17)

3. Nelson[17, 0, 0, 0, 0] : Total (17)

Does anyone want to sit? y/n:

n

Die1: 2 Die2: 6

List of standing players

1. Kerry[25, 0, 0, 0, 0] : Total (25)

2. Lisa[25, 0, 0, 0, 0] : Total (25)

3. Nelson[25, 0, 0, 0, 0] : Total (25)

Does anyone want to sit? y/n:

n

Die1: 6 Die2: 2

List of standing players

1. Kerry[33, 0, 0, 0, 0] : Total (33)

2. Lisa[33, 0, 0, 0, 0] : Total (33)

3. Nelson[33, 0, 0, 0, 0] : Total (33)

Does anyone want to sit? y/n:

y

Enter the sl no. of players who wish to sit : 1 3

Die1: 6 Die2: 5

List of standing players

1. Lisa[44, 0, 0, 0, 0] : Total (44)

Does anyone want to sit? y/n:

n

Die1: 3 Die2: 5

List of standing players

1. Lisa[52, 0, 0, 0, 0] : Total (52)

Does anyone want to sit? y/n:

y

Enter the sl no. of players who wish to sit : 1

Die1: 2 Die2: 4

Die1: 3 Die2: 6

Die1: 2 Die2: 1

***** End Round 'S' *****

Scores of players is as follows:

Kerry[33, 0, 0, 0, 0] : Total (33)

Lisa[52, 0, 0, 0, 0] : Total (52)

Nelson[33, 0, 0, 0, 0] : Total (33)

***** Start Round 'K' *****

Die1: 2 Die2: 1

***** End Round 'K' *****

Scores of players is as follows:

Kerry[33, 0, 0, 0, 0] : Total (33)

Lisa[52, 0, 0, 0, 0] : Total (52)

Nelson[33, 0, 0, 0, 0] : Total (33)

***** Start Round 'U' *****

Die1: 5 Die2: 6

List of standing players

1. Kerry[33, 0, 11, 0, 0] : Total (44)

2. Lisa[52, 0, 11, 0, 0] : Total (63)

3. Nelson[33, 0, 11, 0, 0] : Total (44)

Does anyone want to sit? y/n:

n

Die1: 2 Die2: 3

List of standing players

1. Kerry[33, 0, 16, 0, 0] : Total (49)

2. Lisa[52, 0, 16, 0, 0] : Total (68)

3. Nelson[33, 0, 16, 0, 0] : Total (49)

Does anyone want to sit? y/n:

n

Die1: 6 Die2: 3

List of standing players

1. Kerry[33, 0, 25, 0, 0] : Total (58)

2. Lisa[52, 0, 25, 0, 0] : Total (77)

3. Nelson[33, 0, 25, 0, 0] : Total (58)

Does anyone want to sit? y/n:

y

Enter the sl no. of players who wish to sit : 1 2

Die1: 5 Die2: 2

List of standing players

1. Nelson[33, 0, 32, 0, 0] : Total (65)

Does anyone want to sit? y/n:

n

Die1: 6 Die2: 5

List of standing players

1. Nelson[33, 0, 43, 0, 0] : Total (76)

Does anyone want to sit? y/n:

n

Die1: 3 Die2: 5

List of standing players

1. Nelson[33, 0, 51, 0, 0] : Total (84)

Does anyone want to sit? y/n:

y

Enter the sl no. of players who wish to sit : 1

Die1: 1 Die2: 2

***** End Round 'U' *****

Scores of players is as follows:

Kerry[33, 0, 25, 0, 0] : Total (58)

Lisa[52, 0, 25, 0, 0] : Total (77)

Nelson[33, 0, 51, 0, 0] : Total (84)

***** Start Round 'N' *****

Die1: 4 Die2: 4

List of standing players

1. Kerry[33, 0, 25, 8, 0] : Total (66)

2. Lisa[52, 0, 25, 8, 0] : Total (85)

3. Nelson[33, 0, 51, 8, 0] : Total (92)

Does anyone want to sit? y/n:

n

Die1: 5 Die2: 6

List of standing players

1. Kerry[33, 0, 25, 19, 0] : Total (77)

2. Lisa[52, 0, 25, 19, 0] : Total (96)

3. Nelson[33, 0, 51, 19, 0] : Total (103)

Does anyone want to sit? y/n:

n

Die1: 1 Die2: 2

***** End Round 'N' *****

Scores of players is as follows:

Kerry[33, 0, 25, 0, 0] : Total (58)

Lisa[52, 0, 25, 0, 0] : Total (77)

Nelson[33, 0, 51, 0, 0] : Total (84)

***** Start Round 'K' *****

Die1: 6 Die2: 1

***** End Round 'K' *****

Scores of players is as follows:

Kerry[33, 0, 25, 0, 0] : Total (58)

Lisa[52, 0, 25, 0, 0] : Total (77)

Nelson[33, 0, 51, 0, 0] : Total (84)

***** Winner is [Nelson] *****