/tic-tac-ai-cmd

Command line version of tic-tac-ai, where you play the game against a simple AI and see what it was thinking

Primary LanguagePython

tic-tac-ai-cmd

Command line versions of tic-tac-ai (Python 3.5.1), where you play the game against a simple AI and see what it was thinking.

  • Version 1.0 allows you to play the game by choosing the tile on which to place your token (0-8).
  • Version 1.4 additionally shows what the AI was thinking by creating a new .txt file called what-AI-saw.

A Django web app version available at:
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 command line 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 in version 1.4 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

Built on Python 3.5.1 as a precursor to the Django version. Code adheres to PEP-8 and was tested via PyLint: * Version 1.0 scored 9.02/10. * Version 1.4 scored 8.90/10.

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/