Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old May 9th, 2008, 8:38 PM   #1
cribbageman2500
Newbie
 
Join Date: Apr 2008
Posts: 8
Rep Power: 0 cribbageman2500 is on a distinguished road
Ten sided die simulator.

Hello everyone! This is the first time I have really tried to program, so I'd like to apologize in advance if the code I'm about to present is god-awful. Here's the basic idea, A certain # of 10 sided dice (w/ values 1-10) will be rolled, if the resulting number on any die is above a certain threshold it will be recorded and if it above another threshold then it will be recorded, and "rolled" again.

For example If I instructed it to roll 5 dice with a threshold of 8 and a repeat of 10 and the results were: 1,3,7,10,8
the program would tally the 2 successes and "roll" the ten again resulting in a 9,
there would then be 3 successes and the program would end and output the tally to a text area.

So far I have an almost functional program, the current issue is that it will only allow the generation of one # at a time. If more then one "roll" is requested, or it hits the again threshold when rolling 1 die it will run forever.

Just so you know I'm implementing it into a GUI, but for simplicity's sake I'll leave that code out. And if it matters I'm working in the Net Beans IDE.

java Syntax (Toggle Plain Text)
  1. import java.util.*;
  2. import javax.swing.*;
  3.  
  4. public class D10sim extends javax.swing.JFrame {
  5.  
  6.  
  7. private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
  8. int gen, tally, num1;
  9. double dice = Double.parseDouble(jTextField1.getText());
  10. double thresh = Double.parseDouble(jTextField2.getText());
  11. double again = Double.parseDouble(jTextField3.getText());
  12. gen = 0;
  13. tally = 0;
  14. {num1 = (int) ((Math.random() * 10) + 1);
  15. while (gen<dice)
  16. {if (num1>=again)
  17. {dice ++;
  18. tally ++;
  19. gen ++;}
  20. else
  21. if (num1>=thresh)
  22. {tally ++;
  23. gen ++;}
  24. else
  25. if (num1<thresh)
  26. {gen ++;}
  27. jTextArea1.setText(String.valueOf(tally));
  28. }
  29. }
  30.  
  31. }

Thank you in advance you your help, any advice is much appreciated.
cribbageman2500 is offline   Reply With Quote
Old May 10th, 2008, 12:15 AM   #2
Jabo
Not a user?
 
Join Date: Sep 2007
Posts: 230
Rep Power: 1 Jabo is on a distinguished road
Re: Ten sided die simulator.

Quote:
Originally Posted by cribbageman2500 View Post
java Syntax (Toggle Plain Text)
  1. {num1 = (int) ((Math.random() * 10) + 1);
This opens with a { and closes with a ) (hint, count your opening and closing brackets, braces and perenthesis to make sure they match).
Also, I think you want your num1 to equal Math.random()%10, the modulus operator. I'm not sure the exact syntax Java uses though.

Quote:
Originally Posted by cribbageman2500 View Post
java Syntax (Toggle Plain Text)
  1. while (gen<dice)
  2. {if (num1>=again)
  3. {dice ++;
  4. tally ++;
  5. gen ++;}
  6. else
  7. if (num1>=thresh)
  8. {tally ++;
  9. gen ++;}
  10. else
  11. if (num1<thresh)
  12. {gen ++;}
  13. jTextArea1.setText(String.valueOf(tally));
  14. }
  15. }
  16.  
  17. }
I don't even know where to start on this. You should look into using arrays for one. An array would let you store each roll (which I'm sure you're going to want to do) or each successful roll, or whatever. An array is perfect for this.

Also, you can clean your code up a bit by removing the redundant statements. Only increment a variable in one conditional statement, not in every conditional statement. You'll find it helps with troubleshooting to limit where a variable is modified.
Jabo is offline   Reply With Quote
Old May 10th, 2008, 3:45 PM   #3
cribbageman2500
Newbie
 
Join Date: Apr 2008
Posts: 8
Rep Power: 0 cribbageman2500 is on a distinguished road
Re: Ten sided die simulator.

from what I've seen though an array has to be a fixed length, thereby it cannot be made flexible enough to accept any # of "rolls", as defined by a variable.
cribbageman2500 is offline   Reply With Quote
Old May 10th, 2008, 3:50 PM   #4
Freaky Chris
Hobbyist Programmer
 
Freaky Chris's Avatar
 
Join Date: Dec 2007
Location: England
Posts: 169
Rep Power: 1 Freaky Chris is on a distinguished road
Send a message via MSN to Freaky Chris
Re: Ten sided die simulator.

If thats the case then Check out ArrayLists and or Vectors, these are used as arrays but are classes, best of all you do not have to specify a size for them. You can add and delete entries as you please.

Chris
__________________
Who said i couldn't program
sarcasm = raw_input('Type in a sarcastic remark: ')
print sarcasm
Freaky Chris is offline   Reply With Quote
Old May 12th, 2008, 12:01 AM   #5
Fall Back Son
Hobbyist Programmer
 
Join Date: Oct 2006
Posts: 188
Rep Power: 2 Fall Back Son is on a distinguished road
Re: Ten sided die simulator.

Use ArrayList. Their use is simple. At the top, import java.util.*;

and an ArrayList works like an array, except that it has methods to help you. Google Java Sun ArrayList and read their method descriptions. For example, the add method will allow you to continue adding to a list in order to make it larger dynamically.
Fall Back Son is offline   Reply With Quote
Old May 13th, 2008, 3:57 PM   #6
cribbageman2500
Newbie
 
Join Date: Apr 2008
Posts: 8
Rep Power: 0 cribbageman2500 is on a distinguished road
Re: Ten sided die simulator.

Awesome sauce, I'll look into it more after finals week is over, and I'll get back to you guys with my progress.

Thank you all again,
Cribbageman
cribbageman2500 is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
cache simulator..PLZ HELP Natsoumi_Maya C 0 Apr 25th, 2008 3:41 AM
Network Simulator design renato Software Design and Algorithms 2 Dec 11th, 2007 10:54 PM
GPS Simulator alogorithm fisherking C++ 10 Apr 28th, 2006 3:39 AM
Starship Combat Simulator ChiHappens Existing Project Development 7 Feb 14th, 2006 4:59 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 5:27 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC