Results 1 to 3 of 3

Thread: Help Wanted: Java Program

  1. #1
    Junior Member
    Join Date
    Oct 2004
    Posts
    1

    Help Wanted: Java Program

    Dear friends,

    I have just registered to antionline and I am a very new user.

    I have an assignment to make :

    I have a problem of making a dice simple game using Java. I am new to Java program and know a little about it. Could you help me?


    These are the questions :

    Senario :

    I have been asked to write a simple program ( using Java ) which determine and display player that obtain the highest dice value among the four player. Dice value for each player will be given by the system randomly. For this reason, I have to make two coding :

    ( a )The first coding must have a Class that can shows the value of dice randomly between 1 until 6 which are given by the system itself.

    ( b )Secondly, I need to make another coding that is Aplication Coding to decide which player will get the highest dice value among the four player. Meaning that, I have to combine the class from ( a ) to obtain the random value for each player.


    The desire Output Format are as followed :

    Ourput Example :


    Dice Value for player One : 4
    Dice Value for player Two : 2
    Dice Value for player Three : 1
    Dice Value for player FOur : 2

    Player One obtain the highest dice value.


    Thank you.

    vandome03@yahoo.com

  2. #2
    Elite Hacker
    Join Date
    Mar 2003
    Posts
    1,407
    Check out the API on the Random class, you're probably going to want to use that for your class that generates the numbers and holds the values. I'm guessing you will have a separate instance for each player, so let's say you called the class RandomRoll, from another class you could use it by creating an instance like
    RandomRoll player1 = new RandomRoll();
    then you could have a method in there that was something like player1.roll();
    and you could either have that return a value, or have it store it in a variable within the class which you could have another method to retrieve such as player1.getRollValue();
    and you would have that for each player. Then for the class to compare them just have a method take in either 4 ints or 4 instances of the class. Or actually you could do that any number of ways. A suggestion which probably isn't a very good one would be to have a method like this
    compareRolls(player1, player2, player3, player4)
    all of those being instances of the RandomRoll class. Then use the getRoll() method or whatever accessor you have for the data of the roll to compare them. I think I'm overdoing it and my method is probably bad, but it is an idea which you can expand or contract upon :P. Good luck.

  3. #3
    Banned
    Join Date
    Sep 2004
    Posts
    305
    Just typing into the text field, not even compiling any of the code so you're going to have to add the necessary class statements to make it run if I make any errors...


    a)
    Code:
    import java.util.Random;
    Random generator = new Random();
    for(int counter = 1; counter < 5; counter++) {
          System.out.println("Player " + counter + " rolled a: " + generator.nextInt(6)+1);
    }
    b) I have a highest variable that automatically sets the highest roll as well as which player rolled it... see below:

    Code:
    import java.util.Random;
    Random generator = new Random();
    int highest = 0;
    int player = 0;
    int roll = 0;
    for(int counter = 1; counter < 5; counter++) {
          roll = generator.nextInt(6)+1;
          System.out.println("Player " + counter + " rolled a: " + roll);
          if(roll > highest) {
               highest = roll;
               player = counter;
          }
    }
    System.out.println("Player " + player + " rolled the higest with a: " + roll);

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •