Page 1 of 1

[php] Memory Game

Posted: Mon Jul 23, 2012 7:34 am
by Callan S.
Thought it'd be nice to post this here, since php can do this sort of game quite well and it engages more than just clicking does. You can adjust $gridwidth and gridheight for larger grids. Play: Try and make pairs!

Planning to use this in an extension to my game, Fight Cycle
Perhaps pop a link in the credits to it if you use this code in your game.

Code: Select all

<?php
session_start();

$gridwidth=5;
$gridheight=4;

$cardnumb=$gridwidth*$gridheight;



if (!isset($_SESSION['memgame_show1x']))
   {
   $_SESSION['memgame_show1x']=0;
   $_SESSION['memgame_show1y']=0;
   $_SESSION['memgame_show2x']=0;
   $_SESSION['memgame_show2y']=0;
   
   $_SESSION['memgame_cardsflipped']=0;
   $_SESSION['memgame_reset']=0;
   }

if (!isset($_SESSION['memgame_grid']) || $_SESSION['memgame_reset']==1)
{
  $_SESSION['memgame_reset']=0;
  for ($loopy=1;$loopy<=$gridheight;$loopy+=1)
    {
    for ($loopx=1;$loopx<=$gridwidth;$loopx+=1)
       {
          if (($loopx+(($loopy-1)*$gridwidth))<=($cardnumb/2)) $_SESSION['memgame_grid'][$loopx][$loopy]=($loopx+(($loopy-1)*$gridwidth));
          else $_SESSION['memgame_grid'][$loopx][$loopy]=($loopx+(($loopy-1-($gridheight/2))*$gridwidth));
       }
    }

// jumble them
  for ($loopy=1;$loopy<=$gridheight;$loopy+=1)
    {
    for ($loopx=1;$loopx<=$gridwidth;$loopx+=1)
      {
      $loc2x=rand(1,$gridwidth);
      $loc2y=rand(1,$gridheight);
      $store=$_SESSION['memgame_grid'][$loc2x][$loc2y];
      $_SESSION['memgame_grid'][$loc2x][$loc2y]=$_SESSION['memgame_grid'][$loopx][$loopy];
      $_SESSION['memgame_grid'][$loopx][$loopy]=$store;
      }
    }
}


if (isset($_GET['x']))
   {
   if ($_SESSION['memgame_show1x']==0)
      {
      $_SESSION['memgame_show1x']=floor($_GET['x']);
      $_SESSION['memgame_show1y']=floor($_GET['y']);
      }
   else if ($_SESSION['memgame_show2x']==0)
      {
      $_SESSION['memgame_show2x']=floor($_GET['x']);
      $_SESSION['memgame_show2y']=floor($_GET['y']);
      if ($_SESSION['memgame_show2x']==$_SESSION['memgame_show1x'] && $_SESSION['memgame_show2y']==$_SESSION['memgame_show1y'])
         {$_SESSION['memgame_show2x']=0;$_SESSION['memgame_show2y']=0;}
      }

   }


echo "<table border=0 cellspacing=0 cellpadding=0>";
  for ($loopy=1;$loopy<=$gridheight;$loopy+=1)
    {
    echo "<tr>";
    for ($loopx=1;$loopx<=$gridwidth;$loopx+=1)
       {
          $val=$_SESSION['memgame_grid'][$loopx][$loopy];
          echo "<td width=50px height=100px>";

          if ($val>0)
          {
          if (($_SESSION['memgame_show1x']==$loopx && $_SESSION['memgame_show1y']==$loopy) || ($_SESSION['memgame_show2x']==$loopx && $_SESSION['memgame_show2y']==$loopy))
             {
                echo "<a href='?x=".$loopx."&y=".$loopy."'>";
                // this is where you would show the card face. Give each card a name like 'card1.jpg' 'card2.jpg' and
                // it will show one that corresponds to $val, which is the value of the current grid position.

                // echo "<img src='card".$val.".jpg'>";

                echo $val; // comment this out once you have graphics to use for the cards

                echo "</a>";
             }
             else echo "<a href='?x=".$loopx."&y=".$loopy."'><img src='card.gif'></a>";
          }

          echo "</td>";
       }
    echo "</tr>";
    }
echo "</table>";

// did the player get it right?
if ($_SESSION['memgame_show1x']>0 && $_SESSION['memgame_show2x']>0)
   {
   $loc1x=$_SESSION['memgame_show1x'];
   $loc1y=$_SESSION['memgame_show1y'];
   $loc2x=$_SESSION['memgame_show2x'];
   $loc2y=$_SESSION['memgame_show2y'];

   if ($_SESSION['memgame_grid'][$loc1x][$loc1y]==$_SESSION['memgame_grid'][$loc2x][$loc2y] && $_SESSION['memgame_grid'][$loc1x][$loc1y]>0)
      {
      echo "Excellent, you got a pair!<p>";
      $_SESSION['memgame_grid'][$loc1x][$loc1y]=0;
      $_SESSION['memgame_grid'][$loc2x][$loc2y]=0;
      $_SESSION['memgame_cardsflipped']+=2;
      // enter your reward for success here
      }
      else
      {
      echo "Not a pair, sorry!<p>";
      // if you have a minor reward just for clicking, put it here
      }
   $_SESSION['memgame_show1x']=0;
   $_SESSION['memgame_show1y']=0;
   $_SESSION['memgame_show2x']=0;
   $_SESSION['memgame_show2y']=0;

   if ($_SESSION['memgame_cardsflipped']>=$cardnumb)
      {
      echo "You finished the grid! Well done!<p>";
      $_SESSION['memgame_cardsflipped']=0;
      $_SESSION['memgame_reset']=1;
      // if you have a reward for finishing a whole grid, put it here
      echo "<font size=1><a href='?'>(( Refresh ))</a></font><br>";
      }

   header("Refresh: 2; url='?'");
   }

?>
And a graphic I got from public domain for a card back is attached. Just put it in the same directory, for now.