/tic-tac-ai

Django web app via which you can play tic tac toe vs an artificial intelligence and see what it was thinking

Primary LanguageJavaScript

tic-tac-ai

Django web app via which you can play tic tac toe vs an artificial intelligence and see what it was thinking. http://tictacai.pythonanywhere.com/

You can read a step-by-step walkthrough of this project on my website:
https://mateuszjurewicz.wordpress.com/

2016 10 06

Description

Play around with a perfect opponent in a game of online tic tac toe. The simple (weak) AI uses a strategy called the minimax algorithm1, it uses recursion to look through the entire tree of possible game states and chooses its best move assuming that it's playing against a perfect opponent. Key improvement included taking into consideration the number of turns left (it prefers to win quick and lose late).
* Additional feature allows you to see visualisations of the board at each AI turn, showing what the machine was thinking.

Acknowledgments

Inspired by Michael Dawson's "Python Programming for the Absolute Beginner"2 , Brett Slatkin's "Effective Python: 59 Specific Ways to Write Better Python"3 and most importantly a Ruby version of a similar concept by NeverStopBuilding4. Kudos to you all, thanks for the opportunity to learn from you.

Technologies

I've decided to use the Django 1.10 framework due to the fact that it was recommended to me based on my background in Python 3.5.1. Code adheres to PEP-8 and was tested via PyLint (scored 7.05/10). On the front-end the simple choice was javaScript + jQuery. I aimed for cross-browser compatibility (tested primarily on Mozilla and Chrome) as well as decent RWD features.

Possible improvements

I would love it if other people took my work and created something better by building on it. I have thought about interesting projects that could stem from my tic-tac-ai and below is a list of such possibilities:
  • improve the runtime (analysis with cProfile showed that the has_won() function eats up a lot of time)
  • create a game of tic tac toe with a bigger board than 3x3 and define more ways to win
  • randomize the tiles chosen by AI if all are given the same value by the algorithm (increases re-playability)

1 More on the minimax algorithm at https://www.encyclopediaofmath.org/index.php/Minimax_principle
2 Available at http://www.goodreads.com/book/show/80443.Python_Programming_for_the_Absolute_Beginner
3 Available at https://books.google.pl/books/about/Effective_Python.html?id=bTUFCAAAQBAJ&redir_esc=y
4 Article at http://neverstopbuilding.com/minimax and heroku implementation at http://perfecttictactoe.herokuapp.com/