Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 57 additions & 2 deletions pucauto.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,61 @@ def find_highest_value_bundle(trades):
return None


def optimal_bundle(highest_value_bundle):
"""The best bundle if you have more tradable cards than the other guy has
points is the group of cards that comes closest to reaching their
available points.

This does the old sort from complete_trades prior to this commit if there
is less value than the reciever has available points.

Otherwise, it finds the biggest bundle under their available poits, sorts
that by card value, then sorts the leftovers by card value just in case
one of the things you're trying to trade out gets jumped on by a third
party."""



cards = highest_value_bundle[1]["cards"]
sorted_cards = []



Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style: within a function, please use only single whitespace lines.

if highest_value_bundle[1]["value"] <= highest_value_bundle[1]["points"]:
sorted_cards = sorted(cards, key=lambda k: k["value"], reverse=True)
else:
largest = 0
sorted_cards = []
Copy link
Owner

@tomreece tomreece Jul 17, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorted_cards has already been initialized to [] above on line 336. Do we need this line?

for i in range (1, len(cards)):
can_change = False
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe rename can_change to continue_optimizing or something a bit more clear.

for j in itertools.combinations(cards, i):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe rename j to combination for readability.

bundle_value = 0
Copy link
Owner

@tomreece tomreece Jul 17, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe rename bundle_value to combination_value for clarity.

for k in j:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe rename k to card for readability.

bundle_value += k["value"]
if bundle_value <= highest_value_bundle[1]["points"]:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be just < instead of <=. If a combination (aka bundle_value) is equal to the number of points the user has, does the loop need to iterate again?

can_change = True
if bundle_value > largest:
largest = bundle_value
sorted_cards = list(j)

if can_change == 0:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rewrite as if not can_change or if you take my suggestion to rename this variable, if not continue_optimizing

break


sorted_cards.sort( key=lambda k: k["value"], reverse=True)

leftovers = []
for m in cards:
if m not in sorted_cards:
leftovers.append(m)

for n in leftovers:
sorted_cards.append(n)

return(sorted_cards)



def complete_trades(highest_value_bundle):
"""Sort the cards by highest value first and then send them all.

Expand All @@ -329,8 +384,8 @@ def complete_trades(highest_value_bundle):
return

cards = highest_value_bundle[1]["cards"]
# Sort the cards by highest value to make the most valuable trades first.
sorted_cards = sorted(cards, key=lambda k: k["value"], reverse=True)
# Here's the fancy new change. Trade as much stuff to the other guy as possible
sorted_cards = optimal_bundle(highest_value_bundle)

member_name = highest_value_bundle[1]["name"]
member_points = highest_value_bundle[1]["points"]
Expand Down