Jump to content

PHP-arrays and sessions


Nightin

Recommended Posts

Hey, complete coding noob here.

 

I am trying to make a memory game/matching game with php that does the following:

  • Include 8, each with a duplicate, displaying a total of 16 cards in the game
  • When a card is not clicked, the card is "face down" and shows a default tile image
  • When a card is clicked, it "flips over", displaying one of the 8 card images
  • Uses a session to remember the cards that have been flipped
  • When two cards are flipped over to the image side, and the two images are identical, keep them both faced up and keep them like that for the remainder of the game
  • Randomizes the pictures when a new game session starts

 

Here is the code I have so far. The images are named "0.jpg", "1.jpg" , "2.jpg" .... all the way up to "7.jpg"

 

<?
session_start(); 

?>


<html>
<head>
  <title> Memory Game! </title>
</head>
<body bgcolor="#FFCCCC" link="#FFFFFF" vlink="#000000" alink="#FF3399">

  <center>
   <font size=+2 color=white><b>Have fun!</b></font> <br><br><br>

<?


  $card[0][0] = "0.jpg";
  $card[0][1] = "0.jpg";
  $card[0][2] = "1.jpg";
  $card[0][3] = "1.jpg";
  $card[1][0] = "2.jpg";
  $card[1][1] = "2.jpg";
  $card[1][2] = "3.jpg";
  $card[1][3] = "3.jpg";
  $card[2][0] = "4.jpg";
  $card[2][1] = "4.jpg";
  $card[2][2] = "5.jpg";
  $card[2][3] = "5.jpg";
  $card[3][0] = "6.jpg";
  $card[3][1] = "6.jpg";
  $card[3][2] = "7.jpg";
  $card[3][3] = "7.jpg";

/*  for($r=0;$r<4;$r++){
    for($c=0;$c<4;$c++){

    echo "<img src=".$card[$r][$c]." width=100 height=100>";

    }
  }
*/


  $value[0][0] = 0; 
  $value[0][1] = 0; 
  $value[0][2] = 0; 
  $value[0][3] = 0; 
  $value[1][0] = 0; 
  $value[1][1] = 0; 
  $value[1][2] = 0; 
  $value[1][3] = 0; 
  $value[2][0] = 0; 
  $value[2][1] = 0; 
  $value[2][2] = 0; 
  $value[2][3] = 0; 
  $value[3][0] = 1; 
  $value[3][1] = 0; 
  $value[3][2] = 1; 
  $value[3][3] = 1; 

  // if the value is selected

  $value[$r][$c] = 1; 



  for($i=0; $i<100; $i++)
/*    $card = mixup($card);*/


  echo "<table border=1>";
  for($r=0; $r<4; $r++){
    echo "<tr>";
    for($c=0; $c<4; $c++){
      echo "<td>";
      if($value[$r][$c]==1){
         echo "<a href=game.php><img src=\"tile.jpg\" width=150 height=150> </a> ";
      }
      else{
         echo "<a href=game.php><img src=".$card[$r][$c]." width=150 height=150></a>";
      }
      echo "</td>";
    }
    echo "</tr>";
  }
  echo "</table>";



  echo "<form action=\"$PHP_SELF\" method=post>"; 
  echo "<input type=submit value=Restart></form>"; 


?>





<?


  function mixup($a){
    //we know that it's 4x4
    $x1 = rand(0,3);
    $y1 = rand(0,3);

    $x2 = rand(0,3);
    $y2 = rand(0,3);

    $t = $a[$x1][$y1];
    $a[$x1][$y1] = $a[$x2][$y2];
    $a[$x2][$y2] = $t;

    return $a;
  }


?>

</font> 
</body> 
</center>
</html> 

I used arrays to display all the images in a table, and set values for each cell.

If the value of the cell is 0, then the card is flipped to the image side.

Otherwise the value is 1, and the card is face down.

 

How can I set it so that when a card is clicked, the value automatically sets to "0", and changes back to "1" when another card is flipped and it doesn't match?

 

Help would be much appreciated.

 

Thank you in advance!

Link to comment
https://forums.phpfreaks.com/topic/200509-php-arrays-and-sessions/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.