SHARE
TWEET

Untitled

a guest May 19th, 2019 151 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. player_hands = []
  2.  
  3. def run(sorted_new_card):
  4.     length = 1
  5.     for k in range(len(sorted_new_card)-1):
  6.         if (sorted_new_card[k][0] + 1 == sorted_new_card[k+1][0]
  7.         and sorted_new_card[k][1] != sorted_new_card[k+1][1]):
  8.             length += 1
  9.         else:
  10.             break
  11.     if length >= 3:
  12.        return True
  13.     return False
  14.  
  15. def factorial(num):
  16.     if num == 1:
  17.         return num
  18.     else:
  19.         return num * factorial(num-1)
  20.    
  21. def test(cards):
  22.     d_cards = {'2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8 , '9': 9,
  23.                '0': 10, 'J': 11, 'Q': 12, 'K': 13, 'A': 20}
  24.     ace_suit = []
  25.     no_of_ace = 0        
  26.     score = 0
  27.     new_card = []
  28.     for i in cards:
  29.         if i[0] == 'A':
  30.             no_of_ace += 1
  31.             if i[1] == 'D' or i[1] == 'H':
  32.                 ace_suit.append('r')
  33.             else:
  34.                 ace_suit.append('b')
  35.         elif i[1] == 'D' or i[1] == 'H':
  36.             new_card.append((d_cards[i[0]],'r'))
  37.         else:
  38.             new_card.append((d_cards[i[0]],'b'))
  39.     sorted_new_card = sorted(new_card)
  40.     for i in sorted_new_card:
  41.         score += i[0]
  42.     if no_of_ace == 0:
  43.         if run(sorted_new_card) is True:
  44.             return score
  45.         else:
  46.             return -score        
  47.     else:
  48.         if len(sorted_new_card) < 2:
  49.             return - score - (20 * no_of_ace)
  50.         min_value = sorted_new_card[0][0]
  51.         max_value = sorted_new_card[-1][0]
  52.         max_score = -999999999999999999999999999999999999999999999999
  53.         if (max_value - min_value) < 2:
  54.             return - score - (20*no_of_ace)
  55.         if no_of_ace == 1:
  56.             for i in range(min_value+1,max_value):
  57.                 sorted_new_card = sorted(new_card + [(i, ace_suit[0])])
  58.                 if run(sorted_new_card) is True:
  59.                     score1 = score + i
  60.                 else:
  61.                     score1 = -score - 20
  62.                 max_score = max(max_score,score1)
  63.         elif no_of_ace == 2:
  64.             for i in range(min_value+1,max_value):
  65.                 for j in range(min_value+1,max_value):
  66.                     sorted_new_card = sorted(new_card + [(i, ace_suit[0])]+
  67.                                          [(j, ace_suit[1])])
  68.                     if run(sorted_new_card) is True:
  69.                         score2 = score + i + j
  70.                     else:
  71.                         score2 = -score - 20 - 20
  72.                     max_score = max(max_score,score2)
  73.         elif no_of_ace == 3:
  74.             for i in range(min_value+1,max_value):
  75.                 for j in range(min_value+1,max_value):
  76.                     for k in range(min_value+1,max_value):
  77.                         sorted_new_card = sorted(new_card + [(i, ace_suit[0])]+
  78.                                          [(j, ace_suit[1])] + [(k,ace_suit[2])])
  79.                         if run(sorted_new_card) is True:
  80.                             score3 = score + i + j + k
  81.                         else:
  82.                             score3 = -score - 20 - 20 - 20
  83.                         max_score = max(max_score,score3)
  84.         elif no_of_ace == 4:
  85.             for i in range(min_value+1,max_value):
  86.                 for j in range(min_value+1,max_value):
  87.                     for k in range(min_value+1,max_value):
  88.                         for z in range(min_value+1,max_value):
  89.                             sorted_new_card = sorted(new_card
  90.                                                      + [(i, ace_suit[0])]
  91.                                          +[(j, ace_suit[1])] + [(k,ace_suit[2])]
  92.                                          + [(z, ace_suit[3])])
  93.                             if run(sorted_new_card) is True:
  94.                                 score4 = score + i + j + k + z
  95.                             else:
  96.                                 score4 = -score - 20 - 20 - 20 - 20
  97.                             max_score = max(max_score,score4)
  98.         return max_score
  99.  
  100. def score_group(cards):
  101.     d_cards = {'2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8 , '9': 9,
  102.                '0': 10, 'J': 11, 'Q': 12, 'K': 13, 'A': 20}
  103.     card = cards[0][0]
  104.     score1 = test(cards)
  105.     n_of_a_kind = False
  106.     number = 0
  107.     for value in cards:
  108.         if value[0] == card:
  109.             number += 1
  110.     if number == len(cards) and len(cards) >= 2 and card != 'A':
  111.         n_of_a_kind = True
  112.         score2 = d_cards[card] * factorial(number)
  113.        
  114.     if n_of_a_kind is False:
  115.         return score1
  116.     else:
  117.         return max(score1, score2)
  118.  
  119. # question 2 code
  120. def is_valid(groups):
  121.     if groups == []:
  122.         return True
  123.     if len(groups) > 10:
  124.         return False
  125.    
  126.     for i in groups:
  127.         if len(i) == 1:
  128.             continue
  129.            
  130.         number = 0
  131.         for value in i:
  132.             if value[0] == i[0][0]:
  133.                 number += 1
  134.         if number == len(i) and len(i) >= 2 and i[0][0] != 'A':
  135.             continue
  136.        
  137.         if test(i) is True:
  138.             continue
  139.         return False
  140.     return True
  141.  
  142. def comp10001go_play(discard_history, player_no, hand):
  143.     ''' Takes a list of lists containing discard pile, the player number and
  144.     the current hand to choose a card to add to the discard pile.'''
  145.     d_cards = {'2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8 , '9': 9,
  146.                '0': 10, 'J': 11, 'Q': 12, 'K': 13, 'A': 20}
  147.    
  148.     possible_cards = {'2': 0, '3': 0, '4': 0, '5': 0, '6': 0, '7': 0, '8': 0 ,
  149.                   '9': 0, '0': 0, 'J': 0, 'Q': 0, 'K': 0, 'A': 0}
  150.    
  151.     taken_cards = {'2': 0, '3': 0, '4': 0, '5': 0, '6': 0, '7': 0, '8': 0 ,
  152.                   '9': 0, '0': 0, 'J': 0, 'Q': 0, 'K': 0, 'A': 0}
  153.    
  154.     my_cards = {'2': 0, '3': 0, '4': 0, '5': 0, '6': 0, '7': 0, '8': 0 ,
  155.                   '9': 0, '0': 0, 'J': 0, 'Q': 0, 'K': 0, 'A': 0}
  156.    
  157.     my_cards_suit ={'2': {'r': 0, 'b': 0},
  158.                     '3': {'r': 0, 'b': 0},
  159.                    '4': {'r': 0, 'b': 0},
  160.                    '5': {'r': 0, 'b': 0},
  161.                    '6': {'r': 0, 'b': 0},
  162.                    '7': {'r': 0, 'b': 0},
  163.                    '8': {'r': 0, 'b': 0},
  164.                   '9': {'r': 0, 'b': 0},
  165.                   '0': {'r': 0, 'b': 0},
  166.                   'J': {'r': 0, 'b': 0},
  167.                   'Q': {'r': 0, 'b': 0},
  168.                   'K': {'r': 0, 'b': 0},
  169.                   'A': {'r': 0, 'b': 0}}
  170.        
  171.     if len(player_hands) != 4:
  172.         player_hands.append([hand])
  173.     else:
  174.         for discarded in discard_history:
  175.             i=0
  176.             for card in [player_no,(player_no+1)%4,(player_no+2)%4,(player_no+3)%4]:
  177.                 try:
  178.                     player_hands[i].remove(discarded[card])
  179.                     i += 1
  180.                 except:
  181.                     i += 1
  182.                     pass
  183.    
  184.     new_hand = []
  185.     for card in hand:
  186.         if card[1] in 'DH':
  187.             new_hand.append((card[0], 'r'))
  188.         else:
  189.             new_hand.append((card[0], 'b'))
  190.            
  191.     for card in hand:
  192.         possible_cards[card[0]] += 1
  193.        
  194.     for discarded in discard_history:
  195.         for card in range(len(discarded)):
  196.             if card == player_no:
  197.                 my_cards[discarded[card][0]] += 1
  198.                 if discarded[card][1] in 'DH':
  199.                     my_cards_suit[discarded[card][0]]['r'] += 1
  200.                 else:
  201.                     my_cards_suit[discarded[card][0]]['b'] += 1
  202.             else:
  203.                 taken_cards[discarded[card][0]] += 1
  204.    
  205.     for card in hand:
  206.         if d_cards[card[0]] in [10,9,8,7]:
  207.             if possible_cards[card[0]] == 1 or possible_cards[card[0]] == 4:
  208.                 if taken_cards[card[0]] == 0:
  209.                     return card
  210.                
  211.     for test in [10,9,8]:
  212.         for card in hand:
  213.             if d_cards[card[0]] == test:
  214.                 if possible_cards[card[0]] == 1 or possible_cards[card[0]] == 4:
  215.                     if taken_cards[card[0]] == 0:
  216.                         return card
  217.    
  218.     for test in ['J','Q','K']:
  219.         for card in hand:
  220.             if card[0] == test:
  221.                 return card
  222.            
  223.     my_card = [d_cards[card] for card in my_cards.keys() if my_cards[card]==1]
  224.     min_value = min(my_card)
  225.     max_value = max(my_card)
  226.     difference = max_value - min_value
  227.    
  228.     for discarded in discard_history:
  229.         if d_cards[discarded[player_no][0]] == min_value:
  230.             min_value = (min_value, discarded[player_no][1])
  231.         if d_cards[discarded[player_no][0]] == max_value:
  232.             max_value = (max_value, discarded[player_no][1])
  233.    
  234.     if min_value[1] in 'DH':
  235.         min_suit = 'r'
  236.     else:
  237.         min_suit = 'b'
  238.     if max_value[1] in 'DH':
  239.         max_suit = 'r'
  240.     else:
  241.         max_suit = 'b'
  242.    
  243.     for card in hand:
  244.         test_suit = card[1]
  245.         if test_suit in 'DH':
  246.             test_suit = 'r'
  247.         else:
  248.             test_suit = 'b'
  249.         if (card[0] in range(min_value[0]+1,max_value[0]) and test_suit != min_suit
  250.         and test_suit != max_suit):
  251.             return card
  252.  
  253.     for test in [7,6,5,4,3,2]:
  254.         for card in hand:
  255.             if d_cards[card[0]] == test:
  256.                 if possible_cards[card[0]] == 1 or possible_cards[card[0]] == 4:
  257.                     if taken_cards[card[0]] == 0:
  258.                         return card
  259.    
  260.     return hand[0]
  261.  
  262. def comp10001go_group(discard_history, player_no):
  263.     ''' '''
  264.     possible_cards = {'2': 0, '3': 0, '4': 0, '5': 0, '6': 0, '7': 0, '8': 0 ,
  265.                   '9': 0, '0': 0, 'J': 0, 'Q': 0, 'K': 0, 'A': 0}
  266.     d_cards = {'2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8 , '9': 9,
  267.                '0': 10, 'J': 11, 'Q': 12, 'K': 13, 'A': 20}
  268.     my_cards = []
  269.     n_of_a_kind = []
  270.     run = []
  271.     best_group = []
  272.    
  273.     for discarded in discard_history:
  274.         my_cards.append(discarded[player_no])
  275.    
  276.     my_cards_c1 = my_cards.copy()
  277.    
  278.     for card in my_cards:
  279.         possible_cards[card[0]] += 1
  280.    
  281.     for (k, v) in possible_cards.items():
  282.         if v > 1:
  283.             for card in my_cards_c1:
  284.                 n_of_a_kind.append(card)
  285.                 my_cards_c1.remove(card)
  286.     if(n_of_a_kind!=[]):
  287.         best_group.append(n_of_a_kind)
  288.        
  289.     actual_run=[]
  290.     new_cards_c1 = []
  291.    
  292.     for i in my_cards_c1:
  293.         if i[1] == 'D' or i[1] == 'H':
  294.             new_cards_c1.append((d_cards[i[0]],'r'))
  295.         else:
  296.             new_cards_c1.append((d_cards[i[0]],'b'))
  297.            
  298.     new_cards_c1 = sorted(new_cards_c1)
  299.     lost_points = 0
  300.     for j in range(len(new_cards_c1)):
  301.         still_can_add = True
  302.         length = 1
  303.         score = new_cards_c1[j][0] - lost_points
  304.         run = []
  305.         run.append(new_cards_c1[j])
  306.         lost_points += new_cards_c1[j][0]
  307.         for k in range(j,len(my_cards_c1)-1):
  308.             if (new_cards_c1[k][0] + 1 == new_cards_c1[k+1][0]
  309.             and new_cards_c1[k][1] != new_cards_c1[k+1][1]
  310.             and still_can_add is True):
  311.                 length += 1
  312.                 score += new_cards_c1[k+1][0]
  313.                 run.append(new_cards_c1[k+1])
  314.             else:
  315.                 still_can_add = False
  316.                 score -= new_cards_c1[k+1][0]
  317.  
  318.         if length >= 3:
  319.            actual_run = run.copy()
  320.            leftover_score = score
  321.            
  322.     leftover_score = -lost_points
  323.    
  324.     if(actual_run!=[]):
  325.         final_run=[]
  326.         for i in actual_run:
  327.             for j in my_cards:
  328.                 if(i[0]==j[0]):
  329.                     if i[1] == 'r' and j[1] in 'DH':
  330.                         final_run.append(j)
  331.                     elif i[1] == 'b' and j[1] in 'SC':
  332.                         final_run.append(j)
  333.         best_group.append(final_run)
  334.    
  335.     for card in my_cards_c1:
  336.         if(n_of_a_kind!=[]):
  337.             if card in n_of_a_kind:
  338.                 continue
  339.         if actual_run != []:
  340.             if card in actual_run:
  341.                 continue
  342.         best_group.append([card])
  343.        
  344.     return best_group
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top