#!/usr/bin/env python3 #(height, width) import random as rnd from enum import Enum 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: #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[i[0]][i[1]].typ == SquareType.SEAT: candidati.append(passenger.curr[0], passenger.curr[1] + 1) candidati.append(passenger.curr[0], passenger.curr[1] - 1) 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(0,3)] for a in range(0,3): 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) toad = 0 def tick(): done=True global toad # print grid for row in grid: for guy in row: if guy.occupant is not None: print("X", end="") elif guy.typ == SquareType.SEAT: print("_", end="") else: print(".", end="") print() print("---") if toad < len(passengers): grid[4][0].occupant = toad #added passengers[toad].curr = (4, 0) toad += 1 for (i, man) in enumerate(passengers): if man.curr == man.dest: continue if man.curr == (-1, -1): continue done=False #im sexist cp = man.curr man.curr = nextSquare(man, grid) grid[cp[0]][cp[1]].occupant = None #they move out of there grid[man.curr[0]][man.curr[1]].occupant = i if done: exit() def run(): while 1: tick() run()