def test_two_straights(self): straightTo8 = '4S 6H 7S 8D 5H'.split() straightTo9 = '5S 7H 8S 9D 6H'.split() self.assertEqual([straightTo9], poker([straightTo8, straightTo9])) straightTo1 = 'AS QH KS TD JH'.split() straightTo5 = '4S AH 3S 2D 5H'.split() self.assertEqual([straightTo1], poker([straightTo1, straightTo5]))
def test_Poker(): "Test cases for the functions in poker program" sf = "6C 7C 8C 9C TC".split() # Straight Flush fk = "9D 9H 9S 9C 7D".split() # Four of a Kind fh = "TD TC TH 7C 7D".split() # Full House tp = "5S 5D 9H 9C 6S".split() # Two Pairs fkranks = poker.cardRanks(fk) tpranks = poker.cardRanks(tp) assert poker.allMax([1, 2, 3, 3, 3, 2]) == [3, 3, 3 ] assert poker.kind(4, fkranks) == 9 assert poker.kind(3, fkranks) == None assert poker.kind(2, fkranks) == None assert poker.kind(1, fkranks) == 7 assert poker.twoPair(fkranks) == None assert poker.twoPair(tpranks) == (9, 5) assert poker.cardRanks(sf) == [10, 9, 8, 7, 6] assert poker.cardRanks(fk) == [9, 9, 9, 9, 7 ] assert poker.cardRanks(fh) == [10, 10, 10, 7, 7] assert poker.straight([9, 8, 7, 6, 5]) == True assert poker.straight([9, 8, 7, 6, 4]) == False assert poker.flush(sf) == True assert poker.flush(fk) == False assert poker.poker( [ sf, fk, fh ] ) == [sf] assert poker.poker( [ fk, fh ] ) == [fk] assert poker.poker( [ fh, fh ] ) == [fh, fh] assert poker.poker( [ sf, fh ] ) == [sf] assert poker.poker( [ sf ] + 99 * [ fh ] ) == [sf] assert poker.handRank( sf ) == ( 8, 10 ) assert poker.handRank( fk ) == ( 7, 9, 7 ) assert poker.handRank( fh ) == ( 6, 10, 7 ) return "tests pass"
def calculateOdds(hand, ntable, numPlayers): "Given a hand and the community cards, returns the strenght of the hand" score = 0 table = [x for x in ntable] # Sort of deepcopy if len(table) < 3: return 0 for n in range(LOOPS): deck = [r+s for r in '23456789TJQKA' for s in 'SHDC'] # Remove the pockets cards of the deck deck.remove(hand[0]) deck.remove(hand[1]) # Remove the table cards of the deck for card in table: deck.remove(card) # Shuffle and fill the table with cards shuffle(deck) while(len(table)<5): table.append(deck.pop()) # Generate hands players = [generateHands(hand, table)[0]] aux = [] for n in range(numPlayers-1): aux.append(deck.pop()) aux.append(deck.pop()) players.append(generateHands(aux, table)[0]) aux = [] # Check if I have the winner hand if players.index(poker(players)[0]) == 0: score += 1 return score/LOOPS
def test_three_hand_with_tie(self): spadeStraightTo9 = '9S 8S 7S 6S 5S'.split() diamondStraightTo9 = '9D 8D 7D 6D 5D'.split() threeOf4 = '4D 4S 4H QS KS'.split() self.assertEqual( poker([spadeStraightTo9, diamondStraightTo9, threeOf4]), [spadeStraightTo9, diamondStraightTo9])
def test(): "Test cases for the functions in poker program" sf = "6C 7C 8C 9C TC".split() # Straight Flush fk = "9D 9H 9S 9C 7D".split() # Four of a Kind fh = "TD TC TH 7C 7D".split() # Full House tp = "5S 5D AC AS KS".split() # Two Pair s1 = "AC 2S 3C 4D 5D".split() # A-5 Straight s2 = "2S 3C 4D 5D 6S".split() # 2-6 Straight ah = "AC 2S 9C 4D 6D".split() # A High sh = "7C 2S 6C 3D 5D".split() # 7 High assert poker([s1, s2, ah, sh]) == [s2] assert poker([s1, ah, sh]) == [s1] fkranks = card_ranks(fk) tpranks = card_ranks(tp) assert kind(4, fkranks) == 9 assert kind(3, fkranks) is None assert kind(2, fkranks) is None assert kind(1, fkranks) == 7 assert two_pair(fkranks) is None assert two_pair(tpranks) == (14, 5) assert poker([sf, fk, fh]) == [sf] assert poker([fk, fh]) == [fk] assert poker([fh, fh]) == [fh, fh] assert poker([sf]) == [sf] assert poker([sf] + 99*[fh]) == [sf] assert hand_rank(sf) == (8, 10) assert hand_rank(fk) == (7, 9, 7) assert hand_rank(fh) == (6, 10, 7) return 'tests pass'
def test_pair_Qe_Ao_Qo_10o_9e(self): cartas = [ Carta('Q', 'espadas'), Carta('A', 'ouros'), Carta('Q', 'ouros'), Carta('10', 'ouros'), Carta('9', 'espadas'), ] self.assertEqual(poker(cartas), 'Pair')
def test_highcard_Ao_Ko_Qo_10o_9e(self): cartas = [ Carta('A', 'ouros'), Carta('K', 'ouros'), Carta('Q', 'ouros'), Carta('10', 'ouros'), Carta('9', 'espadas'), ] self.assertEqual(poker(cartas), 'High Card')
def test_foak_Qe_Qo_Qp_Qc_9e(self): cartas = [ Carta('Q', 'espadas'), Carta('Q', 'ouros'), Carta('Q', 'paus'), Carta('Q', 'copas'), Carta('9', 'espadas'), ] self.assertEqual(poker(cartas), 'Four of a Kind')
def test_par_de_k_ganha_de_k(self): mao1 = "13C 13P 3O 4E 5O" mao2 = "13O 2E 3P 4O 5E" self.assertEqual( "Mao 1 (%s) ganha da Mao 2 (%s)" % (mao1, mao2), poker(mao1, mao2) )
def test_trinca_de_8_ganha_de_par_de_as(self): mao1 = "8C 8P 8O 4E 5O" mao2 = "14O 14E 3P 4O 5E" self.assertEqual( "Mao 1 (%s) ganha da Mao 2 (%s)" % (mao1, mao2), poker(mao1, mao2) )
def test_trinca_de_2_ganha_de_par_de_8(self): mao1 = "2C 2P 2O 4E 5O" mao2 = "8O 8E 3P 4O 5E" self.assertEqual( "Mao 1 (%s) ganha da Mao 2 (%s)" % (mao1, mao2), poker(mao1, mao2) )
def test_toak_Qe_Qo_Qp_10o_9e(self): cartas = [ Carta('Q', 'espadas'), Carta('Q', 'ouros'), Carta('Q', 'paus'), Carta('10', 'ouros'), Carta('9', 'espadas'), ] self.assertEqual(poker(cartas), 'Three of a Kind')
def test_straight_6o_8o_7p_5p_9p(self): cartas = [ Carta('6', 'ouros'), Carta('8', 'paus'), Carta('7', 'paus'), Carta('5', 'ouros'), Carta('9', 'paus'), ] self.assertEqual(poker(cartas), 'Straight')
def test_poker_tester_4(self): '''testcase poker4 same rank sf = ['JC', 'TC', '9C', '8C', '7C'] sf2 = ['JS', 'TS', '9S', '8S', '7S'] ''' sf = ['JC','TC','9C','8C','7C'] sf2 = ['JS', 'TS','9S','8S','7S'] actual = poker.poker ([sf,sf2]) expected = [['JS', 'TS','9S','8S','7S']] self.assertEqual (actual,expected)
def test_high_card_tester_2(self): '''testcase high_card same kind but different number hc1 = ['2C', '5C', 'KC', '8D', '7S'] hc2 = ['5S', '2H', '7D', 'AC', '3S'] ''' hc1 = ['2C', '5C', 'KC', '8D', '7S'] hc2 = ['5S', '2H', '7D', 'AC', '3S'] actual = poker.poker([hc1,hc2]) expected = [['5S', '2H', '7D', 'AC', '3S']] self.assertEqual (actual,expected)
def test_poker_tester_1(self): '''testcase poker1 normal poker sf = ['JC', 'TC', '9C', '8C', '7C'] fk = ['5S', '5H', '5D', '5C', 'KS'] ''' sf = ['JC', 'TC', '9C', '8C', '7C'] fk = ['5S', '5H', '5D', '5C', 'KS'] actual = poker.poker([sf,fk]) expected = [['JC', 'TC', '9C', '8C', '7C']] self.assertEqual (actual,expected)
def test_k_ganha_de_q(self): mao1 = "12C 2P 3O 4E 5O" mao2 = "13O 2E 3P 4O 5E" self.assertEqual( "Mao 2 (%s) ganha da Mao 1 (%s)" % (mao2, mao1), poker(mao1, mao2) )
def test_high_card_Qo_Jo_Kp_9p_8p(self): cartas = [ Carta('Q', 'ouros'), Carta('J', 'ouros'), Carta('K', 'paus'), Carta('9', 'paus'), Carta('8', 'paus'), ] self.assertEqual(poker(cartas), 'High Card')
def test_flush_Qp_Jp_Kp_9p_8p(self): cartas = [ Carta('Q', 'paus'), Carta('J', 'paus'), Carta('K', 'paus'), Carta('9', 'paus'), Carta('8', 'paus'), ] self.assertEqual(poker(cartas), 'Flush')
def test_full_house_Qe_Qo_Qp_9c_9e(self): cartas = [ Carta('Q', 'espadas'), Carta('Q', 'ouros'), Carta('Q', 'paus'), Carta('9', 'copas'), Carta('9', 'espadas'), ] self.assertEqual(poker(cartas), 'Full House')
def test_poker_tester_3(self): '''testcase poker3 normal poker sf = ['2C','3C','4C','5C','6C'] fk = ['9S','9C','9D','9H','KS'] ''' sf = ['2C','3C','4C','5C','6C'] fk = ['9S','9C','9D','9H','KS'] actual = poker.poker([sf,fk]) expected = [['2C','3C','4C','5C','6C']] self.assertEqual (actual,expected)
def test_check_winner2(self): '''testcase check type card of winner in One pairs Pl1=['TH', '2H', '5H', 'QC', '2S'] Pl2=['3D', '9S', 'AH', '6C', '9C'] Winner is who has card : ['3D', '9S', 'AH', '6C', '9C'] "Win by One pairs" ''' Pl1=['TH', '2H', '5H', 'QC', '2S'] Pl2=['3D', '9S', 'AH', '6C', '9C'] actual = (poker.hand_rank(poker.poker([Pl1,Pl2])[0])[0]==3) expected = False self.assertEqual (actual,expected)
def test_check_winner1(self): '''testcase check type card of winner in Three of a kinds Pl1=['TH', '2H', '5H', 'QC', '2S'] Pl2=['3D', '9S', '9H', '6C', '9C'] Winner is who has card : ['3D', '9S', '9H', '6C', '9C'] "Win by Three of a kinds" ''' Pl1=['TH', '2H', '5H', 'QC', '2S'] Pl2=['3D', '9S', '9H', '6C', '9C'] actual = (poker.hand_rank(poker.poker([Pl1,Pl2])[0])[0]==3) expected = True self.assertEqual (actual,expected)
def test_operation(self): '''testcase operation for check the winner Pl1=['TH', '2H', '5H', 'QC', '2S'] Pl2=['3D', '9S', 'KH', '6C', '9C'] Winner is who has card : ['3D', '9S', 'KH', '6C', '9C'] The winner is Player : 2 ''' Pl1=['TH', '2H', '5H', 'QC', '2S'] Pl2=['3D', '9S', 'KH', '6C', '9C'] actual = poker.poker([Pl1,Pl2]) expected = [['3D', '9S', 'KH', '6C', '9C']] self.assertEqual (actual,expected)
def test_poker_3(self): """ Test poker function with one pair and two pair hands ['5S', '3H', '9D', '8C', '8S'] and ['5S', '5H', '9D', '8C', '8S'] """ op = ['5S', '3H', '9D', '8C', '8S'] tp = ['4S', '4H', '7D', '3C', '3S'] actual = poker.poker([op, tp]) expected = [['4S', '4H', '7D', '3C', '3S']] self.assertEqual(actual,expected)
def test_poker_2(self): """ Test poker function with straight flush and four of a kind hands ['JC', 'TC', '9C', '8C', '7C'] and ['5S', '5H', '5D', '5C', 'KS'] """ sf = ['JC', 'TC', '9C', '8C', '7C'] fk = ['5S', '5H', '5D', '5C', 'KS'] actual = poker.poker([sf, fk]) expected = [['JC', 'TC', '9C', '8C', '7C']] self.assertEqual(actual,expected)
def test_poker(self): """ Test poker function with straight flush and straight flush hands ['JC', 'TC', '9C', '8C', '7C'] and ['JS', 'TS', '9S', '8S', '7S'] """ sf = ['JC', 'TC', '9C', '8C', '7C'] sf2 = ['JS', 'TS', '9S', '8S', '7S'] actual = poker.poker([sf, sf2]) expected = [['JC', 'TC', '9C', '8C', '7C'], ['JS', 'TS', '9S', '8S', '7S']] self.assertEqual(actual,expected)
def sangriento(n, texas_holdem=True): if texas_holdem: hands = texas_holdem_deal(n) else: hands = deal(n) for hand in hands: print hand winner = poker(hands) ranking = hand_rank(winner[0])[0] print "Winner hand(s):" print hand_names[ranking] return winner
async def execute_poker(the_client, the_message): global participants, in_progress rolls = poker.poker(participants) announcement = '' for player in rolls: announcement += player + ': ' + str.join(' ',rolls[player]) + '\n'#sorted(rolls[player], key=lambda x: card_sort[x[0]])) + '\n' # the_client.send_message(the_message.channel, 'Join us in #poker to see the results.') await the_client.send_message(the_message.channel, announcement) in_progress = 0 participants = [] return rolls
def __init__(self, t_id, seats = SEATS): # Mark the table id, and the format of table id is like: # "table0", "table1", "table2", ... self.id = t_id # Mark the number of seats. self.seats = seats # Create a poker class. self.t_poker = poker() # Mark whether the table is in a game. self.is_playing = False # Mark the index of players who have bet. self.bet_idx = 0 # Store the current list players seated here. self.players_list = [] # Store the last hands of players who have played. self.players_last_hands = {} # Randomly assign the number of players. self.player_count = int(random.uniform(1, seats)) # Initialize the list of players seated here. for i in xrange(self.player_count): p_id = int(t_id.lstrip('table'))*seats + i p_id = 'player%i' % p_id new_player = player(p_id) self.players_list.append(new_player)
def test_square_vs_straight_flush(self): squareOf5 = '4S 5H 5S 5D 5H'.split() straightFlushTo9 = '5S 7S 8S 9S 6S'.split() self.assertEqual(poker([squareOf5, straightFlushTo9]), [straightFlushTo9])
def test_one_hand(self): hand = '4S 5S 7H 8D JC'.split() self.assertEqual(poker([hand]), [hand])
def test_two_double_pair_and_high(self): doublePair2and8 = '2S 8H 2C 8C 3H'.split() doublePair2and8high = '2D 2H 8S 8D AH'.split() self.assertEqual(poker([doublePair2and8high, doublePair2and8]), [doublePair2and8high])
def test_two_three(self): threeOf2 = '2S 2H 2C 8D JH'.split() threeOf1 = '4S AH AS 8C AD'.split() self.assertEqual(poker([threeOf2, threeOf1]), [threeOf1])
def test_three_vs_straight(self): threeOf4 = '4S 5H 4C 8D 4H'.split() straight = '3S 4D 2S 6D 5C'.split() self.assertEqual(poker([threeOf4, straight]), [straight])
def test_test_two_double_pair(self): doublePair2and8 = '2S 8H 2D 8D 3H'.split() doublePair4and5 = '4S 5H 4C 8S 5D'.split() self.assertEqual(poker([doublePair2and8, doublePair4and5]), [doublePair2and8])
def test_two_double_pair_tie_kicker(self): doublePair2and8high = 'JD QH JS 8D QC'.split() doublePair2and8 = 'JS QS JC 2D QD'.split() self.assertEqual(poker([doublePair2and8high, doublePair2and8]), [doublePair2and8high])
def test_highest_card_wins(self): first = '4D 5S 6S 8D 3C'.split() second = '2S 4C 7S 9H 10H'.split() third = '3S 4S 5D 6H JH'.split() self.assertEqual(poker([first, second, third]), [third])
def test_full_vs_square(self): full = '4S 5H 4S 5D 4H'.split() squareOf3 = '3S 3H 2S 3D 3H'.split() self.assertEqual(poker([full, squareOf3]), [squareOf3])
def test_flush_vs_full(self): flushTo8 = '3H 6H 7H 8H 5H'.split() full = '4S 5H 4C 5D 4H'.split() self.assertEqual(poker([full, flushTo8]), [full])
def test_two_fulls(self): fullOf4by9 = '4H 4S 4D 9S 9D'.split() fullOf5by8 = '5H 5S 5D 8S 8D'.split() self.assertEqual(poker([fullOf4by9, fullOf5by8]), [fullOf5by8])
def test_two_flushes(self): flushTo9 = '4H 7H 8H 9H 6H'.split() flushTo7 = '2S 4S 5S 6S 7S'.split() self.assertEqual(poker([flushTo9, flushTo7]), [flushTo9])
def test_straight_vs_flush(self): straightTo8 = '4C 6H 7D 8D 5H'.split() flushTo7 = '2S 4S 5S 6S 7S'.split() self.assertEqual(poker([straightTo8, flushTo7]), [flushTo7])
def test_two_square(self): squareOf2 = '2S 2H 2S 8D 2H'.split() squareOf5 = '4S 5H 5S 5D 5H'.split() self.assertEqual(poker([squareOf2, squareOf5]), [squareOf5])
def test_highest_pair_wins(self): pairOf2 = '4S 2H 6S 2D JH'.split() pairOf4 = '2S 4H 6C 4D JD'.split() self.assertEqual(poker([pairOf2, pairOf4]), [pairOf4])
def test_full_vs_four(self): full = '4S 5H 4D 5D 4H'.split() fourOf3 = '3S 3H 2S 3D 3C'.split() self.assertEqual(poker([full, fourOf3]), [fourOf3])
def test_two_pairs_beats_one_pair(self): pairOf8 = '2S 8H 6S 8D JH'.split() doublePair = '4S 5H 4C 8C 5C'.split() self.assertEqual(poker([pairOf8, doublePair]), [doublePair])
def test_two_fours(self): fourOf2 = '2S 2H 2C 8D 2D'.split() fourOf5 = '4S 5H 5S 5D 5C'.split() self.assertEqual(poker([fourOf2, fourOf5]), [fourOf5])
def test_two_double_pair_higher_tie(self): doublePair2andQ = '2S QS 2C QD JH'.split() doublePairJandQ = 'JD QH JS 8D QC'.split() self.assertEqual(poker([doublePair2andQ, doublePairJandQ]), [doublePairJandQ])
def test_two_fours_kicker(self): fourOf3low = '3S 3H 2S 3D 3C'.split() fourOf3high = '3S 3H 4S 3D 3C'.split() self.assertEqual(poker([fourOf3low, fourOf3high]), [fourOf3high])
def test_three_beats_two_pair(self): doublePair2and8 = '2S 8H 2H 8D JH'.split() threeOf4 = '4S 5H 4C 8S 4H'.split() self.assertEqual(poker([doublePair2and8, threeOf4]), [threeOf4])
def test_four_vs_straight_flush(self): fourOf5 = '4S 5H 5S 5D 5C'.split() straightFlushTo9 = '7S 8S 9S 6S 5S'.split() self.assertEqual(poker([fourOf5, straightFlushTo9]), [straightFlushTo9])
def test_two_three_multiple_decks(self): threeOf1Low = '4S AH AS 7C AD'.split() threeOf1High = '4S AH AS 8C AD'.split() self.assertEqual(poker([threeOf1Low, threeOf1High]), [threeOf1High])
def test_two_straight_flushes(self): straightFlushTo8 = '4H 6H 7H 8H 5H'.split() straightFlushTo9 = '5S 7S 8S 9S 6S'.split() self.assertEqual(poker([straightFlushTo8, straightFlushTo9]), [straightFlushTo9])
def test_two_flushes(self): flushTo8 = '3H 6H 7H 8H 5H'.split() flushTo7 = '2S 4S 5S 6S 7S'.split() self.assertEqual(poker([flushTo8, flushTo7]), [flushTo8])
def test_tie_compares_multiple(self): higher = '3S 5H 6S 8D 7H'.split() lower = '2S 5D 6D 8C 7S'.split() self.assertEqual(poker([higher, lower]), [higher])
def test_double_pair_vs_three(self): doublePair2and8 = '2S 8H 2S 8D JH'.split() threeOf4 = '4S 5H 4S 8D 4H'.split() self.assertEqual(poker([doublePair2and8, threeOf4]), [threeOf4])
def test_pair_beats_high_card(self): nothing = '4S 5H 6C 8D KH'.split() pairOf4 = '2S 4H 6S 4D JH'.split() self.assertEqual(poker([nothing, pairOf4]), [pairOf4])
def test_two_fulls_same_triplet(self): fullOf5by9 = '5H 5S 5D 9S 9D'.split() fullOf5by8 = '5H 5S 5D 8S 8D'.split() self.assertEqual(poker([fullOf5by9, fullOf5by8]), [fullOf5by9])
def poker(): n = int(request.args.get('n')) # 接收使用者帶進來的參數 outputDict = p.poker(n) return jsonify( outputDict) # jsonify顯示出json的形式, (寫API傳給前端的人的話必須是json的形式,而不是html的形式)