Concentration

[Home]   [Puzzles & Projects]    [Delphi Techniques]   [Math topics]   [Library]   [Utilities]

 

 

Search

Search WWW

Search DelphiForFun.org

As of October, 2016, Embarcadero is offering a free release of Delphi (Delphi 10.1 Berlin Starter Edition ).     There are a few restrictions, but it is a welcome step toward making more programmers aware of the joys of Delphi.  They do say "Offer may be withdrawn at any time", so don't delay if you want to check it out.  Please use the feedback link to let me know if the link stops working.

 

Support DFF - Shop

 If you shop at Amazon anyway,  consider using this link. 

     

We receive a few cents from each purchase.  Thanks

 


Support DFF - Donate

 If you benefit from the website,  in terms of knowledge, entertainment value, or something otherwise useful, consider making a donation via PayPal  to help defray the costs.  (No PayPal account necessary to donate via credit card.)  Transaction is secure.

Mensa® Daily Puzzlers

For over 15 years Mensa Page-A-Day calendars have provided several puzzles a year for my programming pleasure.  Coding "solvers" is most fun, but many programs also allow user solving, convenient for "fill in the blanks" type.  Below are Amazon  links to the two most recent years.

Mensa® 365 Puzzlers  Calendar 2017

Mensa® 365 Puzzlers Calendar 2018

(Hint: If you can wait, current year calendars are usually on sale in January.)

Contact

Feedback:  Send an e-mail with your comments about this program (or anything else).

Search DelphiForFun.org only

 

 

 

Problem Description

Here is a version of the memory-testing  card game, "Concentration".   The objective is to select matching pairs  from an array of face down cards by turning two cards face up, one after the other, on each turn.   When a match is found, the matching cards are removed and the player gets another turn.   When the two cards do not match, they are returned to their original face down positions.  

As a single player game, the objective is to find all matching pairs in the fewest moves.  In the two-player game, the player that finds the most pairs is the winner.    

Background & Techniques

This program was prompted by a request from a high school student looking for a class Delphi programming project.   By the time I had developed  a graphical demo that would flip or remove cards as a starting point, he had moved on to something different.   So I decided to complete the logic part and post it.

The game allows one or two players and the number of cards in the array can be selected by the user.    

Non-programmers are welcome to read on, but may want to skip to the bottom of this page to download executable version of the program.

The U_CardComponent previously introduced, automates the card image handling.   When a new game is started two decks are created -  one with 52 cards and one with twice the number of pairs specified by the user.    After shuffling the temporary 52 card deck, the first "nbrpairs" cards are used to set the suit and card values of the first half of the real deck,.  The lower half of the deck has card values and suits set to match the first half, ensuring pairs.   The play deck is shuffled of course before dealing them into a face down array. 

The checking and scoring logic is embedded in an OnMouseUp exit  for each TCard object.   The first card clicked of a turn is saved and when a second card is clicked, its value and suit are compared to the 1st card.   Based on results, scores are updated, cards made invisible or simply turned back face down.    When all pairs are matched, the winner and score are displayed and the game is over. 

Expected game lengths

What is the expected length of a game if the player has perfect memory?   That is, once a card has been turned  our player will remember exactly where it is located; it is, in effect, left face up  Since a perfectly lucky game would require N turns to match N pairs, and a perfectly unlucky game would require 2N-1 turns.    Simplistic reasoning says that the average game should be halfway between the shortest and longest possible games which  slightly less than 1.5 times the number of pairs .  My analytical skills bogged down in trying to calculate a value, so I resorted to writing a "Concentration Study" program to play several thousand "perfect memory" games and chart the game length statistics.   I looks like the average game is slightly higher than 1.5 times N.   In other words, there seem to be more ways to be be unlucky than there are to be lucky.  I'll wait for a real mathematician to verify this, or tell me that I have a bug in the program.  In any event the Concentration Study program  can  be downloaded from the links below if you want to play with it.  

Addendum April 12, 2009:  If the original version of Concentration was not difficult enough, here is Version 2 which adds the option of requiring 3 cards to be matched before they are removed from the board.  This version also adds the option of matching on card value and suit, as in the original version, or , on card value only. 

Running/Exploring the Program 

bulletDownload Concentration source
bullet

Download Concentration Study source

bulletDownload  Concentration executable
bulletDownload  Concentration Study executable

Suggestions for Further Explorations

Draw three cards per turn and create triplets?
Implement computer as an opponent - we would have to make him somewhat forgetful of the locations of cards seen.

 

Original Date: May 21, 2004 

Modified: May 15, 2018

 
  [Feedback]   [Newsletters (subscribe/view)] [About me]
Copyright © 2000-2018, Gary Darby    All rights reserved.