🤖A Tic-Tac-Toe solver that uses the minimax algorithm and alpha-beta pruning to make it unbeatable

Overview

Tic-Tac-Toe AI

A Tic-Tac-Toe solver that uses the minimax algorithm and alpha-beta pruning to make it unbeatable

Tic-Tac-Toe AI

How it Works

Tic-Tac-Toe is what is known in game theory as a zero-sum game, meaning that an advantage to one side is a disadvantage in equal magnitude to the opponent. As such, minimizing the maximum potential loss is equivalent to maximizing the minimum potential gain, making a "minimax" algorithm the optimal strategy.

Minimax Algorithm

We let the maximizing player be the AI, and the minimizing player be the human. This means that the AI's optimal strategy is to maximize the game score, while the human's optimal strategy is to minimize the game score.

The minimax algorithm for Tic-Tac-Toe works by considering every possible node in the game tree (i.e. all legal moves), and evaluating each move with a score.

The algorithm recurses through the game tree until it hits one of three base cases:

  1. AI win
  2. Human win
  3. Draw

It then assigns a base score of 10, -10, or 0, respectively.

We also must consider the fact that the algorithm should favour moves which provide quicker wins. Thus, we also increment a depth variable at each move, and either subtract (if the player is maximizing) or add (if the player is minimizing) to the base score, making a higher depth less favourable.

Alpha-Beta Pruning

In order to optimize the computational time of the algorithm, alpha-beta (α/β) pruning is used.

Let α be the best score that the maximizing player can currently guarantee, and β be the best score that the minimizing player can currently guarantee. α is initialized to -Infinity, and β is initialized to Infinity (i.e. the worst possible cases).

At every move, we set α = max(α, score) if the player is maximizing, and β = min(β, score) if the player is minimizing. If at any point β ≤ α, we "prune" the node, as it is no longer favourable to recurse through its subtrees (i.e. a better move already exists, so there is no point in continuing).

With the help of Alpha-Beta pruning, we are able to search through the game tree at a considerably faster rate.

Credits

Tic-Tac-Toe favicon created by Freepik - Flaticon

You might also like...

Automate adding issues and pull requests to GitHub projects (beta)

actions/add-to-project Use this action to automatically add the current issue or pull request to a GitHub project. Note that this is for GitHub projec

Jan 3, 2023

This project is in developer-preview status and will reach the beta in 2023 Q1

rescoped.io This project is in developer-preview status and will reach the beta in 2023 Q1. Currently, the datagrid is under massive refactoring, and

Dec 30, 2022

ScraperTools BETA Version 1.0.1

ScraperTools Official ScraperTools NPM Package Get Started Via NPM: $ npm install scraper-tools Cara Menggunakan const scrapertools = require('scraper

Sep 28, 2022

Add issues to projects (beta)

Add Issue/PR to Project (BETA) ➕ This GitHub action adds issues or pull requests to a Project (beta). Usage Create a workflow (eg: .github/workflows/o

Aug 12, 2022

CodAre'ın resmi sitesi için yapılmış v12tov13 sistemi (BETA)

V12toV13 CodAre'ın resmi sitesi için yapılmış v12tov13 sistemi (BETA) Kurulum Projeyi klonladıktan sonra proje klasörünün içine girin ve klasörde bir

Aug 14, 2022

🌌 Fast, in-memory, full-text search engine written in TypeScript. Now in beta.

🌌  Fast, in-memory, full-text search engine written in TypeScript. Now in beta.

Installation You can install Lyra using npm, yarn, pnpm: npm i @nearform/lyra yarn add @nearform/lyra pnpm add @nearform/lyra Usage Lyra is quite simp

Dec 30, 2022

Croquet Microverse (Beta)

Croquet Microverse (Beta)

Croquet Microverse (Beta) https://croquet.io Description Croquet Microverse is a framework to build multiplayer immersive 3D virtual worlds on the web

Dec 30, 2022

Gthub action for Project (beta) management. Allows to update fields

titoportas/update-project-fields Use this action to automatically update GitHub project (beta) item fields. Note that this action does not support Git

Nov 3, 2022

BETA partytown-qwik

BETA partytown-qwik

Qwik Partytown 🎉 This is a package that facilitates the implementation of PartyTown in Qwik. If you don't know what Qwik is See The implementation is

Dec 20, 2022
Comments
  • [ImgBot] Optimize images

    [ImgBot] Optimize images

    Beep boop. Your images are optimized!

    Your image file size has been reduced by 22% 🎉

    Details

    | File | Before | After | Percent reduction | |:--|:--|:--|:--| | /assets/img/favicon/android-chrome-512x512.png | 28.82kb | 20.90kb | 27.46% | | /docs/tictactoe.png | 20.49kb | 16.06kb | 21.62% | | /assets/img/favicon/apple-touch-icon.png | 13.67kb | 10.74kb | 21.43% | | /assets/img/favicon/mstile-150x150.png | 9.84kb | 7.76kb | 21.11% | | /assets/img/favicon/android-chrome-192x192.png | 14.53kb | 11.87kb | 18.29% | | /assets/img/favicon/safari-pinned-tab.svg | 1.25kb | 1.07kb | 14.66% | | /assets/img/favicon/favicon-32x32.png | 2.37kb | 2.18kb | 7.99% | | | | | | | Total : | 90.98kb | 70.59kb | 22.40% |


    📝 docs | :octocat: repo | 🙋🏾 issues | 🏪 marketplace

    ~Imgbot - Part of Optimole family

    opened by imgbot[bot] 0
Tic Tac Toe game (HTML, CSS and JS)

Tic-Tac-Toe Tic Tac Toe game built as a site written with HTML, CSS and JS. Authors @Sh1Sh0p FAQ Does the game have a multiplayer option? No. Does the

Sh1Sh0 5 Oct 25, 2022
This is a tic-tac-toe game but differs from most others as it carries the option of playing against an AI (COM) or against a friend.

TIC-TAC-TOE This is a simple tic-tac-toe game with the exception of playing against an algorithm or against a friend. At the very start, you have to s

Paul Ibeabuchi C. 4 Jul 2, 2022
Dominating set solver using quantum algorithm Grover

Solution for dominating set problem using improved quantum algorithm Grover, which uses Schoning algorithm for k-SAT problem to accomplish this improvement

Fatemehe Lajevardi 8 Aug 31, 2022
🧩 Sokoban game and automated puzzle solver

?? sokoban My blog post: Building and Solving Sokoban A Sokoban game built with Next.js — skinned with Boxxle sprites, and packaged with 40 tiny eloqu

Andrew Healey 3 Jun 8, 2022
Quadratic Solver: Quadratic Equation Calculator (A X^2 + B X^1 + D = 0)

Quadratic Equation Calculator (Quadratic Solver) Solves the linear equation Ax^2 + Bx + C = 0 for x. I searched about "Quadratic Equation" topic and u

Max Base 6 Dec 15, 2022
El repositorio de cheatsheets de TIC. Pensado para que los alumnos puedan utilizar a la hora de programar como "ayudamemorias".

Cheatsheets de TIC Este es el repositorio de la web de cheatsheets de TIC. Para acceder a la web hacer click acá. ¿Cómo hago un cambio? ¿Mi cambio tie

null 37 Nov 10, 2022
Alpha version of ALBot 2.0, the spiritual successor to ALBot

ALBot 2.0 Alpha Alpha version of ALBot 2.0, the spiritual successor to ALBot. ALBot 2.0 uses Discord.js to interface with the Discord API, supplanting

UF Open Source Club 8 Nov 17, 2022
Team Alpha Super Awesome Cool Dynamite Wolf Squadron - 10 - Project 1

Super Hero Wiki This is a group project for our Interactive Front End Web Site. We created a Super Wiki that uses two (2) APIs to provide users a comi

Vicente Garcia Sepulveda 3 Mar 24, 2022
A full documentation on everything we know about Alpha 1.0.16 versions.

Minecraft's Alpha 1.0.16 Versions Before you start, make sure to watch RetroGamingNow's video about this first. Highly influenced (technically a port

_NexTre_ 44 Dec 23, 2022
ALPHA build of re621 2.0

RE621 is a comprehensive project created to improve the basic user experience while browsing e621.net. It consists of several different modules that e

re621 11 Dec 16, 2022