-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
206 lines (178 loc) · 6.23 KB
/
main.py
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
'''
File: main.py
Authors: Joeri Bes, Haischel Dabian, Jim Buissink, Sebastiaan Joustra and
Wietze Slagman
Date: 16/06/2016
Python version: 3.x
Created for the Bomberbot coorporation
---------------------------------------------
File description:
This file contains the main file in order to run the program.
'''
import parse_json
import parse_output
import objects
import hint_generation
import copy
import astar
import time
import sys
import brute_force
def main():
# Selecting level
filename = "Json/mission" + sys.argv[1] + "-level" + sys.argv[2] + ".json"
# Initializing variables
start_time = time.time()
tile_list = []
tiles = parse_json.get_tiles(filename)
dimension = parse_json.get_dimensions(filename)
has_hammer = parse_json.has_hammer(filename)
start_location = parse_json.get_pos_player(filename)
direction = parse_json.get_dir_player(filename)
best_solution = parse_json.get_best_solutions(filename)
# Searching for a demo for the level
demo = parse_json.get_demo(sys.argv[1],sys.argv[2])
if demo is not None:
moves = demo[0]
goals_collected = demo[1]
else:
print("No demo found")
moves = []
goals_collected = []
# Creating the tile grid
rows = tiles.split(';')
for y in range(len(rows)):
row = rows[y].split(',')
for x in range(len(row)):
tile_list.append(create_tile(row[x], x, y))
# Ceating the goal list
goal_list = create_goal_list(tile_list)
# Receiving the start location on the tiles
start_tile = get_tile(tile_list, start_location[0], start_location[1], \
dimension[1], dimension[0])
# Calling the brute force algorithm
b = brute_force.Brute_force(dimension[1], dimension[0], tile_list, \
goal_list, best_solution, start_tile, has_hammer, direction)
best_path, rotations, permutations = b.solve()
# Displaying the answer in commando's
output = parse_output.parse(best_path, start_tile, best_solution, \
rotations)
print("--- Level information ---")
print("Starting location: (" + str(start_location[0]) + ", " + \
str(start_location[1]) + ")")
print("Starting direction:", direction)
# Displaying the grid for the users
display_grid(start_location, dimension, tiles)
print("\n--- Path info ---")
print ('Path found:', len(best_path), 'moves. | ' 'Best solution:', \
best_solution,' moves. | Permutation used', permutations)
print("Best path: ", ', '.join(output))
print("User path: ",', '.join(moves))
# Creating hints for the user if necessary
print("\n--- Hint generated ---")
hint = hint_generation.Hint_generation(moves, output, goals_collected, \
goal_list, permutations)
hint.process()
print("\n--- Run time: ---")
print("%s seconds" % (time.time() - start_time))
def create_goal_list(tile_list):
'''This function loops through every tile and adds every star,
ruby or hammer it encounters to the goal list.
'''
goal_list = []
for tile in tile_list:
if tile.get_object_name() == "Star" or tile.get_object_name() == "Ruby" \
or tile.get_object_name() == "Hammer":
goal_list.append((tile, tile.obj))
return goal_list
def get_tile(tile_list, x, y, height, width):
'''When given coordinates, this function calculates which tile
matches from the tile list.
'''
return tile_list[y * width + x]
def create_tile(value, x, y):
'''This function creates the tiles by the given values from the json
file.
'''
slidable = False
abyss = False
tile = None
obj = None
if value[0] == "b":
slidable = True
if value[0] == ".":
obj = objects.Brick(False)
else:
if len(value) == 2:
if value[1] == "4":
obj = objects.Star()
elif value[1] == "3":
obj = objects.Ruby()
elif value[1] == "1":
obj = objects.Brick(True)
elif value[1] == "6":
obj = objects.Hammer()
else:
obj = objects.Brick(False)
tile = objects.Tile(slidable, obj, x, y)
return tile
def display_grid(start_location, dimension, tiles):
grid = prep_grid(start_location, dimension, tiles)
printgrid(grid)
def prep_grid(start_location, dimension, tiles):
'''This function displays the grid for the user.'''
grid = [([None]*dimension[0]) for i in range(dimension[1])]
rows = tiles.split(';')
for y in range(len(rows)):
row = rows[y].split(',')
for x in range(len(row)):
grid[y][x] = create_tile(row[x], x, y)
for i in range(len(grid)):
for j in range(len(grid[i])):
tile = grid[i][j]
if tile.has_object():
if tile.get_object_name() == "Brick":
if tile.obj.destroyable == True:
grid[i][j] = "D"
else:
grid[i][j] = "X"
elif tile.get_object_name() == "Star":
grid[i][j] = "S"
elif tile.get_object_name() == "Hammer":
grid[i][j] = "H"
elif tile.get_object_name() == "Ruby":
grid[i][j] = "R"
else:
grid[i][j] = "?"
else:
grid[i][j] = " "
grid[start_location[1]][start_location[0]] = "B"
return grid
def printgrid(grid):
'''This function prints the grid to terminal'''
first=True
for i in range(len(grid)):
middle = ""
bottom = " "
firstline = " "
top = " "
x_axis = " "
count_x_axis = 0
for j in grid[i]:
top += "| "
middle = middle + " | " + j
bottom += "|_____"
firstline += "______"
x_axis += " "+str(count_x_axis)+" "
count_x_axis += 1
if first:
print(" "+firstline+"_")
first = False
else:
print(" "+bottom+"|")
print(" "+ top +"|")
print(str(i) + middle + " |")
print(" " + bottom + "|")
print (" "+x_axis)
if __name__ == "__main__":
main()