From: <umg...@us...> - 2007-03-29 19:53:45
|
Revision: 373 http://svn.sourceforge.net/pybridge/?rev=373&view=rev Author: umgangee Date: 2007-03-29 12:53:46 -0700 (Thu, 29 Mar 2007) Log Message: ----------- Rename Player to Direction, tidy up some code. Modified Paths: -------------- trunk/pybridge/pybridge/bridge/bidding.py trunk/pybridge/pybridge/bridge/deck.py trunk/pybridge/pybridge/bridge/playing.py Modified: trunk/pybridge/pybridge/bridge/bidding.py =================================================================== --- trunk/pybridge/pybridge/bridge/bidding.py 2007-03-29 19:42:57 UTC (rev 372) +++ trunk/pybridge/pybridge/bridge/bidding.py 2007-03-29 19:53:46 UTC (rev 373) @@ -17,10 +17,10 @@ from call import Call, Bid, Pass, Double, Redouble -from symbols import Level, Player, Strain +from symbols import Direction, Level, Strain -class Bidding: +class Bidding(object): """This class models the bidding (auction) phase of a game of bridge. A bidding session is a list of Call objects and the dealer. @@ -28,7 +28,8 @@ def __init__(self, dealer): - assert dealer in Player + if dealer not in Direction: + raise TypeError, "Expected Direction, got %s" % type(dealer) self.calls = [] self.dealer = dealer @@ -72,7 +73,7 @@ redouble = self.getCurrentCall(Redouble) # Determine declarer. partnership = (self.whoCalled(bid), \ - Player[(self.whoCalled(bid).index + 2) % 4]) + Direction[(self.whoCalled(bid).index + 2) % 4]) for call in self.calls: if isinstance(call, Bid) and call.strain == bid.strain \ and self.whoCalled(call) in partnership: @@ -123,7 +124,7 @@ @return: True if call is available, False if not. """ assert isinstance(call, Call) - assert player in Player or player is None + assert player in Direction or player is None # The bidding must not be complete. if self.isComplete(): @@ -150,15 +151,15 @@ # A double must be made on the current bid from opponents, # with has not been already doubled by partnership. if isinstance(call, Double): - opposition = (Player[(self.whoseTurn().index + 1) % 4], - Player[(self.whoseTurn().index + 3) % 4]) + opposition = (Direction[(self.whoseTurn().index + 1) % 4], + Direction[(self.whoseTurn().index + 3) % 4]) return bidder in opposition and not self.getCurrentCall(Double) # A redouble must be made on the current bid from partnership, # which has been doubled by an opponent. elif isinstance(call, Redouble): partnership = (self.whoseTurn(), - Player[(self.whoseTurn().index + 2) % 4]) + Direction[(self.whoseTurn().index + 2) % 4]) return bidder in partnership and self.getCurrentCall(Double) \ and not self.getCurrentCall(Redouble) @@ -173,15 +174,17 @@ """ assert isinstance(call, Call) if call in self.calls: - return Player[(self.calls.index(call) + self.dealer.index) % 4] + return Direction[(self.calls.index(call) + self.dealer.index) % 4] return False # Call not made by any player. def whoseTurn(self): - """If bidding is not complete, returns the player who is next to call. + """Returns position of player who is next to make a call. - @return: the player next to call. + @return: the current turn. + @rtype: Direction """ - player = Player[(len(self.calls) + self.dealer.index) % 4] - return not self.isComplete() and player + if self.isComplete(): + return None + return Direction[(len(self.calls) + self.dealer.index) % 4] Modified: trunk/pybridge/pybridge/bridge/deck.py =================================================================== --- trunk/pybridge/pybridge/bridge/deck.py 2007-03-29 19:42:57 UTC (rev 372) +++ trunk/pybridge/pybridge/bridge/deck.py 2007-03-29 19:53:46 UTC (rev 373) @@ -21,7 +21,7 @@ from random import shuffle from card import Card -from symbols import Player, Rank, Suit +from symbols import Direction, Rank, Suit # See http://mail.python.org/pipermail/edu-sig/2001-May/001288.html for details. @@ -30,17 +30,17 @@ # TODO: consider making Hand a subclass of List, with additional constraints. -class Deck: +class Deck(object): """A Deck object provides operations for dealing Card objects. A hand is a collection of 13 cards from the deck. A deal is a distribution of all 52 cards to four hands. - A deal is represented as a dictionary, mapping Player labels to lists (hands) - of Card objects. + A deal is represented as a dictionary, mapping Direction labels to + lists (hands) of Card objects. There are exactly 52! / (13!)**4 (comb(52,13) * comb(39,13) * comb(26,13)) - distinct deals of 13 cards to 4 players from a standard 52-card deck. + distinct deals of 13 cards to 4 positions from a standard 52-card deck. """ cards = [Card(r, s) for r in Rank for s in Suit] @@ -56,7 +56,7 @@ def isValidDeal(self, deal): """Checks that structure of deal conforms to requirements: - * 4-element dict, mapping Player objects to hand lists. + * 4-element dict, mapping Direction objects to hand lists. * Hand lists contain exactly 13 Card objects. * No card may be repeated in the same hand, or between hands. * The cards in hands may be in any order. @@ -64,7 +64,7 @@ @param deal: a deal dict. @return: True if deal is valid, False otherwise. """ - return True # TODO + return True # TODO - if invalid, perhaps give reason def randomDeal(self): @@ -74,10 +74,10 @@ """ shuffle(self.cards) hands = {} - for player in Player: - hands[player] = [] + for position in Direction: + hands[position] = [] for index, card in enumerate(self.cards): - hands[Player[index % len(Player)]].append(card) + hands[Direction[index % len(Direction)]].append(card) for hand in hands.values(): hand.sort() return hands @@ -98,19 +98,19 @@ indexes = {} # For each hand, compute indexes of cards in cardSeq. - for player in (Player.North, Player.East, Player.South): - indexes[player] = 0 - deal[player].sort(reverse=False) + for position in (Direction.North, Direction.East, Direction.South): + indexes[position] = 0 + deal[position].sort(reverse=False) # It is desirable to remove cards from cardSeq when adding their # indexes, instead of doing so in an extra step. # Removing cards backwards preserves the indexes of later cards. - for i, card in enumerate(deal[player]): - indexes[player] += comb(cardSeq.index(card), 13-i) + for i, card in enumerate(deal[position]): + indexes[position] += comb(cardSeq.index(card), 13-i) cardSeq.remove(card) # Deal index = (Nindex * Emax * Smax) + (Eindex * Smax) + Sindex - indexes[Player.North] *= self.Emax * self.Smax - indexes[Player.East] *= self.Smax + indexes[Direction.North] *= self.Emax * self.Smax + indexes[Direction.East] *= self.Smax return long(sum(indexes.values())) @@ -130,28 +130,28 @@ deal = {} # Split index into hand indexes. - indexes = {Player.North : (num / self.Smax) / self.Emax, - Player.East : (num / self.Smax) % self.Emax, - Player.South : (num % self.Smax) } + indexes = {Direction.North : (num / self.Smax) / self.Emax, + Direction.East : (num / self.Smax) % self.Emax, + Direction.South : (num % self.Smax) } - for player in (Player.North, Player.East, Player.South): - deal[player] = [] + for position in (Direction.North, Direction.East, Direction.South): + deal[position] = [] for k in range(13, 0, -1): - # Find the largest n such that comb(n, k) <= indexes[player]. + # Find the largest n such that comb(n, k) <= indexes[position]. n = k-1 # n < k implies comb(n, k) = 0 # comb(n+1, k) = # n-k = -1 => comb(n, k) * (n+1) # otherwise => (comb(n, k) * (n+1)) / (n+1 - k) - while comb(n+1, k) <= indexes[player]: + while comb(n+1, k) <= indexes[position]: n += 1 # Remove card index from indices, add card to hand. - indexes[player] -= comb(n, k) + indexes[position] -= comb(n, k) card = cardSeq[n] - deal[player].append(card) + deal[position].append(card) cardSeq.remove(card) - deal[Player.West] = cardSeq # West has the remaining cards. + deal[Direction.West] = cardSeq # West has the remaining cards. return deal Modified: trunk/pybridge/pybridge/bridge/playing.py =================================================================== --- trunk/pybridge/pybridge/bridge/playing.py 2007-03-29 19:42:57 UTC (rev 372) +++ trunk/pybridge/pybridge/bridge/playing.py 2007-03-29 19:53:46 UTC (rev 373) @@ -157,7 +157,7 @@ return False elif hand and card not in hand: return False # Playing a card not in hand. - elif player and player is not self.whoseTurn(): + elif player and player != self.whoseTurn(): return False # Playing out of turn. elif self.whoPlayed(card): return False # Card played previously. This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |