@@ -342,20 +342,18 @@ def getImporantTiles(self, importance): # importance => number of important tile
342
342
# player's move with alpha-beta pruning & importance pruning
343
343
def maxieMoveAlphaBetaImportance (self , depth , alpha , beta , importance ):
344
344
assert (alpha < beta )
345
- if not depth : return ( self .evaluate (), None ) # depth = 0
345
+ if not depth : return self .evaluate (), None # depth = 0
346
346
347
347
# get all legal actions and preserve the board
348
348
originalScore = self .GameBoard .score
349
349
actions = self .getLegalMoves ()
350
+ if not actions : return self .evaluate (), None # no legal actions, means player loses => computer wins
350
351
351
- if not actions : return (self .evaluate (), None ) # no legal actions
352
-
353
- (bestScore , bestAction ) = (- float ('inf' ), None )
354
-
352
+ bestScore , bestAction = float ('-inf' ), None
355
353
for action in actions :
356
354
beforeMoveBoard = copy .deepcopy (self .GameBoard .board )
357
355
self .performAction (action )
358
- ( computerScore , computerAction ) = self .minnieMoveAlphaBetaImportance (depth - 1 , alpha , beta , importance - 1 )
356
+ computerScore , computerAction = self .minnieMoveAlphaBetaImportance (depth - 1 , alpha , beta , importance - 1 )
359
357
self .GameBoard .board = beforeMoveBoard
360
358
self .GameBoard .score = originalScore
361
359
@@ -365,12 +363,12 @@ def maxieMoveAlphaBetaImportance(self, depth, alpha, beta, importance):
365
363
alpha = max (alpha , bestScore )
366
364
if (alpha >= beta ): break
367
365
368
- return ( bestScore , bestAction )
366
+ return bestScore , bestAction
369
367
370
368
# computer's move with alpha-beta pruning & importance pruning
371
369
def minnieMoveAlphaBetaImportance (self , depth , alpha , beta , importance ):
372
370
assert (alpha < beta )
373
- if not depth : return ( self .evaluate (), None ) # depth = 0
371
+ if not depth : return self .evaluate (), None # depth = 0
374
372
375
373
originalScore = self .GameBoard .score
376
374
# even though the real computer will put the new numbers randomly,
@@ -384,15 +382,14 @@ def minnieMoveAlphaBetaImportance(self, depth, alpha, beta, importance):
384
382
# can add 2 or 4 on any empty tile
385
383
actions .append ((index , 2 ))
386
384
actions .append ((index , 4 ))
385
+
386
+ if not actions : return bestScore , bestAction
387
387
388
- if not actions : return (self .evaluate (), None ) # no legal actions
389
-
390
- (bestScore , bestAction ) = (float ('inf' ), None )
391
-
388
+ bestScore , bestAction = self .evaluate (), None
392
389
for action in actions :
393
390
beforeMoveBoard = copy .deepcopy (self .GameBoard .board )
394
391
self .addNewNum (action ) # perform computer's action
395
- ( playerScore , playerAction ) = self .maxieMoveAlphaBetaImportance (depth , alpha , beta , importance )
392
+ playerScore , playerAction = self .maxieMoveAlphaBetaImportance (depth , alpha , beta , importance )
396
393
self .GameBoard .board = beforeMoveBoard
397
394
self .GameBoard .score = originalScore
398
395
@@ -402,10 +399,11 @@ def minnieMoveAlphaBetaImportance(self, depth, alpha, beta, importance):
402
399
beta = min (beta , bestScore )
403
400
if (alpha >= beta ) : break
404
401
405
- return ( bestScore , bestAction )
402
+ return bestScore , bestAction
406
403
407
404
def getMaxMove3 (self ):
408
405
score , action = self .maxieMoveAlphaBetaImportance (4 , - float ('inf' ), float ('inf' ), 4 )
406
+ print ("bestScore: {score}, bestAction: {action}" .format (score = score , action = self .GameBoard .directionList [action ]))
409
407
self .performAction (action )
410
408
411
409
def getMaxMove4 (self ):
@@ -426,10 +424,10 @@ def playTheGame(self):
426
424
print ("start board: " , end = "" )
427
425
self .GameBoard .printBoard ()
428
426
while not self .GameBoard .GameOver ():
429
- print ("-------------------------------board before move:" )
430
- self .GameBoard .printBoard ()
431
427
step += 1
432
428
print ("step:%d\n " % step )
429
+ print ("-------------------------------board before move:" )
430
+ self .GameBoard .printBoard ()
433
431
self .nextMove ()
434
432
print ("\n -----------------------------board after move:" )
435
433
self .GameBoard .printBoard ()
0 commit comments