How to write a simple tic-tac-toe game in Python?

王林
Release: 2023-05-08 23:43:17
forward
2392 people have browsed it

Window

Universal window, the realization window can be simply modified and used:

from tkinter import *
root = Tk()         #窗口名称
root.title("憨憨制作的三子棋")
f1=Frame(root)
f1.pack()
w1 = Canvas(f1, width=580,height=580,background='lightcyan')#创建一个画布,设置大小和背景颜色
w1.pack()
root.mainloop()
Copy after login

How to write a simple tic-tac-toe game in Python?

Draw the chessboard

#画出棋盘
for i in range(0, 4):
    w1.create_line(i * 180 + 20, 20, i * 180 + 20, 560)
    w1.create_line(20, i * 180 + 20, 560, i * 180 + 20)
Copy after login

How to write a simple tic-tac-toe game in Python?

Draw horizontal and vertical lines respectively, four horizontal lines and four vertical lines to generate nine grids. You can also draw two horizontal and two vertical lines, which is closer to the tic-tac-toe shape. Just draw a few less lines, for example:

for i in range(1, 3):
    w1.create_line(i * 180 + 20, 20, i * 180 + 20, 560)
    w1.create_line(20, i * 180 + 20, 560, i * 180 + 20)
Copy after login

How to write a simple tic-tac-toe game in Python?

num = 0       #记录点击的次数,用来决定点击后该画哪种图形
A = np.full((3, 3), 0)            #记录每个位置的图形
Copy after login

Algorithm

After each click, we have to draw a picture and determine whether the winning conditions are met. .

Setting function

def dawn(event):
    global w1
    global num, A
    for i in range(0, 3):
        for j in range(0, 3):
            if 20 + j * 180 < event.y and event.y <= 20 + (j+1) * 180:
                break
        if 20 + i * 180 <= event.x and event.x <= 20 + (i+1) * 180:
            break
    if num % 2 == 0 and A[i][j] == 0:
        A[i][j] = 1
        w1.create_line(110 + 180 * i - 45 * math.sqrt(2), 110 + 180 * j - 45 * math.sqrt(2),
                       110 + 180 * i + 45 * math.sqrt(2), 110 + 180 * j + 45 * math.sqrt(2))
        w1.create_line(110 + 180 * i + 45 * math.sqrt(2), 110 + 180 * j - 45 * math.sqrt(2),
                       110 + 180 * i - 45 * math.sqrt(2), 110 + 180 * j + 45 * math.sqrt(2))
        num += 1
    if num % 2 != 0 and A[i][j] == 0:
        A[i][j] = 2
        w1.create_oval(20 + 180 * i, 20 + 180 * j, 20 + 180 * (i + 1), 20 + 180 * (j + 1))
        num += 1
    if A[0][0] == A[0][1] == A[0][2] == 2 or A[1][0] == A[1][1] == A[1][2] == 2 or A[2][0] == A[2][1] == A[2][
        2] == 2 or \
            A[0][0] == A[1][0] == A[2][0] == 2 or A[0][1] == A[1][1] == A[2][1] == 2 or A[0][2] == A[1][2] == \
            A[2][
                2] == 2 or \
            A[0][0] == A[1][1] == A[2][2] == 2 or A[2][0] == A[1][1] == A[0][2] == 2:
        tkinter.messagebox.showinfo(&#39;消息提示&#39;, &#39;圆圈获胜&#39;)
    elif A[0][0] == A[0][1] == A[0][2] == 1 or A[1][0] == A[1][1] == A[1][2] == 1 or A[2][0] == A[2][1] == A[2][
        2] == 1 or \
            A[0][0] == A[1][0] == A[2][0] == 1 or A[0][1] == A[1][1] == A[2][1] == 1 or A[0][2] == A[1][2] == \
            A[2][
                2] == 1 or \
            A[0][0] == A[1][1] == A[2][2] == 1 or A[2][0] == A[1][1] == A[0][2] == 1:
        tkinter.messagebox.showinfo(&#39;消息提示&#39;, &#39;叉号获胜&#39;)
w1.bind("<Button -1>", dawn)
Copy after login

Let’s break it down for analysis:

The first thing is: determine the click position and get our click cell. At the beginning, I used judgment Whether it is within the inscribed circle of the square, it is found that clicking on the remaining part of the square will cause the graphics to be misaligned. Through further analysis, it is found that the clicked square can be determined by respectively locating the horizontal and vertical coordinates of the clicked position.

for i in range(0, 3):
        for j in range(0, 3):
            if 20 + j * 180 < event.y and event.y <= 20 + (j+1) * 180:
                break
        if 20 + i * 180 <= event.x and event.x <= 20 + (i+1) * 180:
            break
Copy after login

Next, after determining the position, we start to judge whether the current position can draw graphics and what graphics should be drawn. Drawing ❌ is more troublesome and requires complex calculations, but drawing ⚪ is relatively simple.

if num % 2 == 0 and A[i][j] == 0:            #若为偶数就画叉号
        A[i][j] = 1
        w1.create_line(110 + 180 * i - 45 * math.sqrt(2), 110 + 180 * j - 45 * math.sqrt(2),
                       110 + 180 * i + 45 * math.sqrt(2), 110 + 180 * j + 45 * math.sqrt(2))
        w1.create_line(110 + 180 * i + 45 * math.sqrt(2), 110 + 180 * j - 45 * math.sqrt(2),
                       110 + 180 * i - 45 * math.sqrt(2), 110 + 180 * j + 45 * math.sqrt(2))
        num += 1
    if num % 2 != 0 and A[i][j] == 0:        #若为奇数就画圆圈
        A[i][j] = 2
        w1.create_oval(20 + 180 * i, 20 + 180 * j, 20 + 180 * (i + 1), 20 + 180 * (j + 1))
        num += 1
Copy after login

After drawing, we have to judge whether the winning conditions are met. I didn’t think of a simple method here, but fortunately there are only a handful of winning situations, only eight (horizontal 3, vertical 3, two diagonal Direction) So eight situations are listed to judge whether the winning conditions are met, and both graphics need to be listed.

if A[0][0] == A[0][1] == A[0][2] == 2 or A[1][0] == A[1][1] == A[1][2] == 2 or A[2][0] == A[2][1] == A[2][
        2] == 2 or \
            A[0][0] == A[1][0] == A[2][0] == 2 or A[0][1] == A[1][1] == A[2][1] == 2 or A[0][2] == A[1][2] == \
            A[2][
                2] == 2 or \
            A[0][0] == A[1][1] == A[2][2] == 2 or A[2][0] == A[1][1] == A[0][2] == 2:
        tkinter.messagebox.showinfo(&#39;消息提示&#39;, &#39;圆圈获胜&#39;)
    elif A[0][0] == A[0][1] == A[0][2] == 1 or A[1][0] == A[1][1] == A[1][2] == 1 or A[2][0] == A[2][1] == A[2][
        2] == 1 or \
            A[0][0] == A[1][0] == A[2][0] == 1 or A[0][1] == A[1][1] == A[2][1] == 1 or A[0][2] == A[1][2] == \
            A[2][
                2] == 1 or \
            A[0][0] == A[1][1] == A[2][2] == 1 or A[2][0] == A[1][1] == A[0][2] == 1:
        tkinter.messagebox.showinfo(&#39;消息提示&#39;, &#39;叉号获胜&#39;)
Copy after login

Finally set the acquisition of click time and the setting of exit button.

w1.bind("<Button -1>", dawn)
def quit():
    root.quit()
button1 = Button(root, text="退出", font=(&#39;楷体&#39;, 20), command=quit)
button1.pack()
Copy after login

Every code of the game has been explained here.

How to write a simple tic-tac-toe game in Python?

Attached is the complete code:

from tkinter import *
import numpy as np
import math
import tkinter.messagebox
root = Tk()         #窗口名称
root.title("憨憨制作的三子棋")
f1=Frame(root)
f1.pack()
w1 = Canvas(f1, width=580,height=580,background='lightcyan')
w1.pack()


#棋盘
for i in range(0, 4):
    w1.create_line(i * 180 + 20, 20, i * 180 + 20, 560)
    w1.create_line(20, i * 180 + 20, 560, i * 180 + 20)
num = 0
A = np.full((3, 3), 0)

def dawn(event):
    global w1
    global num, A
    for i in range(0, 3):
        for j in range(0, 3):
            if 20 + j * 180 < event.y and event.y <= 20 + (j+1) * 180:
                break
        if 20 + i * 180 <= event.x and event.x <= 20 + (i+1) * 180:
            break
    if num % 2 == 0 and A[i][j] == 0:
        A[i][j] = 1
        w1.create_line(110 + 180 * i - 45 * math.sqrt(2), 110 + 180 * j - 45 * math.sqrt(2),
                       110 + 180 * i + 45 * math.sqrt(2), 110 + 180 * j + 45 * math.sqrt(2))
        w1.create_line(110 + 180 * i + 45 * math.sqrt(2), 110 + 180 * j - 45 * math.sqrt(2),
                       110 + 180 * i - 45 * math.sqrt(2), 110 + 180 * j + 45 * math.sqrt(2))
        num += 1
    if num % 2 != 0 and A[i][j] == 0:
        A[i][j] = 2
        w1.create_oval(20 + 180 * i, 20 + 180 * j, 20 + 180 * (i + 1), 20 + 180 * (j + 1))
        num += 1
    if A[0][0] == A[0][1] == A[0][2] == 2 or A[1][0] == A[1][1] == A[1][2] == 2 or A[2][0] == A[2][1] == A[2][
        2] == 2 or \
            A[0][0] == A[1][0] == A[2][0] == 2 or A[0][1] == A[1][1] == A[2][1] == 2 or A[0][2] == A[1][2] == \
            A[2][
                2] == 2 or \
            A[0][0] == A[1][1] == A[2][2] == 2 or A[2][0] == A[1][1] == A[0][2] == 2:
        tkinter.messagebox.showinfo(&#39;消息提示&#39;, &#39;圆圈获胜&#39;)
    elif A[0][0] == A[0][1] == A[0][2] == 1 or A[1][0] == A[1][1] == A[1][2] == 1 or A[2][0] == A[2][1] == A[2][
        2] == 1 or \
            A[0][0] == A[1][0] == A[2][0] == 1 or A[0][1] == A[1][1] == A[2][1] == 1 or A[0][2] == A[1][2] == \
            A[2][
                2] == 1 or \
            A[0][0] == A[1][1] == A[2][2] == 1 or A[2][0] == A[1][1] == A[0][2] == 1:
        tkinter.messagebox.showinfo(&#39;消息提示&#39;, &#39;叉号获胜&#39;)
w1.bind("<Button -1>", dawn)
def quit():
    root.quit()
button1 = Button(root, text="退出", font=('楷体', 20), command=quit)
button1.pack()
root.mainloop()
Copy after login

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

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template