How it works :


Madmem is a classical memory game where the user has to find matching pairs of cards.

12 images named "photo1.jpg", "photo2.jpg" ...through to "photo12.jpg" are stored in a folder called "seacards".
24 virtuel cards are created by creating 12 pairs of integers : 1-1, 2-2, 3-3, etc...
These 24 card-numbers are then shuffled.

The program also creates a 6X4 HTML table which is displayed on the user's screen.

One must imagine that each cell of the HTML table has an address as follow :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
So that, for example, the first card-number 8 will end up at address 3 and the second card-number 8 will end up at address 20.

The program then creates an array which works as follow :
cardarray (24,3) where
  • cardarray(i,1) = the cell address
  • cardarray(i,2) = the card number
  • cardarray(i,3) = 1 if the user found a match otherwise 0

Each time the user clicks on one of the "show" link, the program display the whole table cell by cell :
  • if the user has not clicked on the cell then display nothing, i.e; put a space with html
  • if the user has clicked on the link then display the image with html
  • if the user has not clicked on the cell but it corresponds to pair that was found by the user then display the image with html

How to run the program :


To run this program click here : MadMem

Source Code :

'*******************************************************************
'*******************************************************************
' MADMEM : by Alix Whittal - Mai 2008
'
' Memory game where the user has to find 12 matching pairs of cards
'
'********************************************************************
'********************************************************************

global MadMemDir$, MadMemImgUrl$, MadMemImgDir$
MadMemDir$ = "public\madmem\"
MadMemImgUrl$ = "../public/madmem/sea-cards/"
MadMemImgDir$ = "public\madmem\sea-cards\"
MadMemMusicUrl$ = "../public/madmem/music/"
 
click=0
'
' Shuffle the pack of 12 pairs
'
cardtot=24
dim cardarray(cardtot,3)
'Store cell addresses
for i=1 to cardtot
  cardarray(i,1)=i
  cardarray(i,2)=i
next i
' shuffle
for i = 1 to cardtot
   b=int(rnd(1)*cardtot)+1
   c=cardarray(i,2)
   cardarray(i,2)=cardarray(b,2)
   cardarray(b,2)=c
next i
'store card numbers
for i = 1 to cardtot
   no=cardarray(i,2)
   if no>12 then cardarray(i,2)=no-12
next i
'
' Start by showing an empty board
'
html "<TABLE border=1 ><TR>"
for i=1 to 4
   for j=1 to 6
      cardcount=cardcount+1
      call FancyBoard cardcount,i
      html " "
      link #flipcard, "show", [show]
      #flipcard setkey(str$(cardcount))
      html "</TD>"
   next j
   html "</TR>"
next i
html "</TABLE>"
wait
'
' First click on "show" button
'
[show]
 
if click=0 then click=click+1
 
if click=1 then
   flip1=1
   flip2=0
   flipid1=val(EventKey$)
   flipid2=0
   success=DisplayCards(flip1, flip2, flipid1, flipid2)
   click=click+1
   wait
end if
'
' Second click on "show" button
'
if click=2 then
   flip1=1
   flip2=1
   flipid2=val(EventKey$)
   success=DisplayCards(flip1, flip2, flipid1, flipid2)
   if success=1 then
      win=win+1
      if win=12 then
         print win
         call YouWin
         wait
      end if
      cardarray(flipid1,3)=1
      cardarray(flipid2,3)=1
 
   end if
 
   click=0
   flip1=0
   flip2=0
   wait
end if
wait
 
'**************************************************************
' DISPLAYCARDS FUNCTION :
' display first and second cards that were selected by user
' display winning pairs
' This function returns 1 if the user has found a winning pair
'**************************************************************
Function DisplayCards(flip1, flip2, flipid1, flipid2)
   DisplayCards=0
   cardcount=0
   photo1=0 : photo2=0
   cls
   html "<TABLE border=1><TR>"
   for i=1 to 4
      for j=1 to 6
         cardcount=cardcount+1
         '
         ' Check if it is the user's firts click
         '
         if flip1=1 then
            if cardcount=flipid1  then
               photono$=str$(cardarray(flipid1,2))
               img$ = MadMemImgUrl$+"photo"+photono$+".jpg"
            else
               if cardarray(cardcount,3)= 0 then
                     img$=" "
               else
                  photono$=str$(cardarray(cardcount,2))
                  img$ = MadMemImgUrl$+"photo"+photono$+".jpg"
               end if
            end if
         end if
         '
         ' Check if it is the user's second click
         '
         if flip2=1 then
            if cardcount=flipid1 then
               photo1=cardarray(flipid1,2)
               photono$=str$(photo1)
               img$ = MadMemImgUrl$+"photo"+photono$+".jpg"
            else
               if cardcount=flipid2 then
                  photo2=cardarray(flipid2,2)
                  photono$=str$(photo2)
                  img$ = MadMemImgUrl$+"photo"+photono$+".jpg"
               else
                  if cardarray(cardcount,3)= 0 then
                     img$=" "
                  else
                     photono$=str$(cardarray(cardcount,2))
                     img$ = MadMemImgUrl$+"photo"+photono$+".jpg"
                  end if
               end if
            end if
         end if
         call FancyBoard cardcount,i
         if img$ = " " then
            html " "
         else
            html "<img src="""; img$;""" align=center valign=middle><br>"
         end if
         '
         ' don't display the "show" link if a card is displayed
         '
         if cardcount=flipid1 or cardcount=flipid2 or cardarray(cardcount,3)=1 then
            link #flipcard, "", [show]
         else
            link #flipcard, "show", [show]
         end if
         #flipcard setkey(str$(cardcount))
         html "</TD>"
      next j
      html "</TR>"
   next i
   html "</TABLE>"
   '
   ' Return 1 to main program if user found a pair
   '
   if photo1=photo2 AND photo1<>0  then
                     DisplayCards=1
                  else
                     DisplayCards=0
                  end if
End Function
'*************************************
' SUB FANCYBOARD : display 6X4 table
'*************************************
Sub FancyBoard squareno,row
    squareno = squareno / 2
    row = row / 2
      if  squareno = Int(squareno) XOR row <> Int(row) then
         html "<TD width=115 height=140 align=center valign=middle bgcolor=#FFFF99>"
      else
         html "<TD width=115 height=140 align=center bgcolor=#FFFF66>"
      end if
end Sub
'**********************************
' SUB YOUWIN
'**********************************
Sub YouWin
   print
   print "well done ! You win :-)"
End Sub