This game is meant for two or more players In the game each

This game is meant for two or more players. In the game, each player starts out with 50 points, as each player takes a turn rolling the dice; the amount generated by the dice is subtracted from the player’s points. The first player with exactly one point remaining wins. If a player’s remaining points minus the amount generated by the dice results in a value less than one, then the amount should be added to the player’s points. (As an alternative, the game can be played with a set number turns. In this case, the player with the amount of points closest to one, when all rounds have been played, wins.)

Write a program that simulates the game being played by two players. Use the Die class that was presented in Chapter 6 to simulate the dice. Write a Player class to simulate the players. (in java)

die.java

import java.util.Random;

public class die
{
private int sides;
private int value;

public die()
{
    sides = 6;
    roll();
}

public void roll()
{
    Random r = new Random();
    value = r.nextInt(sides+1);
}

public int getValue()
{
    return value;
}

public int getSides()
{
    return sides;
}


}

Solution


public class Player {

   int numberPlayers;
   int points;
   die d = new die();
   public Player(int n, int points)
   {
       this.numberPlayers = n;
       this.points = points;
   }
  
   public Player()
   {
       this(2, 50);
   }
  
   public int play()
   {
       int player1Points = this.points;
       int player2Points = this.points;
      
       boolean turn = true;
       while((player1Points != 1) && (player2Points != 1))
       {
           d.roll();
           int amount = d.getValue();
           if (turn)
           {
               if (player1Points - amount < 0)
               {
                   player1Points += amount;
               }
               else
               {
                   player1Points -= amount;
               }
               turn = false;
           }
           else
           {
               if (player2Points - amount < 0)
               {
                   player2Points += amount;
               }
               else
               {
                   player2Points -= amount;
               }
               turn = true;
           }
       }
      
       if (player1Points == 1)
       {
           return 1;
       }
       else
       {
           return 2;
       }
   }
  
   public static void main(String[] args)
   {
       Player p = new Player();
      
       System.out.println(\"Player \" + p.play() + \" wins\");
   }
}

This game is meant for two or more players. In the game, each player starts out with 50 points, as each player takes a turn rolling the dice; the amount generat
This game is meant for two or more players. In the game, each player starts out with 50 points, as each player takes a turn rolling the dice; the amount generat

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site