Skip to content

Instantly share code, notes, and snippets.

@MousaHalaseh
Last active June 11, 2018 13:04
Show Gist options
  • Save MousaHalaseh/7135157067a9002377db93071d85e267 to your computer and use it in GitHub Desktop.
Save MousaHalaseh/7135157067a9002377db93071d85e267 to your computer and use it in GitHub Desktop.
# Final plan to do this,
"""
# Define a 10*8 matrix (call it the game_play)
# Give each character a digit, say the following: // In order to perform some mathematical operations.
m = 5, w = 4, s = 3, b = 2, g = 1
#
# Start from a specific point in the grid, find all of it's associated paths.
# check that it's neither in the open_list nor the closed_set, if it does `continue` to next path.
# If a path is valid add it to the open_list,otherwise through it in the closed_set.
// A path is valid if it contains no single mine //
# If the path has gems, add it to the gem_list // Prioritizing stuff
# Whether you have executed the previous statement or nah, get the path off the open_list and add it to the closed_set.
# Take one of the valid paths from the open_list, redo all of the above (while the open_list is not empty).
"""
class Solver:
def __init__(self):
self.closed_list, self.open_list, self.moves, self.game_gems, self.sol_path, self.path = [], set(), 0, 0, [], []
self.game_play = [[-1 for _ in range(10)] for _ in range(8)]
seed = input('Game ID:\n')
index = 0
for r in range(8):
for c in range(10):
if seed[index] == 'm':
value = 5
elif seed[index] == 'w':
value = 4
elif seed[index] == 's':
value = 3
elif seed[index] == 'b':
value = 2
elif seed[index] == 'g':
self.game_gems += 1
value = 1
else:
value = 9
self.pac_man = r, c
self.game_play[r][c] = value
index += 1
self.solve(self.pac_man[0], self.pac_man[1])
self.solve_west(self.pac_man[0], self.pac_man[1] - 1)
self.statistics()
def solve(self, x, y):
self.game_gems -= 1 if self.game_play[x][y] == 1 else 0
self.moves += 1 if self.game_play[x][y] == 3 else 0
self.path.append((x, y))
if self.game_gems == 0:
return True
# assure that this point hadn't already been traversed
if not (x, y) in self.open_list:
# Go East
if 0 < y < 9:
self.open_list.add((x, y))
if self.game_play[x][y + 1] < 4:
if self.solve(x, y + 1):
self.sol_path.append(self.path)
return True
else:
if self.game_play[x][y + 1] == 4:
self.moves += 1
self.open_list.add((x, y + 1))
self.sol_path.append(self.path)
return True
else:
self.open_list.add((x, y + 1))
self.closed_list.append(self.path)
path = []
return False
def solve_west(self, x, y):
self.game_gems -= 1 if self.game_play[x][y] == 1 else 0
self.moves += 1 if self.game_play[x][y] == 3 else 0
self.path.append((x, y))
if x < y < 9:
self.open_list.add((x, y))
if self.game_play[x][y - 1] < 4:
if self.solve_west(x, y - 1):
self.sol_path.append(self.path)
return True
else:
if self.game_play[x][y - 1] == 4:
self.moves += 1
self.open_list.add((x, y - 1))
self.sol_path.append(self.path)
return True
else:
self.open_list.add((x, y - 1))
self.closed_list.append(self.path)
path = []
return False
def statistics(self):
for row in self.game_play: print(row)
print("gems = ", self.game_gems, "\nclosed_list:", self.closed_list,
"\nopen_set:", "\nsol_path:",
self.sol_path, "\npath:", self.path, "\nMoves:", self.moves)
# bwmswsbmmbwggmsgbbmwssgwgwsggwmmsbswwmmbbbwggSbmwgmbsmwgwssgsmggwsbbssbwgmwmgbms
solver = Solver()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment