CS 204: Software Design
Due 8:30AM Friday, 5 February 2010
You may work with a partner for this assignment.
Implementing part of the Mancala engine
See the previous assignment for a description
of the context of this assignment.
Your job for this assignment will be to implement the
move
and test
methods described in the following
class. On Friday, we will meet in the lab and run your implementation
against everybody else's test cases to see if we can find and fix
problems in your program.
class MancalaBoard:
def __init__(self):
self.reset()
def reset(self):
'''Initializes the board to the starting game state.
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 = [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.
If the move specified by binIndex is not legal, this
method will raise an Exception object with argument equal
to one of the following:
'WrongPlayer'
'AttemptToMoveFromStore'
'AttemptToMoveFromEmptyBin'
'BinIndexOutOfRange'
'GameAlreadyOver'
Note that once one player's side is cleared, the stones
on the other player's side will remain in their bins
rather than being moved immediately to the corresponding
store. Thus, GameAlreadyOver happens only if the
non-clear player tries to move from a bin that has
stones in it.
An index < 0 or > 13 counts as BinIndexOutOfRange,
not any of the other errors.
Player 1 owns bins 0-6, and Player 2 owns bins 7-13.
Thus, a move from bin 6 is AttemptToMoveFromStore,
not WrongPlayer.
'''
pass
def test(self, testCaseFile):
'''Reads lines of the specified file object (assumed to be already
open for reading) and runs each test. Reports test results
to standard output.
'''
pass
if __name__ == '__main__':
import sys
if len(sys.argv) != 2:
sys.stderr.write('Usage: %s testcasefile\n' % sys.argv[0])
exit()
testCaseFileName = sys.argv[1]
try:
testCaseFile = open(testCaseFileName)
except Exception, e:
sys.stderr.write('Cannot open %s for reading.\n' % testCaseFileName)
exit()
board = MancalaBoard()
board.test(testCaseFile)
testCaseFile.close()