From: James V. <jam...@us...> - 2005-08-10 22:31:11
|
Update of /cvsroot/wtfibs/WTFibs/src In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv9723/src Modified Files: Backgammon.py Log Message: More Unix line endings. Index: Backgammon.py =================================================================== RCS file: /cvsroot/wtfibs/WTFibs/src/Backgammon.py,v retrieving revision 1.3 retrieving revision 1.4 diff -u -d -r1.3 -r1.4 --- Backgammon.py 10 Aug 2005 21:06:02 -0000 1.3 +++ Backgammon.py 10 Aug 2005 22:30:58 -0000 1.4 @@ -1,170 +1,170 @@ -### -# Copyright (c) 2005 Brett Kelly -# All rights reserved. -# -# Redistribution and use in source and binary forms, with or without -# modification, are permitted provided that the following conditions are met: -# -# * Redistributions of source code must retain the above copyright notice, -# this list of conditions, and the following disclaimer. -# * Redistributions in binary form must reproduce the above copyright notice, -# this list of conditions, and the following disclaimer in the -# documentation and/or other materials provided with the distribution. -# * Neither the name of the author of this software nor the name of -# contributors to this software may be used to endorse or promote products -# derived from this software without specific prior written permission. -# -# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" -# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE -# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE -# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE -# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR -# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF -# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS -# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN -# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) -# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE -# POSSIBILITY OF SUCH DAMAGE. -### - -import random - -### -# A series of classes that represent a backgammon board -### - -class Player(object): - """Represents a Backgammon player""" - - def __init__(self,name,rating,color): - """Init, sets name, color, rating""" - self.name = name - self.rating = rating - self.color = color - -class Checker(object): - """Represents an individual checker""" - - def __init__(self,color): - "Init, sets color" - self.color = color - -class BoardPoint(object): - """Represents a single point on the board""" - - 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): - """Pushes checker onto the point (Stack-like)""" - self.checkers.insert(0,checker) - - def pop(self): - """Removes the top checker and returns it""" - if self.checkers: - return self.checkers.pop(0) - else: - return None - -class Die(object): - """Represents a single die""" - def __init__(self, sides=6): - self.sides = sides - self.roll() - - def roll(self): - """Roll the die, get a number back between 1 and self.sides""" - self.face = random.randint(1, self.sides) - return self.face - -class ExtraBoardPoint(BoardPoint): - """Generic class for non-board-point areas (Bar and Bearoff Area)""" - - def __init__(self,name,color): - """Each color will have it's own bar and bearoff area""" - self.color = color - self.name = name - -class Board(object): - """A Backgammon Board class""" - - def __init__(self): - """Create all points and checkers""" - self.points = [] - self.buildPoints() - self.setInitialPosition() - - 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") - self.blackBearOff = ExtraBoardPoint("Black","Bearoff") - - def setInitialPosition(self): - """ - Create checker objects and place them in the correct - 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", - 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) - for i in range(int(cnt)): - self.points[p-1].push(Checker(color)) - - def move(self,startpos,endpos): - """Move piece from startpos to endpos""" - c = self.points[startpos].pop() - self.points[endpos].push(c) - - def moveToBar(self,color,startpos): - """Move checker from startpos to the bar""" - c = self.points[startpos].pop() - if color == "White": - self.whiteBar.push(c) - elif color == "Black": - self.blackBar.push(c) - - def bearOff(self,color,pos): - """Bear off one checker from pos""" - c = self.points[pos].pop() - if color == "White": - self.whiteBearOff.push(c) - elif color == "Black": - self.blackBearOff.push(c) - - ## Need a method here to calculate (and perform) 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 - - ## UNIT. TESTS. NOW. - - ## right now, i'm going to bed +### +# Copyright (c) 2005 Brett Kelly +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are met: +# +# * Redistributions of source code must retain the above copyright notice, +# this list of conditions, and the following disclaimer. +# * Redistributions in binary form must reproduce the above copyright notice, +# this list of conditions, and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# * Neither the name of the author of this software nor the name of +# contributors to this software may be used to endorse or promote products +# derived from this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +# POSSIBILITY OF SUCH DAMAGE. +### + +import random + +### +# A series of classes that represent a backgammon board +### + +class Player(object): + """Represents a Backgammon player""" + + def __init__(self,name,rating,color): + """Init, sets name, color, rating""" + self.name = name + self.rating = rating + self.color = color + +class Checker(object): + """Represents an individual checker""" + + def __init__(self,color): + "Init, sets color" + self.color = color + +class BoardPoint(object): + """Represents a single point on the board""" + + 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): + """Pushes checker onto the point (Stack-like)""" + self.checkers.insert(0,checker) + + def pop(self): + """Removes the top checker and returns it""" + if self.checkers: + return self.checkers.pop(0) + else: + return None + +class Die(object): + """Represents a single die""" + def __init__(self, sides=6): + self.sides = sides + self.roll() + + def roll(self): + """Roll the die, get a number back between 1 and self.sides""" + self.face = random.randint(1, self.sides) + return self.face + +class ExtraBoardPoint(BoardPoint): + """Generic class for non-board-point areas (Bar and Bearoff Area)""" + + def __init__(self,name,color): + """Each color will have it's own bar and bearoff area""" + self.color = color + self.name = name + +class Board(object): + """A Backgammon Board class""" + + def __init__(self): + """Create all points and checkers""" + self.points = [] + self.buildPoints() + self.setInitialPosition() + + 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") + self.blackBearOff = ExtraBoardPoint("Black","Bearoff") + + def setInitialPosition(self): + """ + Create checker objects and place them in the correct + 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", + 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) + for i in range(int(cnt)): + self.points[p-1].push(Checker(color)) + + def move(self,startpos,endpos): + """Move piece from startpos to endpos""" + c = self.points[startpos].pop() + self.points[endpos].push(c) + + def moveToBar(self,color,startpos): + """Move checker from startpos to the bar""" + c = self.points[startpos].pop() + if color == "White": + self.whiteBar.push(c) + elif color == "Black": + self.blackBar.push(c) + + def bearOff(self,color,pos): + """Bear off one checker from pos""" + c = self.points[pos].pop() + if color == "White": + self.whiteBearOff.push(c) + elif color == "Black": + self.blackBearOff.push(c) + + ## Need a method here to calculate (and perform) 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 + + ## UNIT. TESTS. NOW. + + ## right now, i'm going to bed |