Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # ATTEMPT 107, take the best card from highest score of enemy player
- # ONLY LOOKING AT 4 HANDS
- from part1 import score_group
- import random
- player_hands = [0]
- def group(my_cards):
- ''' Takes a list of cards and returns the best possible score of the group
- by using random permutations on the list.'''
- num_cards = len(my_cards)
- best_score = -99999999
- perm = my_cards.copy()
- # Go through 1000 random permutations of the given list and find the best
- # possible score by constantly updating the best score
- for k in range(1000):
- random.shuffle(perm)
- dp = [-999999999] * (num_cards)
- dp.append(0)
- # Go through length of the list until 0 in reverse
- # Check if it is better to take current permutation or not
- # After going through all possible grouping of current permutation,
- # increase the length of list being grouped
- # updates best score depending on the result of permutation
- for i in range(num_cards - 1, - 1, - 1):
- for j in range(i, num_cards):
- dp[i] = max(dp[i], score_group(perm[i:j + 1]) + dp[j + 1])
- best_score = max(best_score, dp[0])
- return best_score
- def card_grouping(my_cards):
- ''' Takes a list of cards and returns the best possible group of the list
- of cards by using random permutations on the list. '''
- num_cards = len(my_cards)
- best_group = [[card] for card in my_cards]
- best_score = -99999999
- perm = my_cards.copy()
- # Go through 8500 random permutations of the given list to find the best
- # possible grouping
- for k in range(8500):
- random.shuffle(perm)
- dp = [-999999999] * (num_cards)
- dp_ans = [0] * num_cards
- dp.append(0)
- # Go through length of the list until 0 in reverse
- # Check if it is better to take current permutation of the group or
- # keep the current group
- # After going through all possible grouping of current permutation,
- # increase the length of list being grouped
- # Return the best possible group
- for i in range(num_cards - 1, - 1, - 1):
- for j in range(i, num_cards):
- score = score_group(perm[i:j + 1]) + dp[j + 1]
- if score > dp[i]:
- dp[i] = score
- dp_ans[i] = perm[i:j + 1]
- if dp[0] > best_score:
- best_score = dp[0]
- best_group = []
- x = 0
- while x < num_cards:
- best_group.append(dp_ans[x])
- x += len(dp_ans[x])
- return best_group
- def comp10001go_play(discard_history, player_no, hand):
- ''' Takes a discard history containing a list of list of cards that are
- discarded by each player each turn, the player number and the current
- hand of the player to decide which card is best to discard from the
- current hand. '''
- enemy_cards = [[], [], [], []]
- my_cards = []
- # player_hands[0] is the turn number
- player_hands[0] += 1
- turn = player_hands[0]
- # Add all the player's hand into the global variable player_hands
- if len(player_hands) != 5:
- player_hands.append([hand])
- else:
- # Update the current hand every player is holding every turn by
- # removing cards that are in the discard history
- for discarded in discard_history:
- i=0
- for card in [player_no + 1, ((player_no + 1) % 4) + 1,
- ((player_no + 2) % 4) + 1, ((player_no + 3) % 4) + 1]:
- try:
- player_hands[i].remove(discarded[card])
- i += 1
- except:
- i += 1
- pass
- # Go through discarded cards in discard history and add cards of
- # players into a list
- for discarded in discard_history:
- for player in range(len(discarded)):
- if player == player_no:
- my_cards.append(discarded[player])
- else:
- enemy_cards[player].append(discarded[player])
- # For the first four cards, pick the highest available card in test
- if len(my_cards) <= 1:
- for test in "09KQJ87654A32":
- for card in hand:
- if card[0] == test:
- return card
- else:
- # Obtain the current score of discard pile of the player
- best_card = ''
- best_score = group(my_cards)
- # Go through cards in the hand
- # If we know all the possible card in each player's hand and the player
- # to the right does not have 1 card in the hand,
- # Find the best possible card by updating the best score each time
- # a card is chosen
- for card in hand:
- if (len(player_hands) == 5 and
- len(player_hands[(((turn + 2) % 4) + 1)]) != 1):
- for next_card in player_hands[((turn + 2) % 4) + 1]:
- for next2_card in player_hands[((turn + 1) % 4) + 1]:
- for next3_card in player_hands[(turn % 4) + 1]:
- score = group(my_cards + [card, next_card,
- next2_card, next3_card])
- if score > best_score:
- best_card = card
- best_score = score
- else:
- # Find the current score of the player's discard pile and the
- # given hand. Find the best possible card by updating the
- # best score each time a card is chosen
- score = group(my_cards + [card])
- if score > best_score:
- best_card = card
- best_score = score
- # If the best possible card for our player is not found,
- # Find the best possible card for the player with the highest score and
- # take that card instead
- if best_card == '' and len(player_hands) == 5:
- best_score = -99999999999
- for enemy_hand in range(len(enemy_cards)):
- if enemy_cards[enemy_hand] == []:
- continue
- enemy_score = group(enemy_cards[enemy_hand])
- if enemy_score > best_score:
- best_score = enemy_score
- enemy_player = enemy_hand
- for card in player_hands[(enemy_player +
- player_no + turn + 1) % 4 + 1]:
- if len(player_hands[(enemy_player +
- player_no + turn + 1) % 4 + 1]) != 1:
- for next_card in hand:
- score = group(enemy_cards[enemy_player] +
- [card, next_card])
- if score > best_score:
- best_card = next_card
- best_score = score
- # If best possible card is still not found, take the highest possible
- # card in test
- if best_card == '':
- for test in "09KQJ87654A32":
- for card in hand:
- if card[0] == test:
- return card
- return best_card
- def comp10001go_group(discard_history, player_no):
- ''' Group the given discard pile of the player to obtain the best possible
- score using random permutations. '''
- # Finding the discard pile of the player
- # and return the best possible grouping of the list of cards
- my_cards = []
- for discarded in discard_history:
- my_cards.append(discarded[player_no])
- return card_grouping(my_cards)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement