Introduction:
General Description:
There are constants and global variables specified. Pin assignments, LED count, grid size, button cooldown time, and variables that monitor the current player, player colors, grid status, and player scores are examples of these.
The code then declares function prototypes for all of the functions utilized.
To initialize the hardware and set up the game, the setup() method is invoked. It sets up the NeoPixel strip and LCD display, as well as calling the restart() and pickColor() methods.
The main game loop (loop()) gets started. It keeps running the game logic till the game is over.
To check for button presses and manage the relevant actions, the handleRightButton(), handleLeftButton(), and handleOkButton() routines are invoked. These functions handle the input from the player in order to position their marker in the grid.
The colorBorder() function is used to change the color of the LED strip border based on the player currently in use.
To update the LED strip to match the current state of the grid, the colorGrid() function is invoked.
The code determines whether the game is over by comparing the sum of player scores to the highest possible score. If the game has concluded, it runs the rejoice() function if there is a winner or the draw() function if there is a tie, and then calls the restart() function to restart the game.
The gameStart() function is used to display a welcome message and alert the first player that the game is about to begin.
The restart() function returns the game to its initial condition. It resets player scores, clears the grid, and resets the current player to 1.
The pickColor() function shows a message on the LCD to inform the players of the colors they have been allotted (red and green).
The updateCurrentPlayerFactor() function uses the current time to update the animation factor of the current player's color. It produces a pulsating effect on the edge of the LED strip.
The blinkCurrentPlayer() function highlights the spots on the LED strip where the current player has placed their marker for a brief period of time. To represent the player's movement, it generates a flashing effect.
By changing the value of the currentPlayer variable, the switchCurrentPlayer() function passes the turn to the next player.
To announce the winning player, the celebrate() method shows a message on the LCD.
To signify a tie game, the draw() method shows a message on the LCD.
Code Source: tictactoe.rar