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

2018/1/3

Quick question with random numbers

ThatOneGuy125 ThatOneGuy125

2018/1/3

#
I'm getting back into programming in greenfoot after about a year long hiatus and I've realized I've forgotten all the simple stuff I used to know. If somebody could help me out and jog my memory when it comes to creating a random number. I'm making a little bop it style game just for fun and I'm using a random number to determine which command the player will have to do. For example, I've got three buttons and each option is assigned a number (0, 1, or 2) and I just need the random number generator to choose a random number between 0 and 2 everytime the appropriate button is clicked and then choose another number and so on and so forth.
1
2
3
4
5
6
7
8
9
10
getRandomNumber(2);
            if(getRandomNumber == 0) {
                addObject(new OpYellow(), 280, 65);
            }
            if(getRandomNumber == 1) {
                addObject(new OpRed(), 280, 65);
            }
            if(getRandomNumber == 2) {
                addObject(new OpBlue(), 280, 65);
            }
This is what I've attempted so far but obviously this isn't working. This is inside an if statement inside the act method. Thanks for the help in advance.
Super_Hippo Super_Hippo

2018/1/3

#
The 'getRandomNumber' method is not a method of World, it is a method of the Greenfoot class, so you have to use 'Greenfoot.getRandomNumber'.
xbLank xbLank

2018/1/4

#
Keep in mind that the Value will never be x if you call "Greenfoot.getRandomNumber(x)". I suggest that you use this method instead:
1
2
3
4
private int getRandomNumber(int start, int range)
{
        return start + (int)Greenfoot.getRandomNumber(range-start+1);
}
This will return a value between (int)start and (int)range (returned value can be start and range). So what you want to do is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public void foo()
{
    switch(getRandomNumber(0,2))
    {
        default:
            break;
        case 0:
            addObject(new OpYellow(), 280, 65);
            break;
        case 1:
            addObject(new OpRed(), 280, 65)
            break;
        case 2:
            addObject(new OpBlue(), 280, 65);
            break;
    }
}
private int getRandomNumber(int start, int range)
{
        return start + (int)Greenfoot.getRandomNumber(range-start+1);
}
ThatOneGuy125 ThatOneGuy125

2018/1/4

#
Ok so I know I didn't really do what you said at all and to be quite honest it's because I wasn't too sure how your code you gave me works. But I did sorta figure things out but I ran into a somewhat related issue. I managed to get the game to choose a random button objective and display it. I'm also able to make it so that whenever the appropriate button is pressed, the timer resets back to zero. However I don't know how to change the objective now. The random number is only ever chosen once. Which I know is an issue. However I can't figure out how or where to put the code so that it chooses a new button objective every time I press the appropriate button. Hopefully I explained it well enough. This is what I've got so far. I tried to only show the relevant parts.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
public class MyWorld extends World {
    //Buttons being clicked booleans
    public static boolean BlueButtonClicked = false;
    public static boolean RedButtonClicked = false;
    public static boolean YellowButtonClicked = false;
 
    //move timer
    public static int countdownTimer;
 
    //Declaring actors
    Red redButton = new Red();
    Yellow yellowButton = new Yellow();
    Blue blueButton = new Blue();
     
    //Choosing a random button
    int x = Greenfoot.getRandomNumber(3);
 
    public void act() {
        //when the start button is pressed, set up the game and start. startPressed is rendered true in the buttons own seperate actor code.
        if(startPressed == true) {
 
            //game now in progress
            inProgress = true;
 
            //start button is now irrelevant
            startPressed = false;
        }
        //during game code
        if(inProgress == true) {
            //timer
            countdownTimer++;
 
            //deciding which button the player needs to press
            if(x == 0) {
                //putting in the objective
                addObject(new OpYellow(), 200, 50);
                if(YellowButtonClicked == true) {
                    //resetting the timer
                    countdownTimer = 0;
 
                    //resetting the pressed button
                    YellowButtonClicked = false;
                }
            }
            if(x == 1) {
                //putting in the objective
                addObject(new OpRed(), 200, 50);
                if(RedButtonClicked == true) {
                    //resetting the timer
                    countdownTimer = 0;
 
                    //resetting the pressed button
                    RedButtonClicked = false;
                }
            }
            if(x == 2) {
                //putting in the objective
                addObject(new OpBlue(), 200, 50);
                if(BlueButtonClicked == true) {
                    //resetting the timer
                    countdownTimer = 0;
 
                    //resetting the pressed button
                    BlueButtonClicked = false;
                }
            }
        }
        //time's up
        if(countdownTimer == 120) {
            //removing main buttons
            removeObject(redButton);
            removeObject(yellowButton);
            removeObject(blueButton);
 
            //ending game and stopping timer
            inProgress = false;
            countdownTimer = 0;
        }
    }
}
Once again, that's really only the relevant parts. I tried to avoid the lines of codes that weren't necessary to explaining what I was doing. Please send help lmao. I've been stuck on this for the past few hours. And I know once I figure this out I'll feel stupid for not noticing earlier.
ThatOneGuy125 ThatOneGuy125

2018/1/4

#
Oh and sorry if my code is a bit messy. I have a particular style of coding that I tend to stick to.
danpost danpost

2018/1/4

#
ThatOneGuy125 wrote...
Oh and sorry if my code is a bit messy. I have a particular style of coding that I tend to stick to.
Actually, the code style is just fine. However, it is not good that you left out a lot of stuff that could be important. There are quite a few fields used in the code whose declaration lines are not shown and the constructor of the class is important as well. There might be some things that could be improved on as far as how you code some stuff -- particularly questionable are the use of static fields and the placement of the action codes of the buttons.
Super_Hippo Super_Hippo

2018/1/4

#
Whenever you want to change the objective, you can use
1
x = Greenfoot.getRandomNumber(3);
again. Also, if you want to remove the buttons from the world in lines 71-73, then you might need to add those into the world in lines 36, 47 and 58 instead of creating new ones. (Ignore this if you want those to be different and you add them somewhere else.)
xbLank xbLank

2018/1/4

#
Why don't you use a switch()? It kinda looks like a mess like this. I mean I provided you with a copy & paste Version and I explained what my method does. I dont really know what you do not understand. Please ask me a proper question.
Super_Hippo Super_Hippo

2018/1/4

#
It would probably be sufficient to have one button class for all three buttons. This would simplify it even more.
danpost danpost

2018/1/4

#
일리아스 wrote...
Why don't you use a switch()? It kinda looks like a mess like this
Might be easier to use an array for the buttons.
xbLank xbLank

2018/1/4

#
I agree.
You need to login to post a reply.