Tic-Tac-Toe: Python3
Tags: python, programming, computerscience, codenewbie
Published: 2023-10-16 11:42:54

Hello Dev(s),

This is my first blog post of [**DEV**](https://dev.to) 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**] (https://www.codecademy.com/) 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t14gkrlacpzqsvrn0joy.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ptz2xx6k0s9t0x9ja148.png)

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()_).

1. 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.

1. 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lbhaad3h8m0jqxaug876.png)


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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xxh5kmkmzognjyvnbaek.png)

**REFACTORING:**

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


![[rf] Win Or Tie](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6jgc8093mm5rtsf0iofd.png)

Wanna see how it runs? ⚡️
<img width="200%" style="width:200%" src="https://media.giphy.com/media/v1.Y2lkPTc5MGI3NjExMzZ4aHl5NHMwaThpeDFobmltdWd2MXVwbzFzOXg0MmU5em00YXBkbCZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw/VbhO2XcJr5T4Rm1XU7/giphy.gif">

Haha Fun eh? If you are still interested to look at my code then check out my _**GitHub**_ repo [here](https://github.com/AdhishriKothiyal/Codecademy/tree/main/Computer%20Science/Python%203/TicTacToe)