<?php

error_reporting(0 );
class Crisscross {
    public $field = [];

//отображаем поле
    function showField(){
        for ($i=1;$i<=count($this->field);$i++){
            if (($i!=0) and ($i%3===0)){
                echo "|{$this->field[$i]}|<br>";
            }
            else{
                echo "|{$this->field[$i]}";
            }
        }
    }

//получаем данные из консоли
    function getConsolCommand(){
        switch (isset($_GET["consol"])){
            case 1:
            case 2:
            case 3:
            case 4:
            case 5:
            case 6:
            case 7:
            case 8:
            case 9:
                $key = $_GET["consol"];
                if (is_string($this->field[$key])){
                    break;
                }
                else {
                    $this->field[$key] = "x";
                    $this->save2file();
                }
            //case "Выход":;
            //case "Справка":;
            //default:;
        }

    }

    //проверяем массив на пустоту
    function isBeginGame(){
        if (empty($this->field)){
            $this->field = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
        }
    }

    // сохраняем в массив файл
    function save2file(){
        $f = fopen('memory', 'w+');
        $memFile = json_encode($this->field);
        fwrite($f, $memFile);
        fclose($f);
    }

    //загружаем массив из файла
    function loadFromFile(){
        if(!is_file('memory')){
            return false;
        }
        else {
            $f = fopen('memory', "r");
            $output = fread($f, filesize('memory'));
            $this->field = json_decode($output);
            fclose($f);
        }
    }
}

$game = new Crisscross();
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<a>введите цифру</a>
<form >
    <input type = "text"  name ="consol" value = ""> <br>
    <input type="submit" value="Нажми на меня!"> <br>
</form>
<p>
    <?php
    $game->loadFromFile();
    $game->isBeginGame();
    $game->showField();
    $game->getConsolCommand();

    ?>
</p>

</body>
</html>