You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
188 lines
5.5 KiB
188 lines
5.5 KiB
#!/usr/bin/env python3
|
|
#(height, width)
|
|
import random as rnd
|
|
import os
|
|
from enum import Enum
|
|
import time
|
|
import math
|
|
|
|
DEBUG=True
|
|
|
|
class SquareType(Enum):
|
|
AISLE = 0
|
|
SEAT = 1
|
|
WALL = 2
|
|
|
|
height=0
|
|
width=0
|
|
|
|
grid=[]
|
|
# entries=[(3,0)]
|
|
entries=[(0,3)]
|
|
|
|
class GridSquare:
|
|
#typ: SquareType, occupant: index
|
|
def __init__(self, typ, occupant):
|
|
self.typ = typ
|
|
self.occupant = occupant
|
|
def __str__(self):
|
|
return "(%s|%s)" % (str(self.typ), str(self.occupant))
|
|
def __repr__(self):
|
|
return str(self)
|
|
|
|
def makeGrid():
|
|
return [[GridSquare(SquareType.AISLE, None) for x in range(0,width)] for y in range(0,height)]
|
|
|
|
class Passenger:
|
|
shuffled = 0
|
|
inter = None
|
|
#dest and curr are both 2-tuples
|
|
def __init__(self, dest, curr):
|
|
self.dest = dest
|
|
self.curr = curr
|
|
def __str__(self):
|
|
return "(%s|%s)" % (dest, curr)
|
|
|
|
def manhattanDistance(pos1, pos2):
|
|
return abs(pos1[0] - pos2[0]) + abs(pos1[1] - pos2[1])
|
|
|
|
def mooreNeighbourhood(pos):
|
|
return [i for i in [(pos[0]+1, pos[1]), (pos[0], pos[1]+1), (pos[0]-1, pos[1]), (pos[0], pos[1]-1)] if
|
|
i[0] >= 0 and i[1] >= 0 and i[0] < height and i[1] < width]
|
|
|
|
def nextSquare(passenger, grid):
|
|
#simple behaviour for now
|
|
candidati = []
|
|
dest = None
|
|
if passenger.inter == None:
|
|
dest = passenger.dest
|
|
else:
|
|
dest = passenger.inter
|
|
for i in mooreNeighbourhood(passenger.curr):
|
|
if manhattanDistance(dest, i) < manhattanDistance(dest, passenger.curr):
|
|
# if grid[passenger.curr[0]][passenger.curr[1]].typ == SquareType.SEAT and i[1] != passenger.curr[1] and False:
|
|
if grid[passenger.curr[0]][passenger.curr[1]].typ == SquareType.SEAT and i[1] != passenger.curr[1]:
|
|
pass
|
|
else:
|
|
candidati.append(i)
|
|
for i in candidati:
|
|
if grid[i[0]][i[1]].typ == SquareType.AISLE and grid[i[0]][i[1]].occupant != None: #this should work well enough for one aisle plane
|
|
return i
|
|
#todo: implement shufflin'
|
|
return candidati[0]
|
|
|
|
def GenPassList():
|
|
global height
|
|
global width
|
|
global grid
|
|
border=[]
|
|
arrFile = open("figure2.txt")
|
|
|
|
height=int(arrFile.readline())
|
|
width=int(arrFile.readline())
|
|
print(height)
|
|
print(width)
|
|
|
|
grid = [[GridSquare(SquareType.AISLE, None) for x in range(0,width)] for y in range(0,height)]
|
|
|
|
for y in range(0,height):
|
|
for x in range(0,width+1):
|
|
char = arrFile.read(1);
|
|
match char:
|
|
case 'S':
|
|
grid[y][x].typ = SquareType.SEAT
|
|
border.append(Passenger((y,x),(-1,-1)))
|
|
case 'W':
|
|
grid[y][x].typ = SquareType.WALL
|
|
case '.':
|
|
grid[y][x].typ = SquareType.AISLE
|
|
|
|
for x in border:
|
|
for i in range(1,7):
|
|
inc = (-1)**i * -math.floor((i+1)/2)
|
|
if (inc + x.dest[0] > 0) and (inc + x.dest[0] < height) and (grid[x.dest[0]+inc][x.dest[1]].typ == SquareType.AISLE):
|
|
x.inter=(inc + x.dest[0], x.dest[1])
|
|
|
|
return border
|
|
|
|
#boarding order
|
|
passengers = GenPassList()
|
|
|
|
# rnd.seed(605)
|
|
# passengers.reverse()
|
|
rnd.shuffle(passengers)
|
|
toad = 0
|
|
btime = 3 #passengers board every btime ticks
|
|
|
|
|
|
|
|
def nunty(x):
|
|
if x is None: return -1
|
|
return x
|
|
|
|
def tick(t):
|
|
done=True
|
|
global toad
|
|
global btime
|
|
# print grid
|
|
if DEBUG:
|
|
for row in grid:
|
|
for guy in row:
|
|
if guy.occupant is not None:
|
|
print(chr(0x20000 + guy.occupant), end="")
|
|
elif guy.typ == SquareType.SEAT:
|
|
print(":", end="")
|
|
elif guy.typ == SquareType.WALL:
|
|
print("W", end="")
|
|
else:
|
|
print("_", end="")
|
|
print()
|
|
print("---")
|
|
# print(grid[3][0].occupant, grid[3][1].occupant, passengers[nunty(grid[3][1].occupant)].dest)
|
|
if t % btime == 0:
|
|
for x in entries:
|
|
if toad < len(passengers) and grid[x[0]][x[1]].occupant is None:
|
|
grid[x[0]][x[1]].occupant = toad #added
|
|
passengers[toad].curr = (x[0], x[1])
|
|
toad += 1
|
|
for (i, man) in enumerate(passengers):
|
|
if man.curr == man.dest:
|
|
continue
|
|
if man.inter is not None and man.curr == man.inter:
|
|
man.shuffled = 3
|
|
man.inter = None
|
|
if man.curr == (-1, -1):
|
|
continue
|
|
# if i == 36 and DEBUG:
|
|
# print(nextSquare(man, grid))
|
|
print(str(man.curr) + ", " + str(man.inter) + ", " + str(man.dest))
|
|
done=False
|
|
#im sexist
|
|
nextS=nextSquare(man,grid)
|
|
if passengers[i].shuffled == 0 and not ( grid[nextS[0]][nextS[1]].typ == SquareType.AISLE and grid[nextS[0]][nextS[1]].occupant is not None):
|
|
cp = man.curr
|
|
man.curr = nextS
|
|
other = grid[cp[0]][cp[1]]
|
|
other.occupant = grid[man.curr[0]][man.curr[1]].occupant #they move out of there
|
|
if other.occupant is not None:
|
|
passengers[other.occupant].curr = cp
|
|
passengers[other.occupant].shuffled = 5
|
|
grid[man.curr[0]][man.curr[1]].occupant = i
|
|
for man in passengers:
|
|
if man.shuffled != 0:
|
|
man.shuffled-=1
|
|
if DEBUG:
|
|
time.sleep(0.01)
|
|
# time.sleep(0.05)
|
|
os.system("clear")
|
|
if done and toad >= len(passengers):
|
|
print("Number of ticks: " + str(t))
|
|
exit()
|
|
|
|
def run():
|
|
t = 0
|
|
while 1:
|
|
tick(t)
|
|
t += 1
|
|
|
|
run()
|
|
|