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

2019/4/24

Please help with picking random set location

N0way N0way

2019/4/24

#
Hello im making a memory game and in 1st difficulty level i want to spawn 4 cards randomly but in set location 300, 100 300, 400 600, 100 600, 400 and i been thinking how can i make sure that they wont going to spawn in the same place This is my code in my word LETTERS are one types of cards that will have to match with correct PICTURES array for example LetterA with Apple i been trying to make array in to set up x and y location and make a random varible that will get random location from them in while loop i been trying to check if first card x and y location are the same as in card 2 but its saying: cannot infer type-varribles A
        if(difficulty==1)
        {
            int[] x={300, 600};
            int[] y={100, 400};
            int x1 = Greenfoot.getRandomNumber(2);
            int y1 = Greenfoot.getRandomNumber(2);
            if(card1==card1 && card1spawn == true)
            {
                addObject(LETTERS[card1], 300, 100);
                addObject(PICTURES[card1], 600, 400);
                card1spawn = false;
            }
            if(card2==card2 && card2spawn == true)
            {
                addObject(LETTERS[card2], 600, 100);
                addObject(PICTURES[card2], 300, 400);
                card2spawn = false;
            }
            while(getObjectsAt(x[x1], y[y1], LETTERS[card1]) == getObjectsAt(x[x1], y[y1], PICTURES[card1]))
            {
            
            }
        }
N0way N0way

2019/4/24

#
later on i will change settled location in lines 9,10,15 and 16 to x and y array i just had it make sure everything else is working fine
danpost danpost

2019/4/24

#
I would suggest that you take the elements in your letter and picture arrays and add them all into a list. Then you can shuffle the list and add them into the world in a normal standard loop fashion (that is, in position order, without having to worry about whether a card was placed at any location):
List<Actor> actors = new ArrayList<Actor>();
for (int i=0; i<2; i++)
{
    actors.add(LETTERS[i]);
    actors.add(PICTURES[i]);
}
Collections.shuffle(actors);
for (int i=0; i<4; i++)
{
    addObject(actors[i], 300+300*(i%2), 100+300*(i/2));
}
You will need the following imports for the above:
import java.util.List;
import java.util.ArrayList;
import java.util.Collections;
N0way N0way

2019/4/24

#
Thank you very much! does that java imports is for .add and .shuffle? and the problem is i have 26 classes of letters and 26 classes of pictures and i have to pick randomly 4 cards (2 letters and 2 corresponding pictures)
danpost danpost

2019/4/24

#
N0way wrote...
does that java imports is for .add and .shuffle?
In a way -- yes. The add method is a member of the List class and the shuffle method is a member of the Collections class. We need the ArrayList class also, because you cannot create a List object directly, being the List class is declared abstract.
the problem is i have 26 classes of letters and 26 classes of pictures and i have to pick randomly 4 cards (2 letters and 2 corresponding pictures)
I will presume you have 26 images for each class also. You can get away with only 2 classes if you rename your images with "<< type >>+<< value >>+'.png'". For example, you could have "letter0.png" thru "letter25.png" and "picture0.png" thru "picture25.png". Then you can pick a random number and create the appropriate actor from both classes.
N0way N0way

2019/4/25

#
thats what i done so its peaking random number in card1 and 2 to get from 0 up to 26 then making sure that pictures array with coresponding pictures to letter will spawn i have added them as well
  actors.add(LETTERS[card1]);
            actors.add(PICTURES[card1]);
            actors.add(LETTERS[card2]);
            actors.add(PICTURES[card2]);
            Collections.shuffle(actors);
            for (int k=0; k<4; k++)
            {
                    addObject(actors[k], 300+300*(k%2), 100+300*(k/2));
            }
but its highlighting me actors and saying array required but.java.util.List<greenfoot.Actor>found any idea how can i fix it?
danpost danpost

2019/4/25

#
Sorry. Line 8 should be:
addObject(actors.get(k), 300+300*(k%2), 100+300*(k/2));
You need to login to post a reply.