
Untitled
By: a guest on
Aug 8th, 2012 | syntax:
None | size: 1.52 KB | hits: 8 | expires: Never
Chess program OO design
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
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.