猫史档案馆


井字棋编程

用户:L0_0L0_0查看:0 回复:2 评论:0 创建时间:2021-02-24T16:34:40


python编程井字棋

代码如下:

import random
from tkinter import Tk, Button, Label, messagebox

root = Tk()
root.title('Tic Tac Toe')
root.resizable(width=False, height=False)

over = False

def draw(x, y, text):
    global over
    global win_count
    global lose_count
    global draw_count
    if not over:
        if button[x, y]['text'] != '':
            messagebox.showerror('Tic Tac Toe', 'There are already pieces here.')
            return
        button[x, y]['text'] = text
        if win('⭕'):
            over = True
            win_count += 1
            win_text['text'] = 'win: ' + str(win_count)
            messagebox.showinfo('Tic Tac Toe', 'You win!')
            return
        if is_draw():
            over = True
            draw_count += 1
            draw_text['text'] = 'draw: ' + str(draw_count)
            messagebox.showinfo('Tic Tac Toe', 'Draw.')
            return
        a = [0, 1, 2, 3, 4, 5, 6, 7, 8]
        random.shuffle(a)
        for i in range(9):
            b = a[i]
            if button[b%3, b//3]['text'] == '':
                button[b%3, b//3]['text'] = '❌'
                break
        if win('❌'):
            over = True
            lose_count += 1
            lose_text['text'] = 'lose: ' + str(lose_count)
            messagebox.showinfo('Tic Tac Toe', 'You lose!')
        if is_draw():
            over = True
            draw_count += 1
            draw_text['text'] = 'draw: ' + str(draw_count)
            messagebox.showinfo('Tic Tac Toe', 'Draw.')
    else:
        messagebox.showerror('Tic Tac Toe', 'The game is over.')

def show_play():
    messagebox.showinfo('How to play?', "Tic-tac-toe's goal is to win in 3 companies, with ⭕ and ❌ on the grid. First one to three wins.")

def play_new_game():
    global over
    global button
    over = False
    button = {}
    for i in range(9):
        b = Button(root, command=lambda x=i%3, y=i//3: draw(x, y, '⭕'), width=9, height=3)
        b.grid(column=i%3+1, row=i//3)
        button[i%3, i//3] = b
    if random.randint(1, 2) == 1:
        button[random.randint(0, 2), random.randint(0, 2)]['text'] = '❌'

def win(text):
    nw = button[0, 0]['text']
    n = button[1, 0]['text']
    ne = button[2, 0]['text']
    w = button[0, 1]['text']
    centre = button[1, 1]['text']
    e = button[2, 1]['text']
    sw = button[0, 2]['text']
    s = button[1, 2]['text']
    se = button[2, 2]['text']
    return nw == n == ne == text or w == centre == e == text or sw == s == se == text or nw == w == sw == text or n == centre == s == text or ne == e == se == text or nw == centre == se == text or ne == centre == sw == text

def is_draw():
    nw = button[0, 0]['text']
    n = button[1, 0]['text']
    ne = button[2, 0]['text']
    w = button[0, 1]['text']
    centre = button[1, 1]['text']
    e = button[2, 1]['text']
    sw = button[0, 2]['text']
    s = button[1, 2]['text']
    se = button[2, 2]['text']
    return '' not in [nw, n, ne, w, centre, e, sw, s, se] and not (win('⭕') or win('❌'))

button = {}
for i in range(9):
    b = Button(root, command=lambda x=i%3, y=i//3: draw(x, y, '⭕'), width=9, height=3)
    b.grid(column=i%3+1, row=i//3)
    button[i%3, i//3] = b

win_count = 0
lose_count = 0
draw_count =0
win_text = Label(root, width=18, height=3, text='win: 0', anchor='w')
win_text.grid(column=5, row=0)
lose_text = Label(root, width=18, height=3, text='lose: 0', anchor='w')
lose_text.grid(column=5, row=1)
draw_text = Label(root, width=18, height=3, text='draw: 0', anchor='w')
draw_text.grid(column=5, row=2)
new_game = Button(root, width=18, height=3, text='new game', command=play_new_game)
new_game.grid(column=0, row=0)
how_to_play = Button(root, width=18, height=3, text='How to play?', command=show_play)
how_to_play.grid(column=0, row=1)
Control_panel = Label(root, width=18, height=3, text='I: ⭕, computer: ❌\nYou turn.')
Control_panel.grid(column=0, row=2)
if random.randint(1, 2) == 1:
    button[random.randint(0, 2), random.randint(0, 2)]['text'] = '❌'

root.mainloop()

效果预览:

center_image


回复

上一页1 页 / 共 1下一页
蕉饼电脑无限公司蕉饼电脑无限公司

竟然还能这么用

点赞0


评论


ZhaoChenZhaoChen

这电脑技术不行啊

点赞0


评论