package uk.ac.cam.eeci.framework.test;

import uk.ac.cam.eeci.framework.EndSimulationException;
import uk.ac.cam.eeci.framework.ISimulation;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

/**
 * Created by daniel on 08/02/17.
 */
public class Simulation implements ISimulation {

    private final static int NUMBER_TIME_STEPS = 10;

    final int nTiles=6;
    final int tileSize = 100;
    TileReference [] tilerefs = new TileReference[nTiles];
    private int remainingSteps;

    public Simulation() {
        this.remainingSteps = NUMBER_TIME_STEPS;
        int i;
        for(i=0; i<nTiles; ++i) {
            tilerefs[i] = new TileReference(tileSize);
        }
        Tile.join(tilerefs);
    }

    public void step() throws ExecutionException, InterruptedException, EndSimulationException {
        if (this.remainingSteps > 0) {
            this.performStep();
            this.remainingSteps -= 1;
        } else {
            throw new EndSimulationException();
        }
    }

    private void performStep() throws ExecutionException, InterruptedException {
        CompletableFuture<Void>[] steps = new CompletableFuture[nTiles];
        for(int i=0; i<nTiles; ++i) {
            steps[i] = tilerefs[i].step();
        }
        CompletableFuture.allOf(steps).get();
        CompletableFuture<Void>[] updates = new CompletableFuture[nTiles];
        for(int i=0; i<nTiles; ++i) {
            updates[i] = tilerefs[i].update();
        }
        CompletableFuture.allOf(updates).get();
        printState();
    }

    public void printState() throws ExecutionException, InterruptedException {
        for(int n=0; n<nTiles; ++n) {
            printState(n).get();
        }
        System.out.println("");
    }

    public CompletableFuture<Void> printState(int n) {
        return tilerefs[n].printState(n*tileSize, 0);
    }

    @Override
    public void stop() {

    }
}

@@ -1549,345 +1549,8 @@
 ();%0A
-        printState();%0A    %7D%0A%0A    public void printState() throws ExecutionException, InterruptedException %7B%0A        for(int n=0; n%3CnTiles; ++n) %7B%0A            printState(n).get();%0A        %7D%0A        System.out.println(%22%22);%0A    %7D%0A%0A    public CompletableFuture%3CVoid%3E printState(int n) %7B%0A        return tilerefs%5Bn%5D.printState(n*tileSize, 0);%0A
     

Remove sysout from framework tests