Tic Tac Toe Game with Ai opponent

This is a traditional Tic Tac Toe game. You can play this game with a simple Ai opponent

This game is created to solidify my knowledge in JavaScript concepts of Objects, Factory Functions, Closures, Private functions, IIFE (Immediately Invoked Function Expressions), HTML, CSS, User interface design, etc..

Please insert your name and click start to play the game

Game Status:

Winner Is:

Winning Conditions audit:

Last Turn by:

Technical info for Software Engineers

Tech Stack: JavaScript, HTML and CSS

Source Code available on GitHub or you can inspect the code using Firefox or Chrome Developer tools by right clicking this page and selecting "inspect"

Architecture design and coding by Amal Kariyawasam, 2024 March

Tic Tac Toe Game Architecture

Game Logic

Tic Tac Toe Board Coordinates;

0 1 2
3 4 5
6 7 8

Conditions for wins

Straight Line: All three Row wins:

Straight Line: All three Column wins

Straight Line: All three Diagonal wins

Conditions for Tie/Draw

Anything that is not in the above win conditions will be a draw/Tie after all 9 grid slots have been filled

Main Coding rules

User Interface design (HTML and CSS)

Logic design (JavaScript)

Pseudo Code

  1. Game Loaded, check whether game has been run before and choose from the following
  2. If it is the first time;
    • Prompt for Player Name
    • When Player clicks submit button store the player name and start the game
  3. If Game has been played before;
  4. Set following variables to zero: "gameBoard" array
  5. Check how many turn numbers have been made, Prompt the Player to make selection on the grid and store the grid location on the "gameBoard" array and player, update the turn number and update the display
  6. AiBot will make a calculation and store the grid location on the "gameBoard" array, update the turn number and update the display
  7. Keep checking If player or AiBot has fulfilled any of conditions for wins. If winning rules are fulfilled, declare the name of the winner if not keep playing until the end of the 9th turn

Code will be broken up into three Objects as follows for code readability and memory efficiency;

gameMemory: Used only as a memory to store the player positions, player details, player statistics, etc..

gameLogic: Object to control Flow of the game and the logic(Check whether the game has been run before, Check winning condition by comparing values in gameBoardObject, Keep track of number of turns and after 9 turns Game will be Over, decide who will take the next turn)

displayController: Only function is to check player inputs, then store to the gameMemory and update the display with data stored in the gameMemory to update the HTML DOM and to received user input