Created
April 19, 2020 04:35
-
-
Save ExperimentMonty/878db558a54a0452d7a6d3378534e4bc 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
from random import shuffle | |
total_moves = 0 | |
push_moves = 0 | |
def play_round(): | |
global total_moves | |
global push_moves | |
deck = 4 * [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14] | |
shuffle(deck) | |
print(deck) | |
board = [] | |
last_slot = 100 | |
same_slot_count = 0 | |
locked_slot = False | |
for i in range(9): | |
board.append(deck.pop()) | |
print(board) | |
while len(deck) != 0: | |
# Try to place a card | |
probabilities = [(probability_higher(deck, slot), probability_same(deck, slot), probability_lower(deck, slot)) for slot in board] | |
best_probability = 0 | |
best_slot = 0 | |
best_move = '' | |
if locked_slot: | |
best_slot = last_slot | |
number = board[best_slot] | |
high, same, low = (probability_higher(deck, number), probability_same(deck, number), probability_lower(deck, number)) | |
if max(high, same, low) == high: | |
best_move = 'high' | |
elif max(high, same, low) == same: | |
best_move = 'push' | |
elif max(high,same, low) == low: | |
best_move = 'low' | |
else: | |
for idx, slot in enumerate(probabilities): | |
if slot != 'X': | |
if slot[0] > best_probability: | |
best_probability = slot[0] | |
best_slot = idx | |
best_move = 'high' | |
elif slot[1] > best_probability: | |
best_probability = slot[1] | |
best_slot = idx | |
best_move = 'push' | |
elif slot[2] > best_probability: | |
best_probability = slot[2] | |
best_slot = idx | |
best_move = 'low' | |
next_card = deck.pop() | |
print("Next card: {}, slot {}, slotted card: {}, action: {}".format(next_card, best_slot, board[best_slot], best_move)) | |
if last_slot == best_slot: | |
same_slot_count += 1 | |
if same_slot_count == 2: | |
# second card is same_slot_count = 1, 2 is when the third card is played and we're locked in | |
locked_slot = True | |
print("Locked in on slot {}".format(last_slot)) | |
else: | |
same_slot_count = 0 | |
last_slot = best_slot | |
total_moves += 1 | |
if best_move == 'high': | |
if next_card > board[best_slot]: | |
board[best_slot] = next_card | |
else: | |
locked_slot = False | |
board[best_slot] = 'X' | |
elif best_move == 'push': | |
push_moves += 1 | |
if next_card == board[best_slot]: | |
board[best_slot] = next_card | |
else: | |
locked_slot = False | |
board[best_slot] = 'X' | |
elif best_move == 'low': | |
if next_card < board[best_slot]: | |
board[best_slot] = next_card | |
else: | |
locked_slot = False | |
board[best_slot] = 'X' | |
print(board) | |
if all(x == 'X' for x in board): | |
print("failed attempt, {} cards left".format(len(deck))) | |
return False | |
return True | |
def probability_higher(deck, number): | |
if number == 'X': | |
return 0 | |
total = len(deck) | |
higher = len([x for x in deck if x > number]) | |
return higher / total | |
def probability_lower(deck, number): | |
if number == 'X': | |
return 0 | |
total = len(deck) | |
lower = len([x for x in deck if x < number]) | |
return lower / total | |
def probability_same(deck, number): | |
if number == 'X': | |
return 0 | |
total = len(deck) | |
same = len([x for x in deck if x == number]) | |
return same / total | |
success = 0 | |
failure = 0 | |
for i in range(10000): | |
result = play_round() | |
if result: | |
success += 1 | |
else: | |
failure += 1 | |
print("Successes: {}, failures: {}".format(success, failure)) | |
print("Total moves: {}, push moves: {}".format(total_moves, push_moves)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment