Codecademy Chronicles: Mastering Python Through the Classic Game of Tic Tac Toe

AdrianAdrian
4 min read

I've been exploring Full Stack web development (MERN) but decided it's time to dive into the world of Python, especially since I'm eyeing a future in cybersecurity. And what better way to get my feet wet than with a fun project like this? Big shoutout to CDcodes on YouTube for their awesome Tic Tac Toe tutorial—it provided the perfect foundation for initiating this project as part of Codecademy's computer science course.

The Game Comes to Life

This game is fun and easy to play, while I can't demonstrate it live, picture yourself strategically placing 'X' or 'O' to outwit your opponent in your computers terminal.

Behind the Scenes of My Game

My game's heart beats with Python. It's got all these cool parts that work together to make playing a breeze:

  • Setting Up the Board: The game starts with a board that's basically a map of numbers you can pick from to make your move.

  • This representation is intuitive and user-friendly, as it allows players to easily select a position on the board by entering its corresponding number. The initial values of the dictionary are strings of the numbers themselves, indicating that no moves have been made yet.

    The draw_board function takes the current state of the spots dictionary as its argument and constructs a visual representation of the game board. It uses string formatting to create a string that visually represents the board, with each spot's value inserted into its respective position in the layout. After constructing this string, it prints it to the console, allowing players to see the current state of the game. The use of f-strings (formatted string literals) in Python makes it straightforward to insert the values from the spots dictionary into the string representing the board. The \n character is used to insert line breaks, ensuring that the board appears correctly formatted in the console.

  • Keeping It Clean: Every time you make a move, the screen clears up, so it's super easy to see what's going on.

    The clear_screen function clears the console screen to keep the game interface clean, enhancing the player's experience by ensuring the current state of the game is easily visible.

  • Who's Up?: The game keeps track of turns, so you always know if it's your turn or your friend's. The turn variable is used to determine this, incrementing with each move.

  • Making Your Move: You pick where you want to go, and the game makes sure the spot's open and that everything's fair. Players input their choice of position, which is then checked to ensure the spot is available (not already marked with 'X' or 'O') before assigning the current player's marker to the chosen spot.

  • Winning and Tying: The game checks for a win using the check_for_win function, which evaluates rows, columns, and diagonals for matching symbols ('X' or 'O'). A tie is checked by determining if the board is full and no win has been detected

  • Want to Play Again?: After a game concludes, players are prompted for a rematch. If they agree, the board and turn counter are reset, allowing for a new game to start immediately.

This code collectively offers a comprehensive Tic Tac Toe game experience, from game setup and play to determining outcomes and facilitating rematches, all within a simple and interactive console application.

Check Out My Code

Curious about how I built it? You can take a closer look at my GitHub repository Here. Everything’s there – the full Python script, and some extra bits and pieces about how this whole thing came together.

Wrapping It Up

Turning Tic Tac Toe into a coding project has been a fantastic ride. It's not just about reliving those childhood games; it's about building something cool and learning a ton along the way. As I keep tweaking and improving my game, I'm getting more and more excited about where Python can take me, especially into the cybersecurity world. A huge thanks to CDcodes for the inspiration, and to all of you for checking out my project. Here's to many more coding adventures ahead!

The captivating Tic Tac Toe board images featured in this blog post are credited to the talented Solstice Hannan. You can explore more of Solstice's work on Unsplash.

0
Subscribe to my newsletter

Read articles from Adrian directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Adrian
Adrian

Hi, I'm Adrian! A former nurse turned Full stack developer, I'm navigating the tech world one line of code at a time. On 'Code Compass,' I share my journey from healthcare to coding, aiming to become a digital nomad. Join me as I explore new technologies, face challenges, and transform my career. Let's learn and grow together in this exciting realm of tech!