Advertisement
Guest User

Untitled

a guest
May 22nd, 2019
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.87 KB | None | 0 0
  1. # ATTEMPT 107, take the best card from highest score of enemy player
  2. # ONLY LOOKING AT 4 HANDS
  3.  
  4. from part1 import score_group
  5. import random
  6. player_hands = [0]
  7.  
  8. def group(my_cards):
  9. ''' Takes a list of cards and returns the best possible score of the group
  10. by using random permutations on the list.'''
  11. num_cards = len(my_cards)
  12. best_score = -99999999
  13. perm = my_cards.copy()
  14.  
  15. # Go through 1000 random permutations of the given list and find the best
  16. # possible score by constantly updating the best score
  17. for k in range(1000):
  18. random.shuffle(perm)
  19. dp = [-999999999] * (num_cards)
  20. dp.append(0)
  21.  
  22. # Go through length of the list until 0 in reverse
  23. # Check if it is better to take current permutation or not
  24. # After going through all possible grouping of current permutation,
  25. # increase the length of list being grouped
  26. # updates best score depending on the result of permutation
  27. for i in range(num_cards - 1, - 1, - 1):
  28. for j in range(i, num_cards):
  29. dp[i] = max(dp[i], score_group(perm[i:j + 1]) + dp[j + 1])
  30. best_score = max(best_score, dp[0])
  31.  
  32. return best_score
  33.  
  34. def card_grouping(my_cards):
  35. ''' Takes a list of cards and returns the best possible group of the list
  36. of cards by using random permutations on the list. '''
  37. num_cards = len(my_cards)
  38. best_group = [[card] for card in my_cards]
  39. best_score = -99999999
  40. perm = my_cards.copy()
  41.  
  42. # Go through 8500 random permutations of the given list to find the best
  43. # possible grouping
  44. for k in range(8500):
  45. random.shuffle(perm)
  46. dp = [-999999999] * (num_cards)
  47. dp_ans = [0] * num_cards
  48. dp.append(0)
  49.  
  50. # Go through length of the list until 0 in reverse
  51. # Check if it is better to take current permutation of the group or
  52. # keep the current group
  53. # After going through all possible grouping of current permutation,
  54. # increase the length of list being grouped
  55. # Return the best possible group
  56. for i in range(num_cards - 1, - 1, - 1):
  57. for j in range(i, num_cards):
  58. score = score_group(perm[i:j + 1]) + dp[j + 1]
  59. if score > dp[i]:
  60. dp[i] = score
  61. dp_ans[i] = perm[i:j + 1]
  62. if dp[0] > best_score:
  63. best_score = dp[0]
  64. best_group = []
  65. x = 0
  66. while x < num_cards:
  67. best_group.append(dp_ans[x])
  68. x += len(dp_ans[x])
  69.  
  70. return best_group
  71.  
  72. def comp10001go_play(discard_history, player_no, hand):
  73. ''' Takes a discard history containing a list of list of cards that are
  74. discarded by each player each turn, the player number and the current
  75. hand of the player to decide which card is best to discard from the
  76. current hand. '''
  77. enemy_cards = [[], [], [], []]
  78.  
  79. my_cards = []
  80.  
  81. # player_hands[0] is the turn number
  82. player_hands[0] += 1
  83. turn = player_hands[0]
  84. # Add all the player's hand into the global variable player_hands
  85. if len(player_hands) != 5:
  86. player_hands.append([hand])
  87.  
  88. else:
  89. # Update the current hand every player is holding every turn by
  90. # removing cards that are in the discard history
  91. for discarded in discard_history:
  92. i=0
  93. for card in [player_no + 1, ((player_no + 1) % 4) + 1,
  94. ((player_no + 2) % 4) + 1, ((player_no + 3) % 4) + 1]:
  95. try:
  96. player_hands[i].remove(discarded[card])
  97. i += 1
  98. except:
  99. i += 1
  100. pass
  101.  
  102. # Go through discarded cards in discard history and add cards of
  103. # players into a list
  104. for discarded in discard_history:
  105. for player in range(len(discarded)):
  106. if player == player_no:
  107. my_cards.append(discarded[player])
  108. else:
  109. enemy_cards[player].append(discarded[player])
  110.  
  111. # For the first four cards, pick the highest available card in test
  112. if len(my_cards) <= 1:
  113. for test in "09KQJ87654A32":
  114. for card in hand:
  115. if card[0] == test:
  116. return card
  117. else:
  118. # Obtain the current score of discard pile of the player
  119. best_card = ''
  120. best_score = group(my_cards)
  121.  
  122.  
  123. # Go through cards in the hand
  124. # If we know all the possible card in each player's hand and the player
  125. # to the right does not have 1 card in the hand,
  126. # Find the best possible card by updating the best score each time
  127. # a card is chosen
  128. for card in hand:
  129. if (len(player_hands) == 5 and
  130. len(player_hands[(((turn + 2) % 4) + 1)]) != 1):
  131. for next_card in player_hands[((turn + 2) % 4) + 1]:
  132. for next2_card in player_hands[((turn + 1) % 4) + 1]:
  133. for next3_card in player_hands[(turn % 4) + 1]:
  134. score = group(my_cards + [card, next_card,
  135. next2_card, next3_card])
  136. if score > best_score:
  137. best_card = card
  138. best_score = score
  139.  
  140. else:
  141. # Find the current score of the player's discard pile and the
  142. # given hand. Find the best possible card by updating the
  143. # best score each time a card is chosen
  144. score = group(my_cards + [card])
  145. if score > best_score:
  146. best_card = card
  147. best_score = score
  148.  
  149. # If the best possible card for our player is not found,
  150. # Find the best possible card for the player with the highest score and
  151. # take that card instead
  152. if best_card == '' and len(player_hands) == 5:
  153. best_score = -99999999999
  154. for enemy_hand in range(len(enemy_cards)):
  155. if enemy_cards[enemy_hand] == []:
  156. continue
  157. enemy_score = group(enemy_cards[enemy_hand])
  158. if enemy_score > best_score:
  159. best_score = enemy_score
  160. enemy_player = enemy_hand
  161.  
  162. for card in player_hands[(enemy_player +
  163. player_no + turn + 1) % 4 + 1]:
  164. if len(player_hands[(enemy_player +
  165. player_no + turn + 1) % 4 + 1]) != 1:
  166. for next_card in hand:
  167. score = group(enemy_cards[enemy_player] +
  168. [card, next_card])
  169. if score > best_score:
  170. best_card = next_card
  171. best_score = score
  172.  
  173. # If best possible card is still not found, take the highest possible
  174. # card in test
  175. if best_card == '':
  176. for test in "09KQJ87654A32":
  177. for card in hand:
  178. if card[0] == test:
  179. return card
  180.  
  181. return best_card
  182.  
  183. def comp10001go_group(discard_history, player_no):
  184. ''' Group the given discard pile of the player to obtain the best possible
  185. score using random permutations. '''
  186.  
  187. # Finding the discard pile of the player
  188. # and return the best possible grouping of the list of cards
  189. my_cards = []
  190. for discarded in discard_history:
  191. my_cards.append(discarded[player_no])
  192. return card_grouping(my_cards)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement