私のチェス プログラムには、Move というクラスがあります。ピースがどこに置かれたかを保存します。何がピースで、何がキャプチャされたのか。
問題は、移動してキャプチャしたピースを取得するために、Board オブジェクト全体を __init__ メソッドに渡すことです。したがって、IMO では、Move クラスのすべてのメソッドを Board クラスに格納する必要があるように思われます。Board クラスにも、特定の正方形のピースを取得するメソッドがあります。
私はOOを学び始めたばかりなので、これに関するアドバイスと、より一般的な設計上の決定をいただければ幸いです。
省略した方がよいと思われる Move クラスは次のとおりです。
class Move(object):
def __init__(self, from_square, to_square, board):
""" Set up some move infromation variables. """
self.from_square = from_square
self.to_square = to_square
self.moved = board.getPiece(from_square)
self.captured = board.getPiece(to_square)
def getFromSquare(self):
""" Returns the square the piece is taken from. """
return self.from_square
def getToSquare(self):
""" Returns the square the piece is put. """
return self.to_square
def getMovedPiece(self):
""" Returns the piece that is moved. """
return self.moved
def getCapturedPiece(self):
""" Returns the piece that is captured. """
return self.captured