-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2048_a_start.py
More file actions
32 lines (24 loc) · 906 Bytes
/
2048_a_start.py
File metadata and controls
32 lines (24 loc) · 906 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
open_set = set()
def objectiveFunc(game):
return game.is_game_finished()
def adjFunc(game):
if game.is_game_finished():
return []
for move in "nsew":
new_game = game.play(move)
if new_game != game and not new_game in open_set:
open_set.add(new_game)
yield new_game
def heuristicFunc(game):
return max(0, 2048 - sum(sum(game.board, []))/2)
if __name__ == '__main__':
from search import AStart
from problems.game_2048 import Game
import time
game = Game(4, 2048)
search_algorithm = AStart(heuristicFunc=heuristicFunc)
start = time.time()
node = search_algorithm.find(game, objectiveFunc, adjFunc)
print('spent time:', time.time() - start)
print('length of path:', search_algorithm.map_parent(node, func = lambda x,y: x + 1, default= 0))
print('explored instaces:', search_algorithm.expanded)