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.
142 lines
4.4 KiB
142 lines
4.4 KiB
#!/usr/bin/env python3
|
|
#(height, width)
|
|
import random as rnd
|
|
from enum import Enum
|
|
import time
|
|
|
|
class SquareType(Enum):
|
|
AISLE = 0
|
|
SEAT = 1
|
|
|
|
height=7
|
|
width=32
|
|
|
|
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
|
|
#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], pos[1]+1), (pos[0]+1, pos[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 = []
|
|
for i in mooreNeighbourhood(passenger.curr):
|
|
if manhattanDistance(passenger.dest, i) < manhattanDistance(passenger.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(grid):
|
|
border = [Passenger((a, 0),(-1,-1)) for a in range(4,7)]
|
|
for a in range(4,7):
|
|
grid[a][0].typ = SquareType.SEAT
|
|
for x in range(1,width):
|
|
border += [Passenger((a, x),(-1,-1)) for a in range(0,3)]
|
|
for a in range(0,3):
|
|
grid[a][x].typ = SquareType.SEAT
|
|
border += reversed([Passenger((a, x),(-1,-1)) for a in range(4,7)])
|
|
for a in range(4,7):
|
|
grid[a][x].typ = SquareType.SEAT
|
|
return border
|
|
|
|
grid=makeGrid()
|
|
|
|
#boarding order
|
|
passengers = GenPassList(grid)
|
|
|
|
rnd.seed(505)
|
|
# passengers.reverse()
|
|
rnd.shuffle(passengers)
|
|
toad = 0
|
|
btime = 3 #passengers board every btime ticks
|
|
|
|
|
|
import os
|
|
|
|
def nunty(x):
|
|
if x is None: return -1
|
|
return x
|
|
def tick(t):
|
|
done=True
|
|
global toad
|
|
global btime
|
|
# print grid
|
|
for row in grid:
|
|
for guy in row:
|
|
if guy.occupant is not None:
|
|
print(chr(0x4e00 + guy.occupant), end="")
|
|
elif guy.typ == SquareType.SEAT:
|
|
print(":", end="")
|
|
else:
|
|
print("。", end="")
|
|
print()
|
|
print("---")
|
|
print(grid[3][0].occupant, grid[3][1].occupant, passengers[nunty(grid[3][1].occupant)].dest)
|
|
if toad < len(passengers) and grid[3][0].occupant is None and t % btime == 0:
|
|
grid[3][0].occupant = toad #added
|
|
passengers[toad].curr = (3, 0)
|
|
toad += 1
|
|
for (i, man) in enumerate(passengers):
|
|
if man.curr == man.dest:
|
|
continue
|
|
if man.curr == (-1, -1):
|
|
continue
|
|
if i == 36:
|
|
print(nextSquare(man, grid))
|
|
done=False
|
|
#im sexist
|
|
if passengers[i].shuffled == 0 and not ( grid[nextSquare(man,grid)[0]][nextSquare(man,grid)[1]].typ == SquareType.AISLE and grid[nextSquare(man,grid)[0]][nextSquare(man,grid)[1]].occupant is not None):
|
|
cp = man.curr
|
|
man.curr = nextSquare(man, grid)
|
|
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
|
|
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()
|
|
|