fork download
  1. <?php
  2.  
  3. class Crisscross {
  4. public $field = [];
  5.  
  6. //отображаем поле
  7. function showField(){
  8. for ($i=1;$i<=count($this->field);$i++){
  9. if (($i!=0) and ($i%3===0)){
  10. echo "|{$this->field[$i]}|<br>";
  11. }
  12. else{
  13. echo "|{$this->field[$i]}";
  14. }
  15. }
  16. }
  17.  
  18. //получаем данные из консоли
  19. function getConsolCommand(){
  20. switch (isset($_GET["consol"])){
  21. case 1:
  22. case 2:
  23. case 3:
  24. case 4:
  25. case 5:
  26. case 6:
  27. case 7:
  28. case 8:
  29. case 9:
  30. $key = $_GET["consol"];
  31. if (is_string($this->field[$key])){
  32. break;
  33. }
  34. else {
  35. $this->field[$key] = "x";
  36. $this->save2file();
  37. }
  38. //case "Выход":;
  39. //case "Справка":;
  40. //default:;
  41. }
  42.  
  43. }
  44.  
  45. //проверяем массив на пустоту
  46. function isBeginGame(){
  47. if (empty($this->field)){
  48. $this->field = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
  49. }
  50. }
  51.  
  52. // сохраняем в массив файл
  53. function save2file(){
  54. $f = fopen('memory', 'w+');
  55. $memFile = json_encode($this->field);
  56. fwrite($f, $memFile);
  57. fclose($f);
  58. }
  59.  
  60. //загружаем массив из файла
  61. function loadFromFile(){
  62. if(!is_file('memory')){
  63. return false;
  64. }
  65. else {
  66. $f = fopen('memory', "r");
  67. $output = fread($f, filesize('memory'));
  68. $this->field = json_decode($output);
  69. fclose($f);
  70. }
  71. }
  72. }
  73.  
  74. $game = new Crisscross();
  75. ?>
  76. <!DOCTYPE html>
  77. <html lang="en">
  78. <head>
  79. <meta charset="UTF-8">
  80. <title>Title</title>
  81. </head>
  82. <body>
  83. <a>введите цифру</a>
  84. <form >
  85. <input type = "text" name ="consol" value = ""> <br>
  86. <input type="submit" value="Нажми на меня!"> <br>
  87. </form>
  88. <p>
  89. <?php
  90. $game->loadFromFile();
  91. $game->isBeginGame();
  92. $game->showField();
  93. $game->getConsolCommand();
  94.  
  95. ?>
  96. </p>
  97.  
  98. </body>
  99. </html>
Success #stdin #stdout 0s 82560KB
stdin
2
stdout
<!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>
    |1|2|3|<br>|4|5|6|<br>|7|8|9|<br>|</p>

</body>
</html>