Skip to content
Open
Show file tree
Hide file tree
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
63 changes: 61 additions & 2 deletions playout_strategy.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ def __init__(self,X=None):
super(Twenty48Strategy, self).__init__(X)

def estimate_score(self,numiter=50):
# raise NotImplementedError("You haven't implemented this yet")
vals = []
for i in range(numiter):
t = Twenty48(self.X)
Expand All @@ -30,6 +29,55 @@ def take_best_move(self,numiter=50):
self.move(best_action)
return best_action

class Twenty48StrategyWeighted(Twenty48Strategy):
""" Takes into account the score, highest number on the board,
and the number of empty slots left on the board, all weighted """

def __init__(self, X=None):
super(Twenty48Strategy, self).__init__(X)
self.sumWeight = 1
self.maxWeight = 1
self.emptyWeight = 1

def set_weights(self, sumw, maxw, emptyw):
self.sumWeight = sumw
self.maxWeight = maxw
self.emptyWeight = emptyw

def estimate_score(self,numiter=50):
vals = []
for i in range(numiter):
t = Twenty48(self.X)
t.run_until_dead()
vals.append(self.sumWeight * t.score() + self.maxWeight * t.highest()
+ self.emptyWeight * t.get_open_slots_num())
return sum(vals)/float(numiter)

class Twenty48StrategyMaximizeSum(Twenty48StrategyWeighted):
""" Maximizes the sum of numbers on the board
same strategy as initial Twenty48Strategy """
def __init__(self):
super(Twenty48StrategyWeighted, self).__init__()
self.sumWeight = 1
self.maxWeight = 0
self.emptyWeight = 0

class Twenty48StrategyMaximizeHighest(Twenty48StrategyWeighted):
""" Maximizes the highest number currently on the board """
def __init__(self):
super(Twenty48StrategyWeighted, self).__init__()
self.sumWeight = 0
self.maxWeight = 1
self.emptyWeight = 0

class Twenty48StrategyMaximizeEmpty(Twenty48StrategyWeighted):
""" Maximizes the number of empty slots left on the board """
def __init__(self):
super(Twenty48StrategyWeighted, self).__init__()
self.sumWeight = 0
self.maxWeight = 0
self.emptyWeight = 1

class TwoLayerStrategy(Twenty48Strategy):
"""docstring for TwoLayerStrategy"""
def __init__(self, X=None):
Expand All @@ -51,9 +99,20 @@ def estimate_moves(self,numiter=10):

if __name__ == '__main__':

t = Twenty48Strategy()
# t = Twenty48Strategy()
t = Twenty48StrategyWeighted()
t.set_weights(3.1, 2.4, 1.6)
# t = Twenty48StrategyMaximizeSum()
# t = Twenty48StrategyMaximizeHighest()
# t = Twenty48StrategyMaximizeEmpty()
## The three above all seem to be suboptimal strategies
# t = TwoLayerStrategy()
print t
count = 0
while not t.is_dead():
print "best action is %s" % t.take_best_move(numiter=50)
print t
if t.highest() >= 2048:
print "Game was won"
else:
print "Game was lost"
10 changes: 9 additions & 1 deletion twenty48.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ def set(self,i,j,val): self.X[4*i + j] = val
def get(self,i,j): return self.X[4*i + j]

def score(self): return sum(self.X)

def highest(self): return max(self.X)

def add_random_piece(self):
pair = self.pick_rand_slot()
Expand All @@ -74,9 +76,15 @@ def add_random_piece(self):
def get_open_slots(self):
return [(i,j) for i in range(4) for j in range(4) if self.get(i,j) == 0]

def get_open_slots_num(self):
return len([(i,j) for i in range(4) for j in range(4) if self.get(i,j) == 0])

def get_full_slots(self):
return [(i,j) for i in range(4) for j in range(4) if self.get(i,j) != 0]

def get_full_slots_num(self):
return len([(i,j) for i in range(4) for j in range(4) if self.get(i,j) != 0])

def pick_rand_slot(self):
open_slots = self.get_open_slots()
if len(open_slots) == 0: return False
Expand Down Expand Up @@ -115,4 +123,4 @@ def estimate_score(self,numiter=50):