fork download
  1. <?php
  2.  
  3. // เริ่มต้นเกม
  4. if (!isset($_SESSION['board'])) {
  5. $_SESSION['board'] = array_fill(0, 9, '');
  6. $_SESSION['turn'] = 'X';
  7. }
  8.  
  9. // ตรวจสอบการชนะ
  10. function check_winner($board) {
  11. $winning_combinations = [
  12. [0, 1, 2],
  13. [3, 4, 5],
  14. [6, 7, 8],
  15. [0, 3, 6],
  16. [1, 4, 7],
  17. [2, 5, 8],
  18. [0, 4, 8],
  19. [2, 4, 6]
  20. ];
  21.  
  22. foreach ($winning_combinations as $combo) {
  23. if ($board[$combo[0]] && $board[$combo[0]] == $board[$combo[1]] && $board[$combo[1]] == $board[$combo[2]]) {
  24. return $board[$combo[0]];
  25. }
  26. }
  27.  
  28. return null;
  29. }
  30.  
  31. // เมื่อผู้เล่นเลือกตำแหน่ง
  32. if (isset($_POST['position']) && $_SESSION['board'][$_POST['position']] == '') {
  33. $_SESSION['board'][$_POST['position']] = $_SESSION['turn'];
  34. $_SESSION['turn'] = $_SESSION['turn'] == 'X' ? 'O' : 'X';
  35. }
  36.  
  37. // ตรวจสอบผู้ชนะ
  38. $winner = check_winner($_SESSION['board']);
  39. ?>
  40.  
  41. <!DOCTYPE html>
  42. <html lang="th">
  43. <head>
  44. <meta charset="UTF-8">
  45. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  46. <title>เกม XO</title>
  47. <style>
  48. .board { display: grid; grid-template-columns: repeat(3, 100px); gap: 5px; }
  49. .cell { width: 100px; height: 100px; text-align: center; line-height: 100px; border: 1px solid black; font-size: 24px; cursor: pointer; }
  50. .winner { color: green; font-size: 20px; }
  51. </style>
  52. </head>
  53. <body>
  54. <h1>เกม XO</h1>
  55. <?php if ($winner): ?>
  56. <p class="winner">ผู้ชนะคือ: <?php echo $winner; ?></p>
  57. <form method="post">
  58. <button type="submit" name="reset" value="true">เริ่มเกมใหม่</button>
  59. </form>
  60. <?php else: ?>
  61. <p>ผลหมุนเวียน: <?php echo $_SESSION['turn']; ?></p>
  62. <?php endif; ?>
  63.  
  64. <div class="board">
  65. <?php foreach ($_SESSION['board'] as $index => $value): ?>
  66. <form method="post" style="margin: 0;">
  67. <button type="submit" name="position" value="<?php echo $index; ?>" class="cell">
  68. <?php echo $value; ?>
  69. </button>
  70. </form>
  71. <?php endforeach; ?>
  72. </div>
  73.  
  74. <?php
  75. // รีเซ็ตเกม
  76. if (isset($_POST['reset'])) {
  77. header("Location: index.php");
  78. }
  79. ?>
  80. </body>
  81. </html>
Success #stdin #stdout 0.02s 25952KB
stdin
Standard input is empty
stdout
<!DOCTYPE html>
<html lang="th">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>เกม XO</title>
    <style>
        .board { display: grid; grid-template-columns: repeat(3, 100px); gap: 5px; }
        .cell { width: 100px; height: 100px; text-align: center; line-height: 100px; border: 1px solid black; font-size: 24px; cursor: pointer; }
        .winner { color: green; font-size: 20px; }
    </style>
</head>
<body>
    <h1>เกม XO</h1>
            <p>ผลหมุนเวียน: X</p>
    
    <div class="board">
                    <form method="post" style="margin: 0;">
                <button type="submit" name="position" value="0" class="cell">
                                    </button>
            </form>
                    <form method="post" style="margin: 0;">
                <button type="submit" name="position" value="1" class="cell">
                                    </button>
            </form>
                    <form method="post" style="margin: 0;">
                <button type="submit" name="position" value="2" class="cell">
                                    </button>
            </form>
                    <form method="post" style="margin: 0;">
                <button type="submit" name="position" value="3" class="cell">
                                    </button>
            </form>
                    <form method="post" style="margin: 0;">
                <button type="submit" name="position" value="4" class="cell">
                                    </button>
            </form>
                    <form method="post" style="margin: 0;">
                <button type="submit" name="position" value="5" class="cell">
                                    </button>
            </form>
                    <form method="post" style="margin: 0;">
                <button type="submit" name="position" value="6" class="cell">
                                    </button>
            </form>
                    <form method="post" style="margin: 0;">
                <button type="submit" name="position" value="7" class="cell">
                                    </button>
            </form>
                    <form method="post" style="margin: 0;">
                <button type="submit" name="position" value="8" class="cell">
                                    </button>
            </form>
            </div>

    </body>
</html>