Jump to content

Help with program?

- - - - -

  • Please log in to reply
13 replies to this topic

#1
Misaki

Misaki

    Newbie

  • Members
  • Pip
  • 7 posts
I'm making a text-based game based on chance for school and need some help with how to code some parts of it. Basically, you start the game with $3000 and each week you lose $500 for supplies. Each week you can choose a site with probabilities of certain events happening. Example: Site #1 has 10% of gaining $1000, 30% of gaining $500, and 60% of losing half your money to a thief. The game ends when you lose all your money or your life. So the questions:

1) I need to use arrays somewhere in the program but I'm not sure where I can use arrays?
2) What is the most efficient way of generating the % at each site? I don't really want to generate #'s between 1 and 10 and then multiplying it by 10 and put it in a loop until you get right % for each site because I don't want to put all the extra if statements if its not one of the %'s for the site. And what if one site has two different 40% chance of something happening, how would I generate that?
Thanks in advance for your help and time.

#2
cdg10620

cdg10620

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 389 posts
What code do you currently have written for this assignment? If you have some, post and i'm sure we'll be more than happy to comment and give you pointers.
-CDG10620
Software Developer

#3
Misaki

Misaki

    Newbie

  • Members
  • Pip
  • 7 posts
I haven't started the coding yet. I just want to plan out the algorithm first? I want to use an input box to get which site from user. Then generate the % and use println to display the result. I just want to know how I can use arrays with this program and how to generate the % efficiently.

#4
gregwarner

gregwarner

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 853 posts
  • Location:Arkansas
Begin by drawing a flowchart for the application logic. You can worry about details such as how to calculate percentage later, as this will likely be a method function. Just draw a box labeled "Calculate %" in your flowchart to represent that abstract task, then come implementation time, you can fill in the body of that function with your algorithm.

Since you're still in the design phase of the project, now is when you'll be deciding how you want to represent your data. You said you want to use arrays, but you don't know how. Do you have some idea as to why you're supposed to use an array? (Teacher just said you have to use them, or you think you have an application in mind for them?)
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.

– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid


#5
Misaki

Misaki

    Newbie

  • Members
  • Pip
  • 7 posts
They're supposed to be implemented somewhere in the program (teacher said). I don't really have an application in mind hence I'm asking where I can use arrays in this game.

#6
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP
Possibly an array< [], ArrayList<>> of Sites?

class Site {

    //information about site...

}

// ... somewhere in main

main {

    Site[] sites = new Site[4];

    //or

    ArrayList<Site> sites = new ArrayList<Site>();

}



#7
gregwarner

gregwarner

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 853 posts
  • Location:Arkansas
An array is usually used for storing a limited number of instances of the same thing, accessible by index, or numerical position in the array. What data types do you have in your application, and are any of them representative of a sequence of similar objects?
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.

– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid


#8
Misaki

Misaki

    Newbie

  • Members
  • Pip
  • 7 posts
@lethalwire Okay, so what can I do with the array of sites? Like what can I use it for then? I can't just declare the array and not use it at all, right?

@gregwarner String for when the user types in the site they want and double for the percentages.

#9
gregwarner

gregwarner

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 853 posts
  • Location:Arkansas

Misaki said:

2) What is the most efficient way of generating the % at each site? I don't really want to generate #'s between 1 and 10 and then multiplying it by 10 and put it in a loop until you get right % for each site because I don't want to put all the extra if statements if its not one of the %'s for the site.

Suppose you store the chance percentages for each of the sites as an integer array of length n, where n is how many sites you have. The chance percentages would be represented by an integer between 1 and 100, inclusive. Each site would have a designated number, or index, in the array. Calculating the chance event is as simple as generating a random integer between 0 and 99, inclusive, and comparing to see if this random number is less than the one in the specified position in the array.

Say, for instance, the user picks site #3. Your program would first do the bounds checking to make sure that doesn't exceed the length of the array (and not less than 1 either), then you subtract 1, because array indices start from 0 rather than 1 (but your user will most likely be picking numbers from 1 to n rather than 0 to n-1). Now you have the array index of the site they picked, so all you have to do is look to see if your random int is less than array[2].

Does that make sense? You can have another array to store the rewards if the chance succeeds. The indices of the various sites would need to correlate between these two arrays, naturally.

That's just one suggestion of how to do it.
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.

– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid


#10
Misaki

Misaki

    Newbie

  • Members
  • Pip
  • 7 posts
I'm not sure what you mean by, "...so all you have to do is look to see if your random int is less than array[2]." Okay, so I get you have the index of the site but why are you checking to see if the random int is less than array[2]? :confused: So you're saying to make array like this?
int []Sites = new int [4];
Site[0]=1; -> So that represents index of site? Or are you saying to make array for %'s? Where the 1 would represent 10% of gaining $1000?
And then something like this? randomInt = (int)((99-0+1) * Math.random() + 0);

Also, say there is 5 sites. The user picks the 3rd site. Then you subtract 1 to get the index. And the random int is 6 which represents 40% at gaining $300. Would I have to do an if statement for each of the int? Like,

if (randomInt == 6)
{
totalMoney = currentMoney + 300;
}
else if (randomInt == n) ...etc.

Edited by Misaki, 07 January 2012 - 10:40 AM.


#11
gregwarner

gregwarner

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 853 posts
  • Location:Arkansas

Misaki said:

I'm not sure what you mean by, "...so all you have to do is look to see if your random int is less than array[2]." Okay, so I get you have the index of the site but why are you checking to see if the random int is less than array[2]?

That was just an example if the user picks site #3. (Indices are represented by x-1, where x is the natural number position.) You wouldn't want to ask your user to select a site from 0 to n-1, you'd rather want to present a much more user friendly list of 1 to n and do the math yourself.

Incidentally, how much instruction have you had on arrays thus far?

Quote

Site[0]=1; -> So that represents index of site? Or are you saying to make array for %'s? Where the 1 would represent 10% of gaining $1000?

If you did that, it would mean site #0 (site 1 in your menu list) would have a chance of 1% of succeeding (if you chose to generate random numbers on the scale of 0 to 99 as in my previous example.)

Quote

And then something like this? randomInt = (int)((99-0+1) * Math.random() + 0);

No, this is all wrong. I'll put it in a more general case. Assume your chances array is called 'chances'. Suppose you store your user's selection in x. After you've checked to see that x is positive and less than or equal to n, where n is the length of the array, you can calculate a success or a failure like so:


boolean success;

if ( (int) (Math.random() * 100) < chances[x - 1] ) {

    success = true;

} else {

    success = false;

}


That will get you what you need. You don't have to store the result in success if you'd rather just place your code directly in the appropriate bodies there.

Does that make everything clear?
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.

– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid


#12
Misaki

Misaki

    Newbie

  • Members
  • Pip
  • 7 posts
Yes, now I understand what you mean. Thank you for your help and time, it was greatly appreciated. :) If I have any more questions, may I send you a pm for help?




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users