Programming Forums
User Name Password Register
 

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

Reply
 
Thread Tools Display Modes
Old Apr 3rd, 2005, 10:59 PM   #1
dayglowdave
Newbie
 
dayglowdave's Avatar
 
Join Date: Apr 2005
Location: Kansas
Posts: 22
Rep Power: 0 dayglowdave is on a distinguished road
OOP language question

I am new to programming. For a first major project I wanted to develope a dice rolling form with the following categories:

[ dicepool ](#ofdice) [ value ](d6, d12, etc.) and modifier. [ modifier ](-3,+2, etc.)

I have the GUI built as three input fields a button to roll the dice and a dynamic text field that shows the final roll after all the calculating.

My current code is as follows:

on(press){
	
	fate = Math.floor(Math.random() * (_root.sideDi * _root.numDi)) + 1;
	
	fate = fate + (1* modDi);
	
	rollRes = fate;
	
}

As you can see there is a problem with the math because I'm telling it to multiply the number of dice, by the number of sides, instead of rolling a certain kind of dice a number of times and adding up the rolls.

After some pondering I thought I'll have to write a function that loops the roll = to the number of dice designated by the user, and then some how add all those rolls up before adding it to the modifier for the final result.

That is where I'm stuck... I'm unsure of how to write the loop, for, while?? I'm lost . I'm not looking for the answer to be handed to me but if anyone has some ideas as to how I would begin breaking this down I'd love to hear them.

Dave
dayglowdave is offline   Reply With Quote
Old May 2nd, 2005, 10:34 AM   #2
mackenga
Professional Programmer
 
Join Date: Mar 2005
Location: Glasgow, Scotland
Posts: 317
Rep Power: 4 mackenga is on a distinguished road
I'm really not clear on what language you're using there, although that reference to Math.random() (and other mentions of the Math object) are making me think JavaScript. If not, then I suspect this will still be heflpul because the language you're using seems /very/ JavaScript like.

sum = 0; // set sum to zero initially
for (i = 0; i < numDice; i++) {
  // roll and add result to sum
  sum = sum + Math.floor(Math.random() * numSides) + 1;
}

This code assumes numDice is defined and set to the number of dice, and numSides is similarly defined and set to the number of sides. The one line of code within the loop is similar to your dice-rolling line, but adds its result to sum.

After this block of code, the total result rolled is in sum.

The way the for loop works is:

for (<initialisation>; <condition>; <increment>) <statement_or_block>;

where <initialisation> is an expression which is run once before the loop starts (in this case, it sets the loop counter variable i to zero), the <condition> is an expression which is evaluated before each time round the loop, terminating the loop if it's false (in this case, it checks if i is less than numSides; not less than or equal, but less, since we started with i at zero), and the <increment> is an expression to evaluate at the end of each loop cycle (in this case, i++; add one to i).

You can repeat a single statement with a for loop, or wrap up a block of statements in curly braces {}. In the example, I used curly braces for clarity even though there's only one statement; I could have left them out but it wouldn't make any real difference to efficiency and would have made the example harder to read.

A for loop is ideal for this problem because we already know (from numDice) how many times the loop will need to go round; while loops are better when you're not sure how much you've got to do, but will know when you're finished.

I'm not sure how this ties in with the GUI side of things, but that's the JS for the actual roll. Hope it helps.

Last edited by mackenga; May 2nd, 2005 at 10:38 AM.
mackenga is offline   Reply With Quote
Old Jun 26th, 2005, 12:44 PM   #3
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
McKenga's code is very good. None of the content thus far has anything to do with OOP, though. An OOP approach, just off the top of my head, would have a basic die object for which the number of sides could be defined (as well as the value for each of the sides). There would be a "roll" method for it. There would also be a diceCup object which would have die members of the appropriate quantity and type. It would have a roll (or "play" or whatever) method of its own which would accumulate the sum of the rolls of its individual dice.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Jul 2nd, 2005, 10:34 PM   #4
cypherkronis
Newbie
 
Join Date: Jun 2005
Posts: 12
Rep Power: 0 cypherkronis is on a distinguished road
are you using flash actionscript? lol, just curious. pre flash mx 2004, the oop in flash was very interesting, i remember it looked very hacked. every class declaration, was like a function. you were basically initiatin variables and methods in a function, and then u could instantiate that function. get back to me if ure curious, i think i can remember it. flash mx 2004 actionscrip,t looks JUST like java.
cypherkronis 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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 9:58 PM.

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