|
![]() |
Overclock.net - Overclocking.net > Software, Programming and Coding > Coding and Programming > Application Programming | |
JAVA: Randoming amount of objects added to World
|
||
![]() |
|
|
LinkBack | Thread Tools |
|
|
#1 (permalink) | |||||||||||
|
New to Overclock.net
![]() |
I need help on randomizing the number of objects added to the world.
public void addedToWorld(World world) { this.world = (AlienWorld)world; setImage(new GreenfootImage(1, 1)); aliens = new HashSet<Alien>(); for(int i = getX() ; i<=getX()+800 ; i+=50) { for(int j = getY() ; j<getY()+(ROWS*(Greenfoot.getRandomNumber(50)+1)) ; j+=50) { Alien alien = new Alien(); world.addObject(alien, i, j); aliens.add(alien); } } } Some things to know is that ROWS = 3, so basically there will be 3 rows. The first row according to this code, adds a whole row of aliens which means it is not randomizing at all. The second and third row does do what it is doing, randomizing the amount in the row and also the position. Example: O = object X = space/empty slot OOOOOOOOOO OOXOXXOOOXO XOOOOXOXXXO When I reset the scenario: OOOOOOOOOO XOXOXXOOXOO OOOXOXXOOXO When I reset the scenario only row 2 and 3 are randomizing, but the first row is not, and I have no idea how to fix it, anyone know? Thanks
__________________
I am currently very busy with moving, so if you have helped me and not get what you deserved or any replies from me in PM or on Threads, I apologize. When I have time, I will definitely reply and give you what you guys deserve. Thank you for understanding, since I also lost my summer with this sudden move. I promise that I will get back to all of you guys. OCN honor! ![]() 今学校のため、忙しくがんばってる※運命の人を探してる中だっ
folding/フォールディング:what is folding?※my folding※OCN's 24 Hour Fold-A-Thon javascript/ジャバスクリプト:profit calculator for sale/フォー・セール:coming soon project/プロジェクト:coming soon
|
|||||||||||
|
|
|
|
|
#2 (permalink) | ||||||||||||||||
|
New to Overclock.net
|
okay well i don't know if this will work but maybe what you need to do is think about the random number generator....see if the random number generator isn't setup correct or what not you will get repeats......
another tip is maybe name objects different than the objects name.....so it has better readability... http://java.sun.com/j2se/1.4.2/docs/...il/Random.html this is the java doc for random...which is the superclass for anything about random number generators and what not.... what you got to worry about is this part....which i quote directly from the documentation of random class Quote:
__________________
x2 4600+ @2.6ghz x2 6400+ @ 3.5ghz Quote:
Quote:
Quote:
|
||||||||||||||||
|
|
|
|
|
#3 (permalink) | ||||||||||||||
|
4.0 GHz
![]() |
Out of context it isn't too easy to parse your code. But I'll give it a shot.
If I'm understanding you're problem, it's that the first row is consistently being filled with objects. This is expected from how you're "for" loop is set up: Code:
for(int j = getY() ; j<getY()+(ROWS*(Greenfoot.getRandomNumber(50)+1)) ; j+=50) {...}
So this loop will always execute one iteration, so you're always going to get a first row full of objects. At this point I'm not sure if you're trying to completely randomize each spot on a column, or have a "random amount" in each (that is, each column gets either 0,1,2,3 objects filling up from the bottom). Using a for loop in the manner you have suggests the later, but your comment suggests the former (in which case just iterate through the rows and do a "coin flip" to decide if to add an object or not).
__________________
3DMark06: 19091 - 3DMark Vantage: P15264 - SuperPi: 10.968s Programming Quote of the Day: Bjarne Stroustrup: Quote:
|
||||||||||||||
|
|
|
|
|
#4 (permalink) | |||||||||||
|
New to Overclock.net
![]() |
Yeah I noticed that problem in the for loop afterward, but I have no idea how to fix this issue. Would you happen to know how I can make it randomized all over the place?
The quesiton that I was asked to do was ambiguous. It only said to make the objects have a random amount. Originally it has liek 3 rows and I believe 17 column, so that makes 51 objects. In the pattern shown above. I would have put the other classes, but there was too much to put since most were prefilled and i was only assigned to edit some parts.
__________________
I am currently very busy with moving, so if you have helped me and not get what you deserved or any replies from me in PM or on Threads, I apologize. When I have time, I will definitely reply and give you what you guys deserve. Thank you for understanding, since I also lost my summer with this sudden move. I promise that I will get back to all of you guys. OCN honor! ![]() 今学校のため、忙しくがんばってる※運命の人を探してる中だっ
folding/フォールディング:what is folding?※my folding※OCN's 24 Hour Fold-A-Thon javascript/ジャバスクリプト:profit calculator for sale/フォー・セール:coming soon project/プロジェクト:coming soon
Last edited by ninjinsamax3 : 10-16-09 at 10:37 PM |
|||||||||||
|
|
|
|
|
#5 (permalink) | |||||||||||||||
|
4.0 GHz
![]() |
Quote:
Code:
//assume the object array has been initialized to all empty.
void randomize()
{
int coinflip;
for(int i=0;i<ROWS;i++)
{
for(int j=0;j<COLUMNS;j++)
{
//here my_random will return 0 or 1
coinflip=my_random(1);
//if 1, add object
if(coinflip==1)
{
add_object(i,j);
}
}
}
__________________
3DMark06: 19091 - 3DMark Vantage: P15264 - SuperPi: 10.968s Programming Quote of the Day: Bjarne Stroustrup: Quote:
|
|||||||||||||||
|
|
|
|
|
#6 (permalink) | |||||||||||
|
New to Overclock.net
![]() |
To me it seems great, I didn't think abotu this.
My professor just want's it to have a random amount of objects, instead of all the rows having objects. I guess thsi would work. I will try this on monday when I have access to Uni computers! Thanks a lot. edit: Your idea worked great! Thanks again!
__________________
I am currently very busy with moving, so if you have helped me and not get what you deserved or any replies from me in PM or on Threads, I apologize. When I have time, I will definitely reply and give you what you guys deserve. Thank you for understanding, since I also lost my summer with this sudden move. I promise that I will get back to all of you guys. OCN honor! ![]() 今学校のため、忙しくがんばってる※運命の人を探してる中だっ
folding/フォールディング:what is folding?※my folding※OCN's 24 Hour Fold-A-Thon javascript/ジャバスクリプト:profit calculator for sale/フォー・セール:coming soon project/プロジェクト:coming soon
Last edited by ninjinsamax3 : 4 Weeks Ago at 04:22 PM |
|||||||||||
|
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
|
|