简体   繁体   English

象棋的主教运动

[英]Bishop movement in chess

I'm doing a chess game for a school project in python3. 我正在为python3中的学校项目做国际象棋游戏。 I'm stuck at the bishop movement. 我陷入了主教运动。 I did everything move for all the pieces but the bishop is really hard. 我竭尽所能,但是主教真的很难。 Here is how I code my board: 这是我编写董事会代码的方式:

board=[["R1W","C1W","F1W","K1W","D1W","F2W","C2W","R2W"],
       ["P1W","P2W","P3W","P4W","P5W","P6W","P7W","P8W"],
       ["___","___","___","___","___","___","___","___"],
       ["___","___","___","___","___","___","___","___"],
       ["___","___","___","___","___","___","___","___"],
       ["___","___","___","___","___","___","___","___"],
       ["P1N","P2N","P3N","P4N","P5N","P6N","P7N","P8N"],
       ["R1N","C1N","F1N","D1N","K1N","F2N","C2N","R2N"]]

and here is how I program my bishop: 这是我对主教编程的方式:

def fou_valide(piece,l,m,c,n):
    if piece[0]=="F":
        if (abs(m-l)+abs(n-c))%2==0 and l!=m and  n!=c and board_fou_dame[l][c]==board_fou_dame[m][n] and abs(l-m)==abs(c-n):
            if m>l and n>c:
                for x in range(l+1,m):# on scanne les cases ou va passer la piece si elle ne passe par-dessus une pieces en effet elle ne peut pas sauter au dessus d\'une autre piece
                    for y in range(c+1,n):
                        if board[x][y]!="___":
                            print ("faux")
                            return False
                return True
            elif m>l and n<c:
                for z in range(l+1,m):# on scanne les cases ou va passer la piece si elle ne passe par-dessus une pieces en effet elle ne peut pas sauter au dessus d\'une autre piece
                    for j in range(c-1,n,-1):
                        if board[z][j]!="___":
                            print ("fau")
                            return False

That's just half the code only l , m , n , and c are changing. 那只是lmnc改变的代码的一半。 My problem is that even though n>c , it shows "fau" instead of "faux" 我的问题是,即使n>c ,它也会显示"fau"而不是"faux"

Here is how I ask for position: 这是我要求职位的方式:

l = int(input("ligne de selection?:\n"))-1 #on demande au joueur la ligne de la piece a selectionné
c = int(input("colonne de selection?:\n"))-1#on demande au joueur la colonne de la piece a selectionné
m = int(input("ligne de destination ?:\n"))-1#on demande au joueur la ligne ou il veut pose la piece
n = int(input("colonne de destination?:\n"))-1#on demande au joueur la colonne ou il veut pose la piece
piece = board[l][c] # piece correspond a la piece selectionné

In fact, piece is useles since you have l and c . 实际上,因为您有lc ,所以piece是有用的。
In your function, you have to verify four things. 在您的职能中,您必须验证四件事。

1) that the piece is indeed a bishop 1)该作品确实是主教
2) that l and c are differnt from m and c 2) lcmc
3) that they are on the same diagonal 3)他们在同一对角线上
4) that the cells between the two are free 4)两者之间的单元格是空闲的

The 4) is the hardest unless you remark that the direction you need to check is (sign(m - l), sign(n - c)) . 除非您指出需要检查的方向是(sign(m - l), sign(n - c))否则4)是最难的。 There's no need to write a different piece of code for each direction or each color. 无需为每个方向或每种颜色编写不同的代码。

EDIT: There's no built-in sign function, you need to write it yourself. 编辑:没有内置的sign功能,您需要自己编写。

def sign(n):
    return 1 if n >= 0 else -1

Then, you can check the cells with a single while loop that works for any direction. 然后,您可以使用一个适用于任何方向的while循环检查单元格。

def can_eat(l, c, m, n):
    dl = sign(m - l)
    dc = sign(n - c)
    x, y = c + dc, l + dl
    while x != n and y != m:
        if board[y][x] != '___': # non empty cell
            return False
        x += dc
        y += dl
    return True

So thanks to Bruno L, it now works fine, here is the code for the bishop that i used, this is a simplified version : 因此,感谢Bruno L,它现在可以正常工作,这是我使用的主教代码,这是简化版本:

def fou_valide(piece,l,m,c,n,):
if piece[0]=="F":#F in french stand for bishop, "Fou", 
    if l!=m and  n!=c  and abs(l-m)==abs(c-n): #the condition for the bishop movement
        dl = sign(m - l)#it get the me direction of the movement
        dc = sign(n - c)
        x, y = c + dc, l + dl
        while x != n and y != m:#This check if they are any non-empty cell on the way
            if board[y][x] != '___': # non empty cell
                return False
            x += dc
            y += dl
        return True
    return False
return True

Here is the sign function: 这是符号函数:

def sign(z):
if z >= 0:
    return 1  
else:
    return -1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM