Skip to content

Commit 332da46

Browse files
made some miscellaneous revisions
1 parent b61abc6 commit 332da46

File tree

5 files changed

+17
-20
lines changed

5 files changed

+17
-20
lines changed

.DS_Store

0 Bytes
Binary file not shown.

2048/AI.py

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -342,20 +342,18 @@ def getImporantTiles(self, importance): # importance => number of important tile
342342
# player's move with alpha-beta pruning & importance pruning
343343
def maxieMoveAlphaBetaImportance(self, depth, alpha, beta, importance):
344344
assert(alpha < beta)
345-
if not depth: return (self.evaluate(), None) # depth = 0
345+
if not depth: return self.evaluate(), None # depth = 0
346346

347347
# get all legal actions and preserve the board
348348
originalScore = self.GameBoard.score
349349
actions = self.getLegalMoves()
350+
if not actions: return self.evaluate(), None # no legal actions, means player loses => computer wins
350351

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
355353
for action in actions:
356354
beforeMoveBoard = copy.deepcopy(self.GameBoard.board)
357355
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)
359357
self.GameBoard.board = beforeMoveBoard
360358
self.GameBoard.score = originalScore
361359

@@ -365,12 +363,12 @@ def maxieMoveAlphaBetaImportance(self, depth, alpha, beta, importance):
365363
alpha = max(alpha, bestScore)
366364
if (alpha >= beta): break
367365

368-
return (bestScore, bestAction)
366+
return bestScore, bestAction
369367

370368
# computer's move with alpha-beta pruning & importance pruning
371369
def minnieMoveAlphaBetaImportance(self, depth, alpha, beta, importance):
372370
assert(alpha < beta)
373-
if not depth: return (self.evaluate(), None) # depth = 0
371+
if not depth: return self.evaluate(), None # depth = 0
374372

375373
originalScore = self.GameBoard.score
376374
# even though the real computer will put the new numbers randomly,
@@ -384,15 +382,14 @@ def minnieMoveAlphaBetaImportance(self, depth, alpha, beta, importance):
384382
# can add 2 or 4 on any empty tile
385383
actions.append((index, 2))
386384
actions.append((index, 4))
385+
386+
if not actions: return bestScore, bestAction
387387

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
392389
for action in actions:
393390
beforeMoveBoard = copy.deepcopy(self.GameBoard.board)
394391
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)
396393
self.GameBoard.board = beforeMoveBoard
397394
self.GameBoard.score = originalScore
398395

@@ -402,10 +399,11 @@ def minnieMoveAlphaBetaImportance(self, depth, alpha, beta, importance):
402399
beta = min(beta, bestScore)
403400
if (alpha >= beta) : break
404401

405-
return (bestScore, bestAction)
402+
return bestScore, bestAction
406403

407404
def getMaxMove3(self):
408405
score, action = self.maxieMoveAlphaBetaImportance(4, -float('inf'), float('inf'), 4)
406+
print("bestScore: {score}, bestAction: {action}".format(score=score, action=self.GameBoard.directionList[action]))
409407
self.performAction(action)
410408

411409
def getMaxMove4(self):
@@ -426,10 +424,10 @@ def playTheGame(self):
426424
print("start board: ", end = "")
427425
self.GameBoard.printBoard()
428426
while not self.GameBoard.GameOver():
429-
print("-------------------------------board before move:")
430-
self.GameBoard.printBoard()
431427
step += 1
432428
print("step:%d\n" % step)
429+
print("-------------------------------board before move:")
430+
self.GameBoard.printBoard()
433431
self.nextMove()
434432
print("\n-----------------------------board after move:")
435433
self.GameBoard.printBoard()

2048/__pycache__/AI.cpython-39.pyc

-2 KB
Binary file not shown.
23 Bytes
Binary file not shown.

2048/board.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,13 +118,12 @@ def getLargestTileNumber(self):
118118

119119
# the game is over once the player reach the number 2048 or
120120
# cannot make any legal move on the board
121-
def GameOver(self):
122-
# board = copy.deepcopy(self.board)
121+
def GameOver(self, verbose=True):
123122
originalScore = self.score
124123

125124
# the player get 2048
126125
if self.reaches2048():
127-
print("\nCongratulations! you get 2048 and win!\n")
126+
if verbose: print("\nCongratulations! you get 2048 and win!\n")
128127
# return True
129128

130129
# the player cannot make any legal move before getting 2048
@@ -150,7 +149,7 @@ def GameOver(self):
150149
self.score = originalScore
151150

152151
if not (canMoveUp or canMoveDown or canMoveLeft or canMoveRight):
153-
print("\nYou don't have any legal moves! Game Over!")
152+
if verbose: print("\nYou don't have any legal moves! Game Over!")
154153
return True
155154
else: return False
156155

0 commit comments

Comments
 (0)