Home >Backend Development >Python Tutorial >How to make a simple tic-tac-toe game based on Python

How to make a simple tic-tac-toe game based on Python

WBOY
WBOYforward
2023-05-19 14:29:371497browse

Basic Framework

from tkinter import *  
import tkinter.messagebox as msg  
  
root = Tk()  
root.title('井字棋')  
# labels  
Label(root, text="player1 : X", font="times 15").grid(row=0, column=1)  
Label(root, text="player2 : O", font="times 15").grid(row=0, column=2)

In this code, we first imported the tkinter module and the tkinter.messagebox module. Then, we create a Tk object named root and set its title. We named it Tic Tac Toe here.

Next, we create two Label objects and place them in the first row and second column of the root window. The text of the Label objects are player1: X and player2: O respectively, and their font size is 15 pixels.

Finally, we create a messagebox object named msg and display it in the root window. When the user clicks a button in the message box, the message box is displayed in the user's main window so that the user can see it. In this example, we simply display a message box.

Define button

button1 = Button(root, width=15, font=('Times 16 bold'), height=7, command=lambda: checker(1))  
button1.grid(row=1, column=1)  
button2 = Button(root, width=15, height=7, font=('Times 16 bold'), command=lambda: checker(2))  
button2.grid(row=1, column=2)  
button3 = Button(root, width=15, height=7, font=('Times 16 bold'), command=lambda: checker(3))  
button3.grid(row=1, column=3)  
button4 = Button(root, width=15, height=7, font=('Times 16 bold'), command=lambda: checker(4))  
button4.grid(row=2, column=1)

This code is the Python implementation of the Button component in the Tkinter module. In Tkinter, the Button widget can be used to create buttons that can be clicked in response to user events.

We have declared nine button components and configured the properties and methods of each component. The code snippet of each Button component is slightly different from the previous Button component, because this code is a complete Tkinter application, including windows, labels, buttons and other widgets.

In our example, we use the layout manager Grid in Tkinter to arrange the 9 Button components in columns, and set the width of each button to 15 pixels. What can be adjusted according to needs is the width of the Button component in the Tkinter window.

Finally, we set up a lambda function to perform specific operations for each Button component. This lambda function is an unnamed function that accepts one parameter representing the number of the Button component. In this example, we set up a lambda function for each Button component to call the Checker component, which is a recursive function that checks the specified number. For specific details on implementing the Checker component, please refer to the individual functions in the Tkinter official documentation.

Function Implementation

We will only show part of the code here, and the rest of the writing is the same.

if digit == 1 and digit in digits:  
digits.remove(digit)  
##player1 will play if the value of count is even and for odd player2 will play  
if count % 2 == 0:  
mark = 'X'  
panels[digit] = mark  
elif count % 2 != 0:  
mark = 'O'  
panels[digit] = mark  
  
button1.config(text=mark)  
count = count + 1  
sign = mark  
  
if (win(panels, sign) and sign == 'X'):  
msg.showinfo("Result", "Player1 wins")  
root.destroy()  
elif (win(panels, sign) and sign == 'O'):  
msg.showinfo("Result", "Player2 wins")  
root.destroy()

Our code defines a counter and two markers, digit and mark. The counter count indicates how many loops to judge and perform the corresponding operation.

If digit is equal to 1 and digit is in digits, then digit is removed from digits and the action of player 1 or 2 is decided based on the parity of count. If count is an even number, player 1 will play the game, otherwise player 2 will play the game.

In each loop, if the remainder of count divided by 2 is 0, then mark is equal to X, and panel[digit] is set to mark. If mark is equal to O, then set panel[digit] to mark.

Then, set mark to the text of button component button1, add count by 1, set sign to mark, and determine the winner of the game based on the value of sign. If player 1 wins the game, the "Result" message is displayed and the window is closed. If player 2 wins the game, the "Result" message is displayed and the window is closed.

In each loop, the winner of the game is determined based on the value of sign. When the game is won by player 1 and sign equals X, a "Result" message will be displayed and the window will be closed. When player 2 wins and sign is O, the "Result" message should be displayed and the window closed.

Effect

How to make a simple tic-tac-toe game based on Python

The above is the detailed content of How to make a simple tic-tac-toe game based on Python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete