File size: 5.05Kb
<?php
include("head.php");
// X image
$x = '<img src="images/x.gif" />';
// O image
$o = '<img src="images/o.gif" />';
// Store square data. 0 is not moved and is available to be selected, 1 is occupied by an x, and 2 is occupied by an o
$a1 = $_POST['a1'];
$a2 = $_POST['a2'];
$a3 = $_POST['a3'];
$a4 = $_POST['a4'];
$a5 = $_POST['a5'];
$a6 = $_POST['a6'];
$a7 = $_POST['a7'];
$a8 = $_POST['a8'];
$a9 = $_POST['a9'];
// If the game has been won, reset all variables!
if ($_POST['won'] == true)
{
unset($a1, $a2, $a3, $a4, $a5, $a6, $a7, $a8, $a9);
$i = 1;
}
?>
<?php
// Begin the COMPUTER intelligence
// Make computer move
include('compai.php');
?>
<!-- Start the Form -->
<!-- Echo PHP self for form action -->
<form action="<?php echo $PHP_SELF; ?>" method="POST">
<table border="1" cellpadding="2" cellspacing="3">
<tr>
<td width="20">
<!-- Print out the game field, inserting x's and o's -->
<?php if ($a1 == 0) { echo '<input type="checkbox" name="a1" value="1" />'; } if ($a1 == 1) { echo "$x <input type=\"hidden\" name=\"a1\" value=\"1\">"; } if ($a1 == 2) { echo "$o <input type=\"hidden\" name=\"a1\" value=\"2\">"; }?>
</td>
<td width="20">
<?php if ($a2 == 0) { echo '<input type="checkbox" name="a2" value="1" />'; } if ($a2 == 1) { echo "$x <input type=\"hidden\" name=\"a2\" value=\"1\">"; } if ($a2 == 2) { echo "$o <input type=\"hidden\" name=\"a2\" value=\"2\">"; }?>
</td>
<td width="20">
<?php if ($a3 == 0) { echo '<input type="checkbox" name="a3" value="1" />'; } if ($a3 == 1) { echo "$x <input type=\"hidden\" name=\"a3\" value=\"1\">"; } if ($a3 == 2) { echo "$o <input type=\"hidden\" name=\"a3\" value=\"2\">"; }?>
</td>
</tr>
<tr>
<td width="20">
<?php if ($a4 == 0) { echo '<input type="checkbox" name="a4" value="1" />'; } if ($a4 == 1) { echo "$x <input type=\"hidden\" name=\"a4\" value=\"1\">"; } if ($a4 == 2) { echo "$o <input type=\"hidden\" name=\"a4\" value=\"2\">"; }?>
</td>
<td width="20">
<?php if ($a5 == 0) { echo '<input type="checkbox" name="a5" value="1" />'; } if ($a5 == 1) { echo "$x <input type=\"hidden\" name=\"a5\" value=\"1\">"; } if ($a5 == 2) { echo "$o <input type=\"hidden\" name=\"a5\" value=\"2\">"; }?>
</td>
<td width="20">
<?php if ($a6 == 0) { echo '<input type="checkbox" name="a6" value="1" />'; } if ($a6 == 1) { echo "$x <input type=\"hidden\" name=\"a6\" value=\"1\">"; } if ($a6 == 2) { echo "$o <input type=\"hidden\" name=\"a6\" value=\"2\">"; }?>
</td>
</tr>
<tr>
<td width="20">
<?php if ($a7 == 0) { echo '<input type="checkbox" name="a7" value="1" />'; } if ($a7 == 1) { echo "$x <input type=\"hidden\" name=\"a7\" value=\"1\">"; } if ($a7 == 2) { echo "$o <input type=\"hidden\" name=\"a7\" value=\"2\">"; }?>
</td>
<td width="20">
<?php if ($a8 == 0) { echo '<input type="checkbox" name="a8" value="1" />'; } if ($a8 == 1) { echo "$x <input type=\"hidden\" name=\"a8\" value=\"1\">"; } if ($a8 == 2) { echo "$o <input type=\"hidden\" name=\"a8\" value=\"2\">"; }?>
</td>
<td width="20">
<?php if ($a9 == 0) { echo '<input type="checkbox" name="a9" value="1" />'; } if ($a9 == 1) { echo "$x <input type=\"hidden\" name=\"a9\" value=\"1\">"; } if ($a9 == 2) { echo "$o <input type=\"hidden\" name=\"a9\" value=\"2\">"; }?>
</td>
</tr>
</table>
<br /><br />
<?php
// To validate that only one entry has been submitted, make sure the variables only increase by 1
$movecount = $a1 + $a2 + $a3 + $a4 + $a5 + $a6 + $a7 + $a8 + $a9;
echo "<input type=\"hidden\" name=\"movecount\" value=\"{$movecount}\" />";
// Show the score, and increase fields accordingly
$t = $_POST['ties'];
$l = $_POST['loses'];
$w = $_POST['wins'];
if ($tie == 'true')
{
$t++;
}
elseif ($lose == 'true')
{
$l++;
}
elseif ($win == 'true')
{
$w++;
}
echo "wins:$w, loses:$l, ties:$t <br /><br />";
echo "<input type=\"hidden\" name=\"wins\" value=\"$w\">";
echo "<input type=\"hidden\" name=\"loses\" value=\"$l\">";
echo "<input type=\"hidden\" name=\"ties\" value=\"$t\">";
// If the game has been won already or tied, offer a restart button
if ($won == 'true')
{
echo "<input type=\"submit\" name=\"won\" value=\"Play Again\" />";
echo "<br /><input type=\"checkbox\" name=\"compfirst\" value=\"true\" /> Computer goes first!";
}
// Count turns to make sure the right AI is used
elseif (!isset($_POST['turn']))
{
// If the computer is going first, $i needs to be set to 2 instead to put the turn count back on track
if ($seti != 'false')
{
$i = 1;
}
echo "<input type=\"hidden\" name=\"turn\" value=\"{$i}\" />";
echo "<input type=\"submit\" name=\"submit\" value=\"Move!\" />";
echo "<br/>\n";
echo "<a href=\"/index.php?action=main\">HOME</a><br/>";
}
elseif (isset($_POST['turn']))
{
$i = $_POST['turn'];
$i++;
echo "<input type=\"hidden\" name=\"turn\" value=\"{$i}\" />";
echo "<input type=\"submit\" name=\"submit\" value=\"Move!\" />";
echo "<br/>\n";
echo "<a href=\"/index.php?action=main\">HOME</a><br/>";
}
?>
<!-- End the form -->
</form>