Exemplo n.º 1
0
 def setUp(self):
     print 'Rotating around the table,', self.shortDescription()
     self.table = Table()
Exemplo n.º 2
0
 def __init__(self, interacts):
     self._interacts = interacts
     self.evt_done = Event()
     self._interacts.evt_playerResponse += self._on_PlayerResponse
     self.lastToRaise = None
     self.table = Table()
Exemplo n.º 3
0
class testRotatingAroundTheTable(unittest.TestCase):
    def setUp(self):
        print 'Rotating around the table,', self.shortDescription()
        self.table = Table()

    def testA_DealingToFirstAtTable(self):
        '''deal to the first person at the table'''
        self.table.seat(['p1', 'p2', 'p3'])

        self.assertEqual('p1', self.table.dealingTo())

    def testB_MovingToTheNext(self):
        '''move to the next'''
        self.table.seat(['p1', 'p2', 'p3'])
        self.table.nextPlayer()

        self.assertEqual('p2', self.table.dealingTo())

    def testC_ThenTheLast(self):
        '''all the way to the last'''
        self.table.seat(['p1', 'p2', 'p3'])

        self.table.nextPlayer()
        self.table.nextPlayer()

        self.assertEqual('p3', self.table.dealingTo())

    def testD_BackToFirst(self):
        '''back to first after last'''
        self.table.seat(['p1', 'p2', 'p3'])

        self.table.nextPlayer()
        self.table.nextPlayer()
        self.table.nextPlayer()

        self.assertEqual('p1', self.table.dealingTo())
Exemplo n.º 4
0
class TakesBets(object):

    def __init__(self, interacts):
        self._interacts = interacts
        self.evt_done = Event()
        self._interacts.evt_playerResponse += self._on_PlayerResponse
        self.lastToRaise = None
        self.table = Table()

    def start(self):
        self.table.seat(self._interacts.players)
        self.lastToRaise = self.table.dealingTo()
        self.next()

    def next(self):
        if self._notEnoughWithChips():
            self._finish()
        elif self._onePlayerLeft():
            self._finish()
        elif self.table.dealingTo().isPlaying():
            dealTo = self.table.dealingTo()
            self._interacts.sendMessage(dealTo, 'GO')
        else:
            self.table.nextPlayer()
            self.next()

    def _on_PlayerResponse(self, sender, response):
        player = response[0]
        amount = response[1]

        if player != self.table.dealingTo():
            self._kickOutOfGame(player, 'OUT_OF_TURN')
            return

        if not amount.isdigit():
            self._kickOutOfGame(player, 'NOT_A_NUMBER')
            return

        self._add(player, int(amount))

        self.table.nextPlayer()

        if self._done():
            self._finish()
        else:
            self.next()

    def getMinimumBet(self, player):
        highestContribution = max([p.pot for p in self.table.playing()])
        minBet = highestContribution - player.pot
        return minBet

    def _notEnoughWithChips(self):
        hasChips = len([p for p in self.table.playing() if p.chips > 0])
        return hasChips <= 1

    def _finish(self):
        self.evt_done(self)

    def _done(self):
        if self._notEnoughWithChips():
            return True

        return self.lastToRaise == self.table.dealingTo()

    def _onePlayerLeft(self):
        hasCards = len([p for p in self._interacts.players if p.isPlaying()])
        return hasCards == 1

    def _add(self, player, amount):
        if self._folding(player, amount):
            self._fold(player)
        elif amount > player.chips:
            self._kickOutOfGame(player, 'OVERDRAWN')
            amount = 0

        self._interacts.broadcast('BET ' + player.name + ' ' + str(amount))
        if amount > self.getMinimumBet(player):
            self.lastToRaise = player

        player.bet(amount)

    def _folding(self, player, amount):
        return amount < self.getMinimumBet(player) and player.chips - amount > 0

    def _fold(self, player):
        player.dropCards()
        self._interacts.sendMessage(player, 'OUT FOLD')

    def _kickOutOfGame(self, player, reason):
        self._interacts.sendMessage(player, "OUT " + reason)
        player.chips = 0
        player.dropCards()
Exemplo n.º 5
0
 def __init__(self, interacts):
     self._interacts = interacts
     self.evt_done = Event()
     self._interacts.evt_playerResponse += self._on_PlayerResponse
     self.lastToRaise = None
     self.table = Table()
Exemplo n.º 6
0
class TakesBets(object):
    def __init__(self, interacts):
        self._interacts = interacts
        self.evt_done = Event()
        self._interacts.evt_playerResponse += self._on_PlayerResponse
        self.lastToRaise = None
        self.table = Table()

    def start(self):
        self.table.seat(self._interacts.players)
        self.lastToRaise = self.table.dealingTo()
        self.next()

    def next(self):
        if self._notEnoughWithChips():
            self._finish()
        elif self._onePlayerLeft():
            self._finish()
        elif self.table.dealingTo().isPlaying():
            dealTo = self.table.dealingTo()
            self._interacts.sendMessage(dealTo, 'GO')
        else:
            self.table.nextPlayer()
            self.next()

    def _on_PlayerResponse(self, sender, response):
        player = response[0]
        amount = response[1]

        if player != self.table.dealingTo():
            self._kickOutOfGame(player, 'OUT_OF_TURN')
            return

        if not amount.isdigit():
            self._kickOutOfGame(player, 'NOT_A_NUMBER')
            return

        self._add(player, int(amount))

        self.table.nextPlayer()

        if self._done():
            self._finish()
        else:
            self.next()

    def getMinimumBet(self, player):
        highestContribution = max([p.pot for p in self.table.playing()])
        minBet = highestContribution - player.pot
        return minBet

    def _notEnoughWithChips(self):
        hasChips = len([p for p in self.table.playing() if p.chips > 0])
        return hasChips <= 1

    def _finish(self):
        self.evt_done(self)

    def _done(self):
        if self._notEnoughWithChips():
            return True

        return self.lastToRaise == self.table.dealingTo()

    def _onePlayerLeft(self):
        hasCards = len([p for p in self._interacts.players if p.isPlaying()])
        return hasCards == 1

    def _add(self, player, amount):
        if self._folding(player, amount):
            self._fold(player)
        elif amount > player.chips:
            self._kickOutOfGame(player, 'OVERDRAWN')
            amount = 0

        self._interacts.broadcast('BET ' + player.name + ' ' + str(amount))
        if amount > self.getMinimumBet(player):
            self.lastToRaise = player

        player.bet(amount)

    def _folding(self, player, amount):
        return amount < self.getMinimumBet(
            player) and player.chips - amount > 0

    def _fold(self, player):
        player.dropCards()
        self._interacts.sendMessage(player, 'OUT FOLD')

    def _kickOutOfGame(self, player, reason):
        self._interacts.sendMessage(player, "OUT " + reason)
        player.chips = 0
        player.dropCards()
Exemplo n.º 7
0
 def setUp(self):
     print 'Rotating around the table,', self.shortDescription()
     self.table = Table()
Exemplo n.º 8
0
class testRotatingAroundTheTable(unittest.TestCase):

    def setUp(self):
        print 'Rotating around the table,', self.shortDescription()
        self.table = Table()

    def testA_DealingToFirstAtTable(self):
        '''deal to the first person at the table'''
        self.table.seat(['p1', 'p2', 'p3'])

        self.assertEqual('p1', self.table.dealingTo())

    def testB_MovingToTheNext(self):
        '''move to the next'''
        self.table.seat(['p1', 'p2', 'p3'])
        self.table.nextPlayer()

        self.assertEqual('p2', self.table.dealingTo())

    def testC_ThenTheLast(self):
        '''all the way to the last'''
        self.table.seat(['p1', 'p2', 'p3'])

        self.table.nextPlayer()
        self.table.nextPlayer()

        self.assertEqual('p3', self.table.dealingTo())

    def testD_BackToFirst(self):
        '''back to first after last'''
        self.table.seat(['p1', 'p2', 'p3'])

        self.table.nextPlayer()
        self.table.nextPlayer()
        self.table.nextPlayer()

        self.assertEqual('p1', self.table.dealingTo())