![]() ![]() Return ((boards = boards = boards = mark) #boards is a list to store the mark with respect to the cell number # x is the counter to keep counting the number of chances If(x>8 and result(boards,'X')=False and result(boards,'O')=False): # If we have not got any winner, display the dialogbox stating the match has bee tied. #Call the destroy function to close the GUI #If Player1 is the winner show a dialog box stating the winner # Here we are calling the result() to decide whether we have got the winner or not #Using config, we write mark the button with appropriate value. # If the value of x is even, Person1 will play and vivee versa Has been already clicked or not to avoid over-writing """ """ Checking which button has been clicked and checking if the button With the help of these numbers, you can fetch the respective mark. This list contains all the cell numbers of the tic tac tow board. Implementing the Constants and define_sign() function Here, boards=boards=boards=’X’, so our function will return True. The board will always contain ‘1’ at index 0 because the index of a list starts with 0, so we have to adjust to that.įor example, if the value passed in the first parameter is - and the mark = ’X’ our function will check if these values satisfy the conditions in our function. If yes, it will return True otherwise, False. The second parameter, ‘mark,’ contains the value either ’X’ or ‘O.’ This function will check if the list passed (boards) has the mark (‘X’ or ‘O’) in these cells or not. For example, if player1 had clicked on button3, this list will have the value=’X’ at index 3. In the first parameter, ‘boards’, we will give a list containing the mark on each cell. Here, we will create a function named result(), which will return True if the mark in all three cells is the same. Return ((boards = boards = boards = mark) or (boards = boards = boards = mark) or (boards = boards = boards = mark) or (boards = boards = boards = mark) or (boards = boards = boards = mark) or (boards = boards = boards = mark) or (boards = boards = boards = mark) or (boards = boards = boards = mark)) Implementing the Tic Tac Toe Logic in Python – So, there are eight combinations when the Winner can be decided. If the mark in either of the diagonals is identical, the Winner is decided. The Winner is decided if all the values of any of the rows or columns have the same mark (‘X’ or ‘O’). ![]() ‘X’ is the mark used by Player 1, and ‘Player2 uses O’. In tic tac toe, there are two players playing the game. ![]() How is the Winner decided in Tic Tac Toe? Wasn’t that easy? Believe me, the rest of the code is as easy as this. Voila! We have successfully created the graphical interface for our game. Root.mainloop() #Implements the main window apllication Similarly, we have to create 8 more buttons same as button1.ī2=Button(root,width=20,height=10,command=lambda:define_sign(2))ī3=Button(root,width=20,height=10,command=lambda: define_sign(3))ī4=Button(root,width=20,height=10,command=lambda: define_sign(4))ī5=Button(root,width=20,height=10,command=lambda: define_sign(5))ī6=Button(root,width=20,height=10,command=lambda: define_sign(6))ī7=Button(root,width=20,height=10,command=lambda: define_sign(7))ī8=Button(root,width=20,height=10,command=lambda: define_sign(8))ī9=Button(root,width=20,height=10,command=lambda: define_sign(9)) We will understand it when we know the concept used behind the game.Īfter that, we will put the button in the 1st row and 1 column just as we put the Label widget. And don’t worry about the define_sign() method. We have passed ‘1’ to the define_sign() method because button1 has been clicked. For now, think of it as a way to call the define_sign() method when button1 is clicked. After that, we need to do something when this button gets click, so in the 4th parameter, we are giving the command to call the ‘define_sign()’ using the lambda keyword whenever this button is clicked.ĭon’t get confused if you don’t know the lambda keyword. You can play with those values for better understanding. In the second and third parameters, we have set the values of the button’s width and height. ![]() As the first parameter, we have given the value root as we want the button to be inside the parent window. In the above code, we have used the Button method of Tkinter. So that when a player clicks a button, we will know which cell. And for every cell, we need to create a button. In a tic tac toe, there are a total of 9 cells. B1=Button(root,width=20,height=10,command=lambda:define_sign(1)) ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |