Skip to content

Instantly share code, notes, and snippets.

@dhandeo
Created August 29, 2014 14:43
Show Gist options
  • Save dhandeo/1055813b76b158d53fa9 to your computer and use it in GitHub Desktop.
Save dhandeo/1055813b76b158d53fa9 to your computer and use it in GitHub Desktop.
How much score is expected for in 2048 for given highest tile on board, and probability of 4 popping up instead of 2
import sys
import random
# import argparse
def score_2048(highest_tile, probability_of_four = 0.2, verbose=False):
if verbose:
print "Input: ", highest_tile
if highest_tile == 4:
if random.random() <= probability_of_four:
if verbose:
print " Four"
return 0
else:
return 4
else:
# If we need highest tile, we must have summed its two constituents
result = highest_tile + score_2048(highest_tile / 2) + score_2048(highest_tile / 2)
if verbose:
print "result: ", result
return result
if __name__ == "__main__":
if len(sys.argv) != 4:
print "Usage: python score_2048.py <highest_tile_on_board> <probability_of_four> <verbose>"
sys.exit(0)
result = 0
# do 100 times and take average
for i in range(100):
result = result + score_2048(int(sys.argv[1]), float(sys.argv[2]), verbose=bool(int(sys.argv[3])))
print "Result: ", result / 100.0
print bool(int(sys.argv[3]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment