Package de.axxeed.animosy.sandbox

Source Code of de.axxeed.animosy.sandbox.MovementTree

/**
*
*/
package de.axxeed.animosy.sandbox;

import java.util.HashSet;
import java.util.Iterator;

import org.apache.log4j.Logger;

import de.axxeed.animosy.model.BoardModel;
import de.axxeed.animosy.model.Constants;
import de.axxeed.animosy.model.Link;
import de.axxeed.animosy.model.Node;

/**
* MovementTree.java
* Created 08.02.2008 16:46:53
* @author Markus J. Luzius
*
*/
public class MovementTree implements Constants {
  private static Logger  log  = Logger.getLogger(MovementTree.class);

  private static final int levels = 4;
  private HashSet<Link>[] possibleLocs = new HashSet[levels];
 
  private void calc() {
    int loc = 64;
    possibleLocs[0] = getLinks(loc);
    for (int i=1;i<levels;i++) {
      possibleLocs[i] = new HashSet<Link>();
      for(Iterator<Link> it=possibleLocs[i-1].iterator();it.hasNext();) {
        possibleLocs[i].addAll(getLinks( it.next().getToNode().getPosition() ));
      }
    }
  }
 
  private void dump() {
    for (int i=0;i<levels;i++) {
      log.debug("------------------------------------------------------");
      log.debug("Level: "+i);
      for(Iterator<Link> it=possibleLocs[i].iterator();it.hasNext();) {
        log.debug(it.next());
      }
    }
  }

  private void dumpGraph() {
    StringBuilder buffer = new StringBuilder();
    buffer.append("digraph G").append("\n");
    buffer.append("{").append("\n");
    buffer.append("    rankdir=UD; node [fontname=\"Arial\", fontsize=10];").append("\n");
    for (int i=0;i<levels;i++) {
      for(Iterator<Link> it=possibleLocs[i].iterator();it.hasNext();) {
        Link l = it.next();
        buffer.append(" \""+l.getFromNode().getPosition()+"("+i+")\" ->  \""+l.getToNode().getPosition()+"("+(i+1)+")\"");
        buffer.append(" [color="+decodeColor(l.getType())+"]");
        buffer.append(";\n");
      }
    }
    buffer.append("}").append("\n");
    log.debug("\n"+buffer);
  }

  private String decodeColor(int type) {
    switch(type) {
      case TAXI: return "gold";
      case UG: return "red";
      case BUS: return "green";
      case BLACK: return "black";
    }
    return "blue";
  }
 
  private HashSet<Link> getLinks(int pos) {
    HashSet<Link> result = new HashSet();
    Link[] l = BoardModel.getNode(pos).getLinks();
    for(int i=0;i<l.length;i++) {
      result.add(l[i]);
    }
    return result;
  }
 
  /**
   * @param args
   */
  public static void main(String[] args) {
  // XXX Auto-generated method stub
    MovementTree mt = new MovementTree();
    mt.calc();
    mt.dump();
    mt.dumpGraph();
  }

}
TOP

Related Classes of de.axxeed.animosy.sandbox.MovementTree

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.