Tic Tac Toe game in PHP
How to Play Tic Tac Toe Game
- Input the game participant/contestant/players names to play and click start.
- The software will show the game participant/contestant/players’ names for each participant/contestant/player’s turn. The participant/contestant/player’s move will be submitted by pressing the Play Button on the interface.
- The first participant/contestant/player to align their marks horizontally, vertically, or diagonally will be the champion of the game.
Functional requirements of Tic Tac Toe Game
The game Participant/contestant/players Choosing Side Form/Module
- Game Module
- The participant/contestant/players Score Board Module
participant contestant
Screenshot of output is given below for your general understandings.
Tic Tac Toe Game using PHP
index.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
<?php require_once "./templates/header.php"; ?> <form method="post" action="register-players.php"> <div class="welcome"> <h1>Start playing Tic Tac Toe!</h1> <h2>Please fill in your names</h2> <div class="p-name"> <label for="player-x"> Player (First)</label> <input type="text" id="player-x" name="player-x" required /> </div> <div class="p-name"> <label for="player-o"> Player (Sceond)</label> <input type="text" id="player-o" name="player-o" required /> </div> <button type="submit">Start</button> </div> </form> <?php require_once "./templates/footer.php"; |
play.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
<?php require_once "templates/header.php"; if (! playersRegistered()) { header("location: index.php"); } if ($_POST['cell']) { $win = play($_POST['cell']); if ($win) { header("location: result.php?player=" . getTurn()); } } if (playsCount() >= 9) { header("location: result.php"); } ?> <h2><?php echo currentPlayer() ?>'s turn</h2> <form method="post" action="play.php"> <table class="tic-tac-toe" cellpadding="0" cellspacing="0"> <tbody> <?php $lastRow = 0; for ($i = 1; $i <= 9; $i++) { $row = ceil($i / 3); if ($row !== $lastRow) { $lastRow = $row; if ($i > 1) { echo "</tr>"; } echo "<tr class='row-{$row}'>"; } $additionalClass = ''; if ($i == 2 || $i == 8) { $additionalClass = 'vertical-border'; } else if ($i == 4 || $i == 6) { $additionalClass = 'horizontal-border'; } else if ($i == 5) { $additionalClass = 'center-border'; } ?> <td class="cell-<?= $i ?> <?= $additionalClass ?>"> <?php if (getCell($i) === 'x'): ?> X <?php elseif (getCell($i) === 'o'): ?> O <?php else: ?> <input type="radio" name="cell" value="<?= $i ?>" onclick="enableButton()"/> <?php endif; ?> </td> <?php } ?> </tr> </tbody> </table> <button type="submit" disabled id="play-btn">Play</button> </form> <script type="text/javascript"> function enableButton() { document.getElementById('play-btn').disabled = false; } </script> <?php require_once "templates/footer.php"; |
register-players.php
1 2 3 4 5 6 7 8 9 |
<?php require_once "functions.php"; registerPlayers($_POST['player-x'], $_POST['player-o']); if (playersRegistered()) { header("location: play.php"); } |
result.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
<?php require_once "templates/header.php"; if (! playersRegistered()) { header("location: index.php"); } resetBoard(); ?> <table class="wrapper" cellpadding="0" cellspacing="0"> <tr> <td> <div class="welcome"> <h1> <?php if ($_GET['player']) { echo currentPlayer() . " won!"; } else { echo "It's a tie!"; } ?> </h1> <div class="player-name"> <?php echo playerName('x')?>'s score: <b><?php echo score('x')?></b> </div> <div class="player-name"> <?php echo playerName('o')?>' score: <b><?php echo score('o')?></b> </div> <a href="play.php">Play again</a><br /> <a href="index.php" class="reset-btn">Reset</a> </div> </td> </tr> </table> </body> </html> |
functions.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
<?php session_start(); error_reporting(E_ERROR | E_PARSE); function registerPlayers($playerX="", $playerO="") { $_SESSION['PLAYER_X_NAME'] = $playerX; $_SESSION['PLAYER_O_NAME'] = $playerO; setTurn('x'); resetBoard(); resetWins(); } function resetBoard() { resetPlaysCount(); for ( $i = 1; $i <= 9; $i++ ) { unset($_SESSION['CELL_' . $i]); } } function resetWins() { $_SESSION['PLAYER_X_WINS'] = 0; $_SESSION['PLAYER_O_WINS'] = 0; } function playsCount() { return $_SESSION['PLAYS'] ? $_SESSION['PLAYS'] : 0; } function addPlaysCount() { if (! $_SESSION['PLAYS']) { $_SESSION['PLAYS'] = 0; } $_SESSION['PLAYS']++; } function resetPlaysCount() { $_SESSION['PLAYS'] = 0; } function playerName($player='x') { return $_SESSION['PLAYER_' . strtoupper($player) . '_NAME']; } function playersRegistered() { return $_SESSION['PLAYER_X_NAME'] && $_SESSION['PLAYER_O_NAME']; } function setTurn($turn='x') { $_SESSION['TURN'] = $turn; } function getTurn() { return $_SESSION['TURN'] ? $_SESSION['TURN'] : 'x'; } function markWin($player='x') { $_SESSION['PLAYER_' . strtoupper($player)
. '_WINS']++; } function switchTurn() { switch (getTurn()) { case 'x': setTurn('o'); break; default: setTurn('x'); break; } } function currentPlayer() { return playerName(getTurn()); } function play($cell='') { if (getCell($cell)) { return false; } $_SESSION['CELL_' . $cell] = getTurn(); addPlaysCount(); $win = playerPlayWin($cell); if (! $win) { switchTurn(); } else { markWin(getTurn()); resetBoard(); } return $win; } function getCell($cell='') { return $_SESSION['CELL_' . $cell]; } function playerPlayWin($cell=1) { if (playsCount() < 3) { return false; } $column = $cell % 3; if (! $column) { $column = 3; } $row = ceil($cell / 3); $player = getTurn(); return isVerticalWin($column, $player) || isHorizontalWin($row, $player) || isDiagonalWin($player); } function isVerticalWin($column=1, $turn='x') { return getCell($column) == $turn && getCell($column + 3) == $turn && getCell($column + 6) == $turn; } function isHorizontalWin($row=1, $turn='x') { return getCell($row) == $turn && getCell($row + 1) == $turn && getCell($row + 2) == $turn; } function isDiagonalWin($turn='x') { $win = getCell(1) == $turn && getCell(9) == $turn; if (! $win) { $win = getCell(3) == $turn && getCell(7) == $turn; } return $win && getCell(5) == $turn; } function score($player='x') { $score = $_SESSION['PLAYER_' . strtoupper($player) . '_WINS']; return $score ? $score : 0; } |
Download Tic Tac Toe game in PHP