From: James V. <jam...@us...> - 2005-08-16 04:57:32
|
Update of /cvsroot/wtfibs/WTFibs/src In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv9585/src Modified Files: Backgammon.py Log Message: Change some formatting and clean up a little code, style-wise. Index: Backgammon.py =================================================================== RCS file: /cvsroot/wtfibs/WTFibs/src/Backgammon.py,v retrieving revision 1.7 retrieving revision 1.8 diff -u -d -r1.7 -r1.8 --- Backgammon.py 15 Aug 2005 06:49:48 -0000 1.7 +++ Backgammon.py 16 Aug 2005 04:57:24 -0000 1.8 @@ -30,14 +30,14 @@ import random ### -# A series of classes that represent a backgammon board, as well as +# A series of classes that represent a backgammon board, as well as # providing moving functionality. ### class Player(object): """Represents a Backgammon player""" - def __init__(self,name,rating,color): + def __init__(self, name, rating, color): """Init, sets name, color, rating""" self.name = name self.rating = rating @@ -46,21 +46,21 @@ class Checker(object): """Represents an individual checker""" - def __init__(self,color): - "Init, sets color" + def __init__(self, color): + """Init, sets color""" self.color = color class BoardPoint(object): """Represents a single point on the board""" - def __init__(self,pos): + def __init__(self, pos): """Init, sets position on the board and empty list of checker objects""" self.pos = pos self.checkers = [] - def push(self,checker): + def push(self, checker): """Pushes checker onto the point (Stack-like)""" - self.checkers.insert(0,checker) + self.checkers.insert(0, checker) def pop(self): """Removes the top checker and returns it""" @@ -71,7 +71,7 @@ class Die(object): """Represents a single die""" - + def __init__(self, sides=6): self.sides = sides self.roll() @@ -84,9 +84,9 @@ class ExtraBoardPoint(BoardPoint): """Generic class for non-board-point areas (Bar and Bearoff Area)""" - def __init__(self,name,color): + def __init__(self, name, color): """Each color will have it's own bar and bearoff area""" - BoardPoint.__init__(self,color) + BoardPoint.__init__(self, color) self.color = color self.name = name @@ -96,7 +96,7 @@ def __init__(self): """Create all points and checkers""" self.points = [] - self.colors = ["Black","White"] + self.colors = {'Black':'White', 'White':'Black'} self.buildPoints() self.setInitialPosition() self.whitePipCount = 167 @@ -104,13 +104,13 @@ def buildPoints(self): """Create BoardPoint objects and Bar/Bearoff points""" - for i in range(1,25): + 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") - self.blackBearOff = ExtraBoardPoint("Black","Bearoff") + self.whiteBar = ExtraBoardPoint('White', 'Bar') + self.blackBar = ExtraBoardPoint('Black', 'Bar') + self.whiteBearOff = ExtraBoardPoint('White', 'Bearoff') + self.blackBearOff = ExtraBoardPoint('Black', 'Bearoff') def setInitialPosition(self): """ @@ -120,92 +120,83 @@ shouldn't be - maybe use a config option or somesuch). """ pointConfig = { - 1 : "2 Black", - 6 : "5 White", - 8 : "3 White", - 12 : "5 Black", - 13 : "5 White", - 17 : "3 Black", - 19 : "5 Black", - 24 : "2 White" } + 1 : '2 Black', + 6 : '5 White', + 8 : '3 White', + 12 : '5 Black', + 13 : '5 White', + 17 : '3 Black', + 19 : '5 Black', + 24 : '2 White' } for p in pointConfig.keys(): - cnt, color = pointConfig[p].split(None,1) + (cnt, color) = pointConfig[p].split(None, 1) for i in range(int(cnt)): - self.points[p-1].push(Checker(color)) + self.points[p - 1].push(Checker(color)) - def isValidMove(self,color,startpos,die): + 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"] \ + color = color.capitalize() + if color not in self.colors \ 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") + 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)]) + if color == 'White': + cks = len([x for x in self.points[(startpos - 1) - die].checkers + if x.color == self.colors[color]]) + elif color == 'Black': + cks = len([x for x in self.points[(startpos - 1) + die].checkers + if x.color == self.colors[color]]) return cks <= 1 - - def move(self,startpos,endpos): + + def move(self, startpos, endpos): """Move piece from startpos to endpos""" if startpos < 1 or endpos < 1: - raise ValueError("Negative Point Index") - c = self.points[startpos-1].pop() - self.points[endpos-1].push(c) + raise ValueError('Negative Point Index') + c = self.points[startpos - 1].pop() + self.points[endpos - 1].push(c) - def moveToBar(self,color,startpos): + 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() - getattr(self,color.lower() + 'Bar').push(c) + raise ValueError('Invalid point index') + if color.capitalize() not in self.colors: + raise ValueError('Invalid color') + c = self.points[startpos - 1].pop() + getattr(self, color.lower() + 'Bar').push(c) - def bearOff(self,color,pos): + def bearOff(self, color, pos): """Bear off one checker from pos""" - if color not in self.colors: - raise ValueError("Invalid Color") + if color.capitalize() 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() - getattr(self,color.lower() + 'BearOff').push(c) + raise ValueError('Invalid point index') + c = self.points[pos - 1].pop() + getattr(self, color.lower() + 'BearOff').push(c) - def moveFromBar(self,color,pos): + def moveFromBar(self, color, pos): """Move from the bar to the board""" - if color not in self.colors: - raise ValueError("Invalid color") + if color.capitalize() 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) + raise ValueError('Invalid point index') + c = getattr(self, color.lower() + 'Bar').pop() + self.points[pos - 1].push(c) 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"]) + 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" + t += 'Point %s\n' % p.pos if wc: - t += "\tWhite Checkers: %d" % \ - len([x for x in p.checkers if x.color == "White"]) \ - + "\n" + t += '\tWhite Checkers: %s\n' % \ + len([x for x in p.checkers if x.color == 'White']) elif bc: - t += "\tBlack Checkers: %d" % \ - len([x for x in p.checkers if x.color == "Black"]) \ - + "\n" + t += '\tBlack Checkers: %s\n' % \ + len([x for x in p.checkers if x.color == 'Black']) return t - - 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) |