Practice bit manipulation with Tic Tac Toe and PHP


Tic Tac Toe is a fun game that you’ve probably played before. But did you know there are secret codes hidden inside the game? In this post, we’ll uncover these secret codes and learn how they can help you become a Tic Tac Toe master!

Understanding Secret Codes:

Before we dive into Tic Tac Toe, let’s talk about secret codes. These codes are like special tricks that help us do cool things with numbers. Imagine you have a bunch of switches that can turn on and off. Secret codes let us flip these switches in clever ways to make magic happen!

Playing Tic Tac Toe with Secret Codes:

Now, let’s see how secret codes can help us play Tic Tac Toe. We’ll use special numbers to represent the game board. Each number has a bunch of switches that show whether a square is X, O, or empty.

  1. Setting Up the Game: First, we create a magic box called “Tic Tac Toe” that knows all our secret codes:

   class TicTacToe {
       private $playerX;
       private $playerO;

       public function __construct() {
           // Initialize secret codes for X and O players
           $this->playerX = 0b0;
           $this->playerO = 0b0;

       // Other magic tricks will go here!
  1. Making Moves: Let’s learn a magic spell called playMove(). This spell lets us make moves on the game board. We say which player we are (X or O) and which square we want to play in:
   public function playMove($player, $row, $col) {
       // Calculate the secret number for the square
       $position = $row * 3 + $col;

       // Make sure the square isn't already taken
       if ((($this->playerX >> $position) & 1) || (($this->playerO >> $position) & 1)) {
           echo "Oops, that square is taken!\n";

       // Use our magic spell to claim the square
       if ($player === 'X') {
           $this->playerX |= 1 << $position;
       } elseif ($player === 'O') {
           $this->playerO |= 1 << $position;
       } else {
           echo "Huh? Who are you?\n";

       // Show the magic board after our move
  1. Showing the Magic Board: Finally, we need a way to show the magic board after each move. Here’s our magic spell called showBoard():
   public function showBoard() {
       // Show the magic board
       echo "  0 1 2\n";
       for ($i = 0; $i < 3; $i++) {
           echo $i . " ";
           for ($j = 0; $j < 3; $j++) {
               $position = $i * 3 + $j;
               if (($this->playerX >> $position) & 1) {
                   echo 'X'; // Show X's magic spell
               } elseif (($this->playerO >> $position) & 1) {
                   echo 'O'; // Show O's magic spell
               } else {
                   echo '-'; // Show empty square
               echo " ";
           echo "\n";
       echo "\n";
  1. Let’s Play!: Now, let’s use our magic box to play Tic Tac Toe:
   // Create a new magic box
   $game = new TicTacToe();

   // Let's play some moves
   $game->playMove('X', 0, 0);
   $game->playMove('O', 1, 1);
   $game->playMove('X', 0, 1);
   $game->playMove('O', 2, 2);
   $game->playMove('X', 0, 2);

Watch as the magic board changes after each move!

Using Magic Codes in Real Life:

These magic codes aren’t just for Tic Tac Toe. They can be used in all sorts of fun games and puzzles! By learning how to use them, you’ll become a master of magic and math.


Tic Tac Toe is a fun game made even more exciting with secret codes! By mastering these codes, you can impress your friends and become a Tic Tac Toe champion. So, grab a pen and paper, or better yet, learn some PHP magic, and start playing Tic Tac Toe like a pro!