Part 4: A Turn of Play
Video Part 4a
(If you don't see the video, follow this link to view Part 4a.)
Video Part 4b
(If you don't see the video, follow this link to view Part 4b.)
Cheat Sheet for Part 4a
If you don't trust your ability to type in the more complicated expressions, you can copy them from here:
Match test expression 1:
How does the test for matches work?
If you'd like a little more information on how the test for matches used in this game works, read on.
Consider the cards in our game. Remember I told you that the matching cards had to be side by side in the cardface animation or it wouldn't work.
Card face frames:
The images for frames 0 and 1 match, frames 2 and 3 match, frames 4 and 5 match, etc. But the frame numbers are not equal.
However if we subtract 0 from the first card frame number of a pair and 1 from the second card frame number of a pair, then they are both equal to the first card frame number.
This is true regardless of the order in which we pick the cards.
So how do we do that?
Notice that the frame number of the first card of a match is even, and the frame number of the second card of a match is odd.
Recall that the modulus operator (%) returns the remainder of a division operation. If we divide an integer >= 0 by 2, the remainder is always 0 if the number is 0 or even, and 1 if the number is odd.
So if the cards match, then the condition:
is true. If they don't match, it is false.