Solved

Dice code modification Question

Posted on 2007-12-03
2
666 Views
Last Modified: 2008-02-01
The code below works for finding 5, 4, 3 and a pair for dice and compare hands. Now I would like to add a small and large straight and full house..(like a yahtzee hand). I plan on writing methods for the straights and full house...but my questions are:

1. Where would I need to add this in existing code.
2. I assume I would have to once determining either straight or flush I would have to assign a value for it...What would be good?

Some comments appreciated...
public class FiveDice3
{
  public static void main(String[] args)
  {
     final int NUM = 5;
     Die[] comp = new Die[NUM];
     Die[] player =  new Die[NUM];
     int x;
     for(x = 0; x < NUM; ++x)
     {
        comp[x] = new Die();
        player[x] = new Die();
     }
     int compMatch, playerMatch;
     int compHigh, playerHigh;
     String computerName = "Computer";
     String playerName = "You";
     display(computerName, comp, NUM);
     display(playerName, player, NUM);
     compMatch = howManySame(comp, NUM);
     playerMatch = howManySame(player, NUM);
     compHigh = compMatch / 10;
     playerHigh = playerMatch / 10;
     compMatch = compMatch % 10;
     playerMatch = playerMatch % 10;
     if(compMatch == 1)
       System.out.println("Computer has nothing");
     else
       System.out.println("Computer has " + compMatch + " of a kind");
     if(playerMatch == 1)
       System.out.println("You have nothing");
     else
       System.out.println("You have " + playerMatch + " of a kind");
     if(compMatch > playerMatch)
        System.out.println("Computer wins");
     else
        if(compMatch < playerMatch)
           System.out.println("You win");
        else
        {
          if(compHigh > playerHigh)
            System.out.println("Computer's values are higher - computer wins");
          else
            if(compHigh < playerHigh)
               System.out.println("Your values are higher - you win");
            else
              System.out.println("It's a tie");
        }
  }
  public static void display(String who, Die[] die, int num)
  {
     int x;
     String dice = "";
     for(x = 0; x < num; ++x)
        dice = dice + die[x].getValue() + " ";
     System.out.println(who + " rolled: " + dice);
  }
  public static int howManySame(Die[] die, int num)
  {
     int[] same = new int[num];
     int x, y;
     int highest;
     int highValue = 0;
     for(x = 0; x < num; ++x)
       same[x] = 1; // every die matches itself
     for(x = 0; x < num - 1; ++x)
     {
       for(y = x + 1; y < num; ++y)
         if(die[x].getValue() == die[y].getValue())
         {
             same[x]++;
             if(die[x].getValue() > highValue)
                highValue = die[x].getValue();
         }
     }
     highest = same[0];
     for(x = 1; x < num; ++x)
        if(same[x] > highest)
          highest = same[x];
     highest = highest + 10 * highValue;
     return highest;
  } 
}
public class Die
{
  private int value;
  private static final int HIGHEST_DIE_VALUE = 6;
  private static final int LOWEST_DIE_VALUE = 1;
  public Die()
  {
    value = ((int)(Math.random() * 100) % HIGHEST_DIE_VALUE +
       LOWEST_DIE_VALUE);
  }
  public int getValue()
  {
    return value;
  }
}

Open in new window

0
Comment
Question by:CPlusJavaCSharp
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 20402018
Excuse my ignorance, but how do you get a flush in dice - there are no suits..?
0
 
LVL 92

Accepted Solution

by:
objects earned 350 total points
ID: 20402043
1. add a method (or define an interface and a class) for each check thats required.
2. value them based on their rank
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
servlet filter example 37 85
how to install java on RHEL image on EC2 4 58
Java: anonymous class 4 39
What is the use of Forwarding Class in java 1 37
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question