/** Advance the game world by one tick. Ask gameworld for winners and print statistics. */ private void tick(GameWorld gameWorld) { gameWorld.tick(); String gameOverMessage = ""; List<Player> winner = gameWorld.getWinner(); if (winner.size() == 1) { if (gameWorld.tickCount() >= gameWorld.parameters.MAX_TICKS_BEFORE_END) { gameOverMessage = "The game was stopped since the maximum number of ticks is reached.\n"; } gameOverMessage = winner.get(0) + " has won the game in tick " + gameWorld.tickCount(); } else if (winner.size() > 1) { for (GameWorld.Player aWinner : winner) { gameOverMessage += aWinner + " "; } gameOverMessage += "have won the game in tick " + gameWorld.tickCount(); } running = (winner.size() == 0); if (running == false) { System.out.println(statisticsView); System.out.println(gameOverMessage); } }
/** * Start the simulation and keep the view up to date. 1. Update the Graphics at the rate * framesPerSecond 2. Update the Simulation at the rate ticksPerSecond do nothing in the remaining * time or if times get in conflict only update the graphics. * * <p>keyboard/mouse input should be fetched before graphics in every loop. */ public void runSimulation() { GameWorld gameWorld; while (!abortRequested) { gameWorld = gameWorldFactory.nextGameWorld(); if (gameWorld == null) { break; } SeededRandomizer.resetSeed(gameWorld.seed); System.out.println("seed next game: " + gameWorld.seed); StatisticsLogger logger = new StatisticsLogger(gameWorld.getPlayers()); gameWorld.setLogger(logger); statisticsView = new StatisticsView(gameWorld, logger); statisticsView.run(); if (isGraphical) { try { Display.setTitle("Wota"); Display.setDisplayMode(new DisplayMode(width, height)); Display.create(new PixelFormat(8, 0, 0, 0)); } catch (LWJGLException e) { e.printStackTrace(); System.exit(0); } createKeyboard(); gameView = new GameView(gameWorld, width, height); gameView.setup(); } running = true; startTime = System.nanoTime(); resetReferenceValues(); long lastMeasurementTime = System.nanoTime(); // time for TPS / FPS measurements int measureFrameCount = 0; // Frame counter to determine TPS int measureTickCount = 0; // Tick counter to determine FPS // events for graphics update and tick are created uniformly. Call them with priority on // graphics while (running && !abortRequested) { if (isGraphical) { handleKeyboardInputs(); } // only check if is not graphical since we want the // maximal number of updates possible if (ticksToDo() > SKIP_TICKS_THRESHOLD && isGraphical) { resetReferenceValues(); } // now update simulation if tick event if ((!isGraphical || ticksToDo() > 0) && !paused) { tick(gameWorld); measureTickCount++; referenceTickCount++; } // Update Graphics if event for graphic update is swept. if (framesToDo() > 0) { statisticsView.refresh(); frameCount++; measureFrameCount++; referenceFrameCount++; if (isGraphical) { // calc graphics if (!paused) { gameView.render(); } // must be called in any case to catch keyboard input abortRequested = Display.isCloseRequested(); Display.update(); } } // measurements of FPS / TPS long timeDiff = System.nanoTime() - lastMeasurementTime; if (timeDiff > 1.e9 * MEASUREMENT_INTERVAL) { measuredFramesPerSecond = measureFrameCount * 1.e9 / timeDiff; measuredTicksPerSecond = measureTickCount * 1.e9 / timeDiff; measureFrameCount = 0; measureTickCount = 0; lastMeasurementTime = System.nanoTime(); System.out.format("Frames per second: %.1f\n", measuredFramesPerSecond); System.out.format("Ticks per second: %.1f\n", measuredTicksPerSecond); } } // game done. Add to stats. List<Player> winner = gameWorld.getWinner(); List<Player> loosers = new ArrayList<Player>(gameWorld.getPlayers()); loosers.removeAll(winner); if (winner.size() == 0) { System.err.println("winner should not be empty."); } else if (winner.size() == 1) { resultCollection.addGame(getNames(winner), null, getNames(loosers)); } else { // multiple winners count as draw. But we could still have loosers. resultCollection.addGame(null, getNames(winner), getNames(loosers)); } System.out.println("seed last game: " + gameWorld.seed); statisticsView.frame.dispose(); if (isGraphical) { Display.destroy(); } } // last gameWorld done System.out.println(resultCollection); }