Hilfestellung

Wenn du dir nicht sicher bist, in welchem der anderen Foren du die Frage stellen sollst, dann bist du hier im Forum für allgemeine Fragen sicher richtig.
Antworten
Lostboys
User
Beiträge: 1
Registriert: Dienstag 10. März 2020, 14:12

Hallo
Wir sind Python Anfänger und haben gehofft, hilfe bei einem Programm zu erhalten an dem wir nun schon länger schreiben.
Es handelt sich um ein spiel namens tactyx in dem wir statt mit einer Alpha Beta Suche zu arbeiten eine If/else Schleife als Bot verwenden.
Nun wird unsere Bedingung nicht erkannt und wir haben keine Ahnung warum.
Dies hat uns nun schon viele frustvolle Stunden beschert und wir hoffen jemand kann uns helfen.

Code: Alles auswählen

from tkinter import *
import random
import time
import copy
wechsel = False


master = Tk()
canvas_height = 9*master.winfo_screenheight()//10
canvas_width = canvas_height
cx=0  
cy=0
cw=3*canvas_height//11 
ch=3*canvas_height//11
rand=canvas_height//11
dran="X"
feld=[0,0,0,0,0,0,0,0,0]
master.title( "Tactix" )
nonono = True
w = Canvas(master, width=canvas_width,height=canvas_height,bg="#C0C0C0")
w.pack(expand = YES, fill = BOTH)
bild1=PhotoImage(file="Coin.gif")
bild2=PhotoImage(file="Woin.gif")
bild3=PhotoImage(file="Goin.gif")
bild4=PhotoImage(file="Moin.gif")
bild5=PhotoImage(file="Soin.gif")



def Funktion():
   if (knopf1["image"]==bild2 and knopf4["image"]==bild2):
      knopf5["image"]=bild4
      print ("heho")
# if (feld[1] and feld[3]==(knopf1["image"]== bild2 and knopf4["image"]==bild2)):
#(knopf1["image"]==bild2 and knopf4["image"]==bild2):


def watnoch():
   global wechsel
   if wechsel:
      knopf0["image"]=bild1
   else:
      knopf0["image"]=bild2
   Funktion()
   print ("hi")
   
      
      
   

def watnoch2():
   global wechsel
   if wechsel:
      knopf1["image"]=bild1
   else:
      knopf1["image"]=bild2
   print ("he")
  

def watnoch3():
   global wechsel
   if wechsel:
      knopf3["image"]=bild1
   else:
      knopf3["image"]=bild2
   


def watnoch4():
   global wechsel
   if wechsel:
      knopf4["image"]=bild1
   else:
       knopf4["image"]=bild2
   print ("ho")

def watnoch5():
   global wechsel
   if wechsel:
      knopf5["image"]=bild1
   else:
       knopf5["image"]=bild2

def watnoch6():
   global wechsel
   if wechsel:
      knopf6["image"]=bild1
   else:
       knopf6["image"]=bild2

def watnoch7():
   global wechsel      
   if wechsel:
      knopf7["image"]=bild1
   else:
       knopf7["image"]=bild2

def watnoch8():
   global wechsel
   if wechsel:
      knopf8["image"]=bild1
   else:
       knopf8["image"]=bild2

def watnoch9():
   global wechsel
   if wechsel:
      knopf9["image"]=bild1
   else:
       knopf9["image"]=bild2


def  verloren():
   wechsel = not wechsel
   knopf5["image"]=bild4







def Loser():
    knopf5["image"]=bild4

bild2 == True


def Schlecht2():
   if knopf0["image"]==bild2 and knopf1["image"]==bild2 and knopf3["image"]==bild2 and knopf4["image"]==bild2 and knopf5["image"]==bild2 and knopf6["image"]==bild2 and knopf7["image"]==bild2 and knopf8["image"]==bild2 and knopf9["image"]==bild2():
      Loser()


#def Stopschild():
 #  global nonono
 #  if (feld[0]==feld[1]==feld[2] and feld[0]!=0) or (feld[3]==feld[4]==feld[5] and feld[3]!=0) or (feld[6]==feld[7]==feld[8] and feld[6]!=0) or (feld[0]==feld[3]==feld[6] and feld[0]!=0) or (feld[1]==feld[4]==feld[7] and feld[1]!=0) or (feld[2]==feld[5]==feld[8] and feld[2]!=0) or (feld[0]==feld[4]==feld[8] and feld[0]!=0)or (feld[2]==feld[4]==feld[6] and feld[2]!=0):
  #    nonono=False

#if nonono==False:
  # knopf5["image"]==bild4



#def Mario():
#   global nonono
#   if (feld[0]==feld[1]==feld[2]== 
#      nonono=False
      









      

knopf0=Button(master, font="Bahnschriftstr "+ str(rand),text= "",bg="white",command=watnoch)
knopf0.place(x=rand+cx, y=rand+cy, width = cw, height = ch)
knopf0["image"]=bild1
knopf1=Button(master, font="Bahnschriftstr "+ str(rand),text= "",bg="white",command=watnoch2)
knopf1.place(x=rand+cx+1*cw, y=rand+cy, width = cw, height = ch)
knopf1["image"]=bild1
knopf3=Button(master, font="Bahnschriftstr "+ str(rand),text= "",bg="white",command=watnoch3)
knopf3.place(x=rand+cx+2*cw, y=rand+cy, width = cw, height = ch)
knopf3["image"]=bild1
knopf4=Button(master, font="Bahnschriftstr "+ str(rand),text= "",bg="white",command=watnoch4)
knopf4.place(x=rand+cx, y=rand+cy+1*cw, width = cw, height = ch)
knopf4["image"]=bild1
knopf5=Button(master, font="Bahnschriftstr "+ str(rand),text= "",bg="white",command=watnoch5)
knopf5.place(x=rand+cx+1*cw, y=rand+cy+1*cw, width = cw, height = ch)
knopf5["image"]=bild1
knopf6=Button(master, font="Bahnschriftstr "+ str(rand),text= "",bg="white",command=watnoch6)
knopf6.place(x=rand+cx+2*cw, y=rand+cy+1*cw, width = cw, height = ch)
knopf6["image"]=bild1
knopf7=Button(master, font="Bahnschriftstr "+ str(rand),text= "",bg="white",command=watnoch7)
knopf7.place(x=rand+cx, y=rand+cy+2*cw, width = cw, height = ch)
knopf7["image"]=bild1
knopf8=Button(master, font="Bahnschriftstr "+ str(rand),text= "",bg="white",command=watnoch8)
knopf8.place(x=rand+cx+1*cw, y=rand+cy+2*cw, width = cw, height = ch)
knopf8["image"]=bild1
knopf9=Button(master, font="Bahnschriftstr "+ str(rand),text= "",bg="white",command=watnoch9)
knopf9.place(x=rand+cx+2*cw, y=rand+cy+2*cw, width = cw, height = ch)
knopf9["image"]=bild1
knopf10=Button(master, font="Bahnschriftstr "+ str(rand),text= "",bg="white",command=Loser)
knopf10.place(x=rand+cx+1*cw, y=rand+cy+3*cw, width = cw, height = ch)
knopf10["image"]=bild3

L = [knopf1,knopf4,knopf6,knopf8]



z =random.choice(L)



def  test2():
    global knopf0
    global knopf1
    global knopf3
    global knopf4
    global knopf5
    global knopf6
    global knopf7
    global knopf8
    global knopf9
    print("Laser")
    knopf0["image"]=bild1
    knopf1["image"]=bild1
    knopf3["image"]=bild1
    knopf4["image"]=bild1
    knopf5["image"]=bild1
    knopf6["image"]=bild1
    knopf7["image"]=bild1
    knopf8["image"]=bild1
    knopf9["image"]=bild1
    z=0
    z= random.choice(L)
    z["image"] = bild2
    

z["image"] = bild2


def gewinnerzug():
    global knopf0
    global knopf1
    global knopf3
    global knopf4
    global knopf5
    global knopf6
    global knopf7
    global knopf8
    global knopf9
    print("ho")
    if  knopf0["image"]==bild2:  #global nochmal anders machen!!!!
         print("ha")
         knopf4["image"]=bild2
         print("halli")
         if knopf5["image"]==bild2:
             knopf8["image"]=bild2
             if knopf9["image"]==bild2:
                 knopf6["image"]=bild2
                 if knopf3["image"]== bild2:
                      knopf5["image"]= bild4
         if knopf7["image"]==bild2:
             knopf8["image"]=bild2
             if knopf5["image"]==bild2:
                knopf6["image"]=bild2
                if knopf3["image"]== bild2 or knopf9["image"] == bild2:
                      knopf5["image"]= bild4
             if knopf9["image"]== bild2:
                knopf6["image"]= bild2
                if knopf5["image"]==bild2 or knopf3["image"] == bild2:
                    knopf5["image"]=bild4
               
    if knopf3["image"]== bild2:
          knopf6["image"]=bild2
          if knopf5["image"]==bild2:
                knopf4["image"]=bild2
                if knopf0["image"]==bild2:
                   knopf5["image"]=bild4
                if knopf7["image"]==bild2:
                   knopf8["image"]=bild2
                   if knopf9["image"]==bild2:
                      knopf5["image"]=bild4
          if knopf9["image"]== bild2:
                knopf8["image"]=bild2
                if knopf5["image"]== bild2:
                   knopf4["image"]=bild2
                   if knopf0["image"]== bild2 or knopf7["image"]==bild2:
                      knopf5["image"]= bild4
                if knopf7["image"]==bild2:
                   knopf4["image"]=bild2
                   if knopf0["image"]==bild2 or knopf5["image"]==bild2:
                      knopf5["image"]=bild4
    if knopf4["image"]==bild2 and knopf0["image"]==bild2:
          knopf1["image"]=bild2
          if knopf3["image"]==bild2:
             knopf6["image"]=bild2
             if knopf5["image"]==bild2:
                knopf8["image"]=bild2
                if knopf7["image"]==bild2 or knopf9["image"]==bild2:
                   knopf5["image"]=bild4
             if knopf9["image"]==bild2:
                knopf8["image"]=bild2
                if knopf5["image"]==bild2:
                   knopf5["image"]=bild4
                if knopf7["image"]==bild2:
                   knopf5["image"]=bild4
          if knopf5["image"]==bild2:
             knopf6["image"]=bild2
             if knopf3["image"]==bild2:
                knopf5["image"]=bild4
             if knopf9["image"]==bild2:
                knopf8["image"]=bild2
                if knopf7["image"]==bild2:
                   knopf5["image"]=bild4
          if knopf5["image"]==bild2:
             knopf8["image"]=bild2
             if knopf7["image"]==bild2:
                knopf5["image"]=bild4
             if knopf9["image"]==bild2:
                knopf6["image"]=bild2
                if knopf3["image"]==bild2:
                   knopf5["image"]=bild4
                     
               
               



   


gewinnerzug
      
         
      
   

neuknopf=Button(master, font="Bahnschrift 10",text= "LASER",command=test2)
neuknopf.place(x=rand//10, y=rand//10, width = 1.2 * rand, height = rand//3)
neuknopf["font"]="Stencil 10"
neuknopf["fg"]="#00EE00"   
knopf11=Button(master, font="Bahnschriftstr "+ str(rand),text= "",bg="white",command=gewinnerzug)
knopf11.place(x=rand+cx+2*cw, y=rand+cy+3*cw, width = cw, height = ch)
knopf11["image"]=bild5 

#while knopf5["image"] != bild4:
   #gewinnerzug()

beschriftung1=Label( master, text = "TacTix" )
beschriftung1.place( x=rand+cx+1.5*cw,y=rand+cy+-0.25*cw,anchor = "n" )
beschriftung1["fg"]="#00FF00"
beschriftung1["anchor"]="center"
beschriftung1["font"]="Stencil 25 bold "

mainloop()
Benutzeravatar
__blackjack__
User
Beiträge: 14052
Registriert: Samstag 2. Juni 2018, 10:21
Wohnort: 127.0.0.1
Kontaktdaten:

@Lostboys: Die Fehlerbeschreibung ist nicht wirklich hilfreich. Was genau funktioniert da nicht? Gibt es eine Fehlermeldung? Falls ja, welche? Tracebacks bitte 1:1 kopieren. Falls es keine Fehlermeldung gibt, was genau macht ihr und was genau passiert dann, und wie genau weicht das vom erwarteten Verhalten ab?

Ansonsten ist der Code sehr schlecht. Sternchen-Import, globale Variablen, Codewiederholungen, keine objektorientierte Programmierung bei einem GUI-Programm, durchnummerierte Namen, …
“Vir, intelligence has nothing to do with politics!” — Londo Mollari
Sirius3
User
Beiträge: 18272
Registriert: Sonntag 21. Oktober 2012, 17:20

Als erstes solltet ihr den Code aufräumen. Alle Variablen mit Zahl sollen durch Listen ersetzt werden. Die durchnummerierten Funktionen durch eine mit Knopf als Parameter. Das Erzeugen der Knöpfe mit einer Schleife. Aller Code auf oberster Ebene in Funktionen schreiben. Global eliminieren. place durch grid ersetzen. Trennt die Programmlogik von der GUI. Am besten das Spiel erst ohne Graphik entwickeln. Dazu auch eine Spielklasse definieren. Die Logik sollte nicht auf Knopfbildern beruhen.
Benutzeravatar
__blackjack__
User
Beiträge: 14052
Registriert: Samstag 2. Juni 2018, 10:21
Wohnort: 127.0.0.1
Kontaktdaten:

@Lostboys: `time` und `copy` werden importiert, aber nicht verwendet. `copy` braucht man auch eher selten.

Einrückung ist vier Leerzeichen pro Ebene.

Sternchen-Importe sind Böse™. Gerade bei `tkinter` holt man sich da fast 200 Namen ins Modul von denen nur ein kleiner Bruchteil verwendet wird und wo auch Namen dabei sind die gar nicht in `tkinter` definiert wurden sondern von dem Modul von woanders importiert wurden.

Auf Modulebene sollte nur Code stehen der Konstanten, Funktionen, und Klassen definiert. Das Hauptprogramm steht üblicherweise in einer Funktion die `main()` heisst. Am unübersichtlichen ist es wenn man das Hauptprogramm zwischen Funktionsdefinitionen verteilt.

Funktionen und Methoden sollten alles was sie ausser Konstanten benötigen als Argument(e) übergeben bekommen. ``global`` hat in einem sauberen Programm nichts zu suchen und ist auch nicht nötig.

Die Funktion `verloren()` wird nirgends verwendet.

Was bedeutet `wechsel`? Nummerierte Namen sind schlecht weil nichtssagend. Da will man sich entweder bessere Namen ausdenken oder gar keine Einzelnamen sondern eine Datenstruktur, oft eine Liste. Und bei `watnoch()`, `watnoch2()` bis `watnoch…()` fragt sich der Leser dann „geht's noch!?“
“Vir, intelligence has nothing to do with politics!” — Londo Mollari
Benutzeravatar
__blackjack__
User
Beiträge: 14052
Registriert: Samstag 2. Juni 2018, 10:21
Wohnort: 127.0.0.1
Kontaktdaten:

Habe jetzt mal nach den Spielregeln für TacTix im Netz geschaut und ich sehe in diesem Ansatz auch gar nicht wie die durchgesetzt werden sollen in dem Code. Der Spieler muss ja irgendwie die Möglichkeit haben mehrere Spielsteine/Münzen in einer Reihe oder Spalte die aneinandergrenzen zu selektieren. Und im Gegenzug muss das Selektieren von Spielsteinen/Münzen aus anderen Reihen oder Spalten und solche die nicht an bereits selektierte angrenzen verhindert oder zumindest zurückgeswiesen werden.

So eine riesige ``if``-Kaskade ist auch nicht wirklich sinnvoll. Ach und: http://if-schleife.de/
“Vir, intelligence has nothing to do with politics!” — Londo Mollari
Antworten