首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Tic脚趾游戏在平地机上的修复

Tic脚趾游戏在平地机上的修复
EN

Stack Overflow用户
提问于 2021-12-31 06:59:59
回答 1查看 175关注 0票数 0

我当时正准备在tkinter中玩Tic-Tac-Toe游戏,就像在我的代码中一样,但是有一个问题。在我的算法中,有些语句只允许在同一个框上打印一次X或0,但在许多情况下会重新打印。此外,win_check()函数没有正常工作(在游戏结束时,它应该打印“您赢了!”)或者“你输了!”或者“画图”,它不打印)。

下面是我的代码:

代码语言:javascript
复制
from tkinter import *
import random

root_3t = Tk()
root_3t.attributes('-fullscreen', True)
root_3t.config(bg='black')

Button(root_3t, text='EXIT', bg='red', fg='black', command=root_3t.destroy, font='Elephant 12').pack(anchor=SE, side=BOTTOM)

def game(event):
    text = event.widget.cget('text')
    coordinate = int(text)
  
    positions = [0,1,2,3,4,5,6,7,8]
    win = None
    places = [canvas1, canvas2, canvas3, canvas4, canvas5, canvas6, canvas7, canvas8, canvas9]
    row_1 = [9,9,9] ; row_2 = [9,9,9] ; row_3 = [9,9,9]
   
    def win_check():
        global win
        if row_1[0] == row_1[1] == row_1[2] :
            if row_1[0] == 1:
                Label(root_3t, text='You Win !', bg='black', fg='white', font='Baskerville 16').place(relx=0.6, rely=0.85)
                win = True
            elif row_1[0] == 0:
                Label(root_3t, text='You Win !', bg='black', fg='white', font='Baskerville 16').place(relx=0.6, rely=0.85)                
                win = False
        elif row_2[0] == row_2[1] == row_2[2] :
            if row_2[0] == 1:
                Label(root_3t, text='You Win !', bg='black', fg='white', font='Baskerville 16').place(relx=0.6, rely=0.85)
                win = True
            elif row_2[0] == 0:
                Label(root_3t, text='You Win !', bg='black', fg='white', font='Baskerville 16').place(relx=0.6, rely=0.85)                
                win = False
        elif row_3[0] == row_3[1] == row_3[2] :
            if row_3[0] == 1:
                Label(root_3t, text='You Win !', bg='black', fg='white', font='Baskerville 16').place(relx=0.6, rely=0.85)
                win = True
            elif row_3[0] == 0:
                Label(root_3t, text='You Win !', bg='black', fg='white', font='Baskerville 16').place(relx=0.6, rely=0.85)                
                win = False
        elif row_1[0] == row_2[0] == row_3[0] :
            if row_1[0] == 1:
                Label(root_3t, text='You Win !', bg='black', fg='white', font='Baskerville 16').place(relx=0.6, rely=0.85)
                win = True
            elif row_1[0] == 0:
                Label(root_3t, text='You Win !', bg='black', fg='white', font='Baskerville 16').place(relx=0.6, rely=0.85)                
                win = False
        elif row_1[1] == row_2[1] == row_3[1] :
            if row_1[1] == 1:
                Label(root_3t, text='You Win !', bg='black', fg='white', font='Baskerville 16').place(relx=0.6, rely=0.85)
                win = True
            elif row_1[1] == 0:
                Label(root_3t, text='You Win !', bg='black', fg='white', font='Baskerville 16').place(relx=0.6, rely=0.85)                
                win = False
        elif row_1[2] == row_2[2] == row_3[2] :
            if row_1[2] == 1:
                Label(root_3t, text='You Win !', bg='black', fg='white', font='Baskerville 16').place(relx=0.6, rely=0.85)
                win = True
            elif row_1[2] == 0:
                Label(root_3t, text='You Win !', bg='black', fg='white', font='Baskerville 16').place(relx=0.6, rely=0.85)                
                win = False
        elif row_1[0] == row_2[1] == row_3[2] :
            if row_1[0] == 1:
                Label(root_3t, text='You Win !', bg='black', fg='white', font='Baskerville 16').place(relx=0.6, rely=0.85)
                win = True
            elif row_1[0] == 0:
                Label(root_3t, text='You Win !', bg='black', fg='white', font='Baskerville 16').place(relx=0.6, rely=0.85)                
                win = False
        elif row_1[2] == row_2[1] == row_3[0] :
            if row_1[2] == 1:
                Label(root_3t, text='You Win !', bg='black', fg='white', font='Baskerville 16').place(relx=0.6, rely=0.85)
                win = True
            elif row_1[2] == 0:
                Label(root_3t, text='You Win !', bg='black', fg='white', font='Baskerville 16').place(relx=0.6, rely=0.85)                
                win = False
    
    boxes = 9

    Label(places[coordinate], text='X', font='CASTELLAR 48 bold').place(relx=0.3, rely=0.3)
    if 0<=coordinate<3 :
        row_1[coordinate] = 1
    elif 3<=coordinate<6 :
        row_2[coordinate-3] = 1
    elif 6<=coordinate<9 :
        row_3[coordinate-6] = 1
    boxes -= 1
    positions.remove(coordinate)

    win_check()

    if boxes == 0 and win == None:
        Label(root_3t, text='Draw', bg='black', fg='white', font='Baskerville 16').place(relx=0.6, rely=0.85)

    bot_choice = random.choice(positions)
    Label(places[bot_choice], text='0', font='CASTELLAR 48 bold').place(relx=0.3, rely=0.3)
    if 0<=bot_choice<3 :
        row_1[bot_choice] = 0
    elif 3<=bot_choice<6 :
        row_2[bot_choice-3] = 0
    elif 6<=bot_choice<9 :
        row_3[bot_choice-6] = 0
    boxes -= 1
    positions.remove(bot_choice)

    win_check()

heading_frame = Canvas(root_3t, bg='black', borderwidth=5, relief=FLAT, highlightbackground='silver', height=100)
heading_frame.pack(fill=X)

button_frame = Canvas(root_3t, bg='grey', highlightbackground='silver', borderwidth=2)
button_frame.place(relx=0.02, rely=0.85, width=200, height=100)

main_screen = Canvas(root_3t)

canvas1 = Canvas(main_screen, borderwidth=2, highlightbackground='black', relief=FLAT)
canvas1.place(x=0,y=0,height=130,width=200)    
canvas2 = Canvas(main_screen, borderwidth=2, highlightbackground='black', relief=FLAT)
canvas2.place(x=200,y=0,height=130,width=200)    
canvas3 = Canvas(main_screen, borderwidth=2, highlightbackground='black', relief=FLAT)
canvas3.place(x=400,y=0,height=130,width=200)    
canvas4 = Canvas(main_screen, borderwidth=2, highlightbackground='black', relief=FLAT)
canvas4.place(x=0,y=130,height=130,width=200)    
canvas5 = Canvas(main_screen, borderwidth=2, highlightbackground='black', relief=FLAT)
canvas5.place(x=200,y=130,height=130,width=200)    
canvas6 = Canvas(main_screen, borderwidth=2, highlightbackground='black', relief=FLAT)
canvas6.place(x=400,y=130,height=130,width=200)    
canvas7 = Canvas(main_screen, borderwidth=2, highlightbackground='black', relief=FLAT)
canvas7.place(x=0,y=260,height=130,width=200)    
canvas8 = Canvas(main_screen, borderwidth=2, highlightbackground='black', relief=FLAT)
canvas8.place(x=200,y=260,height=130,width=200)    
canvas9 = Canvas(main_screen, borderwidth=2, highlightbackground='black', relief=FLAT)
canvas9.place(x=400,y=260,height=130,width=200)

main_screen.place(relx=0.5, rely=0.3, width=600, height=390)

increase = 0

for b in range(0,3):
    button = Button(button_frame, text=f"{b}", width=6)
    button.place(relx=0.06+increase*2, rely=0.1)
    button.bind('<Button-1>', game)

    button = Button(button_frame, text=f"{b+3}", width=6)
    button.place(relx=0.06+increase*2, rely=0.38)
    button.bind('<Button-1>', game)

    button = Button(button_frame, text=f"{b+6}", width=6)
    button.place(relx=0.06+increase*2, rely=0.66)
    button.bind('<Button-1>', game)

    increase += 0.15

root_3t.mainloop()
EN

回答 1

Stack Overflow用户

发布于 2021-12-31 11:46:19

它可以是这样的:

代码语言:javascript
复制
from tkinter import *
import random

board = {} # will keep board values and buttons

def update_board(clear=False):
    for row in range(3):
        for col in range(3):
            if clear: board[(row,col)]["value"] = ""
            board[(row,col)]["button"]["text"] = board[(row,col)]["value"]

def enable_board(state):
    for row in range(3):
        for col in range(3):
            board[(row,col)]["button"]["state"] = "normal" if state else "disabled"

def new_game():
    update_board(clear=True)
    enable_board(True)
    status_text["text"] = "Your move"

def make_move(row, col, who):  # returns True if game is finished
    board[(row,col)]["value"] = who
    update_board()
    if win_check(who):
        status_text["text"] = f"You {'Win' if who == 'X' else 'Loose'} !"
        enable_board(False)
        return True
    if all(board[(row,col)]["value"] for row in range(3) for col in range(3)):
        status_text["text"] = "Draw"
        enable_board(False)
        return True
    return False

def ai_move():
    positions = [(row,col) for row in range(3) for col in range(3) if not board[(row,col)]["value"]]
    for pos in positions:
        for who in "XO":
            board[pos]["value"] = who
            win = win_check(who)
            board[pos]["value"] = ""
            if win:
                return pos

    return random.choice(positions)

def win_check(who):
    win  = all(board[(i,  i)]["value"] == who for i in range(3))
    win |= all(board[(i,2-i)]["value"] == who for i in range(3))
    for i in range(3):
        win |= all(board[(i,col)]["value"] == who for col in range(3))
        win |= all(board[(row,i)]["value"] == who for row in range(3))
    return win

def game(row, col):
    if board[(row,col)]["value"]:  # already occupied
        return
    if make_move(row, col, "X"):
        return

    make_move(*ai_move(), "O")

def resize_board(event):
    main_screen["width"] = main_screen.winfo_height()

root_3t = Tk()
root_3t.title("Tic-Tac-Toe")
#root_3t.attributes('-fullscreen', True)
root_3t.config(bg='black')

heading_frame = Frame(root_3t, bg='black', highlightthickness=3, highlightbackground='silver', height=100)
heading_frame.pack(fill=X)
status_text = Label(heading_frame, text="Your move", bg='black', fg='white', font='Baskerville 16')
status_text.pack(anchor="center")

bottom_frame = Frame(root_3t, bg='black')
bottom_frame.pack(fill=X, side=BOTTOM)
Button(bottom_frame, text='NEW GAME', bg='green', fg='black', command=new_game, font='Elephant 12').pack(side=LEFT)
Button(bottom_frame, text='EXIT', bg='red', fg='black', command=root_3t.destroy, font='Elephant 12').pack(side=RIGHT)

main_screen = Frame(root_3t, bg='black', width=420, height=420)
main_screen.pack(side=RIGHT, fill=Y)
main_screen.columnconfigure(tuple(range(3)), minsize=140, weight=1, uniform="board")
main_screen.rowconfigure(tuple(range(3)), minsize=140, weight=1, uniform="board")
main_screen.bind("<Configure>", resize_board)
main_screen.grid_propagate(False)

for row in range(3):
    for col in range(3):
        board[(row,col)] = {"value":"", "button":Button(main_screen, border=0, font='CASTELLAR 48 bold', command=lambda r=row, c=col: game(r, c))}
        board[(row,col)]["button"].grid(row=row, column=col, sticky="news", padx=5, pady=5)

root_3t.mainloop()

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70539779

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档