Possible Solution :

here’s a PHP implementation of the classic game “Tic Tac Toe”:

function ticTacToe() {
  // Initialize the board
  $board = array_fill(0, 3, array_fill(0, 3, ' '));

  // Initialize the player
  $player = 'X';

  // Loop until the game is over
  while (true) {
    // Display the board
    echo "   1   2   3\n";
    echo "1  {$board[0][0]} | {$board[0][1]} | {$board[0][2]} \n";
    echo "  ---+---+---\n";
    echo "2  {$board[1][0]} | {$board[1][1]} | {$board[1][2]} \n";
    echo "  ---+---+---\n";
    echo "3  {$board[2][0]} | {$board[2][1]} | {$board[2][2]} \n";

    // Prompt the player for a move
    echo "Player $player's turn.\n";
    $row = readline("Enter row (1-3): ");
    $col = readline("Enter column (1-3): ");

    // Validate the move
    if ($row < 1 || $row > 3 || $col < 1 || $col > 3 || $board[$row - 1][$col - 1] != ' ') {
      echo "Invalid move. Please try again.\n";
      continue;
    }

    // Make the move
    $board[$row - 1][$col - 1] = $player;

    // Check if the game is over
    if (checkWin($board, $player)) {
      echo "Player $player wins!\n";
      break;
    } elseif (checkTie($board)) {
      echo "It's a tie!\n";
      break;
    }

    // Switch to the other player
    $player = $player == 'X' ? 'O' : 'X';
  }
}

function checkWin($board, $player) {
  // Check rows
  for ($i = 0; $i < 3; $i++) {
    if ($board[$i][0] == $player && $board[$i][1] == $player && $board[$i][2] == $player) {
      return true;
    }
  }

  // Check columns
  for ($j = 0; $j < 3; $j++) {
    if ($board[0][$j] == $player && $board[1][$j] == $player && $board[2][$j] == $player) {
      return true;
    }
  }

  // Check diagonals
  if ($board[0][0] == $player && $board[1][1] == $player && $board[2][2] == $player) {
    return true;
  }

  if ($board[0][2] == $player && $board[1][1] == $player && $board[2][0] == $player) {
    return true;
  }

  return false;
}

function checkTie($board) {
  foreach ($board as $row) {
    if (in_array(' ', $row)) {
      return false;
    }
  }

  return true;
}

The ticTacToe() function starts by initializing the board as a 3×3 array of spaces. It then sets the current player to ‘X’ and enters a loop that continues.

This is a sample implementation of the classic Tic Tac Toe game. It has three main functions:

  1. ticTacToe(): This function is the main game loop. It initializes the game board as a 3×3 array of spaces (‘ ‘), sets the starting player as ‘X’, and then loops until the game is over. In each iteration of the loop, it displays the game board, prompts the current player for their move (row and column), validates the move, makes the move, checks if the game is over, and then switches to the other player if the game is not over.
  2. checkWin($board, $player): This function checks if the specified player has won the game. It checks for winning rows, columns, and diagonals by looping over the game board array and comparing the values to the specified player.
  3. checkTie($board): This function checks if the game is tied. It does this by checking if there are any spaces left on the game board (i.e., if any rows contain a space ‘ ‘).

The game is played by calling the ticTacToe() function.

LEAVE A REPLY

Please enter your comment!
Please enter your name here