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 )
        {
            
$col 6;
        }
        for ( 
$y 0$y 6$y++ )
        {
            if ( 
$field$y $col ] != )
            {
                break;
            }
        }
        
$y--;
        if ( 
$y > -)
        {
            return 
$y $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($field0) !== false ) ? " <a href=\"?r=0\">V</a> " " V ";
            echo ( 
getindex($field1) !== false ) ? " <a href=\"?r=1\">V</a> " " V ";
            echo ( 
getindex($field2) !== false ) ? " <a href=\"?r=2\">V</a> " " V ";
            echo ( 
getindex($field3) !== false ) ? " <a href=\"?r=3\">V</a> " " V ";
            echo ( 
getindex($field4) !== false ) ? " <a href=\"?r=4\">V</a> " " V ";
            echo ( 
getindex($field5) !== false ) ? " <a href=\"?r=5\">V</a> " " V ";
            echo ( 
getindex($field6) !== 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 $x ] ];
            }
            echo 
"<br />";
        }

        echo 
"</div>";
    }

    function 
check($field = array())
    {
        for ( 
$y 0$y 6$y++ )
        {
            for ( 
$x 0$x 7$x++ )
            {
                if ( ( 
$field$y $x ] != ) and ( $win _check($field$x$y) ) )
                {
                    return 
$win;
                }
            }
        }
        return 
false;
    }

    function 
_check($field = array(), $x$y)
    {
        
$brick $field$y $x ];

        
// up
        
if (    $y and 
                
$field[ ($y-1) * $x ] == $brick and 
                
$field[ ($y-2) * $x ] == $brick and 
                
$field[ ($y-3) * $x ] == $brick )
        {
            return 
$brick;
        }
        
// down
        
elseif ($y and 
                
$field[ ($y+1) * $x ] == $brick and 
                
$field[ ($y+2) * $x ] == $brick and 
                
$field[ ($y+3) * $x ] == $brick )
        {
            return 
$brick;
        }

        
// right
        
elseif ($x and 
                
$field$y + ($x+1) ] == $brick and 
                
$field$y + ($x+2) ] == $brick and 
                
$field$y + ($x+3) ] == $brick )
        {
            return 
$brick;
        }

        
// left
        
elseif ($x and 
                
$field$y + ($x-1) ] == $brick and 
                
$field$y + ($x-2) ] == $brick and 
                
$field$y + ($x-3) ] == $brick )
        {
            return 
$brick;
        }

        
// right-up
        
elseif ($x and $y and 
                
$field[ ($y-1) * + ($x+1) ] == $brick and 
                
$field[ ($y-2) * + ($x+2) ] == $brick and 
                
$field[ ($y-3) * + ($x+3) ] == $brick )
        {
            return 
$brick;
        }

        
// left-up
        
elseif ($x and $y and 
                
$field[ ($y-1) * + ($x-1) ] == $brick and 
                
$field[ ($y-2) * + ($x-2) ] == $brick and 
                
$field[ ($y-3) * + ($x-3) ] == $brick )
        {
            return 
$brick;
        }

        
// right-down
        
elseif ($x and $y and 
                
$field[ ($y+1) * + ($x+1) ] == $brick and 
                
$field[ ($y+2) * + ($x+2) ] == $brick and 
                
$field[ ($y+3) * + ($x+3) ] == $brick )
        {
            return 
$brick;
        }

        
// left-down
        
elseif ($x and $y and 
                
$field[ ($y+1) * + ($x-1) ] == $brick and 
                
$field[ ($y+2) * + ($x-2) ] == $brick and 
                
$field[ ($y+3) * + ($x-3) ] == $brick )
        {
            return 
$brick;
        }
        else
        {
            return 
false;
        }
    }

    
################################################################################

    
session_start();

    if ( !isset(
$_SESSION['fiar_field']) )
    {
        
$_SESSION['fiar_field'] = array(0000000
                                        
0000000
                                        
0000000
                                        
0000000
                                        
0000000
                                        
0000000);
    }
    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;
?>
<!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 == ) ? "<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'] == ) ? "<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>