Programming Example: Concentration
This sample program was originally given as a student exercise, with the directive to write a VB application that implements a game of Concentration. In a game of Concentration, the player is presented with a board containing some number (24 in my implementation) of "covered" images. The player clicks on the covered pictures in sets of two to reveal the randomly assigned pictures beneath. If the two pictures match, they remain displayed; if not, they are flipped back over. The goal of the game is to reveal all matches (12 in my implementation) in as short a time as possible.
The program must maintain (in a file) the best time and display it on the screen as the "time to beat". Whenever the best score is beat, the new time becomes the best score and should be written out to the file.
My implementation also tracks the number of turns it took to reveal all matches (the fewer the better). A perfect score would be 12 in my implementation (but a score of 12 turns would be nearly impossible, as the player would have make 12 correct guesses in a row).
My implementation of the game is shown below. I am using playing cards as the pictures.
When the game first starts, all cards are shown face down. The "Best Scores" area shows the previous best time and number of turns, read in from a file. (Note: The "Reset" button effectively clears the best scores – it sets the best time to 99:99 and the number of turns to 9999.) The clock for "This Game" has started ticking.
This is a screen capture of two unmatched cards. These two cards will be immediately turned back over.
As the game progresses, the pairs that have been matched are removed from the board, and those yet to be matched remain face down. The clock is still ticking (one minute, 51 seconds – looks like we're not going beat the best time of one minute, 25 seconds!). (I'm not very good at this, I'm sure a five-year-old child can beat this ...)
At the end of the game, all cards will be cleared and the clock will stop ticking.
Download the solution for this project here.