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

2019/2/26

Help with arrays

Zestix Zestix

2019/2/26

#
Hey, Right now I'm trying to implement arrays into my code. The idea of the program is that there is a dice, which gives out random Numbers from 0-5. If you roll a number you get a point, if you've already rolled that number before though you're going to lose. How do I save which number has already been rolled and how do I check that to make the game stop?
danpost danpost

2019/2/26

#
Zestix wrote...
Hey, Right now I'm trying to implement arrays into my code. The idea of the program is that there is a dice, which gives out random Numbers from 0-5. If you roll a number you get a point, if you've already rolled that number before though you're going to lose. How do I save which number has already been rolled and how do I check that to make the game stop?
If you were to use an array, it could be a boolean (true/false) array where the index of the element refers to the possible numbers. I guess, if you wanted to, you could save the numbers themselves, as Integer objects, and go that route. It is actually possible to use a single int instead of an array, where each power of 2 represent a number 0-5. With the boolean array, the rolled number points to the element to check for a true value; with an Integer array, you would iterate thru the array, make sure the element is not null and then compare the value to the roll; and with the int, you can AND ( & ) and compare to zero to verify not previously rolled and OR ( | ) to record a value has been rolled.
Zestix Zestix

2019/2/26

#
danpost wrote...
Zestix wrote...
Hey, Right now I'm trying to implement arrays into my code. The idea of the program is that there is a dice, which gives out random Numbers from 0-5. If you roll a number you get a point, if you've already rolled that number before though you're going to lose. How do I save which number has already been rolled and how do I check that to make the game stop?
If you were to use an array, it could be a boolean (true/false) array where the index of the element refers to the possible numbers. I guess, if you wanted to, you could save the numbers themselves, as Integer objects, and go that route. It is actually possible to use a single int instead of an array, where each power of 2 represent a number 0-5. With the boolean array, the rolled number points to the element to check for a true value; with an Integer array, you would iterate thru the array, make sure the element is not null and then compare the value to the roll; and with the int, you can AND ( & ) and compare to zero to verify not previously rolled and OR ( | ) to record a value has been rolled.
So the code should look somewhat like this if I'm correct?
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class MyWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class MyWorld extends World
{
    int dice[] = new int[6];
    boolean rolled[] = new boolean[6];
    int number[] = new int[6];
    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    public MyWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1); 
        addObject(new Go(),200,300);
    }

    public void act()
    {
        for (int i = 0;i < dice.length;i++)
        {
            dice[i] = i;
        }
        for (int i = 0;i < rolled.length;i++)
        {
            rolled[i] = false;
        }
        if (Greenfoot.mousePressed(null))
        {
            int roll = Greenfoot.getRandomNumber(6);
            showText(""+roll,300,200);

            for (int i = 0;i<dice.length;i++)
            {

                if (roll == dice[i])
                {       
                    if (rolled[i] == false)
                    {
                        number[i] = roll;
                        rolled[i] = true;
                    }
                    else if (rolled[i] == true)
                    {

                        getObjects(Go.class).get(0).setImage("ball.png");
                    }
                }

            }

        }
    }

}
danpost danpost

2019/2/26

#
Zestix wrote...
So the code should look somewhat like this if I'm correct? << Code Omitted >>
Really? Try again -- this time, without lines 11 and 13.
Zestix Zestix

2019/2/26

#
danpost wrote...
Zestix wrote...
So the code should look somewhat like this if I'm correct? << Code Omitted >>
Really? Try again -- this time, without lines 11 and 13.
Hmm..That doesn't seem to work either
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class MyWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class MyWorld extends World
{
    
    boolean rolled[] = new boolean[6];
    
    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    public MyWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1); 
        addObject(new Go(),200,300);
    }

    public void act()
    {
        
        for (int i = 0;i < rolled.length;i++)
        {
            rolled[i] = false;
        }
        if (Greenfoot.mousePressed(null))
        {
            int roll = Greenfoot.getRandomNumber(6);
            showText(""+roll,300,200);

            for (int i = 0;i<rolled.length;i++)
            {

                   if (i == roll)
                   {
                    if (rolled[i] == false)
                    {
                        rolled[i] = true;
                    }
                    else if (rolled[i] == true)
                    {

                        getObjects(Go.class).get(0).setImage("ball.png");
                    }
                }

            }

        }
    }
danpost danpost

2019/2/26

#
Zestix wrote...
Hmm..That doesn't seem to work either
Do not use any for loops (it is much simpler than you think).
Zestix Zestix

2019/2/26

#
danpost wrote...
Zestix wrote...
Hmm..That doesn't seem to work either
Do not use any for loops (it is much simpler than you think).
Sorry if it's obvious but I don't understand what you mean
danpost danpost

2019/2/26

#
Zestix wrote...
Sorry if I'm a bit dull, but I dont understand what you mean
Use the value of roll for the index of the element in question:
if (rolled[roll]) << setGoImage >> else rolled[roll] = true;
Zestix Zestix

2019/2/26

#
danpost wrote...
Zestix wrote...
Sorry if I'm a bit dull, but I dont understand what you mean
Use the value of roll for the index of the element in question:
if (rolled[roll]) << setGoImage >> else rolled[roll] = true;
 if (rolled[roll] == true)
            {

                getObjects(Go.class).get(0).setImage("ball.png");
            }
            if (rolled[roll] == false)
            {
                rolled[roll] = true;
            }


        }
I'm sorry for wasting your time but that doesn't work either
danpost danpost

2019/2/26

#
Zestix wrote...
<< Code Omitted >> I'm sorry for wasting your time but that doesn't work either
Show the entire act method.
Zestix Zestix

2019/2/26

#
danpost wrote...
Zestix wrote...
<< Code Omitted >> I'm sorry for wasting your time but that doesn't work either
Show the entire act method.
I've just confirmed how stupid I am...
 public void act()
    {
        int roll = Greenfoot.getRandomNumber(6);
        for (int i = 0;i < rolled.length;i++)
        {
            rolled[i] = false;
        }
        if (Greenfoot.mousePressed(null))
        {

            showText(""+roll,300,200);


            if (rolled[roll] == true)
            {

                getObjects(Go.class).get(0).setImage("ball.png");
            }
            if (rolled[roll] == false)
            {
                rolled[roll] = true;
            }


        }

    }
Of course it won't work if I set new values for rolled everytime Thanks a lot for helping
danpost danpost

2019/2/26

#
Zestix wrote...
I've just confirmed how stupid I am... << Code Omitted >> Of course it won't work if I set new values for rolled everytime
Exactly -- remove lines 4 thru 7. Move line 3 to line 10. Change line 19 to:
else
Zestix Zestix

2019/2/26

#
danpost wrote...
Zestix wrote...
I've just confirmed how stupid I am... << Code Omitted >> Of course it won't work if I set new values for rolled everytime
Exactly -- remove lines 4 thru 7. Move line 3 to line 10. Change line 19 to:
else
Thanks!
You need to login to post a reply.