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

this is a version of code for a dice game called ButtonMen. I\'m trying to incor

ID: 3832724 • Letter: T

Question

this is a version of code for a dice game called ButtonMen. I'm trying to incorporate a game menu of sorts where it lists characters for a player to choose from and play as. This is the list of characters: Soldiers: Avis, Hammer, Bauer, Stark, Kith, Clare, Karl, Iago, Niles, Shore, Hannah, Kublai Vampyres: Angel, Buddy, Tiffany, McGinty, Dunkirk, Starchylde Brom: Coil, Bane, Lucky, Shepherd, Peace, Crusher, Grist, Wastenott, Reaver, Jellybean, Bluff, Strikimport

java.util.ArrayList;
import java.util.Random;
public class ButtonMenGame
{
class Dice
{
  int no_of_sides;
  public Dice(int no_of_sides)
  {
   this.no_of_sides = no_of_sides;
  }
  int rollDice()
  {
   Random r = new Random();
   return r.nextInt(no_of_sides) + 1;
  }
  @Override
  public String toString()
  {
   return no_of_sides + "";
  }
}
private String player1, player2;
private ArrayList<Dice> player1Dices;
private ArrayList<Dice> player2Dices;
private int player1Points = 0, player2Points = 0;
public ButtonMenGame(String player1, String player2)
{
  this.player1 = player1;
  this.player2 = player2;
  player1Dices = new ArrayList<>();
  player2Dices = new ArrayList<>();
  Random r = new Random();
  for(int i=0; i<5; i++)
  {
   player1Dices.add(new Dice(r.nextInt(20) + 1));
   player2Dices.add(new Dice(r.nextInt(20) + 1));
  }
}
public void playGame()
{
  while(player1Dices.size() != 0 && player2Dices.size() != 0)
  {
   Dice d1 = player1Dices.remove(0);
   Dice d2 = player2Dices.remove(0);
   boolean tie = false;
   do
   {
    int roll1 = d1.rollDice();
    int roll2 = d2.rollDice();
    System.out.println("P1 rolled " + roll1 + " on dice of sides " + d1.no_of_sides);
    System.out.println("P2 rolled " + roll2 + " on dice of sides " + d2.no_of_sides);
    if(roll1 > roll2)
    {
     System.out.println("P1 Won.");
     player1Points += d2.no_of_sides;
     player1Dices.add(d1);
     player1Dices.add(d2);
     tie = false;
     }
    else if(roll1 < roll2)
    {
     System.out.println("P2 Won.");
     player2Points += d1.no_of_sides;
     player2Dices.add(d1);
     player2Dices.add(d2);
     tie = false;
     }
    else
    {
     System.out.println("Tie. Roll again.");
     tie = true;
    }
   }
   while(tie);
   System.out.println("Points: " + player1 + ": " + player1Points + ", " + player2 + ": " + player2Points + " ");
   if(player1Points > player2Points)
   {
    System.out.println(" ************* "+ player1 +" Has won ********************");
   }
   else if(player1Points < player2Points)
   {
    System.out.println(" ************* "+ player2 +" Has won ********************");
   }
   else
   {
    System.out.println(" ************* Its a tie!! ********************");
   }
  }
}
public static void main(String[] args)
{
  ButtonMenGame buttonMen = new ButtonMenGame("Player1", "Player2");
  buttonMen.playGame();
}
}

Explanation / Answer

Please find my implementation.

import java.util.ArrayList;

import java.util.Random;

import java.util.Scanner;

public class ButtonMen

{

   class Dice

   {

       int no_of_sides;

       public Dice(int no_of_sides)

       {

           this.no_of_sides = no_of_sides;

       }

       int rollDice()

       {

           Random r = new Random();

           return r.nextInt(no_of_sides) + 1;

       }

       @Override

       public String toString()

       {

           return no_of_sides + "";

       }

   }

   private String player1, player2;

   private ArrayList<Dice> player1Dices;

   private ArrayList<Dice> player2Dices;

   private int player1Points = 0, player2Points = 0;

   public ButtonMen(String player1, String player2)

   {

       this.player1 = player1;

       this.player2 = player2;

       player1Dices = new ArrayList<>();

       player2Dices = new ArrayList<>();

       Random r = new Random();

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

       {

           player1Dices.add(new Dice(r.nextInt(20) + 1));

           player2Dices.add(new Dice(r.nextInt(20) + 1));

       }

   }

   public void playGame()

   {

       while(player1Dices.size() != 0 && player2Dices.size() != 0)

       {

           Dice d1 = player1Dices.remove(0);

           Dice d2 = player2Dices.remove(0);

           boolean tie = false;

           do

           {

               int roll1 = d1.rollDice();

               int roll2 = d2.rollDice();

               System.out.println("P1 rolled " + roll1 + " on dice of sides " + d1.no_of_sides);

               System.out.println("P2 rolled " + roll2 + " on dice of sides " + d2.no_of_sides);

               if(roll1 > roll2)

               {

                   System.out.println("P1 Won.");

                   player1Points += d2.no_of_sides;

                   player1Dices.add(d1);

                   player1Dices.add(d2);

                   tie = false;

               }

               else if(roll1 < roll2)

               {

                   System.out.println("P2 Won.");

                   player2Points += d1.no_of_sides;

                   player2Dices.add(d1);

                   player2Dices.add(d2);

                   tie = false;

               }

               else

               {

                   System.out.println("Tie. Roll again.");

                   tie = true;

               }

           }while(tie);

           System.out.println("Points: " + player1 + ": " + player1Points + ", " + player2 + ": " + player2Points + " ");

           if(player1Points > player2Points)

           {

               System.out.println(" ************* "+ player1 +" Has won ********************");

           }

           else if(player1Points < player2Points)

           {

               System.out.println(" ************* "+ player2 +" Has won ********************");

           }

           else

           {

               System.out.println(" ************* Its a tie!! ********************");

           }

       }

   }

  

   public static String PlayerName(String[] soldiers, String[] vampyres,

           String[] brom, String player, Scanner sc){

      

       System.out.println("Choose "+player+": 1. Soldiers 2.Vampyres and Any key for Brom ");

       int op = sc.nextInt();

      

       String[] arr = null;

       if(op == 1)

           arr = soldiers;

       else if(op == 2)

           arr = vampyres;

       else

           arr = brom;

      

       int index;

       System.out.println("Choose : ");

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

           System.out.println((i+1)+" "+arr[i]);

      

       index = sc.nextInt();

       return arr[index-1];

   }

   public static void main(String[] args)

   {

       String[] soldiers = {"Avis", "Hammer", "Bauer", "Stark", "Kith", "Clare", "Karl", "Iago", "Niles",

               "Shore", "Hannah", "Kublai"};

       String[] vampyres = {"Angel", "Buddy", "Tiffany", "McGinty", "Dunkirk", "Starchylde"};

       String[] brom = {"Coil", "Bane", "Lucky","Shepherd", "Peace", "Crusher", "Grist", "Wastenott",

               "Reaver", "Jellybean", "Bluff", "Strikimport"};

      

       Scanner sc = new Scanner(System.in);

       String player1 = PlayerName(soldiers, vampyres, brom, "Player1", sc);

       String player2 = PlayerName(soldiers, vampyres, brom, "Player2", sc);

       sc.close();

       ButtonMen buttonMen = new ButtonMen(player1, player2);

       buttonMen.playGame();

   }

}

/*

Sample run:

Choose Player1: 1. Soldiers 2.Vampyres and Any key for Brom

1

Choose :

1 Avis

2 Hammer

3 Bauer

4 Stark

5 Kith

6 Clare

7 Karl

8 Iago

9 Niles

10 Shore

11 Hannah

12 Kublai

2

Choose Player2: 1. Soldiers 2.Vampyres and Any key for Brom

3

Choose :

1 Coil

2 Bane

3 Lucky

4 Shepherd

5 Peace

6 Crusher

7 Grist

8 Wastenott

9 Reaver

10 Jellybean

11 Bluff

12 Strikimport

10

P1 rolled 8 on dice of sides 8

P2 rolled 2 on dice of sides 3

P1 Won.

Points: Hammer: 3, Jellybean: 0

************* Hammer Has won ********************

P1 rolled 2 on dice of sides 9

P2 rolled 9 on dice of sides 11

P2 Won.

Points: Hammer: 3, Jellybean: 9

************* Jellybean Has won ********************

P1 rolled 12 on dice of sides 16

P2 rolled 2 on dice of sides 6

P1 Won.

Points: Hammer: 9, Jellybean: 9

************* Its a tie!! ********************

P1 rolled 8 on dice of sides 10

P2 rolled 9 on dice of sides 10

P2 Won.

Points: Hammer: 9, Jellybean: 19

************* Jellybean Has won ********************

P1 rolled 6 on dice of sides 20

P2 rolled 3 on dice of sides 4

P1 Won.

Points: Hammer: 13, Jellybean: 19

************* Jellybean Has won ********************

P1 rolled 4 on dice of sides 8

P2 rolled 7 on dice of sides 9

P2 Won.

Points: Hammer: 13, Jellybean: 27

************* Jellybean Has won ********************

P1 rolled 3 on dice of sides 3

P2 rolled 2 on dice of sides 11

P1 Won.

Points: Hammer: 24, Jellybean: 27

************* Jellybean Has won ********************

P1 rolled 7 on dice of sides 16

P2 rolled 4 on dice of sides 10

P1 Won.

Points: Hammer: 34, Jellybean: 27

************* Hammer Has won ********************

P1 rolled 4 on dice of sides 6

P2 rolled 5 on dice of sides 10

P2 Won.

Points: Hammer: 34, Jellybean: 33

************* Hammer Has won ********************

P1 rolled 19 on dice of sides 20

P2 rolled 8 on dice of sides 8

P1 Won.

Points: Hammer: 42, Jellybean: 33

************* Hammer Has won ********************

P1 rolled 4 on dice of sides 4

P2 rolled 8 on dice of sides 9

P2 Won.

Points: Hammer: 42, Jellybean: 37

************* Hammer Has won ********************

P1 rolled 1 on dice of sides 3

P2 rolled 6 on dice of sides 6

P2 Won.

Points: Hammer: 42, Jellybean: 40

************* Hammer Has won ********************

P1 rolled 9 on dice of sides 11

P2 rolled 6 on dice of sides 10

P1 Won.

Points: Hammer: 52, Jellybean: 40

************* Hammer Has won ********************

P1 rolled 13 on dice of sides 16

P2 rolled 3 on dice of sides 4

P1 Won.

Points: Hammer: 56, Jellybean: 40

************* Hammer Has won ********************

P1 rolled 1 on dice of sides 10

P2 rolled 7 on dice of sides 9

P2 Won.

Points: Hammer: 56, Jellybean: 50

************* Hammer Has won ********************

P1 rolled 3 on dice of sides 20

P2 rolled 3 on dice of sides 3

Tie. Roll again.

P1 rolled 1 on dice of sides 20

P2 rolled 2 on dice of sides 3

P2 Won.

Points: Hammer: 56, Jellybean: 70

************* Jellybean Has won ********************

P1 rolled 1 on dice of sides 8

P2 rolled 4 on dice of sides 6

P2 Won.

Points: Hammer: 56, Jellybean: 78

************* Jellybean Has won ********************

P1 rolled 5 on dice of sides 11

P2 rolled 9 on dice of sides 10

P2 Won.

Points: Hammer: 56, Jellybean: 89

************* Jellybean Has won ********************

P1 rolled 2 on dice of sides 10

P2 rolled 9 on dice of sides 9

P2 Won.

Points: Hammer: 56, Jellybean: 99

************* Jellybean Has won ********************

P1 rolled 7 on dice of sides 16

P2 rolled 19 on dice of sides 20

P2 Won.

Points: Hammer: 56, Jellybean: 115

************* Jellybean Has won ********************

P1 rolled 4 on dice of sides 4

P2 rolled 3 on dice of sides 3

P1 Won.

Points: Hammer: 59, Jellybean: 115

************* Jellybean Has won ********************

P1 rolled 2 on dice of sides 4

P2 rolled 3 on dice of sides 8

P2 Won.

Points: Hammer: 59, Jellybean: 119

************* Jellybean Has won ********************

P1 rolled 2 on dice of sides 3

P2 rolled 3 on dice of sides 6

P2 Won.

Points: Hammer: 59, Jellybean: 122

************* Jellybean Has won ********************

*/