DEV Community

Cover image for Tic-Tac-Toe: Python3
Adhishri Kothiyal
Adhishri Kothiyal

Posted on

Tic-Tac-Toe: Python3

Hello Dev(s),

This is my first blog post of DEV and I am doing this as a part of my CS 101 course.

Lately I have been brushing up my coding skills. I haven't coded since past 6 years and all the concepts are very blurry right now. To start fresh I decided to take up a Codecademy learning path. I enrolled myself in Computer Science career path. The first part of the path's curriculum includes learning a language & its basics. I have invested my time so far in learning Python3. The last assignment of the first section is to build a terminal based game. The objectives of this project were:

  • Build a terminal program using Python
  • Add at least one interactive feature using input()
  • Use Git version control
  • Use the command line and file navigation
  • Write a technical blog post on the project

Thinking about what to build was kind of scary. Since I have never made any game before I was scared about few things like: from where will I begin, what the logic would be and how will I end. I decided to take one step at a time and not think much about the next. Gathering all the courage and lot of motivation from my lovely husband I decided to pick up something simple and a game who's rules I already knew hence Tic-tac-toe

Here's a rundown to my program:

First I printed down a small welcome message for the players. I also created a 1-D list (g_board) to store the markers (X or 0) or the empty blocks in the game board. Initial value of each item in list is " "

Welcome Message and a default game board

Then I created a function to print the board when a player makes a move. For players the blocks are numbered from 1-9 while in the program the g_board list is accessed from 0-8, thus while printing the player's choice I decreased 1 and then tried to access the game board. Every print statement is 1 row in the game board. Each block in the game board was accessed by g_board[block_number].

Print the game board

Next functions are:

  1. To switch the player for turns and decide the marker symbol for each player. Player A marks on the board as X and Player B as 0 (which_playerNmarker()).

  2. To take the choice input from each player in their respective turns (player_ip()). The input() function takes in an input as a string thus I am using int() function to change typer from string -> integer.

  3. To check for empty strings in the game board (contains()). We need to check empty strings because if we have no empty blocks then it means the game board is full and if there is no winner then there is a tie.

Decide the player & marker, take the i/p from the player, check for an empty string in the game board

Next was a function to check for win or tie (winORtie()) and last but not the least a loop to keep the game running until someone either wins or it's a tie. In the end the winner gets printed.

Logic for winORtie(): If the winner is none then the loop runs and checks 8 possible options for a player to win. If the marker is on the following blocks the player wins:
1, 2, 3
4, 5, 6
7, 8, 9
1, 4, 7
2, 5, 8
3, 6, 9
1, 5, 9
3, 5, 7
Lastly the function checks if there is no empty string and none of the above conditions were fulfilled then it's a tie.

(Note: This is the best I could think at that time and I was not sure if that was the best solution. For now I know 2 more logics that can be implemented but I haven't tried them personally. That's a task for some other day. I haven't implemented file system as well. Also I did realize that I need a way to determine if a particular block has been marked by the other player. In this program if Player B chooses a block already marked by Player A the program will rewrite the block with Player B's marker. This shouldn't happen. I haven't figured out how to solve that problem, so I'll probably improve my code down in the future.)

Win or Tie

REFACTORING:

Later I refactored the winORtie() function to make it simpler and neater. The logic remains the same though.

[rf] Win Or Tie

Wanna see how it runs? ⚡️

Haha Fun eh? If you are still interested to look at my code then check out my GitHub repo here

Top comments (0)