Hello friends how are you, Today in this blog post "Tic Tac Toe Game in Python" I am going to teach you how you can create Tic Tac Toe Game in Python using very simple lines of code. If you are a computer science students then this post will help you definitely just go through this post to get complete knowledge.
If you want to understand this through video then watch this video i have explained it step by step live
Step 1: Install Python : Click here to watch a video on how to install python or Open any browser and type Download Python and click the first link you will get official website of python here you will get a Download button and after clicking on this button you will get exe of latest python version just install it into your system.
Step 2: Install Pycharm | Create Project | Install Library: Click here to watch a single video on How to install Pycharm | Create Project | Install Library or To install Pycharm IDE Open any browser and type Download Pycharm and click the first link you will get official website of Pycharm here you will get a black download button and after clicking on this button you will get an exe of Pycharm , just install it into your system. If you are facing problem to install then watch the above video i have explained step by step.
Learn Python:Want to learn complete python for free click here
Step 3: Create Project : Open Pycharm click on File which is in top left of screen and select New Project. Then you will get a screen. Here first you need to select directory of your project and then type a name for your project like "TicTacGame" after directory name, and at last click the create button to create this project.
Step 4: Create Python file: To create a python file for coding in your project just right click on project name "TicTacGame" and select new and click on Python File , you will get popup screen in which you have to type a name like "Program" for python file and press enter, it will create a python file with name Program.py inside the project TicTacGame.
#import needed library from tkinter import * import random as r
#Function to define a button def CellButton(frame): b=Button(frame,padx=1,bg="#27386B",width=3,text=" ",font=('algerian',60,'normal'),relief="sunken",bd=5) return b
Here CellButton is a function and it is taking a single parameter. In the above code i have set many property in button like padding, font style, background color etc to make this button interesting. Text of button is empty to make game board clear.
#changing operator for the next player def changeLetter(): global a for i in ['O','X']: if not(i==a): a=i break
#Reset the gameboard def resetBoard(): global a for i in range(3): for j in range(3): board[i][j]["text"]=" " board[i][j]["state"]=NORMAL a=r.choice(['O','X'])
Here resetBoard is a function that will reset the game board by replacing each button text with space.
#check for winning def checkWinning(): for i in range(3): if(board[i][0]["text"]==board[i][1]["text"]==board[i][2]["text"]==a or board[0][i]["text"]==board[1][i]["text"]==board[2][i]["text"]==a): if a == 'X': lblMesage.config(text="'" + firstPlayer.get() + "' is Winner") else: lblMesage.config(text="'" + secondPlayer.get() + "' is Winner") resetBoard() if(board[0][0]["text"]==board[1][1]["text"]==board[2][2]["text"]==a or board[0][2]["text"]==board[1][1]["text"]==board[2][0]["text"]==a): if a =='X': lblMesage.config(text="'"+firstPlayer.get()+"' is Winner") else: lblMesage.config(text="'" + secondPlayer.get() + "' is Winner") resetBoard() elif(board[0][0]["state"]==board[0][1]["state"]==board[0][2]["state"]==board[1][0]["state"]==board[1][1]["state"]==board[1][2]["state"]==board[2][0]["state"]==board[2][1]["state"]==board[2][2]["state"]==DISABLED): lblMesage.config(text="The match is Tied!") resetBoard()
Here checkWinning is a function that will check all possible condition and show the result if condition satisfies and after that it will reset the game board using resetBoard function.
def click(row,col): board[row][col].config(text=a,state=DISABLED,disabledforeground=colour[a]) lblMesage.config(text="") checkWinning() changeLetter() if a=='X': label.config(text=firstPlayer.get()+"'s Chance") else: label.config(text=secondPlayer.get() + "'s Chance")
Here click is a function that takes the current cell position to set the current letter to it and then it will perform winning operation and at last it will change the current letter for next player.
#Defining window root=Tk() #Setting title for window root.title("Krazy:Tic-Tac-Toe Game") #setting width and height for game window root.geometry("800x490") #setting background color root["bg"]="#03151C" #declaring variables firstPlayer=StringVar() secondPlayer=StringVar() #label for first player Label(root,text="First Player Name[X]",bg="#03151C",fg="white",font=("Arial",15,"bold")).place(x=550,y=20) #textbox for first player Entry(root,font=("Arial",15,"bold"),textvariable=firstPlayer).place(x=550,y=55) #label for second player Label(root,text="Second Player Name[O]",bg="#03151C",fg="white",font=("Arial",15,"bold")).place(x=550,y=90) #textbox for second player Entry(root,font=("Arial",15,"bold"),textvariable=secondPlayer).place(x=550,y=125) #label for displaying game result lblMesage=Label(root,bg="#03151C",fg="yellow",font=("Arial",15,"bold")) lblMesage.place(x=550,y=185) #Defining Two operators a=r.choice(['O','X']) #setting color for operators colour={'O':"yellow",'X':"white"} #variable for creating board board=[[],[],[]] for i in range(3): for j in range(3): board[i].append(CellButton(root)) board[i][j].config(command= lambda row=i,col=j:click(row,col)) board[i][j].grid(row=i,column=j) #label for displaying player's turn label=Label(text="Welcome",font=('arial',15,'bold'),bg="#03151C",fg="white") label.place(x=550,y=450) #run Application root.mainloop()
Here in the above code i have explained each line of code using comment so i think there is need to describe it again.
""" Krazyprogrammer Presents Tic Tac Toe Game """ #import needed library from tkinter import * import random as r #Function to define a button def CellButton(frame): b=Button(frame,padx=1,bg="#27386B",width=3,text=" ",font=('algerian',60,'bold'),relief="sunken",bd=5) return b #changing operator for the next player def changeLetter(): global a for i in ['O','X']: if not(i==a): a=i break #Reset the gameboard def resetBoard(): global a for i in range(3): for j in range(3): board[i][j]["text"]=" " board[i][j]["state"]=NORMAL a=r.choice(['O','X']) #check for winning def checkWinning(): for i in range(3): if(board[i][0]["text"]==board[i][1]["text"]==board[i][2]["text"]==a or board[0][i]["text"]==board[1][i]["text"]==board[2][i]["text"]==a): if a == 'X': lblMesage.config(text="'" + firstPlayer.get() + "' is Winner") else: lblMesage.config(text="'" + secondPlayer.get() + "' is Winner") resetBoard() if(board[0][0]["text"]==board[1][1]["text"]==board[2][2]["text"]==a or board[0][2]["text"]==board[1][1]["text"]==board[2][0]["text"]==a): if a =='X': lblMesage.config(text="'"+firstPlayer.get()+"' is Winner") else: lblMesage.config(text="'" + secondPlayer.get() + "' is Winner") resetBoard() elif(board[0][0]["state"]==board[0][1]["state"]==board[0][2]["state"]==board[1][0]["state"]==board[1][1]["state"]==board[1][2]["state"]==board[2][0]["state"]==board[2][1]["state"]==board[2][2]["state"]==DISABLED): lblMesage.config(text="The match is Tied!") resetBoard() def click(row,col): board[row][col].config(text=a,state=DISABLED,disabledforeground=colour[a]) lblMesage.config(text="") checkWinning() changeLetter() if a=='X': label.config(text=firstPlayer.get()+"'s Chance") else: label.config(text=secondPlayer.get() + "'s Chance") #Defining window root=Tk() #Setting title for window root.title("Krazy:Tic-Tac-Toe Game") #setting width and height for game window root.geometry("800x490") #setting background color root["bg"]="#03151C" #declaring variables firstPlayer=StringVar() secondPlayer=StringVar() #label for first player Label(root,text="First Player Name[X]",bg="#03151C",fg="white",font=("Arial",15,"bold")).place(x=550,y=20) #textbox for first player Entry(root,font=("Arial",15,"bold"),textvariable=firstPlayer).place(x=550,y=55) #label for second player Label(root,text="Second Player Name[O]",bg="#03151C",fg="white",font=("Arial",15,"bold")).place(x=550,y=90) #textbox for second player Entry(root,font=("Arial",15,"bold"),textvariable=secondPlayer).place(x=550,y=125) #label for displaying game result lblMesage=Label(root,bg="#03151C",fg="yellow",font=("Arial",15,"bold")) lblMesage.place(x=550,y=185) #Defining Two operators a=r.choice(['O','X']) #setting color for operators colour={'O':"yellow",'X':"white"} #variable for creating board board=[[],[],[]] for i in range(3): for j in range(3): board[i].append(CellButton(root)) board[i][j].config(command= lambda row=i,col=j:click(row,col)) board[i][j].grid(row=i,column=j) #label for displaying player's turn label=Label(text="Welcome",font=('arial',15,'bold'),bg="#03151C",fg="white") label.place(x=550,y=450) #run Application root.mainloop()
Here in the above screenshot you can see that Letter X satisfies the winning condition so Game is displaying Ankita as a Winner.
0 Comments