Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi everyone,

I need to implement a game GameOfDice to be played by two players. Each player gets to roll a  die. The player who rolls the highest wins that round. A player has to win three rounds to  win the game. A game can have at most nine rolls for each player. After the ninth roll, if no  one wins, the game ends and it’s a draw. [Hint: You may generate nine random rolls for each  player, for example: Random number for Player1 is 345612312, and then the first  move of Player1 is 3, next is 4 and so on. The same follows for Player2].

Implement methods:

game_on() – accepts two player objects

get_move() – get the move for a player for the corresponding round.

You may use additional method to generate the moves. 

Sample Output: 

Player1's Move: 4 

Player2's Move: 3 

Player1's Move: 2 

Player2's Move: 1 

Player1's Move: 1 

Player2's Move: 2 

Player1's Move: 2 

Player2's Move: 3 

Player1's Move: 6 

Player2's Move: 2 

Winner is Player1 

So far, I have tried to generate the dice but I need 9 random rolls from (anywhere 1 through 6) and player with the first 3 games wins, if 9 draw. I'm having trouble with the aforementioned methods where the players are created and the rolls are parsed as individual moves.

Java
import java.util.*;
    public class Dice {
     
    	// Creating an object from the class "Random" to be able to use the "Random Number" generator.
    	Random generator = new Random();
     
    	// Public instance variable called "thro" to store in it the randomly numbers.
    	public int thro;
     
    	// Data member "Length", of type "Integer". To get the length of the dice.
    	private int length;
     
    	// get method to get the vd
Posted

1 solution

See http://docs.oracle.com/javase/7/docs/api/java/util/Random.html[^], and please make more of an effort to do your homework for yourself.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900