国际象棋程序OO设计
在我的国际象棋程序中,我有一个名为Move的课程。 它存储了该片被拍摄和放置的位置。 那件作品是什么,以及那件作品是什么。
但问题是,为了获得被移动和捕获的作品,我将整个Board对象传递给__init__方法。 所以IMO似乎应该将所有Move类方法存储到Board类中,而这些方法也有一个方法可以在给定的正方形上获取该块。
我刚刚开始学习面向对象,所以关于这方面的一些建议,也许一些更一般的设计决定,我们非常感谢。
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
当你创建一个对象时,你正在创建一个东西。 董事会和董事会的东西都是东西。 当你想与这些东西互动时,你需要一种方法去做 - 或者一个动词。
这只是作为建议的方法,以避免使用Move
类。 我打算做的事情是:
我从写作Board
开始; 您可以决定以后如何表示位置。
class Board:
def __init__(self):
self.board = [['-' for i in xrange(8)] for j in xrange(8)]
# You would have to add logic for placing objects on the board from here.
class Piece:
def __init__(self, name):
self.name = name
def move(self, from, to):
# You would have to add logic for movement.
def capture(self, from, to):
# You would have to add logic for capturing.
# It's not the same as moving all the time, though.
class Pawn(Piece):
def __init__(self, name=""):
Piece.__init__(self, name)
def move(self, from, to):
# A rule if it's on a starting block it can move two, otherwise one.
def capture(self, from, to):
# Pawns capture diagonal, or via en passant.
链接地址: http://www.djcxy.com/p/84607.html