2017-07-21 4 views
0

私はチェスプログラムをやっていて、bpawn1-bpawn8のx位置とy位置をチェックしてxとyの位置が白いポーンと同じであるかどうかを調べることで、それはポーンを殺す。クラス内の変数の複数の値をチェックできますか?

class ChessPeice: 
    def __init__(self, color, x_position, y_positon): 
     self.color = color 
     self.x = self.x_position 
     self.y = self.y_position 

    @staticmethod 
    def move(x_position, y_position): 
     pass 

class WPawn(ChessPeice): 

    def move(x_position, y_position): 
     if self.x_position + 1 == x_position and self.y_position == y_position: 
      self.x_position += 1 
     elif self.x_position + 1 == x_position and self.y_position + 1 == y_position: 
      if self.x_positon + 1 == bpawn1.x_position and self.y_positon + 1 == bpawn1.y_position 
      bpawn1 == "dead" 

答えて

0

これはあなたが探しているものでしょうか?

In [15]: pawn_pos = (1, 3) 

In [16]: rook_pos = (4, 6) 

In [17]: pawn_pos == rook_pos 
Out[17]: False 

In [18]: rook_pos = (1, 3) 

In [19]: pawn_pos == rook_pos 
Out[19]: True 

また、あなたは__eq__メソッドをオーバーライドすることができます。

In [26]: class ChessPiece: 
    ...:  def __init__(self, color, x, y): 
    ...:   self.x = x 
    ...:   self.y = y 
    ...:   self.color = color 
    ...:  def __eq__(self, other): 
    ...:   return (self.x, self.y) == (other.x, other.y) 
    ...:  

In [27]: c1 = ChessPiece('black', 1, 3) 

In [28]: c2 = ChessPiece('white', 1, 4) 

In [29]: c1 == c2 
Out[29]: False 

In [30]: c2 = ChessPiece('white', 1, 3) 

In [31]: c1 == c2 
Out[31]: True 
関連する問題