Was haltet ihr von diesem Code?
Das ist ein kleines TikTakToe. Und ich möchte wissen was ihr von diesem Code haltet und ob alles richtig ist.
mfg Andi
Code: Alles auswählen
import Tkinter
import tkMessageBox
def main():
global anzahl
anzahl = -1
liste = [1, 2, 3, 4, 5, 6, 7, 8, 9]
def callback():
a=1
def who(gewonnen):
if gewonnen == "x":
tkMessageBox.showinfo(
title = "Ende", message = "Spieler1 (x) hat gewonnen.")
root.destroy()
elif gewonnen == "o":
tkMessageBox.showinfo(
title = "Ende", message = "Spieler2 (o) hat gewonnen.")
root.destroy()
else:
callback()
def hatgewonnen():
for x in range(0, 9, 3):
if liste[x] == liste[x+1]:
if liste[x+1] == liste[x+2]:
gewonnen = liste[x]
who(gewonnen)
break
for x in range(0, 3, 1):
if liste[x] == liste[x+3]:
if liste[x+3] == liste[x+6]:
gewonnen = liste[x]
who(gewonnen)
break
if liste[0] == liste[4]:
if liste[4] == liste[8]:
gewonnen = liste[0]
who(gewonnen)
if liste[2] == liste[4]:
if liste[4] == liste[6]:
gewonnen = liste[2]
who(gewonnen)
def bPressed(x,y,z):
global anzahl
anzahl = anzahl + 1
player = anzahl % 2
if player == 0:
liste[z] = "x"
player = 1
else:
player = 0
liste[z] = "o"
b = Tkinter.Button(root, text = liste[z], command = callback)
b.grid(row=x, column=y, ipadx = 10, ipady = 5, padx = 2, pady = 2)
if anzahl >= 4:
hatgewonnen()
root = Tkinter.Tk()
root.title("Tik Tak Toe")
root.geometry("200x120")
b1 = Tkinter.Button(root, text = " ", command = lambda:bPressed(0,0,0))
b1.grid(row=0, column=0, ipadx = 10, ipady = 5, padx = 2, pady = 2)
b2 = Tkinter.Button(root, text = " ", command = lambda:bPressed(0,1,1))
b2.grid(row=0, column=1, ipadx = 10, ipady = 5, padx = 2, pady = 2)
b3 = Tkinter.Button(root, text = " ", command = lambda:bPressed(0,2,2))
b3.grid(row=0, column=2, ipadx = 10, ipady = 5, padx = 2, pady = 2)
b4 = Tkinter.Button(root, text = " ", command = lambda:bPressed(1,0,3))
b4.grid(row=1, column=0, ipadx = 10, ipady = 5, padx = 2, pady = 2)
b5 = Tkinter.Button(root, text = " ", command = lambda:bPressed(1,1,4))
b5.grid(row=1, column=1, ipadx = 10, ipady = 5, padx = 2, pady = 2)
b6 = Tkinter.Button(root, text = " ", command = lambda:bPressed(1,2,5))
b6.grid(row=1, column=2, ipadx = 10, ipady = 5, padx = 2, pady = 2)
b7 = Tkinter.Button(root, text = " ", command = lambda:bPressed(2,0,6))
b7.grid(row=2, column=0, ipadx = 10, ipady = 5, padx = 2, pady = 2)
b8 = Tkinter.Button(root, text = " ", command = lambda:bPressed(2,1,7))
b8.grid(row=2, column=1, ipadx = 10, ipady = 5, padx = 2, pady = 2)
b9 = Tkinter.Button(root, text = " ", command = lambda:bPressed(2,2,8))
b9.grid(row=2, column=2, ipadx = 10, ipady = 5, padx = 2, pady = 2)
root.mainloop()
if __name__ == "__main__":
main()