Skip to main content

Build a tic tac toe game using python

 Hello, In this blog i am going to teach you how to create a tic tac toe game using python. If you know the basic of python like function, loops, conditional, list then you are good to go. In this project we are going to create it with a simple format of python without using any import or any object oriented programming.

Requirement:

1.Board

2.Player(Computer and a user)

3.Winner

Python fundamental needed:

1.Variable

2.List

3.If elif else

4.While

5.Function

Here we are going to start

A tic tac toe game is a game which has three rows and three column so we need a list which is the size of 3X3 which is nine to store the empty dash for the board

Line 4: We assign a game_running to True which mean that the game is still runing when we first start the game.

Line 6: Defining a function to display the board when ever we call the display_board function.

Line 18: When we first start the game the board will display as empty with dash in it. Output is in line 8

Line 19:  We set the current player as 'X' by default.

Line 21: A function to check if there is no winner and the board already filled with X and O so it is a tie.


Line 28: It is a function to check for a winner in which after checking the winner it will return the winner and stop the game.

For a tic tac toe game to check a winner we need to check for a row, column and a diagonal if one row or one column or one diagonal filled with only 'X' or 'O' it will consider as a winner.

Line 32 : It check if the first row is filled with only 'X' or 'O' similarly for second row and third row.

Line 43:It check that if the first column is filled with only 'X' or 'O' and similarly for second column and third column.

Line 53: It check that if the first diagonal is filled with only 'X' or 'O' and similarly for the second diagonal.

Note: If you notice that when ever we access the game_running inside a function the first line of the function is 'global game_running' the reason is that game_running is a global variable so in other to change the value of the variable inside a function we need to declare that variable as a global inside a function with a 'global' keyword.


From line 60 the game is control and ask for player input.

Line 60: We have a while loop which will run as long as the game is running

Line 60: It ask the position where the player want to place it 'X' or 'O'

Line 64: In this line we are going to stop the player from overriding the position which the other player have already place it 'X' or 'O' by asking the player again about the position which they want to place.

Line 67: In this line we are going to ask the player again about the position if by mistake they enter nothing or if they enter with string.

Line 69: In this line we are assigning the position the player enter with their 'X' or 'O' in the board.

Line 70:After every position we place we need to display the board to the player so we call the display_board function.

Line 71: After every position we place we need to check that may be there is a winner.

Line 75: It is to swap the 'X','O' for the player that if one player put 'X' the other should put 'O' and not 'X' again.

Conclusion: I hope you get something from this blog let me know if you find any error in your code i will help you out or if you did not understand something just let me know and i highly encourage to improve the game like handling the error because there are lot more to improve in this code, You can also check the code in my github account click here Github.

I will appreciate if you give me some feedback and correct me if i am wrong. Thankyou

Comments

Popular posts from this blog

Best office computer accesories

 Here are some of the best computer accesories for your office at an affordable price in India. 1. Oxyura Gaming Mouse Pad Click here to see the Mouse pad https://amzn.to/3pVY8KL 2. Zebronics Zeb-Alex Wired Usb optical mouse Click here to see and buy the Mouse https://amzn.to/3ILT2t6 3. Usb wired keyboard Click here to see and buy the keyboard https://amzn.to/3mmzjqv 4.Laptop stand Click here to see and buy the laptop stand https://amzn.to/3dTgkih All link are from amazon so go ahead and check it out it is at an afordable price.

Top 5 mistake i made as a computer science engineer.

Top 5 mistake i made as a computer science engineer In this blog i will walk you through my mistake as a computer science engineer Yes as a person we make a lot of mistake and for me as a computer science engineer i have made a lot of mistake but in this blog I'm going to share my top 5 mistake. 1. Involve in a tutorial hell Tutorial hell is a way of learning in which we get stuck in a tutorial without learning outside of the tutorial, it is like learning from the tutorial and build the exact same project which the instructor have taught us and also it may be just copy and paste the code, after completing the tutorial we start another tutorial and repeat the same thing again and again, without adding our feature to the project or build it without any help from the instructor. 2. Trying to learn everything Yes as a computer science engineer student i try to learn everything and it is a huge mistake because i learn a bit of python and then jump to javascript and then a bit of javascr...

Python programe to find the factorial of a number

 Python program to find the factorial of a number Factorial number of a number is a product from 1 to the number which is from 1 to n where n is the number Example : factorial of 5 = 1*2*3*4*5 = 120 Code: #function to find factorial number def facto(n):     factorial = 1     for i in range(1,n+1):         factorial = factorial * i     return factorial #Take the input from the user and store in n n = int(input()) if n == 0: #factorial of 0 is always 1     print(1) else:     print(facto(n)) #other than 0 we have to find the factorial number by calling the function Input : 5 Output : 120 There is another method to find factorial number using recursion. Feel free to check the code in a code editor and try it out and also i am welcome to any suggestion. Here is the previous post : https://shotwithcode.blogspot.com/2020/12/python-automation-with-sele...