-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsokoban.py
More file actions
136 lines (109 loc) · 4.33 KB
/
Copy pathsokoban.py
File metadata and controls
136 lines (109 loc) · 4.33 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import sys
import solver
from Level import Level
def movePlayer(direction,myLevel):
matrix = myLevel.getMatrix()
myLevel.addToHistory(matrix)
matrix.successor(direction, True)
if matrix.isSuccess():
global current_level
current_level += 1
initLevel(level_set,current_level)
def initLevel(level_set,level):
# Create an instance of this Level
global myLevel
myLevel = Level(level_set,level)
def runGame(args):
"""
Execute the game
"""
global current_level
current_level = args.level
global level_set
level_set = "project_levels"
# Initialize Level
if current_level==30:
pygame.quit()
sys.exit()
initLevel(level_set,current_level)
count=0
old_level = current_level - 1
while old_level is current_level - 1:
if current_level==args.last_level:
sys.exit()
old_level = current_level
moves = solve(args, myLevel)
if moves is not "":
for move in moves:
movePlayer(move, myLevel)
else:
print "Failed for level %d"%(current_level)
current_level = current_level + 1
if current_level==args.last_level:
# pygame.quit()
sys.exit()
initLevel(level_set,current_level)
# @profile
def solveInternal(cache, method, cost, heuristic):
solution = solver.solver()
solution.refresh()
moves = []
moves_cache=[]
if method == "dfs":
moves_cache = solution.dfs(myLevel.getMatrix(), cache=cache)
elif method == "bfs":
moves_cache = solution.bfs(myLevel.getMatrix(), cache=cache)
elif method == "ucs":
moves_cache = solution.ucs(myLevel.getMatrix(), cache=cache)
elif method == "back":
moves_cache = solution.back(myLevel.getMatrix(), cache=cache)
elif method == "astar":
moves_cache = solution.astar(myLevel.getMatrix(), cache=cache, cost=cost, heuristic=heuristic)
# elif method == "astarid":
# moves = solution.astarid(myLevel.getMatrix())
elif method == "dfsid":
moves_cache = solution.dfsid(myLevel.getMatrix())
# ret.put(moves_cache)
return moves_cache
def solve(args, myLevel):
solfile=open('sol.txt','w')
moves_cache = solveInternal(method=args.method, cache={}, cost=args.cost, heuristic=args.heuristic)
#solfile.write("Level: %d, Moves: %s Length: %d States Explored: %d" % (current_level, moves_cache[0], len(moves_cache[0]), moves_cache[1]))
solfile.write("%s" % (moves_cache[0]))
solfile.close()
return moves_cache[0]
def default(str):
return str + ' [Default: %default]'
def readCommand(argv):
"""
Processes the command used to run pacman from the command line.
"""
from optparse import OptionParser
usageStr = """
USAGE: python sokoban.py <options>
EXAMPLES: (1) python sokoban.py
(2) python sokoban.py --level 2 to start level 2
"""
parser = OptionParser(usageStr)
parser.add_option('-l', '--level', dest='level', type='int',
help=default('The level to run'), metavar='level', default=1)
parser.add_option('-m', '--method', dest='method', type='string',
help=default('The method set to solve'), metavar='method', default="ucs")
parser.add_option('-c', '--cost', dest='cost', type='string',
help=default('Cost function to use'), metavar='cost', default="default")
parser.add_option('-f', '--heuristic', dest='heuristic', type='string',
help=default('Heuristic function to use'), metavar='heuristic', default="hungarian")
parser.add_option('-x', '--last_level', dest='last_level', type='int',
help=default('The max level to compute to'), metavar='last_level', default=30)
options, otherjunk = parser.parse_args(argv)
if len(otherjunk) != 0:
raise Exception('Command line input not understood: ' + str(otherjunk))
return options
if __name__ == '__main__':
"""
The main function called when sokoban.py is run
from the command line:
> python sokoban.py
"""
args = readCommand(sys.argv[1:]) # Get game components based on input
runGame(args)