Python 中的井字游戏

问题描述 投票:0回答:1

我确实用 python 编写了这段代码,一切正常,直到我们检查谁是获胜者,问题是,如果我在一排中有一个 2'X' 和一个 1'O',代码仍然将其视为胜利但不应该,我必须在 1 个或 3 个“O”中包含 3'X' 才能算胜利

所以这是我的问题,我已经被困在这个问题上几个小时了,我将不胜感激任何帮助

提前谢谢您

from tkinter import *
import random
import time

window = Tk()
window.geometry("800x300")
window.config(background="#00d5ff")
winning=[{1,4,7},{1,5,8},{2,5,8},{7,5,3},{3,6,0},{1,2,3},{4,5,6},{7,8,0}]
available=[0,1,2,3,4,5,6,7,8]

x_player=[]
o_player=[]

positions = [(570,149), (350,0), (350,77), (350,149), (460,0), (460,77), (460,149), (570,0), (570,77)]

def place_O(index):
        button[index].destroy()
        labels_o[index].place(x=positions[index][0], y=positions[index][1])
        o_player.append(index)
        game_logic(o_player,'O')
        button[index] = None
        
        

def place_X(index):

        button[index].destroy()
        labels_x[index].place(x=positions[index][0], y=positions[index][1])
        o_player.append(index)
        game_logic(x_player,'X')
        button[index] = None
        computer()
        

def gride():
    omar = (
        "     |     |     \n"
        "_____|_____|_____\n"
        "     |     |     \n"
        "_____|_____|_____\n"
        "     |     |     \n"
        "     |     |     "
    )
    label = Label(window, text=omar, font=("Courier", 24, "bold"))
    label.place(x=330, y=0)

def labelss():
    global labels_o
    global labels_x
    labels_x = [Label(window, text="X", font=("bold", 25)) for _ in range(9)]
    labels_o = [Label(window, text="O", font=("bold", 25)) for _ in range(9)]

def game_logic(player, a):
    for move in winning:

        if all(single in player for single in move):

            if all(btn in player for btn in move):
                print(f"{a} won!!")
                disabling()
                break

            
def disabling():
    for ele in x_player + o_player:
        if ele in available:
            available.remove(ele)
    
    for sub in available:
        button[sub].config(state=DISABLED)

           
           
     
def buttons():
    global button
    button = [Button(window, text=f"CH{i}", font=("Arial", 24, "bold"), command=lambda i=i: place_X(i)) for i in range(9)]
    
    button[0].place(x=560, y=149)
    button[1].place(x=340, y=0)
    button[2].place(x=340, y=77)
    button[3].place(x=340, y=149)
    button[4].place(x=450, y=0)
    button[5].place(x=450, y=77)
    button[6].place(x=450, y=149)
    button[7].place(x=560, y=0)
    button[8].place(x=560, y=77)

def computer():
    empty_buttons = []
    for i in range(9):
     if button[i] is not None:
        empty_buttons.append(i)
    if empty_buttons:
        computer_choice = random.choice(empty_buttons)
        place_O(computer_choice)
        

def whole_game():
    gride()
    labelss()
    buttons()
    
if __name__ == '__main__':
    whole_game()

window.mainloop()
python tic-tac-toe
1个回答
-1
投票

问题出在你的

place_X()
函数上。 它有
o_player.append(index)
而不是
x_player.append(index)

这应该有效。

from tkinter import *
import random
import time

window = Tk()
window.geometry("800x300")
window.config(background="#00d5ff")
winning=[{1,4,7},{1,5,8},{2,5,8},{7,5,3},{3,6,0},{1,2,3},{4,5,6},{7,8,0}]
available=[0,1,2,3,4,5,6,7,8]

x_player=[]
o_player=[]

positions = [(570,149), (350,0), (350,77), (350,149), (460,0), (460,77), (460,149), (570,0), (570,77)]

def place_O(index):
        button[index].destroy()
        labels_o[index].place(x=positions[index][0], y=positions[index][1])
        o_player.append(index)
        game_logic(o_player,'O')
        button[index] = None



def place_X(index):

        button[index].destroy()
        labels_x[index].place(x=positions[index][0], y=positions[index][1])
        x_player.append(index)
        game_logic(x_player,'X')
        button[index] = None
        computer()


def gride():
    omar = (
        "     |     |     \n"
        "_____|_____|_____\n"
        "     |     |     \n"
        "_____|_____|_____\n"
        "     |     |     \n"
        "     |     |     "
    )
    label = Label(window, text=omar, font=("Courier", 24, "bold"))
    label.place(x=330, y=0)

def labelss():
    global labels_o
    global labels_x
    labels_x = [Label(window, text="X", font=("bold", 25)) for _ in range(9)]
    labels_o = [Label(window, text="O", font=("bold", 25)) for _ in range(9)]

def game_logic(player, a):
    for move in winning:

        if all(single in player for single in move):

            if all(btn in player for btn in move):
                print(f"{a} won!!")
                disabling()
                break


def disabling():
    for ele in x_player + o_player:
        if ele in available:
            available.remove(ele)

    for sub in available:
        button[sub].config(state=DISABLED)




def buttons():
    global button
    button = [Button(window, text=f"CH{i}", font=("Arial", 24, "bold"), command=lambda i=i: place_X(i)) for i in range(9)]

    button[0].place(x=560, y=149)
    button[1].place(x=340, y=0)
    button[2].place(x=340, y=77)
    button[3].place(x=340, y=149)
    button[4].place(x=450, y=0)
    button[5].place(x=450, y=77)
    button[6].place(x=450, y=149)
    button[7].place(x=560, y=0)
    button[8].place(x=560, y=77)

def computer():
    empty_buttons = []
    for i in range(9):
     if button[i] is not None:
        empty_buttons.append(i)
    if empty_buttons:
        computer_choice = random.choice(empty_buttons)
        place_O(computer_choice)


def whole_game():
    gride()
    labelss()
    buttons()

if __name__ == '__main__':
    whole_game()

window.mainloop()

我唯一更改的是第 29 行。如果您愿意,您可以自行更改。

© www.soinside.com 2019 - 2024. All rights reserved.