Tic Tac Toe

Fragen zu Tkinter.
Antworten
stef.kr
User
Beiträge: 4
Registriert: Dienstag 9. Juni 2020, 18:55

Hallo! Ich muss für ein Schul Projekt ein Python-Programm schreiben mit welchem man Tic Tac toe spielen kann. In diesem Programm soll ein Bot zufällig Züge übernehmen, so dass es möglich ist alleine zu Spielen, ich habe jedoch keine Idee wie ich das implementieren soll. Ich wäre froh wenn mir jemand dabei helfen könnte :)
__deets__
User
Beiträge: 14545
Registriert: Mittwoch 14. Oktober 2015, 14:29

Dann fang damit an, das du dir erst mal ein Python-Programm schreibst, mit dem man zu zweit spielen kann. Und dann uebernimmt einfach ein Bot mit zufaelligen Zuegen den einen Spieler davon.
stef.kr
User
Beiträge: 4
Registriert: Dienstag 9. Juni 2020, 18:55

Also ich habe bereits ein Programm mit dem man zu zwei spielen kann, das einzige was mir fehlt ist ein Button der einen zufälligen Zug macht.
Kann ich hier irgendwie meinen Code anheften?
Danke für die schnelle Antwort!
__deets__
User
Beiträge: 14545
Registriert: Mittwoch 14. Oktober 2015, 14:29

Du kannst den einkopieren, und dann musst du ihn markieren, und auf den </>-Knopf druecen (im volllstaendigen Editor!).
stef.kr
User
Beiträge: 4
Registriert: Dienstag 9. Juni 2020, 18:55

Ich hoffe das hilft weiter

Code: Alles auswählen

import tkinter.messagebox
from tkinter import*

# Zuerst erstelle ich eine Benutzeroberfläche und definiere einige Dinge.

root =Tk()
root.geometry("1350x750+0+0")
root.title("Tic Tac Toe")
root.configure(background ='Lightskyblue')

Tops = Frame(root, bg ='Lightskyblue', pady =2, width =1350, height=100, relief =RIDGE)
Tops.grid(row=0, column=0)

lblTitle = Label(Tops, font=('arial',50,'bold'), text="Tic Tac Toe lol", bd=21, bg='Lightskyblue',fg='Cornsilk',justify=CENTER)
lblTitle.grid(row=0,column=0)

MainFrame = Frame(root, bg ='Lightcyan', pady =2, width =1350, height=600, relief =RIDGE)
MainFrame.grid(row=1, column=0)

LeftFrame = Frame(MainFrame ,bd=10, width =750, height=500, pady=2,padx=10,bg="Lightskyblue" , relief=RIDGE)
LeftFrame.pack(side=LEFT)

RightFrame = Frame(MainFrame ,bd=10, width=560, height=500, padx=10,pady=2, bg="Lightskyblue" , relief=RIDGE)
RightFrame.pack(side=RIGHT)

RightFrame1 = Frame(RightFrame ,bd=10, width =560, height=200, padx=10,pady=2, bg="Lightskyblue" , relief=RIDGE)
RightFrame1.grid(row=0, column=0)

RightFrame2 = Frame(RightFrame ,bd=10, width =560, height=200, padx=10,pady=2, bg="Lightskyblue" , relief=RIDGE)
RightFrame2.grid(row=1,column=0)


SpielerX=IntVar()
SpielerO=IntVar()

SpielerX.set(0)
SpielerO.set(0)

buttons = StringVar()
click = True

def checker(buttons):
    global click
    if buttons["text"] == " " and click == True:
        buttons["text"] = "X"
        click = False
        scorekeeper()
    elif buttons["text"] == " " and click == False:
        buttons["text"] = "O"
        click = True
        scorekeeper()

#Hier kümmere ich mich um den Spielmechanismus und die Logik.

def scorekeeper():
    if(button1['text']=="X" and button2['text']=="X" and button3['text']=="X"):
        button1.configure(background ="lavender")
        button2.configure(background ="lavender")
        button3.configure(background ="lavender")
        n = float(SpielerX.get())
        score = (n+1)
        SpielerX.set(score)
        tkinter.messagebox.showinfo("Spieler 1 gewinnt")
        button1['text']=" "
        button2['text']=" "
        button3['text']=" "
        button4['text']=" "
        button5['text']=" "
        button6['text']=" "
        button7['text']=" "
        button8['text']=" "
        button9['text']=" "

        button1.configure(background ="mintcream")
        button2.configure(background ="mintcream")
        button3.configure(background ="mintcream")
        button4.configure(background ="mintcream")
        button5.configure(background ="mintcream")
        button6.configure(background ="mintcream")
        button7.configure(background ="mintcream")
        button8.configure(background ="mintcream")
        button9.configure(background ="mintcream")
        

    if(button4['text']=="X" and button5['text']=="X" and button6['text']=="X"):
        button4.configure(background ="lavender")
        button5.configure(background ="lavender")
        button6.configure(background ="lavender")
        n = float(SpielerX.get())
        score = (n+1)
        SpielerX.set(score)
        tkinter.messagebox.showinfo("Spieler 1 gewinnt")
        button1['text']=" "
        button2['text']=" "
        button3['text']=" "
        button4['text']=" "
        button5['text']=" "
        button6['text']=" "
        button7['text']=" "
        button8['text']=" "
        button9['text']=" "

        button1.configure(background ="mintcream")
        button2.configure(background ="mintcream")
        button3.configure(background ="mintcream")
        button4.configure(background ="mintcream")
        button5.configure(background ="mintcream")
        button6.configure(background ="mintcream")
        button7.configure(background ="mintcream")
        button8.configure(background ="mintcream")
        button9.configure(background ="mintcream")

    if(button7['text']=="X" and button8['text']=="X" and button9['text']=="X"):
        button7.configure(background ="lavender")
        button8.configure(background ="lavender")
        button9.configure(background ="lavender")
        n = float(SpielerX.get())
        score = (n+1)
        SpielerX.set(score)
        tkinter.messagebox.showinfo("Spieler 1 gewinnt")
        button1['text']=" "
        button2['text']=" "
        button3['text']=" "
        button4['text']=" "
        button5['text']=" "
        button6['text']=" "
        button7['text']=" "
        button8['text']=" "
        button9['text']=" "

        button1.configure(background ="mintcream")
        button2.configure(background ="mintcream")
        button3.configure(background ="mintcream")
        button4.configure(background ="mintcream")
        button5.configure(background ="mintcream")
        button6.configure(background ="mintcream")
        button7.configure(background ="mintcream")
        button8.configure(background ="mintcream")
        button9.configure(background ="mintcream")

    if(button1['text']=="X" and button4['text']=="X" and button7['text']=="X"):
        button1.configure(background ="lavender")
        button4.configure(background ="lavender")
        button7.configure(background ="lavender")
        n = float(SpielerX.get())
        score = (n+1)
        SpielerX.set(score)
        tkinter.messagebox.showinfo("Spieler 1 gewinnt")
        button1['text']=" "
        button2['text']=" "
        button3['text']=" "
        button4['text']=" "
        button5['text']=" "
        button6['text']=" "
        button7['text']=" "
        button8['text']=" "
        button9['text']=" "

        button1.configure(background ="mintcream")
        button2.configure(background ="mintcream")
        button3.configure(background ="mintcream")
        button4.configure(background ="mintcream")
        button5.configure(background ="mintcream")
        button6.configure(background ="mintcream")
        button7.configure(background ="mintcream")
        button8.configure(background ="mintcream")
        button9.configure(background ="mintcream")

    if(button5['text']=="X" and button2['text']=="X" and button8['text']=="X"):
        button5.configure(background ="lavender")
        button2.configure(background ="lavender")
        button8.configure(background ="lavender")
        n = float(SpielerX.get())
        score = (n+1)
        SpielerX.set(score)
        tkinter.messagebox.showinfo("Spieler 1 gewinnt")
        button1['text']=" "
        button2['text']=" "
        button3['text']=" "
        button4['text']=" "
        button5['text']=" "
        button6['text']=" "
        button7['text']=" "
        button8['text']=" "
        button9['text']=" "

        button1.configure(background ="mintcream")
        button2.configure(background ="mintcream")
        button3.configure(background ="mintcream")
        button4.configure(background ="mintcream")
        button5.configure(background ="mintcream")
        button6.configure(background ="mintcream")
        button7.configure(background ="mintcream")
        button8.configure(background ="mintcream")
        button9.configure(background ="mintcream")

    if(button6['text']=="X" and button9['text']=="X" and button3['text']=="X"):
        button6.configure(background ="lavender")
        button9.configure(background ="lavender")
        button3.configure(background ="lavender")
        n = float(SpielerX.get())
        score = (n+1)
        SpielerX.set(score)
        tkinter.messagebox.showinfo("Spieler 1 gewinnt")
        button1['text']=" "
        button2['text']=" "
        button3['text']=" "
        button4['text']=" "
        button5['text']=" "
        button6['text']=" "
        button7['text']=" "
        button8['text']=" "
        button9['text']=" "

        button1.configure(background ="mintcream")
        button2.configure(background ="mintcream")
        button3.configure(background ="mintcream")
        button4.configure(background ="mintcream")
        button5.configure(background ="mintcream")
        button6.configure(background ="mintcream")
        button7.configure(background ="mintcream")
        button8.configure(background ="mintcream")
        button9.configure(background ="mintcream")

    if(button1['text']=="X" and button5['text']=="X" and button9['text']=="X"):
        button1.configure(background ="lavender")
        button5.configure(background ="lavender")
        button9.configure(background ="lavender")
        n = float(SpielerX.get())
        score = (n+1)
        SpielerX.set(score)
        tkinter.messagebox.showinfo("Spieler 1 gewinnt")
        button1['text']=" "
        button2['text']=" "
        button3['text']=" "
        button4['text']=" "
        button5['text']=" "
        button6['text']=" "
        button7['text']=" "
        button8['text']=" "
        button9['text']=" "

        button1.configure(background ="mintcream")
        button2.configure(background ="mintcream")
        button3.configure(background ="mintcream")
        button4.configure(background ="mintcream")
        button5.configure(background ="mintcream")
        button6.configure(background ="mintcream")
        button7.configure(background ="mintcream")
        button8.configure(background ="mintcream")
        button9.configure(background ="mintcream")

    if(button3['text']=="X" and button5['text']=="X" and button7['text']=="X"):
        button3.configure(background ="lavender")
        button5.configure(background ="lavender")
        button7.configure(background ="lavender")
        n = float(SpielerX.get())
        score = (n+1)
        SpielerX.set(score)
        tkinter.messagebox.showinfo("Spieler 1 gewinnt")
        button1['text']=" "
        button2['text']=" "
        button3['text']=" "
        button4['text']=" "
        button5['text']=" "
        button6['text']=" "
        button7['text']=" "
        button8['text']=" "
        button9['text']=" "

        button1.configure(background ="mintcream")
        button2.configure(background ="mintcream")
        button3.configure(background ="mintcream")
        button4.configure(background ="mintcream")
        button5.configure(background ="mintcream")
        button6.configure(background ="mintcream")
        button7.configure(background ="mintcream")
        button8.configure(background ="mintcream")
        button9.configure(background ="mintcream")

    if(button1['text']=="O" and button2['text']=="O" and button3['text']=="O"):
        button1.configure(background ="lavender")
        button2.configure(background ="lavender")
        button3.configure(background ="lavender")
        n = float(SpielerO.get())
        score = (n+1)
        SpielerO.set(score)
        tkinter.messagebox.showinfo("Spieler 2 gewinnt")
        button1['text']=" "
        button2['text']=" "
        button3['text']=" "
        button4['text']=" "
        button5['text']=" "
        button6['text']=" "
        button7['text']=" "
        button8['text']=" "
        button9['text']=" "

        button1.configure(background ="mintcream")
        button2.configure(background ="mintcream")
        button3.configure(background ="mintcream")
        button4.configure(background ="mintcream")
        button5.configure(background ="mintcream")
        button6.configure(background ="mintcream")
        button7.configure(background ="mintcream")
        button8.configure(background ="mintcream")
        button9.configure(background ="mintcream")

    if(button4['text']=="O" and button5['text']=="O" and button6['text']=="O"):
        button4.configure(background ="lavender")
        button5.configure(background ="lavender")
        button6.configure(background ="lavender")
        n = float(SpielerO.get())
        score = (n+1)
        SpielerO.set(score)
        tkinter.messagebox.showinfo("Spieler 2 gewinnt")
        button1['text']=" "
        button2['text']=" "
        button3['text']=" "
        button4['text']=" "
        button5['text']=" "
        button6['text']=" "
        button7['text']=" "
        button8['text']=" "
        button9['text']=" "

        button1.configure(background ="mintcream")
        button2.configure(background ="mintcream")
        button3.configure(background ="mintcream")
        button4.configure(background ="mintcream")
        button5.configure(background ="mintcream")
        button6.configure(background ="mintcream")
        button7.configure(background ="mintcream")
        button8.configure(background ="mintcream")
        button9.configure(background ="mintcream")

    if(button7['text']=="O" and button8['text']=="O" and button9['text']=="O"):
        button7.configure(background ="lavender")
        button8.configure(background ="lavender")
        button9.configure(background ="lavender")
        n = float(SpielerO.get())
        score = (n+1)
        SpielerO.set(score)
        tkinter.messagebox.showinfo("Spieler 2 gewinnt")
        button1['text']=" "
        button2['text']=" "
        button3['text']=" "
        button4['text']=" "
        button5['text']=" "
        button6['text']=" "
        button7['text']=" "
        button8['text']=" "
        button9['text']=" "

        button1.configure(background ="mintcream")
        button2.configure(background ="mintcream")
        button3.configure(background ="mintcream")
        button4.configure(background ="mintcream")
        button5.configure(background ="mintcream")
        button6.configure(background ="mintcream")
        button7.configure(background ="mintcream")
        button8.configure(background ="mintcream")
        button9.configure(background ="mintcream")

    if(button1['text']=="O" and button4['text']=="O" and button7['text']=="O"):
        button1.configure(background ="lavender")
        button4.configure(background ="lavender")
        button7.configure(background ="lavender")
        n = float(SpielerO.get())
        score = (n+1)
        SpielerO.set(score)
        tkinter.messagebox.showinfo("Spieler 2 gewinnt")
        button1['text']=" "
        button2['text']=" "
        button3['text']=" "
        button4['text']=" "
        button5['text']=" "
        button6['text']=" "
        button7['text']=" "
        button8['text']=" "
        button9['text']=" "

        button1.configure(background ="mintcream")
        button2.configure(background ="mintcream")
        button3.configure(background ="mintcream")
        button4.configure(background ="mintcream")
        button5.configure(background ="mintcream")
        button6.configure(background ="mintcream")
        button7.configure(background ="mintcream")
        button8.configure(background ="mintcream")
        button9.configure(background ="mintcream")

    if(button5['text']=="O" and button2['text']=="O" and button8['text']=="O"):
        button5.configure(background ="lavender")
        button2.configure(background ="lavender")
        button8.configure(background ="lavender")
        n = float(SpielerO.get())
        score = (n+1)
        SpielerO.set(score)
        tkinter.messagebox.showinfo("Spieler 2 gewinnt")
        button1['text']=" "
        button2['text']=" "
        button3['text']=" "
        button4['text']=" "
        button5['text']=" "
        button6['text']=" "
        button7['text']=" "
        button8['text']=" "
        button9['text']=" "

        button1.configure(background ="mintcream")
        button2.configure(background ="mintcream")
        button3.configure(background ="mintcream")
        button4.configure(background ="mintcream")
        button5.configure(background ="mintcream")
        button6.configure(background ="mintcream")
        button7.configure(background ="mintcream")
        button8.configure(background ="mintcream")
        button9.configure(background ="mintcream")

    if(button6['text']=="O" and button9['text']=="O" and button3['text']=="O"):
        button6.configure(background ="lavender")
        button9.configure(background ="lavender")
        button3.configure(background ="lavender")
        n = float(SpielerO.get())
        score = (n+1)
        SpielerO.set(score)
        tkinter.messagebox.showinfo("Spieler 2 gewinnt")
        button1['text']=" "
        button2['text']=" "
        button3['text']=" "
        button4['text']=" "
        button5['text']=" "
        button6['text']=" "
        button7['text']=" "
        button8['text']=" "
        button9['text']=" "

        button1.configure(background ="mintcream")
        button2.configure(background ="mintcream")
        button3.configure(background ="mintcream")
        button4.configure(background ="mintcream")
        button5.configure(background ="mintcream")
        button6.configure(background ="mintcream")
        button7.configure(background ="mintcream")
        button8.configure(background ="mintcream")
        button9.configure(background ="mintcream")

    if(button1['text']=="O" and button5['text']=="O" and button9['text']=="O"):
        button1.configure(background ="lavender")
        button5.configure(background ="lavender")
        button9.configure(background ="lavender")
        n = float(SpielerO.get())
        score = (n+1)
        SpielerO.set(score)
        tkinter.messagebox.showinfo("Spieler 2 gewinnt")
        button1['text']=" "
        button2['text']=" "
        button3['text']=" "
        button4['text']=" "
        button5['text']=" "
        button6['text']=" "
        button7['text']=" "
        button8['text']=" "
        button9['text']=" "

        button1.configure(background ="mintcream")
        button2.configure(background ="mintcream")
        button3.configure(background ="mintcream")
        button4.configure(background ="mintcream")
        button5.configure(background ="mintcream")
        button6.configure(background ="mintcream")
        button7.configure(background ="mintcream")
        button8.configure(background ="mintcream")
        button9.configure(background ="mintcream")

    if(button3['text']=="O" and button5['text']=="O" and button7['text']=="O"):
        button3.configure(background ="lavender")
        button5.configure(background ="lavender")
        button7.configure(background ="lavender")
        n = float(SpielerO.get())
        score = (n+1)
        SpielerO.set(score)
        tkinter.messagebox.showinfo("Spieler 2 gewinnt")
        button1['text']=" "
        button2['text']=" "
        button3['text']=" "
        button4['text']=" "
        button5['text']=" "
        button6['text']=" "
        button7['text']=" "
        button8['text']=" "
        button9['text']=" "

        button1.configure(background ="mintcream")
        button2.configure(background ="mintcream")
        button3.configure(background ="mintcream")
        button4.configure(background ="mintcream")
        button5.configure(background ="mintcream")
        button6.configure(background ="mintcream")
        button7.configure(background ="mintcream")
        button8.configure(background ="mintcream")
        button9.configure(background ="mintcream")

def reset():
    button1['text']=" "
    button2['text']=" "
    button3['text']=" "
    button4['text']=" "
    button5['text']=" "
    button6['text']=" "
    button7['text']=" "
    button8['text']=" "
    button9['text']=" "

    button1.configure(background ="mintcream")
    button2.configure(background ="mintcream")
    button3.configure(background ="mintcream")
    button4.configure(background ="mintcream")
    button5.configure(background ="mintcream")
    button6.configure(background ="mintcream")
    button7.configure(background ="mintcream")
    button8.configure(background ="mintcream")
    button9.configure(background ="mintcream")

def neuesspiel():
    button1['text']=" "
    button2['text']=" "
    button3['text']=" "
    button4['text']=" "
    button5['text']=" "
    button6['text']=" "
    button7['text']=" "
    button8['text']=" "
    button9['text']=" "

    button1.configure(background ="mintcream")
    button2.configure(background ="mintcream")
    button3.configure(background ="mintcream")
    button4.configure(background ="mintcream")
    button5.configure(background ="mintcream")
    button6.configure(background ="mintcream")
    button7.configure(background ="mintcream")
    button8.configure(background ="mintcream")
    button9.configure(background ="mintcream")

    SpielerX.set(0)
    SpielerO.set(0)
    

#Hier füge ich einen Zähler für die gewonnenen Spiele hinzu

lblSpielerX = Label(RightFrame1, font=('arial',40,'bold'), text="Spieler 1:",padx=2,pady=2, bg='Lightskyblue')
lblSpielerX.grid(row=0,column=0,sticky=W)
txtPlayerX=Entry(RightFrame1,font=('arial',40,'bold'),bd=2,fg="black",textvariable= SpielerX, width=14,
                 justify=LEFT).grid(row=0,column=1)

lblSpielerO = Label(RightFrame1, font=('arial',40,'bold'), text="Spieler 2:",padx=2,pady=2, bg='Lightskyblue')
lblSpielerO.grid(row=1,column=0,sticky=W)
txtPlayerX=Entry(RightFrame1,font=('arial',40,'bold'),bd=2,fg="black",textvariable= SpielerO, width=14,
                 justify=LEFT).grid(row=1,column=1)

#Danach füge ich Buttons hinzu, welche dem Spieler verschiedene möglichkeiten geben

zufall=Button(RightFrame2, text="Zufällig", font=('arial 26 bold'),height = 3, width=10, bg='mintcream')
zufall.grid(row=1, column=2)

reset=Button(RightFrame2, text="Reset", font=('arial 26 bold'), height = 3, width=10, bg='mintcream', command = reset)
reset.grid(row=1, column=0)

neuesspiel=Button(RightFrame2, text="Neues Spiel", font=('arial 26 bold'), height = 3, width=10, bg='mintcream', command = neuesspiel)
neuesspiel.grid(row=1, column=1)

#Hier erstelle ich das Spielfeld.


button1 = Button(LeftFrame, text=" ", font=('arial 26 bold'), height = 3, width=8, bg='mintcream', command=lambda:checker(button1))
button1.grid(row=1, column=0, sticky = S+N+E+W)

button2 = Button(LeftFrame, text=" ", font=('arial 26 bold'), height = 3, width=8, bg='mintcream', command=lambda:checker(button2))
button2.grid(row=1, column=1, sticky = S+N+E+W)

button3 = Button(LeftFrame, text=" ", font=('arial 26 bold'), height = 3, width=8, bg='mintcream', command=lambda:checker(button3))
button3.grid(row=1, column=2, sticky = S+N+E+W)

button4 = Button(LeftFrame, text=" ", font=('arial 26 bold'), height = 3, width=8, bg='mintcream', command=lambda:checker(button4))
button4.grid(row=2, column=0, sticky = S+N+E+W)

button5 = Button(LeftFrame, text=" ", font=('arial 26 bold'), height = 3, width=8, bg='mintcream', command=lambda:checker(button5))
button5.grid(row=2, column=1, sticky = S+N+E+W)

button6 = Button(LeftFrame, text=" ", font=('arial 26 bold'), height = 3, width=8, bg='mintcream', command=lambda:checker(button6))
button6.grid(row=2, column=2, sticky = S+N+E+W)

button7 = Button(LeftFrame, text=" ", font=('arial 26 bold'), height = 3, width=8, bg='mintcream', command=lambda:checker(button7))
button7.grid(row=3, column=0, sticky = S+N+E+W)

button8 = Button(LeftFrame, text=" ", font=('arial 26 bold'), height = 3, width=8, bg='mintcream', command=lambda:checker(button8))
button8.grid(row=3, column=1, sticky = S+N+E+W)

button9 = Button(LeftFrame, text=" ", font=('arial 26 bold'), height = 3, width=8, bg='mintcream', command=lambda:checker(button9))
button9.grid(row=3, column=2, sticky = S+N+E+W)


root.mainloop()
Sirius3
User
Beiträge: 18289
Registriert: Sonntag 21. Oktober 2012, 17:20

@stef.kr: bevor hier noch mehr eingebaut wird, solltest Du den Code aufräumen. Wenn man anfängt, Variablennamen durchzunummerieren, will man eigentlich Listen und Schleifen verwenden.
Alles was Funktionen brauchen, müssen sie über ihre Argumente bekommen, und keine Variablen sollten einfach so aus dem nichts kommen. Warum verwendest Du einmal Eckige Klammern und einmal configure?
Eine Funktion könnte ungefähr so aussehen:

Code: Alles auswählen

def reset(buttons):
    for button in buttons:
        button['text'] = " "
        button['background'] ="mintcream"
Der Rest muß auf ähnliche Weise vereinfacht werden.
Die `reset`-Funktion kann noch an mindestens 10 anderen Stellen aufgerufen werden, statt den selben Code zu kopieren.
stef.kr
User
Beiträge: 4
Registriert: Dienstag 9. Juni 2020, 18:55

Ich hatte versucht es aufzuräumen und den ausführlichen Text durch "reset" zu ersetzen aber ich habe ständig Fehlermeldungen bekommen. Ich bekam am häufigsten die Meldung 'Button' is not callable.
Ich habe es dann lieber unaufgeräumt gelassen.
Sirius3
User
Beiträge: 18289
Registriert: Sonntag 21. Oktober 2012, 17:20

Wie sieht Dein Code aus und was ist die genaue Fehlermeldung?
Antworten