Zend - The PHP Company




Games

Add Code


Memory Game  

Type: application
Added by: bleach
Entered: 12/03/2000
Last modified: 08/12/2008
Rating: **** (8 votes)
Views: 11088
This is a simple memory game, it has plugable html cards. I have it set at letters right now, but its easy to switch over to images. It makes heavy use of php4 sessions, and uses no javascript/etc.


<?
//Memory Game
//Copyright 2000, Derek Young
//I assume no responsibility for damages, have fun with this
//code, do whatever you wish. You can replace the $hidden and
//$card variable to equal whatever you want, hopefully images ;>


session_register("board");
session_register("state");

if(
$reset) {
  unset(
$board); unset($state); 
}

//Init State
if(!$state$state=0;

//Init Hidden Card
$hidden="<FONT SIZE="5">X</FONT>";

//Init Game Board
if(!$board) {

   
//Init 15 Cards, Grid here will be 15x15
   
$card[0]="<FONT SIZE="5">A</FONT>";
   
$card[1]="<FONT SIZE="5">B</FONT>";
   
$card[2]="<FONT SIZE="5">C</FONT>";
   
$card[3]="<FONT SIZE="5">D</FONT>";
   
$card[4]="<FONT SIZE="5">E</FONT>";
   
$card[5]="<FONT SIZE="5">F</FONT>";
   
$card[6]="<FONT SIZE="5">G</FONT>";
   
$card[7]="<FONT SIZE="5">H</FONT>";
   
$card[8]="<FONT SIZE="5">I</FONT>";
   
$card[9]="<FONT SIZE="5">J</FONT>";
   
$card[10]="<FONT SIZE="5">K</FONT>";
   
$card[11]="<FONT SIZE="5">L</FONT>";
   
$card[12]="<FONT SIZE="5">M</FONT>";
   
$card[13]="<FONT SIZE="5">N</FONT>";
   
$card[14]="<FONT SIZE="5">O</FONT>";

   
//DoubleCard
   
for ($temp=15;$temp 30;++$temp) {
   
$card[$temp]=$card[$temp-15];
   }
   
   
//Randomize Cards
   
srand((double)microtime()*1000000);
   
shuffle ($card);

   for (
$temp=1;$temp <= 30;++$temp) {
      
$board[$temp][state]="0";
      
$board[$temp][view]=$hidden;
      list(,
$current) = each ($card);
      
$board[$temp][card]=$current;
   }
}


function 
showboard() {
   global 
$PHP_SELF;
   global 
$board;
   global 
$state;
   
$cols=5;
   
$rows=6;
   
$count=1;
   
//Start HTML
   
echo "<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"n";
   
echo ""http://www.w3c.org/TR/REC-html40/loose.dtd">n";
   
echo "<HTML>n<HEAD>n<TITLE>Memory Game!</TITLE>n</HEAD>n<BODY>n";

   echo 
"<TABLE BORDER="2">n";
   for(
$temp=$rows;$temp 0;$temp--)
    {
    echo 
"<TR>n";
      for(
$temp2=$cols;$temp2 0;$temp2--)
      {
      echo 
"   <TD>";
      switch (
$board[$count][state]) {
        case 
0:
          echo 
"<A HREF="$PHP_SELF?click=$count">";
          echo 
$board[$count][view];
          echo 
"</A>";
          break;
        case 
1:
          echo 
$board[$count][view];
          break;
        case 
2:
          echo 
$board[$count][view];
          break;
        }
      
$count++;
      echo 
"</TD>n";
      }
    echo 
"</TR>n";
    }  
   echo 
"</TABLE>n";   
   echo 
"<BR>n<A HREF="$PHP_SELF?reset=1">Start Over</A>n<BR>n";
   echo 
"</BODY>n</HTML>n";
}



// Main Processing Routine

if($click) {
   if(!
$state) {
      
$state=$click;
      
$board[$click][view]=$board[$click][card];
      
$board[$click][state]=1;
      
showboard(); exit(); 
   }

   if(
$state) {
      
$board[$click][view]=$board[$click][card];
      
$board[$click][state]=1;
      
showboard();
      if(
$board[$click][card]==$board[$state][card]) {
         
$board[$click][state]=2;
         
$board[$state][state]=2;
         
$state=0;
         exit();
      }
      else {
         
$board[$click][state]=0;
         
$board[$state][state]=0;
         
$board[$click][view]=$hidden;
         
$board[$state][view]=$hidden;
         
$state=0;
         exit();
      }    
   }   
}

if (!
$click) { showboard(); exit(); }

?>


Usage Example




Rate This Script





Search



This Category All Categories