DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 4 of 4
  1. #1
    Join Date
    Nov 2005
    Posts
    3

    Dice problem. Help needed.

    Greetings to everyone.

    Assume that we have a dice program a very simple one, just prints line of information.
    Imagine that we don't want to roll two sixes in any six consecutive roll, how can we do that?

    code:
    lastRoll = diceone.roll();
    for(int i = 0; i<600; i++){
    int newRoll = diceone.roll();
    if (newRoll == lastRoll){
    System.out.println("new and last roll are identical");
    break;
    }
    lastRoll = newRoll;
    }


    thanks in advance
    d.

  2. #2
    Join Date
    Sep 2005
    Location
    istanbul / Turkey
    Posts
    133
    rollsIndex = rollsIndex + 1 % 6;
    this dont let more than 6 element in array .
    includes function search in arrray.


    Code:
    import java.util.Random;
    
    public class DiceTest {
      // roll the dice
      public static int diceone_roll(){
        int i =  ( r.nextInt(6) + 1 );//a random between 1-6 
        System.out.println(i);
        return i;
      }
      static Random r = new Random(System.currentTimeMillis());
      
      // if newRoll's value repeated rolls
      public static boolean includes(int newRoll,int[] rolls ){
        for ( int i = 0 ; i < rolls.length ; i++ ){
          if ( newRoll == rolls[i] ) return true;//found
        }
        return false;// not found
      }
      
      public static void main(String[] args) {
        int[] rolls = new int[6];
        int rollsIndex = 0;
        for (int i = 0; i < 600; i++) {
          int newRoll = diceone_roll();
          if ( includes(newRoll,rolls) ) {
            // rollsIndex = 0; // 
            System.out.println("new and last roll are identical");
            break;
          }
         rollsIndex = rollsIndex + 1 % 6;
          rolls[rollsIndex] = newRoll; // dont let more than 6 consecutive
        }
    
      }
    }
    Last edited by mr1yh1; 11-11-2005 at 05:18 AM.

  3. #3
    Join Date
    Nov 2005
    Posts
    3
    thanks for that. really helpful stuff. can anyone tell me how can you create a class that will only roll 50 sixes and 110 of the other values in 600 rolls? and how can yu make a class so that two separate new derived dice objects can communicate and not roll consecutive sixes? i think something with the static int will be an ideal way but how?

    thanks in advance
    d.

  4. #4
    Join Date
    Nov 2005
    Posts
    3
    after getting my head down to the problem I found it. thanks for your help.

Similar Threads

  1. Help with histogram problem
    By RPBLEA in forum Java
    Replies: 3
    Last Post: 10-25-2005, 02:43 PM
  2. TimerTask problem (URGENT HELP NEEDED PLS!)
    By jackbauer in forum Java
    Replies: 1
    Last Post: 09-15-2005, 08:01 AM
  3. Please Help! rolling of two dice.
    By Ana in forum VB Classic
    Replies: 2
    Last Post: 02-24-2005, 01:24 PM
  4. a problem with font and language
    By Roseta in forum VB Classic
    Replies: 0
    Last Post: 11-14-2001, 03:24 AM
  5. Replies: 0
    Last Post: 06-23-2000, 02:17 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links