Package de.axxeed.animosy.model

Source Code of de.axxeed.animosy.model.BoardModel

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

import java.util.StringTokenizer;

import org.apache.log4j.Logger;

import de.axxeed.animosy.tools.FileHandler;

/**
* BoardModel.java
* Created 07.12.2007 13:33:30
* @author Markus J. Luzius
*
*/
public class BoardModel implements Constants {
  private static Logger log = Logger.getLogger(BoardModel.class);

  private static int noOfNodes;
    private static int[][] shortestDistance;
    private static Node[] nodes;

    static {
      readFile();
        int noOfNodes = nodesCount();
        if(shortestDistance == null) {
            shortestDistance=new int[noOfNodes][noOfNodes];
            for(int i=0;i<noOfNodes;i++)
                for(int j=0;j<noOfNodes;j++)
                    shortestDistance[i][j]=baseDistance(BoardModel.getNode(i),BoardModel.getNode(j));
            shortestDistance=getShortestDistanceMatrix(shortestDistance,1);
            // test();
        }
    }
   
    private BoardModel() {
    }
   
    /**This method reads the text file which contains the map*/
    private static void readFile()
    {
        try {
      FileHandler map = new FileHandler("de/axxeed/animosy/scotmap.txt");
      log.info("Reading map network list <"+map.getInfo()+">");
      // RandomAccessFile map=new RandomAccessFile(f,"r");
      String buffer=map.readLine();
      StringTokenizer token;
      token=new StringTokenizer(buffer);
      noOfNodes=Integer.parseInt(token.nextToken());
      nodes=new Node[noOfNodes];
      for(int i=0;i<nodes.length;i++)
          nodes[i]=new Node(i);
      buffer=map.readLine();
      while(buffer!=null && buffer.trim().length()>0) {
        // System.out.println(i+": "+buffer);
        token=new StringTokenizer(buffer);
        int node1=Integer.parseInt(token.nextToken());
        int node2=Integer.parseInt(token.nextToken());
        String strType=token.nextToken();
        int type=INF;
        if(strType.equals("T")) type=TAXI;
        if(strType.equals("B")) type=BUS;
        if(strType.equals("U")) type=UG;
        if(strType.equals("X")) type=BLACK;
        nodes[node1].addLink(nodes[node2],type);
        nodes[node2].addLink(nodes[node1],type);
        buffer=map.readLine();
      }
      map.close();
      }
        catch(Exception e) {
      e.printStackTrace();
          System.out.println("Error in file reading. Exitting!!! "+e);
      System.exit(1);
      }
    }
 
    public static Node getNode(int i) {
      if(i<0 || i>=nodes.length) return null;
      return nodes[i];
    }
   
    public static int nodesCount() {
      return nodes.length;
    }
 
    private static int baseDistance(Node x,Node y)
    {
        if(x.equals(y)) return 0;
        int dist=INF;
        Link []lk=x.getLinks();
        for(int i=0;i<lk.length;i++) {
        Node n=lk[i].getToNode();
        if(n.equals(y)) dist=1;
    }
        return dist;
    }
   
    private static int[][] getShortestDistanceMatrix(int[][] mat,int k)
    {
      int noOfNodes = BoardModel.nodesCount();
        if (k==noOfNodes-1) return mat;
        int newMat[][]=new int[noOfNodes][noOfNodes];
        {
            for(int i=0;i<noOfNodes;i++)
                for(int j=0;j<noOfNodes;j++) {
                    newMat[i][j]=Math.min(mat[i][j],mat[i][k]+mat[k][j]);
                }
        }
        k=k+1;
        return getShortestDistanceMatrix(newMat,k);
    }

    public static int getDistance(int from, int to) {
      if(from<0 || to<0 || from>nodesCount()-1 || to >nodesCount()-1) return -1;
      return shortestDistance[from][to];
    }
   
    public static void test()
    {
      int noOfNodes = BoardModel.nodesCount();
        for(int i=0;i<noOfNodes;i++) {
          StringBuilder buffer = new StringBuilder();
          buffer.append("Node"+i).append(";");
            for(int j=0;j<noOfNodes;j++) {
              buffer.append(shortestDistance[i][j]).append(";");
            }
            System.out.println(buffer);
        }
    }
}
TOP

Related Classes of de.axxeed.animosy.model.BoardModel

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.