#native_company# #native_desc#
#native_cta#

MindMaster

By GrahamKing
on February 6, 2005

Version: 1.0

Type: Full Script

Category: Games

License: GNU General Public License

Description: Like a famous analytical guessing game, you have to guess the coloured pins hidden behind the panel. Simple really – I wrote it to teach myself PHP.

<?php
session_start();
if (!isset($_SESSION['gamerand'])) 
	{ $_SESSION['gamerand'] = array(); }
if (!isset($_SESSION['guesstable']))
	{ $_SESSION['guesstable'] = array(); }
if (!isset($_SESSION['answertable']))
	{ $_SESSION['answertable'] = array(); }
 if (!isset($_SESSION['won']))
	{ $_SESSION['won'] = 0 ; }
?>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Mind Game 1</title>
<meta http-equiv="content-type"
    content="text/html; charset=iso-8859-1" />
</head>
<body>
<H3>The Mind Game.</H3>
This game is devised around the idea of a popular analytical game that's been around for years.
<BR>
Simply put, there are 4 hidden coloured buttons behind the panel. 
<BR>
There are 8 colours to choose from, you get 12 attempts to guess the correct colours and positions. 
<BR>
You are awarded a red tick for each correct colour and correct place, and a black tick for each 
correct colour.
<BR>
Simple Really! 
<HR>
<?php
$colour_table = array(
        'blue_button.jpg',
        'red_button.jpg',
        'green_button.jpg',
        'yellow_button.jpg',
        'white_button.jpg',
        'black_button.jpg',
        'purple_button.jpg',
        'orange_button.jpg');
$lost = 0;

// if new, populate gamerand array with 4 random colours in places
if ( count($_SESSION['gamerand']) == 0)
{
  for ($i=0; $i<4; $i++)
   {
     $j = rand(0,7);		// 56/7 choices - more randomised?
//     $j = int($j/7);
     $_SESSION['gamerand'][] = $colour_table[$j];
   }
}

// get results back from last guess
// -------------------------------- 

if (isset($_POST['colour1']))
{
	$index = count($_SESSION['guesstable']);
	$_SESSION['guesstable'][$index]         = array();
	$_SESSION['answertable'][$index]        = array();

	$_SESSION['guesstable'][$index][0] 	= $_POST['colour1'];
	$_SESSION['guesstable'][$index][1]      = $_POST['colour2'];
	$_SESSION['guesstable'][$index][2]      = $_POST['colour3'];
	$_SESSION['guesstable'][$index][3]      = $_POST['colour4'];
}

// assign ticks for correct results
// --------------------------------

$tick_red = 0;
$tick_white = 0;
$tick_ctr = 0;
$guess_mark = array(0,0,0,0);		//to not assign ticks twice
$match_mark = array(0,0,0,0);


for ($i=0; $i<4; $i++)			// red ticks first
{ 
  if ($_SESSION['guesstable'][$index][$i] == $_SESSION['gamerand'][$i] )
    { $tick_red++;
      $guess_mark[$i] = 1; 	  
      $match_mark[$i] = 1;
    }
}

for ($i=0; $i<4; $i++)			// now white ticks 
{ 
  for ($j=0; $j<4; $j++)
  { 
    if ($guess_mark[$i] == 0 && $j != $i) 
    { 
      if ($_SESSION['guesstable'][$index][$i] == $_SESSION['gamerand'][$j]
	  && $match_mark[$j] != 1)
        { $tick_white++;
          $guess_mark[$i] = 1;
	  $match_mark[$j] = 1;
        }
    }
  }
}

if ($tick_red > 0)
{
  for ($i=0; $i<$tick_red; $i++)
  { $_SESSION['answertable'][$index][$i] = "red"; }
}
if ($tick_white > 0)
{
  for ($i=$tick_red; $i<($tick_red+$tick_white); $i++)
  { $_SESSION['answertable'][$index][$i] = "white"; }
}

if ($tick_red == 4)
  { $_SESSION['won'] = 1; } 

// start table for game
if ($_SESSION['won'] == 1)
{
        echo "<table border=1 width=500 height=60> n";
        echo "<TR><TD align=center width=300> n";
        echo "You won! the colours are: <BR><BR> n";
	echo "&nbsp&nbsp&nbsp&nbsp n";
        echo "<IMG SRC=/images/" . $_SESSION['gamerand'][0] . " /> &nbsp&nbsp&nbsp&nbsp&nbsp n"
           . "<IMG SRC=/images/" . $_SESSION['gamerand'][1] . " /> &nbsp&nbsp&nbsp&nbsp&nbsp n"
           . "<IMG SRC=/images/" . $_SESSION['gamerand'][2] . " /> &nbsp&nbsp&nbsp&nbsp&nbsp n"
           . "<IMG SRC=/images/" . $_SESSION['gamerand'][3] . " /> n" ;
        echo "</TD> n";
        $lost = 1;
        echo "<TD align=center valign=center> n";
        echo "<form action=" . $_SERVER['PHP_SELF'] . " method=post> n";
        echo "<INPUT type=submit value="Play Again" name=playagain> n";
        echo "</FORM> n";
}
elseif (count($_SESSION['guesstable']) == 12)
{
        echo "<table border=1 width=500 height=60> n";
        echo "<TR><TD align=center width=300> n";
        echo "You lost!! The colours are: <BR><BR> n";
	echo "&nbsp&nbsp&nbsp&nbsp n";
        echo "<IMG SRC=/images/" . $_SESSION['gamerand'][0] . " /> &nbsp&nbsp&nbsp&nbsp&nbsp n"
           . "<IMG SRC=/images/" . $_SESSION['gamerand'][1] . " /> &nbsp&nbsp&nbsp&nbsp&nbsp n"
           . "<IMG SRC=/images/" . $_SESSION['gamerand'][2] . " /> &nbsp&nbsp&nbsp&nbsp&nbsp n"
           . "<IMG SRC=/images/" . $_SESSION['gamerand'][3] . " /> n" ;

        echo "</TD>";
        $lost = 1;
        echo "<TD align=center valign=center>";
        echo "<form action=" . $_SERVER['PHP_SELF'] . " method=post>";
        echo "<INPUT type=submit value="Play Again" name=playagain> n";
        echo "</FORM>";
}
else
{
        echo "<table border=1 bgcolor=black width=500 height=60><TR><TD align=center valign=center> n";
        echo "<span style="color:white; font-style:bold;">"
             . "Guess the Four hidden coloured buttons behind this panel n"
	;
//        echo "<IMG SRC=/images/" . $_SESSION['gamerand'][0] . " /> &nbsp&nbsp n"
//           . "<IMG SRC=/images/" . $_SESSION['gamerand'][1] . " /> &nbsp&nbsp n"
//           . "<IMG SRC=/images/" . $_SESSION['gamerand'][2] . " /> &nbsp&nbsp n"
//           . "<IMG SRC=/images/" . $_SESSION['gamerand'][3] . " /> n" ;
        echo '</span> n';

}
echo "</TD></TR></TABLE> n";

// table of choices

echo "<TABLE border=1 width=500> n";


// set up form for selecting the colours
// -------------------------------------
if (count($_SESSION['guesstable']) < 12 && $lost == 0)
{
echo "<TR><TD width=300> n";
echo "<TABLE valign=center align=center><TR> n";
echo "<form action=" . $_SERVER['PHP_SELF'] . " method=post> n";

echo "<TD valign=center>";
for ($i=0; $i<count($colour_table); $i++)
{ $check_value = "";
  if ($_SESSION['guesstable'][$index][0] == $colour_table[$i])
    { $check_value = "checked=checked"; }
  echo "<INPUT TYPE=RADIO NAME=colour1 VALUE="$colour_table[$i]" $check_value >"
	. "<IMG SRC=/images/" . $colour_table[$i] . "><BR> n"; 
}
echo "</TD>";

echo "<TD valign=center>";
for ($i=0; $i<count($colour_table); $i++)
{ $check_value = "";
  if ($_SESSION['guesstable'][$index][1] == $colour_table[$i])
    { $check_value = "checked=checked"; }
  echo "<INPUT TYPE=RADIO NAME=colour2 VALUE="$colour_table[$i]" $check_value >"
	. "<IMG SRC=/images/" . $colour_table[$i] . "><BR> n";
}
echo "</TD>";

echo "<TD valign=center>";
for ($i=0; $i<count($colour_table); $i++)
{ $check_value = "";
  if ($_SESSION['guesstable'][$index][2] == $colour_table[$i])
    { $check_value = "checked=checked"; }
  echo "<INPUT TYPE=RADIO NAME=colour3 VALUE="$colour_table[$i]" $check_value >"
	. "<IMG SRC=/images/" . $colour_table[$i] . "><BR> n";
}
echo "</TD>";

echo "<TD valign=center>";
for ($i=0; $i<count($colour_table); $i++)
{ $check_value = "";
  if ($_SESSION['guesstable'][$index][3] == $colour_table[$i])
    { $check_value = "checked=checked"; }
  echo "<INPUT TYPE=RADIO NAME=colour4 VALUE="$colour_table[$i]" $check_value >"
	. "<IMG SRC=/images/" . $colour_table[$i] . "><BR> n";
}
echo "</TD> n";

echo "</TD></TR></TABLE> n";
echo "</TD> n";

echo "<TD valign=center align=center> n";
echo "<INPUT type=submit value="Enter your guess" name=submit > n";
echo "</TD> n";
echo "</TR> n";
echo "</form n>";
}

// display previous results
// ------------------------
if (count($_SESSION['guesstable']) > 0)
{
$ctr = count($_SESSION['guesstable']) - 1;

for ($i=$ctr; $i>= 0; $i--)
  {
        echo "n <TR><TD width=300 align=center> n";
	echo " &nbsp&nbsp&nbsp&nbsp n";
        echo "<IMG SRC=/images/" . $_SESSION['guesstable'][$i][0] . " /> &nbsp&nbsp&nbsp&nbsp&nbsp n"
           . "<IMG SRC=/images/" . $_SESSION['guesstable'][$i][1] . " /> &nbsp&nbsp&nbsp&nbsp&nbsp n"
           . "<IMG SRC=/images/" . $_SESSION['guesstable'][$i][2] . " /> &nbsp&nbsp&nbsp&nbsp&nbsp n"
           . "<IMG SRC=/images/" . $_SESSION['guesstable'][$i][3] . " /> n";
        echo "</TD><TD> n";
	$attempt = $i  + 1;
	echo "($attempt) &nbsp";
        for ($j=0;$j<4; $j++)
        {
          if ($_SESSION['answertable'][$i][$j] == "red")
            { echo  "<IMG SRC="/images/red_tick.jpg"> n"; }
          elseif ($_SESSION['answertable'][$i][$j] == "white")
            { echo  "<IMG SRC="/images/black_tick.jpg"> n"; }
        }
        echo "</TD></TR> n";
  }
}


echo "</table> nn";


if ($_SESSION['won'] == 1 || count($_SESSION['guesstable']) == 12)
{
	$_SESSION = array();
	session_destroy();
}
?>

<HR>
<body></html>