mouseofgory wrote ...

2017/11/8

## Can anyone explain this code to me?

mouseofgory

2017/11/8

Yehuda

2017/11/8

For the first Rock you have:
```Rock myRock = new Rock();
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

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

2017/11/9

Is there a way I don't use the "x" number so the rocks show up completely random?
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

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++)
{
}```
mouseofgory

2017/11/9

Thanks!!
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

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++)
{