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

2015/11/12

Is it possible to get 6 different random numbers?

decadence18 decadence18

2015/11/12

#
I couldn't think of a better way to phrase the title, so hopefully the description will work. I'm creating a die/counter class, that will roll on a certain key combination. After 1000 act cycles, however, I want it to cycle through each of the 6 images, before pausing on the "?" image. I want this animation to be random, however, and to do this I will need random numbers 1 - 6, with none of the values repeated (6P6 if you will). Is there a way in Greenfoot to do this?
danpost danpost

2015/11/12

#
decadence18 wrote...
I'm creating a die/counter class, that will roll on a certain key combination.
Show the code for the Die class (to start with).
decadence18 decadence18

2015/11/12

#
public class Counter extends MovableActors
{
    private int timerNumber;
    private boolean canExist;
        
    public Counter()
    {
        GreenfootImage image;
        canExist = true;
        
        image = new GreenfootImage("Counter Standby.png");
        image.scale(Utility.SCALE_SIZE, Utility.SCALE_SIZE);
        setImage(image);
    }
    
    public void act() 
    {
        timerNumber += 1;
        dragAndDrop();
        checkExistence();
        if(Greenfoot.mouseClicked(this))
        {
            MouseInfo mouse = Greenfoot.getMouseInfo();
            if ((mouse.getClickCount() == 2) && (Greenfoot.isKeyDown("shift")))
            {
                canExist = false;
            }
        }
        
        if(Greenfoot.isKeyDown("control") && Greenfoot.isKeyDown("space"))
        {
            changeCounter();
            timerNumber = 0;
        }
        
        if(timerNumber >= 1000)
        {
            setAsStandby();
        }
    }

    private void changeCounter()
    {
        int iRandomNumber;
        GreenfootImage image;
        iRandomNumber = Greenfoot.getRandomNumber(5) + 1;
        image = new GreenfootImage("Counter " + iRandomNumber + ".png");
        image.scale(Utility.SCALE_SIZE, Utility.SCALE_SIZE);
        setImage(image);
    }

    private void checkExistence()
    {
        World world = getWorld();
        if(!canExist)
        {
            world.removeObject(this);
        }
    }
    
    private void setAsStandby()
    {
        GreenfootImage image;
        image = new GreenfootImage("Counter Standby.png");
        image.scale(Utility.SCALE_SIZE, Utility.SCALE_SIZE);
        setImage(image);
    }
}
Utility.SCALE_SIZE is in a class of variables corresponding to different values that multiple classes need to access (public static final...) The method I'm hoping to add an animation-type sequence to is the "setAsStandby" method. I want it to randomly switch between the images I have (identified by Counter 1.png, Counter 2.png, Counter 3.png, Counter 4.png, Counter 5.png, and Counter 6.png for simplicity sake) and then, after all 6 images have been cycled through, I'm going to end on the Counter Standby.png image (I have it now as solely the standby, so I can continue development elsewhere). The code I have for adding the counter is in the world and is as follows:
public void act()
{
    String strPress = Greenfoot.getKey();
    if("c".equals(strPress))
        {
            addObject(new Counter(), 100, 100);
        }
}
danpost danpost

2015/11/12

#
To start with, I think it would help if you organize the images the Counter object can take on. These images can also be created during compilation instead of during Counter object creation and during the changing of the value of a Counter object. For example, you could do this:
// creates class array for all images used by Counter objects during compilation
private static final GreenfootImage[] images =
{
    new GreenfootImage("Counter Standby.png"),
    new GreenfootImage("Counter 1.png"),
    new GreenfootImage("Counter 2.png"),
    new GreenfootImage("Counter 3.png"),
    new GreenfootImage("Counter 4.png"),
    new GreenfootImage("Counter 5.png"),
    new GreenfootImage("Counter 6.png")
};

// scales all images in 'images' array during compilation
static
{
    for (int n=0; n<7; n++) images[n].scale(Utility.SCALE_SIZE, Utility.SCALE_SIZE);
}
If you set any Counter object created with an image from the array in the constructor of the class, then its image will always be one in the list; the image of one Counter object can then be compared to that of another for equality.
// the constructor
public Counter()
{
    canExist = true;
    setImage(images[0]);
}
I believe, in general, that your programming is too "loose". I mean, it appears that there is a shortcoming of placing conditions (or enough conditions) on some actions. For example:
Your world act method allows the user to use the "c" key to create a new Counter object. If the user repeatedly presses the key, multiple objects will be created over top of each other. I do not think you would want that.
I also wonder about the removal, by clicking, of Counter objects and the changing of its value. I would think that there would be some restrictions as to when the user would be allowed to perform these actions.
decadence18 decadence18

2015/11/14

#
Danpost, these objects can be moved when the world is running by dragging them; if they add 30 some "dice"/counters, then that's their problem. As far as the removal of the objects, the counter is merely an on-computer replacement for a die. This is a re-program of the game I programmed last year. They need to have a dynamic amount of counters that they can "roll", or get a random number for, at will (in the original game, they never had more than 4, but since I expanded on the original game mechanics they can theoretically need up to 30 for a large-scale game). These counters will need to be removed and, if left on the board and alone for a long period of time, switched to a non-numeric value so that the user cannot claim that they "already made their roll". I didn't consider the array of images; I'll possibly use that to make the code more maintainable. Also, I apologize for the prolonged time in my reply. I had classes all day today, one of which ironically was programming in Java. Finally, would it be better to just set the image to the Standby image after a certain period of time (e.g. 1000 ticks), or is there a way to have a randomized sequence of images produced each time it goes into the standby mode?
Super_Hippo Super_Hippo

2015/11/14

#
I use a similar code in one of my games to shuffle an array. I think that there is even a method which does that. 'order' is the array which will hold the random numbers then. You will probably put 'private int order;' outside methods and remove the 'int' from the first line.
int[] order = new int[6];
final int[] helper = {1,2,3,4,5,6};
for (int i=0; i<6; i++)
{
    while (true)
    {
        final int r = Greenfoot.getRandomNumber(6);
        if (order[r] != 0) continue;
        order[r] = helper[i];
        break;
    }
}
danpost danpost

2015/11/14

#
decadence18 wrote...
would it be better to just set the image to the Standby image after a certain period of time (e.g. 1000 ticks), or is there a way to have a randomized sequence of images produced each time it goes into the standby mode?
I really do not have an answer to this; but, I did not want to leave you waiting for a response, either. I believe that how it should be done is whatever way you believe it should be done.
decadence18 decadence18

2015/11/15

#
Alright, thank you @danpost and @Super_Hippo for your help. I think I'll implement something similar to the array of images; as far as the animation goes, I think I'll just have it cycle through each numeric image for 10 ticks and then end on the Standby image.
You need to login to post a reply.