This site requires JavaScript, please enable it in your browser!
Greenfoot back
Miguel.Valdez
Miguel.Valdez wrote ...

2018/3/22

making a object randomly choose to either turn up, down, left, or right (180, -90, 0, 90)

Miguel.Valdez Miguel.Valdez

2018/3/22

#
this is my code so far but is set for it to decide to get a random direction from a whole 360degree angle. But our professor just changed his mind, in specifically wanting it to either turn up, down, left, or right, randomly. Thanks in advance for HELP.
1
2
3
4
5
6
7
8
9
10
11
12
/**
     * keep count of how many times "act()" has been called.
     * If "act()" has been called 5 times, object with turn a random direction from 0 to 360 degrees.
     */
    public void called()
    {
        if (actCounter == 5)
        {
            turn(Greenfoot.getRandomNumber(360)); //turns, by getting a random number (in degrees) from 0 to 360.
            actCounter = 0;
        }
    }
danpost danpost

2018/3/22

#
What can you say about the values in (180, -90, 0, 90) -- or equivalently (0, 90, 180, 270)? How many choices are there? How can you make use of these values to make an appropriate argument in the turn method call on line 9?
Miguel.Valdez Miguel.Valdez

2018/3/22

#
yes i meant "(0, 90, 180, 270)". Sorry. how can I can i make my object choose a random variable between the choices of (0, 90, 180, 270) which would be up, down, left, or right. (without using key bindings)
Miguel.Valdez Miguel.Valdez

2018/3/22

#
im reading over the greenfoot book chapter 2, but its not really describing anything. Just simply how to make it choose a random number Ex: 0 to 99
danpost danpost

2018/3/22

#
Again -- how many choices do you have in (0, 90, 180, 270)? That is the random part:
1
Greenfoot.getRandomNumber(4)
It will return a value in the set (0, 1, 2, 3). Anything coming to mind now?
Miguel.Valdez Miguel.Valdez

2018/3/22

#
danpost im not tracking.... I understand that
1
Greenfoot.getRandomNumber(4)
will choose between the numbers 0, 1, 2, 3 (because 0 is still a number). Just like you explained that
1
turn(135+Greenfoot.getRandomNumber(90));
will now choose a number between 135 and 244 EX: 135, 136, 137, 138, 139,........... 243, 244. But what im trying to do is just choose between it turning randomly up, down, left or right. Like North, East, South, West.... No NorthEast or SouthWest, etc.
danpost danpost

2018/3/22

#
Don't add -- multiply to get the appropriate angles in degrees. Also, you only have 4 choices -- not 90.
Miguel.Valdez Miguel.Valdez

2018/3/22

#
I DID IT!!!!! well atleast it works!!!!
1
2
3
4
5
6
7
8
public void called()
{
    if (actCounter == 5)
    {
        turn(Greenfoot.getRandomNumber(2)*90+90);
        actCounter = 0;
    }
}
Miguel.Valdez Miguel.Valdez

2018/3/22

#
and to be clear
1
turn(Greenfoot.getRandomNumber(360));
doesnt mean it will choose a degrees between 0 - 359?
danpost danpost

2018/3/22

#
Miguel.Valdez wrote...
I DID IT!!!!! well atleast it works!!!! << Code Omitted >>
You will only get 2 out the 4 directions with that code -- South or West becuase you only allowed two choices, (0, 1). Adding the extra 90 will not be necessary once you allow all 4 directions.
danpost danpost

2018/3/22

#
Miguel.Valdez wrote...
and to be clear
1
turn(Greenfoot.getRandomNumber(360));
doesnt mean it will choose a degrees between 0 - 359?
Yes -- it will choose a value between 0 and 359 to be used as the number of degrees to turn by.
Miguel.Valdez Miguel.Valdez

2018/3/22

#
so now more along the lines of
1
turn(Greenfoot.getRandomNumber(4)*90);
? everything you are telling me is new information I am learning. Our professor hasn't reviewed this with us but expects us to be able to code it. so sorry if some questions when asking how to do something comes out kind of dumbish, just this is all new to me.
Miguel.Valdez Miguel.Valdez

2018/3/22

#
1
turn(Greenfoot.getRandomNumber(4)*90);
does this mean, 4 choices with 90degrees each one when chosen? I just want to be able to know what I am doing for future reference & as well help my classmates.
danpost danpost

2018/3/22

#
Miguel.Valdez wrote...
so now more along the lines of
1
turn(Greenfoot.getRandomNumber(4)*90);
Perfect. The turned amount will always be either 0, 90, 180 or 270 depending on the random choice made.
Miguel.Valdez Miguel.Valdez

2018/3/22

#
awesome THANK YOU!!!!! I have learned A LOT between yesterday and today in how to make simple things functions..... all i know is things will start getting much more complicated though,
You need to login to post a reply.