Advertisement
maha_kaal

strada

Jul 12th, 2012
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.10 KB | None | 0 0
  1. import MyTools.MyTools.*;
  2. public class Strada {
  3.     final static double PROB_AUTO = 0.1;
  4.     final static double PROB_PEDONE = 0.1;
  5.     /** MENU STYLING **/
  6.     final static char SEPARATORE = '|';
  7.     final static char CORNICE = '-';
  8.     final char SPAZIO = ' ';
  9.     final static int LARG_CELLA = 4;
  10.     /** END MENU STYLING **/
  11.    
  12.     final static String MSG_COLLISIONE = "Attenzione! Collisione in posizione :";
  13.    
  14.     private int numRighe;
  15.     private int numColonne;
  16.     private Elemento[][] mappa;
  17.    
  18.     public Strada(int _numRighe, int _numColonne){
  19.         this.numRighe = _numRighe;
  20.         this.numColonne = _numColonne;
  21.         this.mappa = new Elemento [_numRighe][_numColonne];
  22.     }
  23.    
  24.     private Elemento oggettoCasuale(){
  25.         int casuale = MyTools.MyMath.getRandom(9);
  26.         switch (casuale){
  27.         case 1:
  28.             return new Auto();
  29.         case 2:
  30.             return new Pedone();
  31.         default:
  32.             return new Vuoto();
  33.         }
  34.     }
  35.    
  36.     public void inizializza(){
  37.         for(int x = 0; x < mappa.length; x++){
  38.             for(int y = 0; y < mappa[x].length; y++ ){
  39.                 mappa[x][y] = oggettoCasuale();
  40.             }
  41.         }
  42.     }
  43.    
  44.     public void posizionaElemento(Elemento daPos, Coordinate posiz, Elemento[][] newmap){
  45.         newmap[posiz.getRiga()][posiz.getColonna()] = daPos;
  46.     }
  47.    
  48.     public Elemento[][] emptyMap(int nRig, int nCol){
  49.         Elemento[][] newMap = new Elemento[nRig][nCol];
  50.         for(int x = 0; x < newMap.length; x++){
  51.             for(int y = 0; y < newMap[x].length; y++ ){
  52.                 newMap[x][y] = new Vuoto();
  53.             }
  54.         }
  55.        
  56.         return newMap;
  57.     }
  58.    
  59.     public void evoluzione(){
  60.         Elemento[][] nextMap = emptyMap(this.mappa.length, this.mappa[0].length);
  61.         for(int x = 0; x < mappa.length; x++){
  62.             for(int y = 0; y < mappa[x].length; y++ ){
  63.                 Elemento esaminato = mappa[x][y];
  64.                 Coordinate nextPosition = esaminato.prossimaPosizione(x, y);
  65.                 if(nextPosition.appartiene(mappa.length, mappa[0].length)){
  66.                     Elemento altro = nextMap[nextPosition.getRiga()][nextPosition.getColonna()];
  67.                     if(esaminato.collisione(altro)){
  68.                         System.out.println(MSG_COLLISIONE + nextPosition.toString());
  69.                     }
  70.                     if(esaminato.prevale(altro)){
  71.                         this.posizionaElemento(esaminato, nextPosition, nextMap);
  72.                     }
  73.                 }
  74.             }
  75.         }
  76.  
  77.      mappa = nextMap;
  78.     }
  79.    
  80.     public String toString(){
  81.         StringBuffer descrizione = new StringBuffer();
  82.         String cornice = MyTools.MyTools.ripetiChar(CORNICE,LARG_CELLA*(mappa[0].length+1));
  83.         String rientro = MyTools.MyTools.ripetiChar(SPAZIO,LARG_CELLA);
  84.        
  85.         descrizione.append(rientro); // SPAZI INIZIALI SULLA RIGA DEI NUMERI COLONNA
  86.        
  87.         for (int i = 1; i <= mappa[0].length; i++) // RIGA CON I NUMERI DI COLONNA
  88.          descrizione.append(MyTools.MyTools.centrata(Integer.toString(i),LARG_CELLA));
  89.            
  90.         descrizione.append(MyTools.MyTools.rigaIsolata(cornice));
  91.        
  92.         for ( int x = 0; x < mappa.length; ++x )
  93.          {
  94.             descrizione.append(MyTools.MyTools.centrata(Integer.toString(x+1),LARG_CELLA)); // NUMERO DI RIGA
  95.            
  96.             for ( int y = 0; y < mappa[x].length; ++y )
  97.              {
  98.                 descrizione.append(MyTools.MyTools.centrata(mappa[x][y].toString(),LARG_CELLA));
  99.              }
  100.            
  101.             descrizione.append(MyTools.MyTools.rigaIsolata(cornice));    
  102.          }
  103.         return descrizione.toString();
  104.     }  
  105.  
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement