How to write the Tic-Tac-Toe game in PHP

1 Answer

0 votes
/*
    A simple Tic‑Tac‑Toe game written in PHP (CLI).

    Highlights:
      - The board is represented as an array of 9 characters.
      - Functions handle printing, move validation, win checking,
        and turn progression.
      - The game loop is clear and easy to follow.
      - Comments explain the reasoning behind each part of the program.
*/

//---------------------------------------------------------------
// Print the board in a 3×3 layout
//---------------------------------------------------------------
function printBoard(array $board): void {
    echo "\n";
    for ($i = 0; $i < 9; $i++) {
        echo $board[$i];
        if (($i + 1) % 3 === 0) {
            echo "\n";
        } else {
            echo " | ";
        }
    }
    echo "\n";
}

//---------------------------------------------------------------
// Check if a player has won
//---------------------------------------------------------------
function checkWin(array $board, string $player): bool {
    $wins = [
        [0,1,2], [3,4,5], [6,7,8],   // rows
        [0,3,6], [1,4,7], [2,5,8],   // columns
        [0,4,8], [2,4,6]             // diagonals
    ];

    foreach ($wins as $w) {
        if ($board[$w[0]] === $player &&
            $board[$w[1]] === $player &&
            $board[$w[2]] === $player) {
            return true;
        }
    }
    return false;
}

//---------------------------------------------------------------
// Check if the board is full (draw)
//---------------------------------------------------------------
function boardFull(array $board): bool {
    foreach ($board as $c) {
        if ($c === ' ') {
            return false;
        }
    }
    return true;
}

//---------------------------------------------------------------
// Attempt to place a move; return true if successful
//---------------------------------------------------------------
function placeMove(array &$board, int $pos, string $player): bool {
    if ($pos < 0 || $pos >= 9) {
        return false;
    }

    if ($board[$pos] !== ' ') {
        return false;
    }

    $board[$pos] = $player;
    return true;
}

//---------------------------------------------------------------
// Main game loop
//---------------------------------------------------------------
$board = array_fill(0, 9, ' ');
$currentPlayer = 'X';

echo "Tic‑Tac‑Toe\n";
printBoard($board);

while (true) {
    echo "Player $currentPlayer, enter position (0‑8): ";
    $input = trim(fgets(STDIN));

    if (!ctype_digit($input)) {
        echo "Invalid input. Try again.\n";
        continue;
    }

    $pos = (int)$input;

    // Try to place the move
    if (!placeMove($board, $pos, $currentPlayer)) {
        echo "Invalid move. Try again.\n";
        continue;
    }

    printBoard($board);

    // Check win
    if (checkWin($board, $currentPlayer)) {
        echo "Player $currentPlayer wins!\n";
        break;
    }

    // Check draw
    if (boardFull($board)) {
        echo "It's a draw!\n";
        break;
    }

    // Switch player
    $currentPlayer = ($currentPlayer === 'X') ? 'O' : 'X';
}



/*
run:

Tic‑Tac‑Toe

  |   |  
  |   |  
  |   |  

Player X, enter position (0‑8): 0

X |   |  
  |   |  
  |   |  

Player O, enter position (0‑8): 3

X |   |  
O |   |  
  |   |  

Player X, enter position (0‑8): 6

X |   |  
O |   |  
X |   |  

Player O, enter position (0‑8): 4

X |   |  
O | O |  
X |   |  

Player X, enter position (0‑8): 5

X |   |  
O | O | X
X |   |  

Player O, enter position (0‑8): 8

X |   |  
O | O | X
X |   | O

Player X, enter position (0‑8): 2

X |   | X
O | O | X
X |   | O

Player O, enter position (0‑8): 1

X | O | X
O | O | X
X |   | O

Player X, enter position (0‑8): 7

X | O | X
O | O | X
X | X | O

It's a draw!

*/

 



answered Aug 27 by avibootz
...