0

I'm trying to make an algorithm that plays chess. It makes a list of all the possible moves in a certain depth. I'm trying to draw the possible scenarios after every move one by one in order to test it. I use the recursive function cr_list in order to calculate the moves in a depth-first manner. After a move is chosen I change the respective positions of the pieces involved and call the draw function to see the diferrence.But the canvas doesn't update. The way I use the after method seems to not be right. I also tried using the self.canvas.update_idletasks() method but it works only once and than it stops changing again. After I use ctrl+c to stop the programm it updates and I also print the positions of all the pieces in the draw function and they are what they should be.I use the x=input() statement to stop the program to see the changes.

def draw(self,piece):

    if self.flag:
        self.canvas.delete('all')
    self.flag=1
    x=(800-(int(self.n)*int((660/int(self.n)))))/2
    y=(800-(int(self.n)*int((660/int(self.n)))))/2
    for i in range(int(self.n)):
        if i%2==0:
            for j in range(int(self.n)):
                if j%2==0:
                    self.canvas.create_rectangle(x,y,x+int((660/int(self.n))),y+int((660/int(self.n))), fill='navajo white')

                else:
                  self.canvas.create_rectangle(x,y,x+int((660/int(self.n))),y+int((660/int(self.n))), fill='saddle brown')

                x+=int((660/int(self.n)))
        else:
            for j in range(int(self.n)):
                if j%2==0:
                        self.canvas.create_rectangle(x,y,x+int((660/int(self.n))),y+int((660/int(self.n))), fill='saddle brown')
                else:
                        self.canvas.create_rectangle(x,y,x+int((660/int(self.n))),y+int((660/int(self.n))), fill='navajo white')

                x+=int((660/int(self.n)))
        y+=int((660/int(self.n)))
        x=(800-(int(self.n)*int((660/int(self.n)))))/2
    x=(800-(int(self.n)*int((660/int(self.n)))))/2
    y=(800-(int(self.n)*int((660/int(self.n)))))/2
    for i in range(16):
        if piece[i]!=None:
            if (piece[i].pos[0]+piece[i].pos[1])%2==0:
                word=piece[i].species+'_aa.ppm'
            else:
                word=piece[i].species+'_am.ppm'
            self.img[i]=tkinter.PhotoImage(file=word)
               self.canvas.create_image(x+piece[i].pos[1]*int((660/int(self.n)))+(int((660/int(self.n)))-73)/2,y+piece[i].pos[0]*int((660/int(self.n)))+(int((660/int(self.n)))-73)/2, anchor=tkinter.NW,image=self.img[i])

        print(piece[i].pos)
    for i in range(16):
        if piece[i]!=None:
            if (piece[i+16].pos[0]+piece[i+16].pos[1])%2==0:
                word=piece[i+16].species+'_ma.ppm'
            else:
                word=piece[i+16].species+'_mm.ppm'
            self.img[i+16]=tkinter.PhotoImage(file=word)
               self.canvas.create_image(x+piece[i+16].pos[1]*int((660/int(self.n)))+(int((660/int(self.n)))-73)/2,y+piece[i+16].pos[0]*int((660/int(self.n)))+(int((660/int(self.n)))-73)/2, anchor=tkinter.NW,image=self.img[i+16])


def cr_list(self,i,lm,met,pieces):
    # .....
    root.after(1000,self.draw(pieces2))
    self.cr_list(i2,lm,met,pieces2)
    # .....
    x=input()
    # .....
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Acno
  • 1
  • You usage of `after` is broken. Read [Event-driven programming - `.after`](https://stackoverflow.com/a/9343402/7414759) – stovfl Jan 27 '19 at 19:22

1 Answers1

0

In the end I called draw() normally and below I used self.canvas.update() and it worked.

def cr_list(self,i,lm,met,pieces):
    # .....
    self.draw(pieces2)
    self.canvas.update()
    x=input()
    self.cr_list(i2,lm,met,pieces2)
    # .....
Acno
  • 1