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

2015/3/26

question about placing objects!

veyydot veyydot

2015/3/26

#
I made a memory game by adding objects to the playfield like this: myMemory.addObject(new Card("1.png"),50,200); myMemory.addObject(new Card("een.png"),150,200); myMemory.addObject(new Card("2.png"),250,200); myMemory.addObject(new Card("twee.png"),350,200); myMemory.addObject(new Card("3.png"),50,350); myMemory.addObject(new Card("drie.png"),150,350); myMemory.addObject(new Card("4.png"),250,350); myMemory.addObject(new Card("vier.png"),350,350); myMemory.addObject(new Card("5.png"),50,500); myMemory.addObject(new Card("vijf.png"),150,500); myMemory.addObject(new Card("6.png"),250,500); myMemory.addObject(new Card("zes.png"),350,500); myMemory.addObject(new Card("7.png"),450,200); myMemory.addObject(new Card("zeven.png"),550,200); myMemory.addObject(new Card("8.png"),450,350); myMemory.addObject(new Card("acht.png"),550,350); myMemory.addObject(new Card("9.png"),450,500); myMemory.addObject(new Card("negen.png"),550,500); myMemory.addObject(new Card("10.png"),650,200); myMemory.addObject(new Card("tien.png"),750,200); myMemory.addObject(new Card("11.png"),650,350); myMemory.addObject(new Card("elf.png"),750,350); myMemory.addObject(new Card("12.png"),650,500); myMemory.addObject(new Card("twaalf.png"),750,500); But how do i make the cards shuffle , and not make the pairs come direct next to each other
danpost danpost

2015/3/26

#
I would be easier to shuffle the cards before placing them. Also, you could use 'for' loops to place the cards. Finally, having pairs not come directly next to each other would be essentially creating a shuffle that is not normal (in a normal shuffle, all cards have the same chance of ending up next to any other card). However, with what you already have, you would have to use getObjects(Card.class) to get a list of all the cards in the world. You would then repeatedly swap the location of two randomly chosen cards from the list (it would not matter if you selected the same card both times on the same swap occasionally). Once the swapping is done a selected number of times, you would then have to compare all cards adjacent to each other to ensure that they are not a matching pair. If any matches pairs are adjacent to each other, swap one with another random card and start a fresh check for matching adjacent pairs. Continue until no matching adjacent pairs are found. I will tell you now, that this is somewhat complicated, logically, and probably will require programming techniques you may not yet be familiar with.
Super_Hippo Super_Hippo

2015/3/26

#
I think his only problem is that ALL pairs are next to each other (because he places them like that). If all cards would be placed randomly and some pairs are next to each other, this wouldn't be a problem.
You need to login to post a reply.