Skip to content
Open

Maze #31

Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .atom-build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
cmd: python3 problems/maze/run_game.py

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be on the git repo?

16 changes: 13 additions & 3 deletions problems/maze/env/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,26 @@
import json
import random

games = {}

app = Flask(__name__)

@app.route('/start')
def start():
key = ''.join(random.choice('0123456789ABCDEF') for i in range(16))
games[key] = Map(21, 21, random.randint(0, 4294967295))
return key

@app.route('/move/<int:move>')
def move(move):
return str(move)
@app.route('/move/<str:key, int:move>')
def move(key, move):
result = games[key].make_move(move)
if result != 0:
send_string("Result: " + ("SUCCESS" if result == 1 else "FAILURE") + " in " + str(time.time()-games[key].start_time) + " seconds.")
del games[key]
return time.time()-games[key].start_time if result == 1 else -1 # -1 indicates failure; any other value is the time it took them to complete the maze.
games[key].update_visibility()
games[key].delay()
send_string(games[key].serialize()) # Josh, I'm assuming that 'send_string' exists, because I have no idea how you want it to be written.

if __name__ == '__main__':
app.run(host='0.0.0.0',port=7500,debug=True)
33 changes: 12 additions & 21 deletions problems/maze/run_game.py → problems/maze/env/maze.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
GOAL = 4 # transparent
PLAYER = 5

# Max amount of time for player to respond.
MAX_SEC = 60
MIN_DELAY = 0.025

# The following class and functions are for testing visibility.
class Point:
def __init__(self, x = 0, y = 0):
Expand Down Expand Up @@ -46,7 +50,7 @@ def does_intersect(l1, l2):
if o4 == 0 and col_is_on_seg(l2[0], l2[1], l1[1]): return True
return False

class Map:
class Maze:
def __init__(self, width, height, seed):
self.height = height
self.width = width
Expand Down Expand Up @@ -74,6 +78,7 @@ def __init__(self, width, height, seed):
self.opaque_walls.append((Point(x+1, y), Point(x+1, y+1)))
self.opaque_walls.append((Point(x, y+1), Point(x+1, y+1)))
self.opaque_walls = set(self.opaque_walls)
self.start_time = time.time()
def make_move(self, dir):
self.contents[self.player_loc[1]][self.player_loc[0]] = EMPTY
if dir == NORTH and self.player_loc[1] != 0 and (self.contents[self.player_loc[1]-1][self.player_loc[0]] == EMPTY or self.contents[self.player_loc[1]-1][self.player_loc[0]] == GOAL):
Expand All @@ -84,17 +89,17 @@ def make_move(self, dir):
self.player_loc = (self.player_loc[0], self.player_loc[1]+1)
elif dir == WEST and self.player_loc[0] != 0 and (self.contents[self.player_loc[1]][self.player_loc[0]-1] == EMPTY or self.contents[self.player_loc[1]][self.player_loc[0]-1] == GOAL):
self.player_loc = (self.player_loc[0]-1, self.player_loc[1])
if self.contents[self.player_loc[1]][self.player_loc[0]] == GOAL: return True
if self.contents[self.player_loc[1]][self.player_loc[0]] == GOAL: return 1
self.contents[self.player_loc[1]][self.player_loc[0]] = PLAYER
return False
def get_neighbors(self, loc):
return 0 if time.time() - self.start_time < MAX_SEC else -1
def _get_neighbors(self, loc):
neighbors = []
if loc[1] != 0: neighbors.append((loc[0], loc[1]-1))
if loc[0] != self.width-1: neighbors.append((loc[0]+1, loc[1]))
if loc[1] != self.height-1: neighbors.append((loc[0], loc[1]+1))
if loc[0] != 0: neighbors.append((loc[0]-1, loc[1]))
return neighbors
def test_visible(self, loc):
def _test_visible(self, loc):
corners = [ Point(loc[0], loc[1]),Point(loc[0]+1, loc[1]),Point(loc[0], loc[1]+1),Point(loc[0]+1, loc[1]+1) ]
center = Point(self.player_loc[0]+0.5, self.player_loc[1]+0.5)
for p in corners:
Expand Down Expand Up @@ -131,6 +136,8 @@ def update_visibility(self):
for x in range(0, self.width):
if self.visibility[y][x] == -1:
self.visibility[y][x] = False
def delay(self):
time.sleep(MIN_DELAY)
def serialize(self):
grid = []
for y in range(0, self.height):
Expand All @@ -142,19 +149,3 @@ def serialize(self):
row.append(UNKNOWN)
grid.append(row)
return json.dumps(grid)

MAX_SEC = 60

m = Map(5, 5, 0)
start_time = time.time();
game_over = False
while not game_over:
m.update_visibility()
send_string(m.serialize())
try:
move = get_move(MAX_SEC + start_time - time.time())
game_over = m.make_move(move)
except Exception:
print(-1)
sys.exit(0)
print(time.time() - start_time)
12 changes: 12 additions & 0 deletions problems/maze/starter/run.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import subprocess, sys

connect_to_server(); # Assumed to exist.

proc = subprocess.Popen(sys.argv[1], stdout=subprocess.PIPE, stdin=subprocess.PIPE, shell=True)

while True:
s = get_string() # Assume to exist.
if s.split(' ')[0] == "Result:":
print(s)
break
proc.communicate(s)