From: <umg...@us...> - 2007-03-29 19:26:45
|
Revision: 371 http://svn.sourceforge.net/pybridge/?rev=371&view=rev Author: umgangee Date: 2007-03-29 12:26:41 -0700 (Thu, 29 Mar 2007) Log Message: ----------- Fix a bug which allowed Redouble on top of another Redouble. Modified Paths: -------------- trunk/pybridge/pybridge/bridge/bidding.py Modified: trunk/pybridge/pybridge/bridge/bidding.py =================================================================== --- trunk/pybridge/pybridge/bridge/bidding.py 2007-03-27 15:39:04 UTC (rev 370) +++ trunk/pybridge/pybridge/bridge/bidding.py 2007-03-29 19:26:41 UTC (rev 371) @@ -152,14 +152,15 @@ if isinstance(call, Double): opposition = (Player[(self.whoseTurn().index + 1) % 4], Player[(self.whoseTurn().index + 3) % 4]) - return not self.getCurrentCall(Double) and bidder in opposition + 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]) - return self.getCurrentCall(Double) and bidder in partnership + return bidder in partnership and self.getCurrentCall(Double) \ + and not self.getCurrentCall(Redouble) return False # Otherwise unavailable. This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <umg...@us...> - 2007-04-01 17:37:23
|
Revision: 380 http://svn.sourceforge.net/pybridge/?rev=380&view=rev Author: umgangee Date: 2007-04-01 10:37:24 -0700 (Sun, 01 Apr 2007) Log Message: ----------- Code cleanup; replace "assert <condition>" statements with "if not <condition>: raise TypeError" statements. Modified Paths: -------------- trunk/pybridge/pybridge/bridge/bidding.py Modified: trunk/pybridge/pybridge/bridge/bidding.py =================================================================== --- trunk/pybridge/pybridge/bridge/bidding.py 2007-04-01 17:35:52 UTC (rev 379) +++ trunk/pybridge/pybridge/bridge/bidding.py 2007-04-01 17:37:24 UTC (rev 380) @@ -16,7 +16,9 @@ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. -from call import Call, Bid, Pass, Double, Redouble +from pybridge.network.error import GameError + +from call import Bid, Pass, Double, Redouble from symbols import Direction, Level, Strain @@ -39,17 +41,18 @@ and the last 3 calls are Pass calls. @return: True if bidding is complete, False if not. + @rtype: bool """ passes = len([c for c in self.calls[-3:] if isinstance(c, Pass)]) return len(self.calls) >= 4 and passes == 3 def isPassedOut(self): - """Bidding is passed out if each player has called Pass on their - first turn. This implies no contract has been established. - Note that this is a special case of isComplete(). - + """Bidding is passed out if each player has passed on their first turn. + In this case, the bidding is complete, but no contract is established. + @return: True if bidding is passed out, False if not. + @rtype: bool """ passes = len([call for call in self.calls if isinstance(call, Pass)]) return len(self.calls) == 4 and passes == 4 @@ -58,22 +61,24 @@ def getContract(self): """When the bidding is complete, the contract is the last and highest bid, which may be doubled or redoubled. - + Hence, the contract represents the "final state" of the bidding. - @return['bid']: the last and highest bid. - @return['declarer']: the partner who first called the contract strain. - @return['doubleBy']: the opponent who doubled the contract, or None. - @return['redoubleBy']: the partner who redoubled an opponent's double - on the contract, or None. + @return: a dict containing the keywords: + @keyword bid: the last and highest bid. + @keyword declarer: the partner who first bid the contract strain. + @keyword doubleBy: the opponent who doubled the contract, or None. + @keyword redoubleBy: the partner who redoubled an opponent's double + on the contract, or None. """ - bid = self.getCurrentCall(Bid) - if bid and self.isComplete() and not self.isPassedOut(): + if self.isComplete() and not self.isPassedOut(): + bid = self.getCurrentCall(Bid) double = self.getCurrentCall(Double) redouble = self.getCurrentCall(Redouble) + # Determine partnership. + caller = self.whoCalled(bid) + partnership = (caller, Direction[(caller.index + 2) % 4]) # Determine declarer. - partnership = (self.whoCalled(bid), \ - 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: @@ -87,15 +92,17 @@ return None # Bidding passed out or not complete, no contract. - def getCurrentCall(self, type): + def getCurrentCall(self, calltype): """Returns most recent current call of specified type, or None. - @param type: call type, in (Bid, Pass, Double, Redouble). + @param calltype: call type, in (Bid, Pass, Double, Redouble). @return: most recent call matching type, or None. """ - assert issubclass(type, Call) + if calltype not in (Bid, Pass, Double, Redouble): + raise GameError, "Expected call type, got %s" % type(calltype) + for call in self.calls[::-1]: - if isinstance(call, type): + if isinstance(call, calltype): return call elif isinstance(call, Bid): break @@ -108,13 +115,14 @@ @param call: the Call object representing player's call. @param player: the player making call, or None. """ - assert isinstance(call, Call) - valid = self.isValidCall(call, player) - assert valid - if valid: # In case assert is disabled. - self.calls.append(call) + if not isinstance(call, (Bid, Pass, Double, Redouble)): + raise GameError, "Expected call type, got %s" % type(call) + if not self.isValidCall(call, player): + raise GameError, "Invalid call" + self.calls.append(call) + def isValidCall(self, call, player=None): """Check that specified call is available to player, with respect to current state of bidding. If specified, player's turn will be checked. @@ -123,7 +131,8 @@ @param player: the player attempting to call, or None. @return: True if call is available, False if not. """ - assert isinstance(call, Call) + if not isinstance(call, (Bid, Pass, Double, Redouble)): + raise GameError, "Expected call type, got %s" % type(call) assert player in Direction or player is None # The bidding must not be complete. @@ -172,7 +181,9 @@ @param call: a Call. @return: the player who made call, or False. """ - assert isinstance(call, Call) + if not isinstance(call, (Bid, Pass, Double, Redouble)): + raise GameError, "Expected call type, got %s" % type(call) + if call in self.calls: return Direction[(self.calls.index(call) + self.dealer.index) % 4] return False # Call not made by any player. @@ -185,6 +196,6 @@ @rtype: Direction """ if self.isComplete(): - return None + raise GameError, "Bidding complete" return Direction[(len(self.calls) + self.dealer.index) % 4] This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |