Exscale - Four-In-A-Row 1.0
Player O, your turn
V V V V V V V . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Restart
Source Code
<?php
function getindex($field = array(), $col)
{
if ( $col > 6 )
{
$col = 6;
}
for ( $y = 0; $y < 6; $y++ )
{
if ( $field[ $y * 7 + $col ] != 0 )
{
break;
}
}
$y--;
if ( $y > -1 )
{
return $y * 7 + $col;
}
else
{
return false;
}
}
function draw($field = array(), $finnished = false)
{
$bricks = array(" <span style=\"color: gray;\">.</span> ", " <span style=\"color: red;\">X</span> ", " <span style=\"color: green;\">O</span> ");
echo "<div id=\"field\">";
if ( !$finnished )
{
echo ( getindex($field, 0) !== false ) ? " <a href=\"?r=0\">V</a> " : " V ";
echo ( getindex($field, 1) !== false ) ? " <a href=\"?r=1\">V</a> " : " V ";
echo ( getindex($field, 2) !== false ) ? " <a href=\"?r=2\">V</a> " : " V ";
echo ( getindex($field, 3) !== false ) ? " <a href=\"?r=3\">V</a> " : " V ";
echo ( getindex($field, 4) !== false ) ? " <a href=\"?r=4\">V</a> " : " V ";
echo ( getindex($field, 5) !== false ) ? " <a href=\"?r=5\">V</a> " : " V ";
echo ( getindex($field, 6) !== false ) ? " <a href=\"?r=6\">V</a> " : " V ";
echo "<br />";
}
for ( $y = 0; $y < 6; $y++ )
{
for ( $x = 0; $x < 7; $x++ )
{
echo $bricks[ $field[ $y * 7 + $x ] ];
}
echo "<br />";
}
echo "</div>";
}
function check($field = array())
{
for ( $y = 0; $y < 6; $y++ )
{
for ( $x = 0; $x < 7; $x++ )
{
if ( ( $field[ $y * 7 + $x ] != 0 ) and ( $win = _check($field, $x, $y) ) )
{
return $win;
}
}
}
return false;
}
function _check($field = array(), $x, $y)
{
$brick = $field[ $y * 7 + $x ];
// up
if ( $y > 2 and
$field[ ($y-1) * 7 + $x ] == $brick and
$field[ ($y-2) * 7 + $x ] == $brick and
$field[ ($y-3) * 7 + $x ] == $brick )
{
return $brick;
}
// down
elseif ($y < 3 and
$field[ ($y+1) * 7 + $x ] == $brick and
$field[ ($y+2) * 7 + $x ] == $brick and
$field[ ($y+3) * 7 + $x ] == $brick )
{
return $brick;
}
// right
elseif ($x < 4 and
$field[ $y * 7 + ($x+1) ] == $brick and
$field[ $y * 7 + ($x+2) ] == $brick and
$field[ $y * 7 + ($x+3) ] == $brick )
{
return $brick;
}
// left
elseif ($x > 2 and
$field[ $y * 7 + ($x-1) ] == $brick and
$field[ $y * 7 + ($x-2) ] == $brick and
$field[ $y * 7 + ($x-3) ] == $brick )
{
return $brick;
}
// right-up
elseif ($x < 4 and $y > 2 and
$field[ ($y-1) * 7 + ($x+1) ] == $brick and
$field[ ($y-2) * 7 + ($x+2) ] == $brick and
$field[ ($y-3) * 7 + ($x+3) ] == $brick )
{
return $brick;
}
// left-up
elseif ($x > 2 and $y > 2 and
$field[ ($y-1) * 7 + ($x-1) ] == $brick and
$field[ ($y-2) * 7 + ($x-2) ] == $brick and
$field[ ($y-3) * 7 + ($x-3) ] == $brick )
{
return $brick;
}
// right-down
elseif ($x < 4 and $y < 3 and
$field[ ($y+1) * 7 + ($x+1) ] == $brick and
$field[ ($y+2) * 7 + ($x+2) ] == $brick and
$field[ ($y+3) * 7 + ($x+3) ] == $brick )
{
return $brick;
}
// left-down
elseif ($x > 2 and $y < 3 and
$field[ ($y+1) * 7 + ($x-1) ] == $brick and
$field[ ($y+2) * 7 + ($x-2) ] == $brick and
$field[ ($y+3) * 7 + ($x-3) ] == $brick )
{
return $brick;
}
else
{
return false;
}
}
################################################################################
session_start();
if ( !isset($_SESSION['fiar_field']) )
{
$_SESSION['fiar_field'] = array(0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0);
}
if ( !isset($_SESSION['fiar_player']) )
{
$_SESSION['fiar_player'] = 1;
}
if ( isset($_GET['q']) )
{
session_destroy();
header("location: ?restarted");
}
if ( isset($_GET['r']) and ( ( $i = getindex($_SESSION['fiar_field'], $_GET['r']) ) !== false ) )
{
$_SESSION['fiar_field'][$i] = $_SESSION['fiar_player'];
}
$_SESSION['fiar_player'] = ( $_SESSION['fiar_player'] == 2 ) ? 1 : 2;
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Language" content="sv">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<meta name="author" content="Andreas Lagerkvist">
<meta name="copyright" content="Andreas Lagerkvist">
<title>Exscale - Four-In-A-Row 1.0</title>
<style type="text/css">
body {
font-family: Verdana;
font-size: 14px;
}
h1 {
font-family: Arial;
font-size: 26px;
}
h2 {
font-family: Arial;
font-size: 22px;
}
#game, #code {
width: 500px;
margin: 0 auto;
}
#game {
text-align: center;
}
#field {
background-color: #fdfdfd;
width: 450px;
padding: 10px;
font-family: Fixedsys, Lucida Console;
font-size: 44px;
border: 20px solid #111;
text-align: center;
}
</style>
</head>
<body>
<div id="game">
<h1>Exscale - Four-In-A-Row 1.0</h1>
<?php
if ( $winner = check($_SESSION['fiar_field']) )
{
echo "<h2>Player " .( ( $winner == 1 ) ? "<span style=\"color: red;\">X</span>" : "<span style=\"color: green;\">O</span>" ) ." WINS!!</h2>";
draw($_SESSION['fiar_field'], true);
}
else
{
echo "<h2>Player " .( ( $_SESSION['fiar_player'] == 1 ) ? "<span style=\"color: red;\">X</span>" : "<span style=\"color: green;\">O</span>" ) .", your turn</h2>";
draw($_SESSION['fiar_field']);
}
?>
<br />
<a href="?q">Restart</a>
</div>
<div id="code">
<h1>Source Code</h1>
<?php highlight_file(__FILE__); ?>
</div>
</body>
</html>