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

2017/6/8

Random Moving // Get Random number

FeVo FeVo

2017/6/8

#
Hey, I want it to get a random number between and including 1 and 6. If its 1 or 2 it should do my method "move()" if its 3 or 4 it should do my methods "turnLeft()" and "move()" and if its 5 or 6 it should do my methods "turnRight()" and "move", so my object/class is always random moving. Is my thought right and how is the code for that? Thanks a lot in front!
Super_Hippo Super_Hippo

2017/6/8

#
switch (Greenfoot.getRandomNumber(3))
{
    case 0: move(); break;
    case 1: turnLeft(); move(); break;
    case 2: turnRight(); move(); break;
}
Or just:
turn((Greenfoot.getRandomNumber(3)-1)*90);
move();
FeVo FeVo

2017/6/8

#
Super_Hippo wrote...
switch (Greenfoot.getRandomNumber(3))
{
    case 0: move(); break;
    case 1: turnLeft(); move(); break;
    case 2: turnRight(); move(); break;
}
Or just:
turn((Greenfoot.getRandomNumber(3)-1)*90);
move();
Thanks, works perfect
You need to login to post a reply.