Tic-tac-toe

project / July 27th, 2021

Tic-tac-toe Initial Screen

GitHub

Background

This is a simple text-based version of the classic 2-player game Tic-tac-toe that is played on the command line and built purely in Python. In Tic-tac-toe, player 1 and player 2 take turns marking the empty spaces of a 3-by-3 grid with X and O, respectively. The player who is successful in marking three spaces in a horizontal, vertical, or diagonal row wins the game. The game can also end in a draw if neither player is able to meet the winning conditions. After a game is finished, the users will have an option to play again or end the program.

This is a professional portfolio project that was created after completing the 100 Days of Code Python Bootcamp.

Purpose

After completing the 100 Days of Code Python Bootcamp, this project was offered as a suggested project idea to test my understanding of Python fundamentals like loops, functions, lists, and dictionaries to write a pure Python script. Just like other professional portfolio projects, developing skills in planning and executing a project from scratch without guidance was the most valuable lesson.

Concepts and Tools Used

  • Python

Process

To start, I had to figure out the requirements and functions the program needed:

  • A function to print the current game board, print_board that will print the current game board.

  • A function called update_board that will mark the empty space on the game board with either X or O, depending on whose turn it is.

  • A function called check_end_of_game that will check if the game-ending conditions have been met. The function will check if three squares have been marked by a player in a vertical, horizontal, or diagonal direction to declare a winner, or if the maximum number of turns (9) have been used. If all turns have been used and neither player has met the winning condition, a draw is declared.

  • A function to run the actual game, play_game that incorporates each of the first three functions to form the game logic. After printing the empty game board and displaying which number keys correspond to which squares on the game board, player 1's turn begins and they are prompted to enter a number to mark an empty space. Once a valid number is entered, the update_board function is called and marks the corresponding empty space. The updated board is then displayed using the print_board function to end the turn. Player 2's turn then begins, and the process repeats. Throughout the process, game-ending conditions are monitored by calling check_end_of_game.

After planning out the project, I wrote code to implement each of these functions. I chose to represent the game board in the form of a Python dictionary, which I felt was suitable because I wanted each space on the game board to be associated with a number key. I also stored each winning combo numbers in a tuple, then stored each tuple in a list called WINNING_COMBOS. Since there are eight possible ways to win Tic-tac-toe, there are eight tuples in WINNING_COMBOS.

In play_game, the game title and game board numbers are printed in the beginning to guide the user in knowing which spaces are represented by each number. A FOR loop is then used to iterate through the player turns. In each turn, the current board is printed by calling print_board, the player is prompted to enter a number to indicate which space to fill, and if their input is valid, update_board is called to fill that space. A WHILE loop and exception handling is also used to ensure the player can input another number if their previous input was already taken, was not between 1 to 9, or was not a number. After update_board is called, check_end_of_game is called to track if one of the game-ending conditions have been met. If so, a winning player or draw is declared and play_game will end by returning True. If not, the next turn will occur and the same process repeats for the other player.

Tic-tac-toe Gameplay

print_board takes the current game board as an argument and manipulates the iterating number in a FOR loop to access each dictionary key and print each row to form the game board. A small label is also printed underneath the game board to indicate which player uses X or O. update_board takes the game board, current player number, and their number input to fill the space on the board with the correct marker. check_end_of_game takes the board, current player number, and the current turn number as arguments. Each combo in WINNING_COMBOS is used to check if any vertical, horizontal, or diagonal rows on the game board have the same marker. If true, the game is over and the current player is declared to have won. If not, the function will check if the number of turns is equal to the maximum number of turns possible (9). If true, the game is over and a draw is declared.

Tic-tac-toe Invalid Inputs

After the game ends, the user is prompted to indicate if they wish to play again or not. If so, the console will clear and play_game is called to start a new game. If not, "Thanks for playing!" is printed and the program will end. To clear a console in Python, I had to research on Google and learned to add a clear function to my code.

Tic-tac-toe Game End

Finishing touches are made by adding ASCII art for my game title.

Outcome and Takeaways

Working on this project was a helpful way to reinforce the fundamental concepts I learned in the first fifteen days of the bootcamp. My knowledge of functions, loops, lists, and dictionaries were really tested and I gained more confidence in my programming skills by having to implement them in a project I made on my own.

Possible Improvements Going Forward

There are many possible improvements that can be made to this game. To provide more choice for single players, it would help to offer a mode where the user can play against an AI bot with varying difficulties. More customization can be provided by enabling the user to choose the grid size. This app can also be converted to a Flask Web App where Jinja tags can be used to update the game board, or a Tkinter desktop app. Both versions of this game would provide an enhanced gaming experience for the user with more visuals and colour.


External Links