简体   繁体   English

go 那里可以吗? 国际象棋 function 斗争在 Python

[英]Can piece go there? Chess function struggle in Python

I have a tuple of the size of the chess board, with objects containing their coordinates and if True (white) or False (black)我有一个棋盘大小的元组,对象包含它们的坐标,如果为真(白色)或假(黑色)

Board: tuple[int, list[Piece]]板:元组[int,列表[Piece]]

I am trying to write a function to check if the piece can move or not, in this case if the cell is not occupied, or of if it is occupied check if it is the same side.我正在尝试编写一个 function 来检查该部件是否可以移动,在这种情况下,如果单元格未被占用,或者如果它被占用,请检查它是否是同一侧。 If there are both of the same side let's say True, and True it cannot move there, if the side it is different it will be able to move there.如果两者都在同一侧,假设为 True,并且 True 它不能在那里移动,如果它不同的一侧将能够移动到那里。

So far I have worked out that I can compare the self with the pieces in the tuple, and the only way to do this seems via a for loop.到目前为止,我已经发现我可以将 self 与元组中的片段进行比较,而做到这一点的唯一方法似乎是通过 for 循环。 The problem is that it contains booleans and it compares to each one and returns what exactly?问题是它包含布尔值,它与每个布尔值进行比较并返回究竟是什么?

You can see that my code output is correct, when the 2 are same it will say you can't go there and viceversa.你可以看到我的代码 output 是正确的,当 2 相同时,它会说你不能 go 在那里,反之亦然。 But I need to return a Bool.但我需要返回一个布尔值。

Can someone help to implement this?有人可以帮助实现这一点吗?

class Rook(Piece):
    def __init__(self, pos_X : int, pos_Y : int, side_ : bool):
        '''sets initial values by calling the constructor of Piece'''
        super().__init__(pos_X, pos_Y, side_)


    def can_reach(self, pos_X : int, pos_Y : int, B: Board) -> bool:
        '''
        checks if this rook can move to coordinates pos_X, pos_Y
        on board B according to rule [vertical and Horizontal move] and 
        [Rule4] A piece of side X (Black or White) cannot move to a location occupied by a piece of side X.] (see section Intro)
        Hint: use is_piece_at
        '''
      
        is_occupied = is_piece_at(pos_X, pos_Y, Board)
        if is_occupied == False:
          return True  #if it is not occupied you can always go there.
        if is_occupied == True:
          for piece in B[1]:
            print ("piece",piece.side_)
            print ("self",self.side_)
            if piece.side_== self.side_:
              print ("you can't go there")
              yes=
            elif piece.side_!= self.side_:
              print ("yes you can go there")
              
R0.can_reach(1,1, Board)

piece True
self True
you can't go there
piece False
self True
yes you can go there
piece False
self True
yes you can go there
True

If you want to return False if any of the possible options in the for loop is False, you can set a parameter to True by default and switch it to False if it encounters a False move.如果你想在 for 循环中任何可能的选项为 False 时返回 False,你可以将一个参数默认设置为 True,如果遇到 False 移动则将其切换为 False。

Does this fix your problem?:这能解决您的问题吗?:

class Rook(Piece):
    def __init__(self, pos_X : int, pos_Y : int, side_ : bool):
        '''sets initial values by calling the constructor of Piece'''
        super().__init__(pos_X, pos_Y, side_)


    def can_reach(self, pos_X : int, pos_Y : int, B: Board) -> bool:
        '''
        checks if this rook can move to coordinates pos_X, pos_Y
        on board B according to rule [vertical and Horizontal move] and 
        [Rule4] A piece of side X (Black or White) cannot move to a location occupied by a piece of side X.] (see section Intro)
        Hint: use is_piece_at
        '''
        reachable = True
        is_occupied = is_piece_at(pos_X, pos_Y, Board)
        if is_occupied == False:
          return reachable  #if it is not occupied you can always go there.
        else:
          for piece in B[1]:
            if piece.side_== self.side_:
              print ("you can't go there")
              reachable = False
          return reachable

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

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