From: Brett K. <in...@us...> - 2005-08-15 06:49:55
|
Update of /cvsroot/wtfibs/WTFibs/src In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv17922/src Modified Files: Backgammon.py Log Message: fixed isValidMove Index: Backgammon.py =================================================================== RCS file: /cvsroot/wtfibs/WTFibs/src/Backgammon.py,v retrieving revision 1.6 retrieving revision 1.7 diff -u -d -r1.6 -r1.7 --- Backgammon.py 11 Aug 2005 00:30:28 -0000 1.6 +++ Backgammon.py 15 Aug 2005 06:49:48 -0000 1.7 @@ -69,21 +69,9 @@ else: return None - def isValidDestination(self,color): - """Checks to see if a checker of 'color' can land here legally""" - if color == "White": - if len([x for x in self.checkers if x.color == "Black"]) > 1: - return False - else: - return True - elif color == "Black": - if len([x for x in self.checkers if x.color == "White"]) > 1: - return False - else: - return True - class Die(object): """Represents a single die""" + def __init__(self, sides=6): self.sides = sides self.roll() @@ -108,15 +96,17 @@ def __init__(self): """Create all points and checkers""" self.points = [] + self.colors = ["Black","White"] self.buildPoints() self.setInitialPosition() + self.whitePipCount = 167 + self.blackPipCount = 167 def buildPoints(self): """Create BoardPoint objects and Bar/Bearoff points""" for i in range(1,25): self.points.append(BoardPoint(i)) - self.whiteBar = ExtraBoardPoint("White","Bar") self.blackBar = ExtraBoardPoint("Black","Bar") self.whiteBearOff = ExtraBoardPoint("White","Bearoff") @@ -128,17 +118,7 @@ starting positions. As it's not likely the starting position will ever change, these values are hardcoded (but they shouldn't be - maybe use a config option or somesuch). - - Point 01 - 2 Black - Point 06 - 5 White - Point 08 - 3 White - Point 12 - 5 Black - Point 13 - 5 White - Point 17 - 3 Black - Point 19 - 5 Black - Point 24 - 2 White """ - pointConfig = { 1 : "2 Black", 6 : "5 White", @@ -154,43 +134,78 @@ for i in range(int(cnt)): self.points[p-1].push(Checker(color)) + def isValidMove(self,color,startpos,die): + """Checks to see if a checker of 'color' can move die spaces legally""" + if color not in ["White","Black"] \ + or startpos < 1 or startpos > 24 \ + or die < 1 or die > 6 \ + or not len(self.points[startpos-1].checkers): + raise ValueError("Invalid function arguments") + + if color == "White": + cks = len([x for x in self.points[(startpos-1)-die].checkers \ + if x.color == self._getOppositeColor(color)]) + elif color == "Black": + cks = len([x for x in self.points[(startpos-1)+die].checkers \ + if x.color == self._getOppositeColor(color)]) + return cks <= 1 + def move(self,startpos,endpos): """Move piece from startpos to endpos""" if startpos < 1 or endpos < 1: - raise Exception("Negative Point Index") + raise ValueError("Negative Point Index") c = self.points[startpos-1].pop() self.points[endpos-1].push(c) def moveToBar(self,color,startpos): """Move checker from startpos to the bar""" + if startpos < 1 or startpos > 24: + raise ValueError("Invalid point index") + if color not in self.colors: + raise ValueError("Invalid color") c = self.points[startpos-1].pop() - if color == "White": - self.whiteBar.push(c) - elif color == "Black": - self.blackBar.push(c) + getattr(self,color.lower() + 'Bar').push(c) def bearOff(self,color,pos): """Bear off one checker from pos""" + if color not in self.colors: + raise ValueError("Invalid Color") + if pos < 1 or pos > 24: + raise ValueError("Invalid point index") c = self.points[pos-1].pop() - if color == "White": - self.whiteBearOff.push(c) - elif color == "Black": - self.blackBearOff.push(c) + getattr(self,color.lower() + 'BearOff').push(c) def moveFromBar(self,color,pos): """Move from the bar to the board""" - if color == "White": - c = self.whiteBar.pop() - self.points[pos-1].push(c) - elif color == "Black": - c = self.blackBar.pop() - self.points[pos-1].push(c) - - ## Need a method here to calculate legal moves - ## as well as some sort of "status dump" thing for debugging - ## should print out nicely the current state of the board - ## don't forget pip counting - that'll be fun + if color not in self.colors: + raise ValueError("Invalid color") + if pos < 1 or pos > 24: + raise ValueError("Invalid point index") + c = getattr(self,color.lower() + 'Bar').pop() + self.points[pos-1].push(c) - ## UNIT. TESTS. NOW. + def _dump(self): + """Return the current board position in readable test - for debugging""" + t = "" + for p in self.points: + wc = len([x for x in p.checkers if x.color == "White"]) + bc = len([x for x in p.checkers if x.color == "Black"]) + if bc or wc: + t += "Point %d" % p.pos + "\n" + if wc: + t += "\tWhite Checkers: %d" % \ + len([x for x in p.checkers if x.color == "White"]) \ + + "\n" + elif bc: + t += "\tBlack Checkers: %d" % \ + len([x for x in p.checkers if x.color == "Black"]) \ + + "\n" + return t - ## right now, i'm going to bed + def _getOppositeColor(self, color): + if color.lower().capitalize() == "Black": + return "White" + elif color.lower().capitalize() == "White": + return "Black" + else: + raise valueError("Invalid color %s" % color) |