PYTHON Tic Tac Toe PART 1: The Game

TicTacToe Screen

Link to GitHub

Introduction to the Project

For my latest coding endeavor, I created a Tic Tac Toe game using the Pygame library. It is currently a local two player game meant to be played on my computer. My goal is to transform this game into a machine learning project by using an applicable ML algorithm to play against the human. This is Part 1 of the series where I will walk you through the fundamental concepts and structure of the inital game with subsequent parts expanding upon it. You can check out the code at the GitHub link above.

Project Structure

The main structure of the project in its current state is a Tic Tac Toe class file, a constants file, a markdown notes file for pygame notes, and a main python file that creates an instance of the class and runs the game loop. All of the game functionality is within the TicTacToe class, specifically within the game loop. The class also stores important game state attributes that can be accessed using ‘self’.

The game loop follows the typical game loop procedures one would expect for a simple game like Tic Tac Toe:

  • draw game screen including text, grid, grid marks, score
  • handling events: QUIT, MOUSEBUTTONDOWN
  • update game state: 1x9 game grid, player turn, player win count
  • check for game over: winner is player 1, winner is player 2, CAT
  • reset necessary game state attributes
  • update screen using pygame.display.flip()

My markdown file contains short pygame notes that I can reference later on and my constants file contains a GameColors class and a GameMeasurements class. The GameColors is for colors I want to reuse and the GameMeasurements are for dimensions of the screen that I can use without having meaningless numbers in my code. Finally, my main program creates an instance of the TicTacToe class and runs the game loop method.

Next up???

Stay tuned for the next couple iterations I have planned for my project!

  • Part 2: Creating a computer opponent
  • Part 3: Creating a tic tac toe visualizer