Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import MyTools.MyTools.*;
- public class Strada {
- final static double PROB_AUTO = 0.1;
- final static double PROB_PEDONE = 0.1;
- /** MENU STYLING **/
- final static char SEPARATORE = '|';
- final static char CORNICE = '-';
- final char SPAZIO = ' ';
- final static int LARG_CELLA = 4;
- /** END MENU STYLING **/
- final static String MSG_COLLISIONE = "Attenzione! Collisione in posizione :";
- private int numRighe;
- private int numColonne;
- private Elemento[][] mappa;
- public Strada(int _numRighe, int _numColonne){
- this.numRighe = _numRighe;
- this.numColonne = _numColonne;
- this.mappa = new Elemento [_numRighe][_numColonne];
- }
- private Elemento oggettoCasuale(){
- int casuale = MyTools.MyMath.getRandom(9);
- switch (casuale){
- case 1:
- return new Auto();
- case 2:
- return new Pedone();
- default:
- return new Vuoto();
- }
- }
- public void inizializza(){
- for(int x = 0; x < mappa.length; x++){
- for(int y = 0; y < mappa[x].length; y++ ){
- mappa[x][y] = oggettoCasuale();
- }
- }
- }
- public void posizionaElemento(Elemento daPos, Coordinate posiz, Elemento[][] newmap){
- newmap[posiz.getRiga()][posiz.getColonna()] = daPos;
- }
- public Elemento[][] emptyMap(int nRig, int nCol){
- Elemento[][] newMap = new Elemento[nRig][nCol];
- for(int x = 0; x < newMap.length; x++){
- for(int y = 0; y < newMap[x].length; y++ ){
- newMap[x][y] = new Vuoto();
- }
- }
- return newMap;
- }
- public void evoluzione(){
- Elemento[][] nextMap = emptyMap(this.mappa.length, this.mappa[0].length);
- for(int x = 0; x < mappa.length; x++){
- for(int y = 0; y < mappa[x].length; y++ ){
- Elemento esaminato = mappa[x][y];
- Coordinate nextPosition = esaminato.prossimaPosizione(x, y);
- if(nextPosition.appartiene(mappa.length, mappa[0].length)){
- Elemento altro = nextMap[nextPosition.getRiga()][nextPosition.getColonna()];
- if(esaminato.collisione(altro)){
- System.out.println(MSG_COLLISIONE + nextPosition.toString());
- }
- if(esaminato.prevale(altro)){
- this.posizionaElemento(esaminato, nextPosition, nextMap);
- }
- }
- }
- }
- mappa = nextMap;
- }
- public String toString(){
- StringBuffer descrizione = new StringBuffer();
- String cornice = MyTools.MyTools.ripetiChar(CORNICE,LARG_CELLA*(mappa[0].length+1));
- String rientro = MyTools.MyTools.ripetiChar(SPAZIO,LARG_CELLA);
- descrizione.append(rientro); // SPAZI INIZIALI SULLA RIGA DEI NUMERI COLONNA
- for (int i = 1; i <= mappa[0].length; i++) // RIGA CON I NUMERI DI COLONNA
- descrizione.append(MyTools.MyTools.centrata(Integer.toString(i),LARG_CELLA));
- descrizione.append(MyTools.MyTools.rigaIsolata(cornice));
- for ( int x = 0; x < mappa.length; ++x )
- {
- descrizione.append(MyTools.MyTools.centrata(Integer.toString(x+1),LARG_CELLA)); // NUMERO DI RIGA
- for ( int y = 0; y < mappa[x].length; ++y )
- {
- descrizione.append(MyTools.MyTools.centrata(mappa[x][y].toString(),LARG_CELLA));
- }
- descrizione.append(MyTools.MyTools.rigaIsolata(cornice));
- }
- return descrizione.toString();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement