This site requires JavaScript, please enable it in your browser!
Greenfoot back
Back to discussions
mouseofgory
mouseofgory wrote ...

2017/11/8

Can anyone explain this code to me?

mouseofgory mouseofgory

2017/11/8

#
Hey guys I am very new to coding. I would like to have rocks appear at random locations each time I reset the game, and I am also confused with the code I am writing. I was just messing around with it till it worked but I'm not exactly sure what I did. For example (myRock9, 9,Greenfoot.getRandomNumber(11)) so obviously its rock9, but then I put another random number (9) and I don't know what its supposed to be but without it, the code doesn't work. And then the 11 is I guess the number that I am pulling the random number from? so it means I'm getting a random number out of 10? And is there a faster way to maybe do this? public WombatWorld() { super(10, 10, 60); setBackground("cell.jpg"); setPaintOrder(Wombat.class, Leaf.class); Wombat myWombat = new Wombat(); addObject(myWombat, 2,2); Rock myRock = new Rock(); addObject(myRock, 1,Greenfoot.getRandomNumber(11)); Rock myRock2 = new Rock(); addObject(myRock2, 2,Greenfoot.getRandomNumber(11)); Rock myRock3 = new Rock(); addObject(myRock3, 3,Greenfoot.getRandomNumber(11)); Rock myRock4 = new Rock(); addObject(myRock4, 4,Greenfoot.getRandomNumber(11)); Rock myRock5 = new Rock(); addObject(myRock5, 5,Greenfoot.getRandomNumber(11)); Rock myRock6 = new Rock(); addObject(myRock6, 6,Greenfoot.getRandomNumber(11)); Rock myRock7 = new Rock(); addObject(myRock7, 7,Greenfoot.getRandomNumber(11)); Rock myRock8 = new Rock(); addObject(myRock8, 8,Greenfoot.getRandomNumber(11)); Rock myRock9 = new Rock(); addObject(myRock9, 9,Greenfoot.getRandomNumber(11)); Rock myRock10 = new Rock(); addObject(myRock10, 10,Greenfoot.getRandomNumber(11)); }
Yehuda Yehuda

2017/11/8

#
For the first Rock you have:
Rock myRock = new Rock();
addObject(myRock, 1, Greenfoot.getRandomNumber(11));
This code will place 'myRock', an instance of the Rock class, at (1,Greenfoot.getRandomNumber(11)). The '1' means it's one cell to the right of the left border. The "Greenfoot.getRandomNumber(11)" means it's x cells from the top border, with x being equal to one integer from 0-10. The x doesn't really exist here, I just used it for explanation purposes.
danpost danpost

2017/11/9

#
In addition to what Yehuda posted, your world is ten cells by ten cells in size whose coordinate values go from 0 to 9. Therefore, you should be using 'Greenfoot.getRandomNumber(10)' for even randomness across a dimension of the world. Also, if you are wanting one Rock at each level across in your world, the first number used should also be in the range zero through nine.
mouseofgory mouseofgory

2017/11/9

#
Is there a way I don't use the "x" number so the rocks show up completely random?
Yehuda Yehuda

2017/11/9

#
mouseofgory wrote...
Is there a way I don't use the "x" number so the rocks show up completely random?
First:
Yehuda wrote...
The x doesn't really exist here, I just used it for explanation purposes.
Second: I was using x to explain the random number. The part that isn't random is the x coordinate. (I guess the use of the variable x could have gotten mixed up with the x coordinate.) To make the x and y coordinates random you just put the getRandomNumber in the first coordinate also (instead of the constant number).
danpost danpost

2017/11/9

#
You could simplify your code using a for loop. Replace everything from the first line with 'Rock' in it to the end with:
for (int i=0; i<10; i++)
{
    addObject(new Rock(), i, Greenfoot.getRandomNumber(10));
}
mouseofgory mouseofgory

2017/11/9

#
Thanks!!
Yehuda Yehuda

2017/11/12

#
That code isn't any more random than what he originally had. I do prefer it from the longer version, but he asked for "completely random" as opposed to what he had.
danpost danpost

2017/11/12

#
Yehuda wrote...
That code isn't any more random than what he originally had. I do prefer it from the longer version, but he asked for "completely random" as opposed to what he had.
mouseofgory wrote...
is there a faster way to maybe do this?
I was only showing a faster way here:
danpost wrote...
You could simplify your code using a for loop. Replace everything from the first line with 'Rock' in it to the end with:
for (int i=0; i<10; i++)
{
    addObject(new Rock(), i, Greenfoot.getRandomNumber(10));
}
It was meant to do exactly what was given to begin with. However, I did make one minor adjustment in that each is spawned in a different row,, where the given code left the top row empty and had two spawned in the bottom row. It seemed that mouseofgory was not understanding how indexing is done in java, which I had previously tried to explain with this:
danpost wrote...
your world is ten cells by ten cells in size whose coordinate values go from 0 to 9. Therefore, you should be using 'Greenfoot.getRandomNumber(10)' for even randomness across a dimension of the world. Also, if you are wanting one Rock at each level across in your world, the first number used should also be in the range zero through nine.
Yehuda Yehuda

2017/11/15

#
Oh.
You need to login to post a reply.