/*
* Datei: TetrisMaster.java
* Autor(en): Lukas König
* Java-Version: 6.0
* Erstellt: 10.02.2012
*
* (c) This file and the EAS (Easy Agent Simulation) framework containing it
* is protected by Creative Commons by-nc-sa license. Any altered or
* further developed versions of this file have to meet the agreements
* stated by the license conditions.
*
* In a nutshell
* -------------
* You are free:
* - to Share -- to copy, distribute and transmit the work
* - to Remix -- to adapt the work
*
* Under the following conditions:
* - Attribution -- You must attribute the work in the manner specified by the
* author or licensor (but not in any way that suggests that they endorse
* you or your use of the work).
* - Noncommercial -- You may not use this work for commercial purposes.
* - Share Alike -- If you alter, transform, or build upon this work, you may
* distribute the resulting work only under the same or a similar license to
* this one.
*
* + Detailed license conditions (Germany):
* http://creativecommons.org/licenses/by-nc-sa/3.0/de/
* + Detailed license conditions (unported):
* http://creativecommons.org/licenses/by-nc-sa/3.0/deed.en
*
* This header must be placed in the beginning of any version of this file.
*/
package eas.users.demos.tetris.automaticTetris;
import java.awt.Color;
import java.util.List;
import java.util.Random;
import eas.math.geometry.Vector2D;
import eas.plugins.masterScheduler.AbstractDefaultMaster;
import eas.plugins.masterScheduler.KeyEventFilter;
import eas.plugins.standard.visualization.AllroundVideoPlugin;
import eas.plugins.standard.visualization.chartPlugin.AllroundChartPlugin;
import eas.plugins.standard.visualization.chartPlugin.ChartEvent;
import eas.simulation.Wink;
import eas.startSetup.ParCollection;
/**
* @author Lukas König
*/
public class AutomaticTetrisMaster extends AbstractDefaultMaster<TetrisEnvAutomatic> {
private static final long serialVersionUID = 2337415757250284208L;
@Override
public TetrisEnvAutomatic[] generateRunnables(ParCollection params) {
return new TetrisEnvAutomatic[] {new TetrisEnvAutomatic(0, params)};
}
@Override
public String id() {
return eas.simulation.ConstantsSimulation.DEFAULT_MASTER_SCHEDULER_ID + "-automaticTetris";
}
private TetrisMoveSequence seq = null;
private Integer currentMove = null; // TODO
private int nextAgent = -1;
public static int tileNum = 1;
private int eins, zwei, drei, vier;
private void copyEnv(TetrisEnvAutomatic envSource, TetrisEnvAutomatic envTarget) {
for (int i = 0; i < envSource.getFeld().length; i++) {
for (int j = 0; j < envSource.getFeld()[0].length; j++) {
envTarget.getFeld()[i][j] = envSource.getFeld()[i][j];
}
}
}
private TetrisEnvAutomatic copyEnv;
@Override
public void runDuringSimulation(
TetrisEnvAutomatic env,
Wink simZyk,
ParCollection params) {
super.runDuringSimulation(env, simZyk, params);
initializeVidPlug(env);
int tile;
if (simZyk.getLastTick() == 0) {
this.initializeVidPlug(env);
tile = this.nextAgent;
this.nextAgent = this.getRandomTile(params);
copyEnv(env, copyEnv);
copyEnv.addTile(tile);
copyEnv.setSeq(copyEnv.bestMove(tile));
seq = new TetrisMoveSequence(copyEnv.getSeq());
currentMove = 0;
env.setTileType(tile);
env.addTile(tile);
env.setNextAgent(this.nextAgent);
copyEnv.addTile(tile);
copyEnv.applySequence(copyEnv.getSeq());
copyEnv.addTile(this.nextAgent);
copyEnv.setSeq(copyEnv.bestMove(this.nextAgent));
}
if (seq == null) {
while (copyEnv.getSeq() == null) {
}
tileNum++;
// System.out.println(tileNum + " - Eins: " + eins + " / Zwei: " + zwei + " / Drei: " + drei + " / Vier: " + vier);
if (env.getFuellHoehe() > env.getFeld()[0].length - 5) {
env.getSimTime().timeTerminate();
}
tile = this.nextAgent;
this.nextAgent = this.getRandomTile(params);
env.addTile(tile);
seq = new TetrisMoveSequence(this.copyEnv.getSeq());
env.setNextAgent(this.nextAgent);
currentMove = 0;
// Versuche Stein in kopiertem Feld:
this.copyEnv.addTile(tile);
this.copyEnv.applySequence(this.copyEnv.getSeq());
this.copyEnv.setTileType(this.nextAgent);
new Thread(this.copyEnv).start();
} else {
TetrisMove move = null;
if (currentMove < seq.size()) {
move = seq.get(currentMove);
currentMove++;
} else {
move = new TetrisMove(TetrisMove.MOVE_SINK);
}
if (move == null || !env.applyMove(move)) {
int del = env.fixiereAgenten();
if (del == 1) {
eins++;
}
if (del == 2) {
zwei++;
}
if (del == 3) {
drei++;
}
if (del == 4) {
vier++;
}
ChartEvent event1 = new ChartEvent("Zeilen", "Einer", eins);
ChartEvent event2 = new ChartEvent("Zeilen", "Zweier", zwei);
ChartEvent event3 = new ChartEvent("Zeilen", "Dreier", drei);
ChartEvent event4 = new ChartEvent("Zeilen", "Vierer", vier);
ChartEvent event5 = new ChartEvent("Zeilen", "Gesamt", eins * 1 + zwei * 2 + drei * 3 + vier * 4);
if (simZyk.getLastTick() < 100) {
event1.setxAxisLabel("Teile versenkt");
event2.setxAxisLabel("Teile versenkt");
event3.setxAxisLabel("Teile versenkt");
event4.setxAxisLabel("Teile versenkt");
event5.setxAxisLabel("Teile versenkt");
event5.setChartFramePos(new Vector2D(550, 0));
// event5.setLineStroke(new CompositeStroke(new BasicStroke(10f), new BasicStroke(1.5f)));
event5.setLineColor(Color.black);
}
event1.setXValue(tileNum);
event2.setXValue(tileNum);
event3.setXValue(tileNum);
event4.setXValue(tileNum);
event5.setXValue(tileNum);
env.getSimTime().broadcastEvent(event1);
env.getSimTime().broadcastEvent(event2);
env.getSimTime().broadcastEvent(event3);
env.getSimTime().broadcastEvent(event4);
env.getSimTime().broadcastEvent(event5);
seq = null;
currentMove = null;
}
try {
Thread.sleep(25);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
private AllroundVideoPlugin vidPlug;
@Override
public void runBeforeSimulation(
TetrisEnvAutomatic env,
ParCollection params) {
super.runBeforeSimulation(env, params);
copyEnv = new TetrisEnvAutomatic(10000, params);
initializeVidPlug(env);
env.addTile(this.getRandomTile(params));
this.nextAgent = this.getRandomTile(params);
}
private void initializeVidPlug(TetrisEnvAutomatic env) {
if (vidPlug == null) {
vidPlug = (AllroundVideoPlugin) env.getSimTime().getPluginObject(new AllroundVideoPlugin().id());
}
if (vidPlug != null) {
env.getSimTime().requestEvents(vidPlug, new KeyEventFilter());
// vidPlug.getLiveWindow().setAlwaysOnTop(true);
// vidPlug.getLiveWindow().setSize(500, 666);
try {
vidPlug.setClickPosition(10, 10);
} catch (Exception e) {
}
}
}
private Random rand;
private int getRandomTile(@SuppressWarnings("unused") ParCollection params) {
if (rand == null) {
rand = new Random();
}
return rand.nextInt(7);
}
@Override
public List<String> getSupportedPlugins() {
List<String> list = super.getSupportedPlugins();
list.add(new AllroundChartPlugin().id());
return list;
}
}