0

I have a main fonction that calls "engine" and several subfunctions related to the main function. These functions are in my engine.py file. I would like to use a subfunction from my engine.py with my Chessboard.py file. How do I do that ? Actually, my shell says that my subfunction isn't defined.

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\Thierry\AppData\Local\Programs\Python\Python35\lib\tkinter\__init__.py", line 1549, in __call__
    return self.func(*args)
  File "D:\Chess Release\Chessboard.py", line 332, in move_image
    movecreate(b)
NameError: name 'movecreate' is not defined
1

You must write

from game_engine import movecreate

but first you must move the definition of movecreate() outside the body of the engine() function. You don't need nested function definitions in your code, so don't use them as they hide the functions from the global namespace.

0

Good Advise ! :) I almost finished the chess game. The last thing to do is to ask my second click to check if my move is valid or not. My engine does that but I don't know how to bound my engine with my gui.
Here is the 2 functions that are implicated :
The GUI, I need to add something after if len(movecreate(tour,b,d))>0

def move_image(event):
    global x1,y1,x2,y2,img, init_image,closest,t,clic,i,j,tour,b,colour,oColour
    init_image=[]
    if clic==0: #First Clic
        if tour%2==0:
            colour="w"
            oColour="b"
        else:
            colour="b"
            oColour="w"
        x1=event.x
        y1=event.y
        i,j=(int(x1/dim_square),int(y1/dim_square))
        print(i,j)
        b = i + 8 * j
        print(b)
        closest=canvas.find_closest(x1,y1)
        t = canvas.coords(closest)
        if len(t)==4:
              clic=0
        elif len(t)==2:
              print(colour)
              if colour=="w":
                    if pieces[b] in ["Kw","Qw","Rw","Bw","Nw","Pw"]:
                        print("WHITE")
                        print(tour)
                    else:
                        clic=0
              elif colour=="b":
                    if pieces[b] in ["Kb","Qb","Rb","Bb","Nb","Pb"]:
                        print("BLACK")
                        print(tour)
                    else:
                        clic=0
    elif clic==1: #Second Clic
        x2=event.x
        y2=event.y
        i,j=(int(x2/dim_square),int(y2/dim_square))
        d = i + 8 * j
        if len(movecreate(tour,b,d))>0:
              if pieces[d]=="O":
                  x_center,y_center=tile_center+i*dim_square,tile_center+j*dim_square
                  canvas.coords(closest, x_center, y_center)
                  pieces[d]=pieces[b]
                  pieces[b]="O"
                  clic=0
                  check(tour,colour,b,d)
                  print(pieces)
                  if check(tour,colour,b,d)==1:
                      showinfo("VICTORY","You win !")
                  else:
                      tour+=1
              elif pieces[d]!="O":
                  if colour=="w":
                      if pieces[d] in ["Kb","Qb","Rb","Bb","Nb","Pb"]:
                          captured=canvas.find_closest(x2,y2)
                          canvas.delete(captured,x2,y2)
                          x_center,y_center=tile_center+i*dim_square,tile_center+j*dim_square
                          canvas.coords(closest, x_center, y_center)
                          pieces[d]=pieces[b]
                          pieces[b]="O"
                          print(tour)
                          clic=0
                          check(tour,colour,b,d)
                          print(pieces)
                          print("Captured")
                          print(pieces[d])
                          if check(tour,colour,b,d)==1:
                              showinfo("VICTORY","You win !")
                          else:
                              tour+=1
              elif pieces[d]!="O":
                  if colour=="b":
                      if pieces[d] in ["Kw","Qw","Rw","Bw","Nw","Pw"]:
                          captured=canvas.find_closest(x2,y2)
                          canvas.delete(captured,x2,y2)
                          x_center,y_center=tile_center+i*dim_square,tile_center+j*dim_square
                          canvas.coords(closest, x_center, y_center)
                          pieces[d]=pieces[b]
                          pieces[b]="O"
                          print(tour)
                          clic=0
                          check(tour,colour,b,d)
                          print(pieces)
                          print("Captured")
                          print(pieces[d])
                          if check(tour,colour,b,d)==1:
                              showinfo("VICTORY","You win !")
                          else:
                              tour+=1
              else:
                  clic=0
        else:
            clic=0

The function that check if the move is correct or not and give me the answer at the end:

def movecreate(tour,b,d): #Create a list of possible moves for the selected piece
    global movelist,pieces,board,correct
    correct=0
    x=board[b]//10
    y=board[b]%10
    if pieces[b].count("P")>0:
            if tour%2==0:
                    movelist=[x*10+y+1,x*10+y+2]
            else:
                    movelist=[x*10+y-1,x*10+y-2]
    elif pieces[b].count("R")>0:
            movelist=[x*10+y-1,x*10+y-2,x*10+y-3,x*10+y-4,x*10+y-5,x*10+y-6,x*10+y-7,x*10+y+1,x*10+y+2,x*10+y+3,x*10+y+4,x*10+y+5,x*10+y+6,x*10+y+7,(x-1)*10+y,(x-2)*10+y,(x-3)*10+y,(x-4)*10+y,(x-5)*10+y,(x-6)*10+y,(x-7)*10+y,(x+1)*10+y,(x+2)*10+y,(x+3)*10+y,(x+4)*10+y,(x+5)*10+y,(x+6)*10+y,(x+7)*10+y]
    elif pieces[b].count("N")>0:
            movelist=[(x-2)*10+y-1,(x-2)*10+y+1,(x-1)*10+y-2,(x-1)*10+y+2,(x+1)*10+y-2,(x+1)*10+y+2,(x+2)*10+y-1,(x+2)*10+y+1]
    elif pieces[b].count("B")>0:
            movelist=[(x-1)*10+y-1,(x-2)*10+y-2,(x-3)*10+y-3,(x-4)*10+y-4,(x-5)*10+y-5,(x-6)*10+y-6,(x-7)*10+y-7,(x-1)*10+y+1,(x-2)*10+y+2,(x-3)*10+y+3,(x-4)*10+y+4,(x-5)*10+y+5,(x-6)*10+y+6,(x-7)*10+y+7,(x+1)*10+y-1,(x+2)*10+y-2,(x+3)*10+y-3,(x+4)*10+y-4,(x+5)*10+y-5,(x+6)*10+y-6,(x+7)*10+y-7,(x+1)*10+y+1,(x+2)*10+y+2,(x+3)*10+y+3,(x+4)*10+y+4,(x+5)*10+y+5,(x+6)*10+y+6,(x+7)*10+y+7]
    elif pieces[b].count("Q")>0:
            movelist=[x*10+y-1,x*10+y-2,x*10+y-3,x*10+y-4,x*10+y-5,x*10+y-6,x*10+y-7,x*10+y+1,x*10+y+2,x*10+y+3,x*10+y+4,x*10+y+5,x*10+y+6,x*10+y+7,(x-1)*10+y,(x-2)*10+y,(x-3)*10+y,(x-4)*10+y,(x-5)*10+y,(x-6)*10+y,(x-7)*10+y,(x+1)*10+y,(x+2)*10+y,(x+3)*10+y,(x+4)*10+y,(x+5)*10+y,(x+6)*10+y,(x+7)*10+y,(x-1)*10+y-1,(x-2)*10+y-2,(x-3)*10+y-3,(x-4)*10+y-4,(x-5)*10+y-5,(x-6)*10+y-6,(x-7)*10+y-7,(x-1)*10+y+1,(x-2)*10+y+2,(x-3)*10+y+3,(x-4)*10+y+4,(x-5)*10+y+5,(x-6)*10+y+6,(x-7)*10+y+7,(x+1)*10+y-1,(x+2)*10+y-2,(x+3)*10+y-3,(x+4)*10+y-4,(x+5)*10+y-5,(x+6)*10+y-6,(x+7)*10+y-7,(x+1)*10+y+1,(x+2)*10+y+2,(x+3)*10+y+3,(x+4)*10+y+4,(x+5)*10+y+5,(x+6)*10+y+6,(x+7)*10+y+7]
    elif pieces[b].count("K")>0:
            movelist=[(x-1)*10+y+1,(x-1)*10+y,(x-1)*10+y-1,x*10+y+1,x*10+y-1,(x+1)*10+y+1,(x+1)*10+y,(x+1)*10+y-1]
    for m in range(0,len(board)):
        n=0
        while n<len(movelist):
            if movelist[n]==board[m]:
                if pieces[m]!="O":
                    if pieces[b].count("P")>0:
                        if n==0:
                            del movelist[1]
                    elif pieces[b].count("R")>0:
                        if n<7:
                            del movelist[n+1:7]
                        elif 6<n and n<14:
                            del movelist[n+1:14]
                        elif 13<n and n<21:
                            del movelist[n+1:21]
                        elif 20<n and n<28:
                            del movelist[n+1:]
                    elif pieces[b].count("B")>0:
                        if n<7:
                            del movelist[n+1:7]
                        elif 14>n>6:
                            del movelist[n+1:14]
                        elif 13<n<21:
                            del movelist[n+1:21]
                        elif 20<n<28:
                            del movelist[n+1:]
                    elif pieces[b].count("Q")>0:
                        if n<7:
                            del movelist[n+1:7]
                        elif 14>n>6:
                            del movelist[n+1:14]
                        elif 13<n<21:
                            del movelist[n+1:21]
                        elif 20<n<28:
                            del movelist[n+1:28]
                        elif 27<n<35:
                            del movelist[n+1:35]
                        elif 34>n>42:
                            del movelist[n+1:42]
                        elif 41<n<49:
                            del movelist[n+1:49]
                        elif 48<n:
                            del movelist[n+1:]
                    if tour%2==0:
                        if pieces[m].count("w")>0:
                            del movelist[n]
                    else:
                        if pieces[m].count("b")>0:
                            del movelist[n]
            n=n+1 #Création de la liste de coups finie
    for x in range(0,len(movelist)):
            if movelist[x]==board[d]:
                    print("Correct Move.\n")
                    correct=1
                    break
            else:
                    x=x+1
                    if x==len(movelist):
                            print("Incorrect move. Try again.\n")
                    correct=0
    return movelist;

Edited by titi157

1

Your code is very difficult to understand because it is not modular enough. You say you need to add something after if len(movecreate(tour,b,d))>0. Start by writing pseudo code to describe what you need to add. Describe it in plain english or french to start with.

0

I think that I just need to access to the "correct" variable that is in my movecreate() function.
Then, I will have something like this :len(movecreate(tour,b,d))>0 and correct==0:

1

If you want to access the correct variable, you need to return its value in movecreate(). For examble, this function could return two values

return correct, movelist

The function that uses this could use

correct, movelist = movecreate(tour, b, d)
if movelist and (correct == 0): ...

edit: also it would be a great improvement if you could remove most of the global statements.

Edited by Gribouillis

0

I'm looking for a better way to remove/delete the captured piece. Actually, it checks if the move is correct then, it checks if there is an opponent's piece in the destination tile. If there is one, it deletes the image thanks to the coordinate of the tile. Finally, it moves the selected piece to the destination tile.

captured=canvas.find_closest(x2,y2)
canvas.delete(captured,x2,y2)
x_center,y_center=tile_center+i*dim_square,tile_center+j*dim_square
canvas.coords(closest, x_center, y_center)
1

I don't think you need x2 and y2 in the call to delete(), canvas.delete(captured) should work. Also shouldn't the last call be written

canvas.coords(closest, (x_center, y_center))

?

What do you mean by a better way to delete the captured piece ?

0

I tried to think about a lot of way of doing this but the method that I'm using is the more efficient so I will keep using it. :)

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.