Create a gist now

Instantly share code, notes, and snippets.

import random
import time
import sys
BLUE = '\033[94m'
GREEN = '\033[92m'
WALL = '\033[93m'
SIGHT = '\033[95m'
ENDC = '\033[0m'
class Person(object):
position = (0, 0)
direction = 0
store = []
sight = []
def __init__(self, store):
self.store = store
self.sight = []
self.init_position()
self.rotate()
def init_position(self):
walkables = []
for i in range(1, len(self.store)):
for j in range(1, len(self.store[0])):
if self.store[i][j] == 0:
walkables.append((j, i))
self.position = random.choice(walkables)
def rotate(self):
self.direction = random.randint(0, 3)
def move(self):
movables = [self.position]
if self.position[0]+1 < len(self.store[0]) and self.store[self.position[1]][self.position[0]+1] != 1:
movables.append((self.position[0]+1, self.position[1]))
if self.position[0]-1 > 0 and self.store[self.position[1]][self.position[0]-1] != 1:
movables.append((self.position[0]-1, self.position[1]))
if self.position[1]+1 < len(self.store) and self.store[self.position[1]+1][self.position[0]] != 1:
movables.append((self.position[0], self.position[1]+1))
if self.position[1]-1 > 0 and self.store[self.position[1]-1][self.position[0]] != 1:
movables.append((self.position[0], self.position[1]-1))
self.position = random.choice(movables)
def sights(self):
self.sight = []
if self.direction == 0:
for i in range(self.position[1]-1, 0, -1):
if self.store[i][self.position[0]] != 0:
break
self.sight.append((self.position[0], i))
elif self.direction == 1:
for i in range(self.position[0]+1, len(self.store[0])):
if self.store[self.position[1]][i] != 0:
break
self.sight.append((i, self.position[1]))
elif self.direction == 2:
for i in range(self.position[1]+1, len(self.store)):
if self.store[i][self.position[0]] != 0:
break
self.sight.append((self.position[0], i))
elif self.direction == 3:
for i in range(self.position[0]-1, 0, -1):
if self.store[self.position[1]][i] != 0:
break
self.sight.append((i, self.position[1]))
return self.sight
class LostSimulation(object):
store = []
mamma = (0, 0)
me = (0, 0)
step = 0
def __init__(self):
self.store = [
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0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 1],
[1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]
self.mamma = Person(self.store)
self.me = Person(self.store)
self.step = 0
def is_found(self):
if self.mamma.position == self.me.position:
return True
elif self.mamma.position in self.me.sights():
return True
elif self.me.position in self.mamma.sights():
return True
else:
return False
def step_forward(self):
self.mamma.rotate()
self.mamma.move()
self.me.rotate()
self.me.move()
self.step += 1
def draw_situation(self):
sys.stdout.flush()
for i in range(0, len(self.store)):
for j in range(0, len(self.store[0])):
someone_in_there = False
if self.mamma.position == (j, i):
someone_in_there = True
print BLUE + "M" + ENDC,
if self.me.position == (j, i):
someone_in_there = True
print GREEN + "X" + ENDC,
if not someone_in_there:
if self.store[i][j] == 0:
if (j, i) in self.mamma.sight or (j, i) in self.me.sight:
print SIGHT + "." + ENDC,
else:
print " ",
else:
print WALL + str(self.store[i][j]) + ENDC,
print ""
for idx in range(0, 100000):
sim = LostSimulation()
#sim.draw_situation()
while not sim.is_found():
# time.sleep(0.1)
# sim.draw_situation()
sim.step_forward()
#sim.draw_situation()
print sim.step
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment