Last active
December 18, 2015 04:18
-
-
Save mmrobins/5724144 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# mod 10 | |
# cards: 0-9 | |
# each one card face up | |
# goal high score | |
# mod 10 | |
# dealer required to take card if < otherwise stands | |
# known card order | |
# | |
# moves = [] | |
# [ | |
# [ | |
# [[4], [5,7], -1], | |
# [[2,7], [3,1], ] | |
# ] | |
# ] | |
# Don't need to track moves, just high score so nevermind that | |
from random import randint | |
def debug(*msgs): | |
print ''.join(map(str,msgs)) | |
class Deck: | |
def __init__(self, deck): | |
self.deck = deck | |
self.high_score = None | |
self.find_best_score() | |
def find_best_score(self): | |
self.find_best_score_helper(0, 0) | |
return self.high_score | |
def find_best_score_helper(self, i, score): | |
# if there aren't enough cards for a possible full round (4 cards) we'll stop playing | |
if i > (len(deck) - 4): | |
debug("finished with score: ", score) | |
debug("") | |
if score > self.high_score: | |
self.high_score = score | |
return | |
# deal out first two cards | |
dealer_card = deck[i] | |
debug("dealer_card ", dealer_card) | |
i += 1 | |
my_card = deck[i] | |
debug("my_card ", my_card) | |
i += 1 | |
self.handle_my_final_card(i, my_card, dealer_card, score) | |
self.handle_second_card(i, score, my_card, dealer_card) | |
def handle_my_final_card(self, i, my_total, dealer_total, score): | |
# i chose to lose | |
if my_total < dealer_total: | |
debug("I lost") | |
self.find_best_score_helper(i, score - 1) | |
elif my_total == dealer_total: | |
debug("We tied") | |
self.find_best_score_helper(i, score) | |
else: | |
dealer_next_card = deck[i] | |
i += 1 | |
dealer_total = (dealer_total + dealer_next_card) % 10 | |
self.handle_dealer_final_card(i, my_total, dealer_total, score) | |
def handle_dealer_final_card(self, i, my_total, dealer_total, score): | |
if my_total > dealer_total: | |
debug("I won") | |
self.find_best_score_helper(i, score + 1) | |
elif my_total == dealer_total: | |
debug("We tied") | |
self.find_best_score_helper(i, score) | |
else: | |
debug("I lost") | |
self.find_best_score_helper(i, score - 1) | |
def handle_second_card(self, i, score, my_card, dealer_card): | |
my_next_card = deck[i] | |
i += 1 | |
debug("cards played so far: ", deck[:(i-1)], " cards to go: ", deck[(i-1):]) | |
my_total = (my_card + my_next_card) % 10 | |
debug("my_next_card: ", my_next_card, " my_total: ", my_total, ' dealer_total: ', dealer_card) | |
self.handle_my_final_card(i, my_total, dealer_card, score) | |
decks = [ | |
[ -1, [4, 1, 2, 6] ], | |
[ 1, [4, 1, 4, 6] ], | |
[ 0, [4, 1, 4, 3, 3, 6, 6, 3] ], # lose first win second | |
] | |
for expected, deck in decks: | |
print "***********************************" | |
print deck | |
score = Deck(deck).find_best_score() | |
result = "should be " + str(expected) + ": " + str(score) | |
if score != expected: | |
raise Exception(result) | |
print result | |
#random_deck = [] | |
#deck_size | |
#for i in range(deck_size): | |
# random_deck.append(randint(0,9)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment