Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 8th, 2012  |  syntax: None  |  size: 1.52 KB  |  hits: 8  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Chess program OO design
  2. class Move(object):
  3.  
  4.     def __init__(self, from_square, to_square, board):
  5.         """ Set up some move infromation variables. """
  6.         self.from_square = from_square
  7.         self.to_square = to_square
  8.         self.moved = board.getPiece(from_square)
  9.         self.captured = board.getPiece(to_square)
  10.  
  11.     def getFromSquare(self):
  12.         """ Returns the square the piece is taken from. """
  13.         return self.from_square
  14.  
  15.     def getToSquare(self):
  16.         """ Returns the square the piece is put. """
  17.         return self.to_square
  18.  
  19.     def getMovedPiece(self):
  20.         """ Returns the piece that is moved. """
  21.         return self.moved
  22.  
  23.     def getCapturedPiece(self):
  24.         """ Returns the piece that is captured. """
  25.         return self.captured
  26.        
  27. class Board:
  28.     def __init__(self):
  29.          self.board = [['-' for i in xrange(8)] for j in xrange(8)]
  30.     # You would have to add logic for placing objects on the board from here.
  31.  
  32. class Piece:
  33.     def __init__(self, name):
  34.          self.name = name
  35.     def move(self, from, to):
  36.          # You would have to add logic for movement.
  37.     def capture(self, from, to):
  38.          # You would have to add logic for capturing.
  39.          # It's not the same as moving all the time, though.
  40.  
  41. class Pawn(Piece):
  42.     def __init__(self, name=""):
  43.         Piece.__init__(self, name)
  44.     def move(self, from, to):
  45.         # A rule if it's on a starting block it can move two, otherwise one.
  46.     def capture(self, from, to):
  47.         # Pawns capture diagonal, or via en passant.