@Override public void render(GameContainer gc, Graphics g) throws SlickException { backgroundImage.draw(); Airplane airplaneHandle; Iterator<Airplane> it; it = airQueue.iterator(); while (it.hasNext()) { airplaneHandle = it.next(); airplaneHandle.render(g, airplaneImage); } it = groundQueue.iterator(); while (it.hasNext()) { airplaneHandle = it.next(); airplaneHandle.render(g, airplaneImage); } airplaneHandle = runway.getCurrentAirplane(); if (airplaneHandle != null) airplaneHandle.render(g, airplaneImage); g.setColor(Color.white); g.drawString("Airport Simulator", gc.getWidth() - 170, 10); g.drawString("Update: " + currentUpdate, gc.getWidth() - 130, gc.getHeight() - 30); }
@Override public void update(GameContainer gc, int delta) throws SlickException { rand.nextDouble(); if (System.currentTimeMillis() - lastUpdate > simulationDelay) { lastUpdate = System.currentTimeMillis(); runSimulation(); } Airplane airplaneHandle; Iterator<Airplane> it; it = airQueue.iterator(); while (it.hasNext()) { airplaneHandle = it.next(); airplaneHandle.update(delta); } it = groundQueue.iterator(); while (it.hasNext()) { airplaneHandle = it.next(); airplaneHandle.update(delta); } airplaneHandle = runway.getCurrentAirplane(); if (airplaneHandle != null) airplaneHandle.update(delta); }
public void writeResults(ArrayList<Airplane> finishedAirplanes, int caseNumber) { String currentLine; boolean validity = true; // If one plane did not land successfully, then the scenario is marked as impossible. for (Airplane airplane : finishedAirplanes) { if (airplane.isResult() != true) { validity = false; } } try { BufferedReader reader = new BufferedReader( new FileReader(this.outputPath)); // Create new reader for our output path BufferedWriter writer = new BufferedWriter( new FileWriter(this.outputPath)); // Create new writer for our output path currentLine = reader.readLine(); while (currentLine != null) // Iterate through the file and check existing contents { currentLine = reader.readLine(); if (currentLine == null) // Ignore spacing between cases { currentLine = reader.readLine(); } } writer.write("CASE " + caseNumber + ": "); if (validity == false) { writer.write("IMPOSSIBLE"); writer.newLine(); } else { writer.write("POSSIBLE"); for (Airplane airplane : finishedAirplanes) { writer.write( airplane.getId() + " LANDED AT " + airplane.getRunwayCompletionTime() + " WITH " + airplane.getFuel() + " REMAINING."); } writer.newLine(); } } catch (IOException e) { e.printStackTrace(); } }
/** Example. */ @Test public void example() { double dblReserve = TankSize.RESERVE.size() * 2.0; // RESERVE size is set to 1600 FuelTank largeReserve = new FuelTank(dblReserve); FuelTank tripleReserve = new FuelTank(TankSize.RESERVE.multi(3)); FuelTank normalReserve = new FuelTank(TankSize.RESERVE); FuelTank miniTank = new FuelTank(2); // 2 Gallon tank FuelTank fiveGalTank = new FuelTank(5); FuelTank largeFuelTank = new FuelTank(TankSize.LARGE); Airplane smallPlane = new Airplane( new Random().nextInt(), Airline.AAL, largeFuelTank, AirplaneState.WAITINHANGAR, Airline.AAL.getHangar()); Airplane p1 = new Airplane(); p1.setId(1); p1.setOwner(Airline.getRandom()); p1.setTank(new FuelTank(new Random().nextGaussian() * 100)); p1.setHolder(p1.getOwner().getHangar()); /** * //How a taxi would be created Taxi t1 = new Taxi(new Random().nextInt(), null, new * FuelTank(TankSize.SMALL.multi((new Random().nextGaussian()*.5))), VehicleState.WAITING, * Taxiway.A); Taxi t2 = new Taxi(); t2.setId(2); t2.setOwner(Airport.AUS); t2.setTank(new * FuelTank(new Random().nextGaussian()*15)); t2.setHolder(t2.getOwner().chooseTaxiway()); */ /* * All Tanks are empty, you must either fill() or add(Fuel f) * Vehicles can refuel() although this fills to FULL not MAX */ try { tripleReserve.fill(); // Magically fill tank to capacity largeReserve.fill(tripleReserve); // Fill tank from source normalReserve.fill(tripleReserve); } catch (FuelTankException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { assert (tripleReserve.isEmpty()); assert (largeReserve.isFull()); assert (normalReserve.isFull()); } try { largeFuelTank.fill(normalReserve); fiveGalTank.fill(largeFuelTank); miniTank.fill(largeFuelTank); smallPlane.getTank().fill(largeFuelTank); } catch (FuelTankException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { assert (!largeFuelTank.isEmpty()); assert (!normalReserve.isEmpty()); assert (miniTank.isFull()); assert (smallPlane.getTank().isFull()); } }
private void runSimulation() { System.out.println("Update: " + currentUpdate); currentUpdate++; Airplane airplaneHandle; // Randomly add planes for (int i = 0; i < 2; i++) { if (newLandingProbability >= Math.pow(rand.nextDouble(), i)) { airplaneHandle = new Airplane( generateAirplaneNumber(), app.getWidth(), landingY, landingX, landingY, 30 * (simulationDelay / 1000.0)); airQueue.add(airplaneHandle); System.out.println("Now queued for landing: " + airplaneHandle); } } if (newTakeoffProbability >= rand.nextDouble()) { airplaneHandle = new Airplane( generateAirplaneNumber(), app.getWidth(), takeoffY, takeoffX, takeoffY, 30 * (simulationDelay / 1000.0)); groundQueue.add(airplaneHandle); System.out.println("Now queued for takeoff: " + airplaneHandle); } // Load the runway if (runway.isVacant()) if (!airQueue.isEmpty()) { runway.setOccupied(airQueue.poll(), true); runway.getCurrentAirplane().setDestPos(runwayX, runwayY); System.out.println("Now landing: " + runway.getCurrentAirplane()); } else if (!groundQueue.isEmpty()) { runway.setOccupied(groundQueue.poll(), false); runway.getCurrentAirplane().setDestPos(runwayX, runwayY); System.out.println("Now taking off: " + runway.getCurrentAirplane()); } // Set queued airplane destinations Iterator<Airplane> it; it = airQueue.iterator(); int i = 0; while (it.hasNext()) { airplaneHandle = it.next(); airplaneHandle.setDestPos(landingX + (airplaneImage.getWidth() + 10) * i, landingY); i++; } it = groundQueue.iterator(); i = 0; while (it.hasNext()) { airplaneHandle = it.next(); airplaneHandle.setDestPos(takeoffX + (airplaneImage.getWidth() + 10) * i, takeoffY); i++; } // Output information about the runway System.out.println( runway + "\nNumber of planes waiting to land: " + airQueue.size() + "\nNumber of planes waiting to take off: " + groundQueue.size()); // Tick runway runway.tick(); System.out.println(); }