Question

In: Computer Science

This game is meant for two or more players. In the game, each player starts out...

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;
}


}

Solutions

Expert 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");
   }
}


Related Solutions

Two players (player A and player B) are playing a game against each other repeatedly until...
Two players (player A and player B) are playing a game against each other repeatedly until one is bankrupt. When a player wins a game they take $1 from the other player. All plays of the game are independent and identical. Suppose player A starts with $6 and player B starts with $6. If player A wins a game with probability 0.5, what is the probability the game ends (someone loses all their money) on exactly the 10th play of...
(10 marks) Two players (player A and player B) are playing a game against each other...
Two players (player A and player B) are playing a game against each other repeatedly until one is bankrupt. When a player wins a game they take $1 from the other player. All plays of the game are independent and identical. a) Suppose player A starts with $2 and player B starts with $1. If player A wins a game with probability p, what is the probability that player A wins all the money? b) Suppose player A starts with...
Consider a game with two players, each of whom has two types. The types of player...
Consider a game with two players, each of whom has two types. The types of player 1 are T1 = (a,b). The types of player 2 are T2 = (c,d). Suppose the beliefs of the types are p1(c/a) = p2(a/c) = 0.25 and p1(c/b) = p2(a/d) = 0.75. Is there a common prior? If yes, construct one; if no, prove why not.
There are two players in the game. Each player can pick any integer number between 1...
There are two players in the game. Each player can pick any integer number between 1 and n. If two numbers are the same then player 1 pays 1 dollar to player 2. If two numbers are different than nothing happens. (a) Prove that there are no equilibria in pure strategies; (b) Prove that in the equilibrium each strategy should be played with a positive probability. (c) Find all NE of the game.
Consider the following game that has two players. Player A has three actions, and player B...
Consider the following game that has two players. Player A has three actions, and player B has three actions. Player A can either play Top, Middle or Bottom, whereas player B can play Left, Middle or Right. The payoffs are shown in the following matrix. Notice that a payoff to player A has been omitted (denoted by x). Player B    Left Middle Right Top (-1,1) (0,3) (1,10) Middle (2,0) (-2,-2) (-1,-1) Bottom (x,-1) (1,2) (3,2) (player A) Both players...
Coin taking game This game is played between 2 players, player 1 and player 2. There...
Coin taking game This game is played between 2 players, player 1 and player 2. There are two piles of coins. The values of a coin can be any integer. Both players know the values of all coins in both piles. Player 1 makes the first move, and play alternates between the players. A move consists of taking a coin from the top of either of the piles (either player can take from either pile). The game ends when both...
Two players play a rock-paper-scissors game. The losing player will give $1 to the winning player....
Two players play a rock-paper-scissors game. The losing player will give $1 to the winning player. If it is a draw, no payment is made. The payoff to a player is the amount of money (s)he gets. Represent the situation in a matrix form. Find all the Nash equilibria.
Two players A and B play a dice game with a 6-face fair dice. Player A...
Two players A and B play a dice game with a 6-face fair dice. Player A is only allowed to roll the dice once. Player B is allowed to roll the dice maximally twice (that is, Player B can decide whether or not she or he would roll the dice again after seeing the value of the first roll). If the player with the larger final value of the dice wins, what is the maximal probability for Player B to...
This question is about the sequential moves Stag Hunt game. There are two players. Player 1...
This question is about the sequential moves Stag Hunt game. There are two players. Player 1 moves first, player 2 observes player 1’s move, and then player 2 moves. Players get 10 jollies each if they both choose Stag; 5 jollies if they choose Hare; and 0 jollies if they choose Stag but the other player does not. What is the set of (pure) strategies for player 1? What is the set of (pure) strategies for player 2? Explain why...
Below is a game between player A and player B. Each player has two possible strategies:...
Below is a game between player A and player B. Each player has two possible strategies: 1 or 2. The payoffs for each combination of strategies between A and B are in the bracket. For example, if A plays 1 and B plays 1, the payoff for A is -3, and the payoff for B is -2. Player B Strategy 1 Strategy 2 Player A Strategy 1 (-3,-2) (10,0) Strategy 2 (0,8) (0,0) How many pure strategy Nash equilibria does...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT