File "memory_game.dir" In this classical game, an even number of cards are arranged face-down on a table in a rectangular fashion. One or more player(s) take turns flipping pairs of cards. If they happen to match, it is a win for the player who turned them over. Then the cards stay open (or removed from the table). If not, they are flipped back over. We will be discussing how to create a prototype of such a game with 8 cards and a single player. The computer facilitates the game playing. To implement the memory game in Lingo, we need to come up first with the data structures that will hold the information of the cards, and with pseudocode that will capture the game's mechanics. Data StructuresWe will use a list to keep track of the cards. We have placed cards in the first several positions of the cast, and for this prototype we will use cards in positions 1-8. Note that there is also a blank card in position 51. It makes sense the initialization to happen in the preparatory frame or when we click the "New Game" button: -- assume 8 cards in sprites 1-8 totalCards = 8 -- create a list of totalCards/2 pairs of cards cardList = list() cardList = [1,1,2,2,3,3,4,4] -- they need to be shuffled To start a game we will need to shuffle the cards. Shuffling the cardsTo create the effect of a shuffle, we need to be able to create a random list of pairs of numbers between 1 and half-the-cards on the table. We can start with the non-sorted cardList and then permute their order by repeatedly removing a random one and placing it into a new pile called aftershuffle.
on shuffleCards
global cardList, totalCards
aftershuffle = [] -- another way to create an empty list
repeat while count(cardList) > 0
pick = random(count(cardList)) -- pick a random card
symbol = getAt(cardList, pick) -- find out which is its symbol
deleteAt cardList, pick -- remove it from the cardList
append aftershuffle, symbol -- and place it in the new list
end repeat
cardList = aftershuffle -- the new list becomes our shuffled cardList
end
After executing shuffleCards, we are ready to start a game! The codeA card is selected for flipping by clicking on in. So, it makes sense to create a handler on the cast member of the unflipped card (the blank card in position 51). Here is in pseudocode what should happen:
on mouseUp
if it is the first card to be flipped then
flip it -- find out its symbol
remember its symbol
else -- if it is the second card flipped in this turn
flip it -- picking a random symbol
check whether its symbol matches the previous card's symbol
end if
end
1. How do we remember the card symbols?
on exitFrame me global card1, card2, cardList card1 = 0 -- no card chosen card2 = 0 end2. How does a random symbol is picked? To find out which symbol was picked we need to see which blank card the user clicked on, and find the corresponding number from the cardList. card1 = the Clickon sprite(card1).member = cardList[card1]
|
|
|
Maintained By: Takis Metaxas
|
|