I thought it would be humorous to send all the game info through the URL... so I did... But I don't know why I thought that was funny at the time.
Anywho, check it out: Tic-Tak-Toe
This be the code. (although there seems to be strange amonts of white space towards the bottom, must be the text editor I used)
Code: Select all
<?php
// PHP Tic Tak Toe
// Version: lame.weak
// By Travis Stuart
echo "<body bgcolor=\"#DDDDDD\"><b><center><font face=tahoma><br>\n\n";
//Randomize if true
$meFirst = $_GET['meFirst'];
if($meFirst == true) { $meFirst = mt_rand(0,1); }
//Grid
$g00 = $_GET['g00'];
$g10 = $_GET['g10'];
$g20 = $_GET['g20'];
$g01 = $_GET['g01'];
$g11 = $_GET['g11'];
$g21 = $_GET['g21'];
$g02 = $_GET['g02'];
$g12 = $_GET['g12'];
$g22 = $_GET['g22'];
//Grid Array
$grid[0][0] = $g00;
$grid[1][0] = $g10;
$grid[2][0] = $g20;
$grid[0][1] = $g01;
$grid[1][1] = $g11;
$grid[2][1] = $g21;
$grid[0][2] = $g02;
$grid[1][2] = $g12;
$grid[2][2] = $g22;
//Do AI here
if($meFirst != 1)
{
$aiDone = false;
//Block human if need be
//Check cols
for($row = 0; $row <= 2; $row++)
{
$xThere = false;
for($col = 0; $col <= 2; $col++)
{
if($grid[$row][$col] == 'x')
{
if($xThere == false)
{
$xThere = true;
} else {
//Uh-Oh, there are 2 x's now... try to put an o in there!
for($colN = 0; $colN <= 2; $colN++)
{
if($grid[$row][$colN] == '_') { $grid[$row][$colN] = 'o'; $aiDone = true; }
}
}
}
if($aiDone == true) break;
}
if($aiDone == true) break;
}
//Check rows
if($aiDone == false)
{
for($col = 0; $col <= 2; $col++)
{
$xThere = false;
for($row = 0; $row <= 2; $row++)
{
if($grid[$row][$col] == 'x')
{
if($xThere == false)
{
$xThere = true;
} else {
//whoa, there are 2 x's now... try to put an o in there!
for($rowN = 0; $rowN <= 2; $rowN++)
{
if($grid[$rowN][$col] == '_') { $grid[$rowN][$col] = 'o'; $aiDone = true; }
}
}
}
if($aiDone == true) break;
}
if($aiDone == true) break;
}
}
//If no need to block, randomize placement later, maybe
if($aiDone == false)
{
//See if there are any spots left to take
$spotsLeft = false;
for($col = 0; $col <= 2; $col++)
{
for($row = 0; $row <= 2; $row++)
{
if($grid[$row][$col] == '_') $spotsLeft = true;
}
}
if($spotsLeft == true)
{
for($col = 0; $col <= 2; $col++)
{
$oThere = false;
for($row = 0; $row <= 2; $row++)
{
if($grid[$row][$col] == 'o')
{
for($rowN = 0; $rowN <= 2; $rowN++)
{
if($grid[$rowN][$col] == '_') { $grid[$rowN][$col] = 'o'; $aiDone = true; break; }
}
}
if($aiDone == true) break;
}
if($aiDone == true) break;
}
if($aiDone == false)
{
$okay = false;
while($okay == false)
{
$randX = mt_rand(0,2);
$randY = mt_rand(0,2);
if($grid[$randX][$randY] == '_')
{
$grid[$randX][$randY] = 'o';
$aiDone = true;
$okay = true;
}
}
}
}
}
}
//See if anyone won
//if x one
for($col = 0; $col <= 2; $col++)
{
$valueCount = 0;
for($row = 0; $row <= 2; $row++)
{
if($grid[$row][$col] == 'x') $valueCount++;
}
if($valueCount == 3) echo "You WON!";
}
for($col = 0; $col <= 2; $col++)
{
$valueCount = 0;
for($row = 0; $row <= 2; $row++)
{
if($grid[$col][$row] == 'x') $valueCount++;
}
if($valueCount == 3) echo "You WON!";
}
$valueCount = 0;
for($dia = 0; $dia <= 2; $dia++)
{
if($grid[$dia][$dia] == 'x') $valueCount++;
}
if($valueCount == 3) echo "You WON!";
$valueCount = 0;
for($dia = 0; $dia <= 2; $dia++)
{
if($grid[2-$dia][$dia] == 'x') $valueCount++;
}
if($valueCount == 3) echo "You WON!";
//if o one
for($col = 0; $col <= 2; $col++)
{
$valueCount = 0;
for($row = 0; $row <= 2; $row++)
{
if($grid[$row][$col] == 'o') $valueCount++;
}
if($valueCount == 3) echo "You LOST!";
}
for($col = 0; $col <= 2; $col++)
{
$valueCount = 0;
for($row = 0; $row <= 2; $row++)
{
if($grid[$col][$row] == 'o') $valueCount++;
}
if($valueCount == 3) echo "You LOST!";
}
$valueCount = 0;
for($dia = 0; $dia <= 2; $dia++)
{
if($grid[$dia][$dia] == 'o') $valueCount++;
}
if($valueCount == 3) echo "You LOST!";
$valueCount = 0;
for($dia = 0; $dia <= 2; $dia++)
{
if($grid[2-$dia][$dia] == 'o') $valueCount++;
}
if($valueCount == 3) echo "You LOST!";
$g00 = $grid[0][0];
$g10 = $grid[1][0];
$g20 = $grid[2][0];
$g01 = $grid[0][1];
$g11 = $grid[1][1];
$g21 = $grid[2][1];
$g02 = $grid[0][2];
$g12 = $grid[1][2];
$g22 = $grid[2][2];
function editGridValues($x,$y)
{
global $g00,$g10,$g20, $g01,$g11,$g21, $g02,$g12,$g22;
switch($x)
{
case 0:
switch($y)
{
case 0: $gridValues = 'g00='.'x'.'&g10='.$g10.'&g20='.$g20 . '&g01='.$g01.'&g11='.$g11.'&g21='.$g21 . '&g02='.$g02.'&g12='.$g12.'&g22='.$g22; break;
case 1: $gridValues = 'g00='.$g00.'&g10='.$g10.'&g20='.$g20 . '&g01='.'x'.'&g11='.$g11.'&g21='.$g21 . '&g02='.$g02.'&g12='.$g12.'&g22='.$g22; break;
case 2: $gridValues = 'g00='.$g00.'&g10='.$g10.'&g20='.$g20 . '&g01='.$g01.'&g11='.$g11.'&g21='.$g21 . '&g02='.'x'.'&g12='.$g12.'&g22='.$g22; break;
}
break;
case 1:
switch($y)
{
case 0: $gridValues = 'g00='.$g00.'&g10='.'x'.'&g20='.$g20 . '&g01='.$g01.'&g11='.$g11.'&g21='.$g21 . '&g02='.$g02.'&g12='.$g12.'&g22='.$g22; break;
case 1: $gridValues = 'g00='.$g00.'&g10='.$g10.'&g20='.$g20 . '&g01='.$g01.'&g11='.'x'.'&g21='.$g21 . '&g02='.$g02.'&g12='.$g12.'&g22='.$g22; break;
case 2: $gridValues = 'g00='.$g00.'&g10='.$g10.'&g20='.$g20 . '&g01='.$g01.'&g11='.$g11.'&g21='.$g21 . '&g02='.$g02.'&g12='.'x'.'&g22='.$g22; break;
}
break;
case 2:
switch($y)
{
case 0: $gridValues = 'g00='.$g00.'&g10='.$g10.'&g20='.'x' . '&g01='.$g01.'&g11='.$g11.'&g21='.$g21 . '&g02='.$g02.'&g12='.$g12.'&g22='.$g22; break;
case 1: $gridValues = 'g00='.$g00.'&g10='.$g10.'&g20='.$g20 . '&g01='.$g01.'&g11='.$g11.'&g21='.'x' . '&g02='.$g02.'&g12='.$g12.'&g22='.$g22; break;
case 2: $gridValues = 'g00='.$g00.'&g10='.$g10.'&g20='.$g20 . '&g01='.$g01.'&g11='.$g11.'&g21='.$g21 . '&g02='.$g02.'&g12='.$g12.'&g22='.'x'; break;
}
break;
}
return $gridValues;
}
echo "<center><br><br>";
echo "<table border=5 cellspacing=0 cellpadding=0>";
echo "<tr><td>";
//(0,0)
if($g00 == '_') echo "<a href=test.php?".editGridValues(0,0).">";
switch($g00)
{
case '_': echo "<img src=_.gif border=0>"; break;
case 'x': echo "<img src=x.gif border=0>"; break;
case 'o': echo "<img src=o.gif border=0>"; break;
}
if($g00 == '_') echo "</a>";
echo "</td><td>";
//(1,0)
if($g10 == '_') echo "<a href=test.php?".editGridValues(1,0).">";
switch($g10)
{
case '_': echo "<img src=_.gif border=0>"; break;
case 'x': echo "<img src=x.gif border=0>"; break;
case 'o': echo "<img src=o.gif border=0>"; break;
}
if($g10 == '_') echo "</a>";
echo "</td><td>";
//(2,0)
if($g20 == '_') echo "<a href=test.php?".editGridValues(2,0).">";
switch($g20)
{
case '_': echo "<img src=_.gif border=0>"; break;
case 'x': echo "<img src=x.gif border=0>"; break;
case 'o': echo "<img src=o.gif border=0>"; break;
}
if($g20 == '_') echo "</a>";
echo "</td></tr><tr><td>";
//(0,1)
if($g01 == '_') echo "<a href=test.php?".editGridValues(0,1).">";
switch($g01)
{
case '_': echo "<img src=_.gif border=0>"; break;
case 'x': echo "<img src=x.gif border=0>"; break;
case 'o': echo "<img src=o.gif border=0>"; break;
}
if($g01 == '_') echo "</a>";
echo "</td><td>";
//(1,1)
if($g11 == '_') echo "<a href=test.php?".editGridValues(1,1).">";
switch($g11)
{
case '_': echo "<img src=_.gif border=0>"; break;
case 'x': echo "<img src=x.gif border=0>"; break;
case 'o': echo "<img src=o.gif border=0>"; break;
}
if($g11 == '_') echo "</a>";
echo "</td><td>";
//(2,1)
if($g21 == '_') echo "<a href=test.php?".editGridValues(2,1).">";
switch($g21)
{
case '_': echo "<img src=_.gif border=0>"; break;
case 'x': echo "<img src=x.gif border=0>"; break;
case 'o': echo "<img src=o.gif border=0>"; break;
}
if($g21 == '_') echo "</a>";
echo "</td></tr><tr><td>";
//(0,2)
if($g02 == '_') echo "<a href=test.php?".editGridValues(0,2).">";
switch($g02)
{
case '_': echo "<img src=_.gif border=0>"; break;
case 'x': echo "<img src=x.gif border=0>"; break;
case 'o': echo "<img src=o.gif border=0>"; break;
}
if($g02 == '_') echo "</a>";
echo "</td><td>";
//(1,2)
if($g12 == '_') echo "<a href=test.php?".editGridValues(1,2).">";
switch($g12)
{
case '_': echo "<img src=_.gif border=0>"; break;
case 'x': echo "<img src=x.gif border=0>"; break;
case 'o': echo "<img src=o.gif border=0>"; break;
}
if($g12 == '_') echo "</a>";
echo "</td><td>";
//(2,2)
if($g22 == '_') echo "<a href=test.php?".editGridValues(2,2).">";
switch($g22)
{
case '_': echo "<img src=_.gif border=0>"; break;
case 'x': echo "<img src=x.gif border=0>"; break;
case 'o': echo "<img src=o.gif border=0>"; break;
}
if($g22 == '_') echo "</a>";
echo "</td></tr></table>";
echo "<br><br><a href=test.php?g00=_&g10=_&g20=_&g01=_&g11=_&g21=_&g02=_&g12=_&g22=_&meFirst=1>New Game</a>";
?>