# Experiment from class, 10/6/10 class WrongPlayerError(Exception): def __init__(self, playerNumber): self.playerNumber = playerNumber class EmptyBinError(Exception): def __init__(self, binNumber): self.binNumber = binNumber class MancalaBoard: def __init__(self): '''Bins 0-5 are Player 1's bins, bin 6 is Player 1's store (i.e. the big bin), bins 7-12 are Player 2's bins, and bin 13 is Player 2's store.''' self.bins = [] self.isFirstPlayerTurn = True self.reset() def reset(self): self.bins = [3, 3, 3, 3, 3, 3, 0, 3, 3, 3, 3, 3, 3, 0] self.isFirstPlayerTurn = True def move(self, binIndex): '''If the move starting in the specified bin index is legal, this method adjusts the game state accordingly. Otherwise, it raises an Exception.''' numberOfBins = len(self.bins) if binIndex >= numberOfBins / 2 and self.isFirstPlayerTurn: raise WrongPlayerError(2) elif binIndex < numberOfBins / 2 and not self.isFirstPlayerTurn: raise WrongPlayerError(1) if self.bins[binIndex] == 0: raise EmptyBinError(binIndex) # More error checking and moving goes here. def isMoveLegal(self, binIndex): '''Returns True if a move starting at the specified binIndex is legal, and False otherwise.''' pass board = MancalaBoard() try: board.move(13) except WrongPlayerError, e: print 'Player %d tried to move out of turn' % (e.playerNumber) except EmptyBinError, e: print 'Player tried to move from empty bin #%d' % (e.binNumber) print 'I got here'