<?
session_start();

//First game
if (!isset($_SESSION['field'])) {
    newGame(9, 10);
}

//Functions

function newGame($fieldSize, $mineAmount)
{
    $_SESSION['smiley']     = 'smile';
    $_SESSION['fieldSize']  = $fieldSize;
    $_SESSION['mineAmount'] = $mineAmount;
    $_SESSION['mineCount']  = $mineAmount;
    $_SESSION['field']      = array();

    createField();
    placeMines();
    getValues();
}

function createField()
{
    for ($y = 1; $y <= $_SESSION['fieldSize']; $y++) {
        for ($x = 1; $x <= $_SESSION['fieldSize']; $x++) {
            $_SESSION['field'][$x . ',' . $y] = array(
                'value'   => '',
                'visible' => 'closed',
                'counted' => '0',
                'flag'    => '0',
                'mark'    => '0'
            );
        }
    }
    return;
}

function placeMines()
{
    $i = 1;
    while ($i <= $_SESSION['mineAmount']) {
        $randX = rand(1, $_SESSION['fieldSize']);
        $randY = rand(1, $_SESSION['fieldSize']);
        if ($_SESSION['field'][$randX . ',' . $randY]['value'] != 'mine') {
            $i++;
            $_SESSION['field'][$randX . ',' . $randY]['value'] = 'mine';
        }
    }
    return;
}

function getValues()
{
    for ($y = 1; $y <= $_SESSION['fieldSize']; $y++) {
        for ($x = 1; $x <= $_SESSION['fieldSize']; $x++) {
            if ($_SESSION['field'][$x . ',' . $y]['value'] != "mine") {
                countBlock($x, $y);
                $_SESSION['field'][$x . ',' . $y]['value'] = "num" . $_SESSION['value'];
            }
        }
    }
    return;
}

function countBlock($x, $y)
{
    $_SESSION['value'] = 0;
    for ($ty = $y - 1; $ty <= $y + 1; $ty++) {
        for ($tx = $x - 1; $tx <= $x + 1; $tx++) {
            if ($_SESSION['field'][$tx . ',' . $ty]['value'] == 'mine') {
                $_SESSION['value']++;
            }

        }
    }
    return;
}

function countForZero($x, $y)
{
    $_SESSION['field'][$x . ',' . $y]['counted'] = 1;
    for ($ty = $y - 1; $ty <= $y + 1; $ty++) {
        for ($tx = $x - 1; $tx <= $x + 1; $tx++) {

            if ($_SESSION['field'][$tx . ',' . $ty]['value'] == 'num0' && $_SESSION['field'][$tx . ',' . $ty]['counted'] != 1) {
                countForZero($tx, $ty);
            } elseif ($_SESSION['field'][$tx . ',' . $ty]['flag'] != 'flag' && isset($_SESSION['field'][$tx . ',' . $ty])) {
                $_SESSION['field'][$tx . ',' . $ty]['counted'] = 1;
                $_SESSION['field'][$tx . ',' . $ty]['visible'] = 1;
            }

        }
    }
    return;
}

function countForWin()
{
    for ($y = 1; $y <= $_SESSION['fieldSize']; $y++) {
        for ($x = 1; $x <= $_SESSION['fieldSize']; $x++) {
            if ($_SESSION['field'][$x . ',' . $y]['visible'] == 'closed') {
                $closedCount++;
            }
        }

    }
    if ($closedCount == $_SESSION['mineAmount']) {
        $_SESSION['smiley'] = 'shades';
        for ($y = 1; $y <= $_SESSION['fieldSize']; $y++) {
            for ($x = 1; $x <= $_SESSION['fieldSize']; $x++) {
                if ($_SESSION['field'][$x . ',' . $y]['value'] == 'mine') {
                    $_SESSION['field'][$x . ',' . $y]['flag'] = 'flag';
                }
            }

        }
    }
    return;
}

// POST
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    //Post right-click
    if (!empty($_POST['flag'])) {
        $explode = explode(",", $_POST['flag']);
        $x       = $explode[0];
        $y       = $explode[1];
        if ($_SESSION['field'][$x . ',' . $y]['visible'] == "closed") {
            if ($_SESSION['field'][$x . ',' . $y]['flag'] == "0" && $_SESSION['field'][$x . ',' . $y]['mark'] == "0") {
                if ($_SESSION['field'][$x . ',' . $y]['value'] == 'mine') {
                    $_SESSION['field'][$x . ',' . $y]['flagcorrect'] = "1";
                }

                $_SESSION['field'][$x . ',' . $y]['flag'] = "flag";
                $_SESSION['mineCount']--;
            } elseif ($_SESSION['field'][$x . ',' . $y]['flag'] == "flag") {
                $_SESSION['field'][$x . ',' . $y]['flag'] = "0";
                $_SESSION['field'][$x . ',' . $y]['flagcorrect'] = "0";
                $_SESSION['field'][$x . ',' . $y]['mark'] = "1";
                $_SESSION['mineCount']++;
            } elseif ($_SESSION['field'][$x . ',' . $y]['mark'] == "1") {
                $_SESSION['field'][$x . ',' . $y]['mark'] = "0";
            }                
        }
    }

    //Post left-click
    if (!empty($_POST['square'])) {
        $explode = explode(",", $_POST['square']);
        $x       = $explode[0];
        $y       = $explode[1];
        if ($_SESSION['field'][$x . ',' . $y]['value'] == 'num0' && $_SESSION['field'][$x . ',' . $y]['visible'] == "closed" && $_SESSION['field'][$x . ',' . $y]['flag'] == "0") {
            //If zero, open field
            countForZero($x, $y);
            countForWin();
        } elseif ($_SESSION['field'][$x . ',' . $y]['value'] == 'mine' && $_SESSION['field'][$x . ',' . $y]['visible'] == "closed") {
            // If mine, game over
            $_SESSION['smiley']                          = 'dead';
            $_SESSION['field'][$x . ',' . $y]['value']   = 'redmine';
            $_SESSION['field'][$x . ',' . $y]['visible'] = "1";

            for ($y = 1; $y <= $_SESSION['fieldSize']; $y++) {
                for ($x = 1; $x <= $_SESSION['fieldSize']; $x++) {
                    if ($_SESSION['field'][$x . ',' . $y]['flagcorrect'] == "1") {
                        $_SESSION['field'][$x . ',' . $y]['visible'] = "1";
                        $_SESSION['field'][$x . ',' . $y]['value']   = "mineflag";
                    } else if ($_SESSION['field'][$x . ',' . $y]['value'] == 'mine') {
                        $_SESSION['field'][$x . ',' . $y]['visible'] = "1";
                    } elseif ($_SESSION['field'][$x . ',' . $y]['flag'] == 'flag') {
                        $_SESSION['field'][$x . ',' . $y]['visible'] = "1";
                        $_SESSION['field'][$x . ',' . $y]['value']   = "wrong";
                    }
                }
            }
        } elseif ($_SESSION['field'][$x . ',' . $y]['visible'] == "closed" && $_SESSION['field'][$x . ',' . $y]['flag'] == "0") {
            $_SESSION['field'][$x . ',' . $y]['visible'] = "1";
            //$_SESSION['field'][$x . ',' . $y]['counted'] = 1;
            countForWin();
        }

    }
    if (!empty($_POST['newgame'])) {
        newGame($_POST['fieldSize'], $_POST['mineAmount']);
    }

}

//Debugging
//print_r($_SESSION['field']["1,1"]);
//echo $_SESSION['field'];

?>

<!DOCTYPE html>
<html>
    <head>
        <link rel="stylesheet" type="text/css" href="style.php">
        <meta content="width=device-width, initial-scale=1" name="viewport">

        <title>Minesweeper</title>
    </head>
    <body>
        <div class="wrapper">
            <div class="infobar">
                <div class="third"><?echo $_SESSION['mineCount']; ?></div>
                <div class="third"><img id="smiley" src="img/button<?echo $_SESSION['smiley']; ?>.png"/></div>
                <div class="third" id="time"></div>
            </div>
        <div id="game">


            <?

for ($y = 1; $y <= $_SESSION['fieldSize']; $y++) {
    echo "<div class='game-row'>";
    for ($x = 1; $x <= $_SESSION['fieldSize']; $x++) {
        if ($_SESSION['field'][$x . ',' . $y]['visible'] == '1') {
            echo "<img src='img/" . $_SESSION['field'][$x . ',' . $y]['value'] . ".png' id='" . $x . ',' . $y . "'/>";
        } elseif ($_SESSION['field'][$x . ',' . $y]['flag'] == 'flag') {
            echo "<img src='img/flag.png' id='" . $x . ',' . $y . "' />";
        } elseif ($_SESSION['field'][$x . ',' . $y]['mark'] == "1") {
            echo "<img src='img/mark.png' id='" . $x . ',' . $y . "' />";
        } elseif ($_SESSION['field'][$x . ',' . $y]['visible'] == 'closed') {
            echo "<img src='img/closed.png' id='" . $x . ',' . $y . "' />";
        }

    }
    echo "</div>";
}
?>
        </div>
        <div id="options-row">
            <div class="half">
                <img src="img/size.png"/><input type='number' min="1" max="24" id='fieldSize' value='<?echo $_SESSION['fieldSize']; ?>' >
            </div>
            <div class="half">
                <img src="img/mine-transparant.png"/><input type='number' min="1" max="288" id='mineAmount' value='<?echo $_SESSION['mineAmount']; ?>'>
            </div>
        </div>
    </div>
    <script type="text/javascript" src="main.js"></script>
</body>
</html>