Package eas.users.demos.tetris

Source Code of eas.users.demos.tetris.AbstractTetrisAgent

/*
* Datei:            AbstractTetrisAgent.java
* Autor(en):        Lukas König
* Java-Version:     6.0
* Erstellt:         ??.06.2010
*
* (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;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.util.TreeMap;

import eas.math.geometry.Vector2D;
import eas.simulation.ConstantsSimulation;
import eas.simulation.Wink;
import eas.simulation.agent.GenericActuator;
import eas.simulation.agent.GenericSensor;
import eas.simulation.event.EASEvent;
import eas.simulation.spatial.sim2D.standardAgents.AbstractAgent2D;
import eas.simulation.spatial.sim2D.standardEnvironments.AbstractEnvironment2D;
import eas.startSetup.ParCollection;

/**
* @author Lukas König
*/
public abstract class AbstractTetrisAgent extends AbstractAgent2D<AbstractEnvironment2D<?>> {

    /**
     *
     */
    private static final long serialVersionUID = 7543782064038999143L;

    public AbstractTetrisAgent(
            final int id,
            final AbstractEnvironment2D<?> env,
            final ParCollection params) {
        super(id, env, params);
       
        this.addSensor(new GenericSensor<String, TetrisEnvironment, AbstractAgent2D<?>>() {
            /**
             *
             */
            private static final long serialVersionUID = -5774391617256666993L;

            @Override
            public String sense(TetrisEnvironment env, AbstractAgent2D<?> agent) {
                TreeMap<Long, String> scorez = env.getScorez();
                if (scorez.size() == 0) {
                    return "NOBODY - 0";
                } else {
                    return scorez.lastEntry().getValue() + " - "
                            + scorez.lastKey();
                }
            }

            @Override
            public String id() {
                return "HiScore";
            }
           
        });
       
        this.addSensor(new GenericSensor<String, TetrisEnvironment, AbstractTetrisAgent>() {

            /**
             *
             */
            private static final long serialVersionUID = 6372848751814934732L;

            @Override
            public String id() {
                return "Points";
            }

            @Override
            public String sense(TetrisEnvironment env, AbstractTetrisAgent agent) {
                return env.getPoints() + " (rank " + env.getPlace() + ")";
            }
        });
       
        this.addSensor(new GenericSensor<String, TetrisEnvironment, AbstractTetrisAgent>() {
           
            /**
             *
             */
            private static final long serialVersionUID = 2932724520568275622L;

            @Override
            public String sense(TetrisEnvironment env, AbstractTetrisAgent agent) {
                return env.getPressed();
            }

            @Override
            public String id() {
                return "KeyPressed";
            }
            });

        this.addSensor(new GenericSensor<Integer, TetrisEnvironment, AbstractTetrisAgent>() {
           
            /**
             *
             */
            private static final long serialVersionUID = -1441031559496351755L;

            @Override
            public Integer sense(TetrisEnvironment env, AbstractTetrisAgent agent) {
                return env.getPause();
            }

            @Override
            public String id() {
                return "Level";
            }
            });

        this.addSensor(new GenericSensor<AbstractTetrisAgent, TetrisEnvironment, AbstractTetrisAgent>() {
           
            /**
             *
             */
            private static final long serialVersionUID = 4241595735076992285L;

            @Override
            public AbstractTetrisAgent sense(TetrisEnvironment env, AbstractTetrisAgent agent) {
                AbstractTetrisAgent ag = env.getNextAgent();
                return ag;
            }

            @Override
            public BufferedImage getSensorView(TetrisEnvironment env,
                    AbstractTetrisAgent agent) {
                BufferedImage tile = new BufferedImage(
                        4 * env.fac + 1,
                        4 * env.fac + 1,
                        BufferedImage.TYPE_INT_RGB);
                Graphics2D g = tile.createGraphics();
                g.setColor(Color.white);
                g.fillRect(0, 0, tile.getWidth(), tile.getHeight());
                Object sensed = agent.sense("nextTile");
                if (sensed == null) {
                    return null;
                }
                for (Vector2D v : ((AbstractTetrisAgent) sensed).getAgentShape()) {
                    g.setColor(Color.orange);
                    g.fillRect(
                            (int) ((v.x + 1) * env.fac),
                            (int) ((v.y + 1) * env.fac),
                            env.fac,
                            env.fac);
                    g.setColor(Color.red);
                    g.drawRect(
                            (int) ((v.x + 1) * env.fac),
                            (int) ((v.y + 1) * env.fac),
                            env.fac,
                            env.fac);
                }
               
                return tile;
            }

            @Override
            public String id() {
                return "nextTile";
            }
            });
       
        this.addSensor(new GenericSensor<Boolean, TetrisEnvironment, AbstractTetrisAgent>() {

            /**
             *
             */
            private static final long serialVersionUID = -9116883969096479680L;

            @Override
            public String id() {
                return "GameOver";
            }

            @Override
            public Boolean sense(TetrisEnvironment env, AbstractTetrisAgent agent) {
                return env.isGameOver();
            }
        });
       
        this.addActuator(new GenericActuator<TetrisEnvironment, AbstractTetrisAgent>() {
            /**
             *
             */
            private static final long serialVersionUID = -223502719425496281L;

            @Override
            public void actuate(TetrisEnvironment env, AbstractTetrisAgent agent) {
                String pressed = (String) AbstractTetrisAgent.this.sense("KeyPressed");
                Vector2D newpos = new Vector2D(env.getAgentPosition(agent.id()));

                if (pressed.equals("N")) {
                    env.reset();
                }
               
                if (agent.id() != 0 || env.isGameOver()) {
                    return;
                }

                if (pressed.equals("8") || pressed.equals(ConstantsSimulation.ARROW_KEY_ENCODING_UP)) {
                    double angle = env.getAgentAngle(agent.id()) - Math.PI / 2;
                    if (angle < 0.1) {
                        angle = Math.PI * 2;
                    }
                    env.setAgentAngle(0, angle);
                }
                if (pressed.equals("4") || pressed.equals(ConstantsSimulation.ARROW_KEY_ENCODING_LEFT)) {
                    newpos.translate(new Vector2D(-1, 0));
                    env.setAgentPosition(agent.id(), newpos);
                }
                if (pressed.equals("6") || pressed.equals(ConstantsSimulation.ARROW_KEY_ENCODING_RIGHT)) {
                    newpos.translate(new Vector2D(1, 0));
                    env.setAgentPosition(agent.id(), newpos);
                }
                if (pressed.equals("5") || pressed.equals(ConstantsSimulation.ARROW_KEY_ENCODING_DOWN)) {
                    newpos.translate(new Vector2D(0, 1));
                    env.setAgentPosition(agent.id(), newpos);
                }
                if (pressed.equals(" ")) {
                    env.senkeAb();
                }
            }

            @Override
            public String id() {
                return "Respond";
            }
        });
    }

    @Override
    public void handleEvent(EASEvent e, Wink lastTick) {
        this.actuate("Respond");
    }
}
TOP

Related Classes of eas.users.demos.tetris.AbstractTetrisAgent

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.