Skip to content

mahadhsn/TicTacToe-AI

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 

Repository files navigation

TicTacToe AI

Project Description

I tasked myself to create an unbeatable TicTacToe AI entirely within Python without the use of any AI libraries. I successfully did so within just over 400 lines of code .

I created this game with 2 different gamemodes: one to play against the AI and one for casual PVP. You are also able to choose whether you want to play the game as X or O.

Libraries

  • PyGame: To setup the GUI
  • Time: As to allow the user time to make a move
  • Numpy: For the 'backend'
  • Sys: To exit the system
  • Random: For the AI's first move
  • Copy: Implemented in the MiniMax Function
  • Const: Just a self-made python file for any constants

Code Structure

The code is written within 3 main classes:

  1. class Board:

    This class is primarily for the computer to understand the moves made and the boards current position. A 3x3 matrix is created which marks that positon by whichever player made the move. This is then interpreted by the next function which then converts it into a visible GUI.

  2. class Game:

    This class focuse on the GUI aspect of the game. Using Pygame, I was able to draw out the whole board along with the Xs, the Os, the lines for when someone wins, and even a text box indicating the outcome of the game.

  3. class AI:

    This class is where the magic for the AI takes place. Although it is the smallest out of the three, it is the most crucial. Its main goal is to implement the Minimax Function.

    Minimax: This is a very popular algorithm which implements recursion and the concept of weights in order to allow the computer to pick the best possible move. Here is a nice explanation if you are willing to learn more: https://brilliant.org/wiki/minimax/

Reflection

This project sparked an interest within me for AI/Machine Learning and its endless possibilites. It also allowed me to gain a vast amount of experience within Object Oriented Programming (OOP) which is always a useful skill to have. Playing around with Pygame also me to gain an interest towards game development: (C# and Unity?). Overall, it was a great experience and I enjoyed it which is really all that truly matters.

Future Plans?

I will definetly put more hours into Machine Learning (specifically reinforcement learning) and spend more time researching Neural Networks and how they function. Learning this skill will open a new world of possibilites for me. Outside of Python, I want to start learning SQL and implement that into Machine Learning for databases and other things. C++ is also a compelling sight for me right now as it has so many endless possibilites.

About

Unbeatable TicTacToe AI in Python!

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages