tuamigo

Untitled

Oct 26th, 2012
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.18 KB | None | 0 0
  1. public String toString()
  2.   {
  3.     StringBuilder tostring = new StringBuilder();
  4.    
  5.     for (int j=7;j>=0;j--)
  6.     {
  7.       StringBuilder auxsb = new StringBuilder();
  8.       StringBuilder auxsb2 = new StringBuilder();
  9.      
  10.       for (int i=7;i>=0;i--)
  11.       {
  12.         if (tablero[i][j]!=null)
  13.         {
  14.         if (tablero[i][j].getTipo()==Tipo.TORRE && tablero[i][j].getColor()==Color.BLANCO) {tostring.append("T");}
  15.         else if (tablero[i][j].getTipo()==Tipo.TORRE && tablero[i][j].getColor()==Color.NEGRO) {tostring.append("t");}
  16.         else if (tablero[i][j].getTipo()==Tipo.CABALLO && tablero[i][j].getColor()==Color.BLANCO) {tostring.append("C");}
  17.         else if (tablero[i][j].getTipo()==Tipo.CABALLO && tablero[i][j].getColor()==Color.NEGRO) {tostring.append("c");}
  18.         else if (tablero[i][j].getTipo()==Tipo.ALFIL && tablero[i][j].getColor()==Color.BLANCO) {tostring.append("A");}
  19.         else if (tablero[i][j].getTipo()==Tipo.ALFIL && tablero[i][j].getColor()==Color.NEGRO) {tostring.append("a");}
  20.         else if (tablero[i][j].getTipo()==Tipo.DAMA && tablero[i][j].getColor()==Color.BLANCO) {tostring.append("D");}
  21.         else if (tablero[i][j].getTipo()==Tipo.DAMA && tablero[i][j].getColor()==Color.NEGRO) {tostring.append("d");}
  22.         else if (tablero[i][j].getTipo()==Tipo.REY && tablero[i][j].getColor()==Color.BLANCO) {tostring.append("R");}
  23.         else if (tablero[i][j].getTipo()==Tipo.REY && tablero[i][j].getColor()==Color.NEGRO) {tostring.append("r");}
  24.         else if (tablero[i][j].getTipo()==Tipo.PEON && tablero[i][j].getColor()==Color.BLANCO) {tostring.append("P");}
  25.         else if (tablero[i][j].getTipo()==Tipo.PEON && tablero[i][j].getColor()==Color.NEGRO) {tostring.append("p");}
  26.         }
  27.         else
  28.           tostring.append(1); //añadimos 1 en las casillas nulas
  29.       }
  30.      
  31.  
  32.  
  33.       if (j<7) //invertimos cada fila del tablero
  34.       {
  35.         auxsb.append(tostring.substring(tostring.lastIndexOf("/"), tostring.length()));
  36.         tostring.insert(tostring.lastIndexOf("/")+1,auxsb.reverse().toString());
  37.         tostring.delete(tostring.lastIndexOf("/"), tostring.length());//borramos la fila no invertida      
  38.        // --
  39.         //reemplazamos los unos utilizando la función sumaUnos
  40.         auxsb2.append(tostring.substring(tostring.lastIndexOf("/")+1, tostring.length()));  
  41.         tostring.replace(tostring.lastIndexOf("/")+1,tostring.length(),sumaUnos(auxsb2,0).toString());
  42.       }
  43.        
  44.       else
  45.       {          
  46.         tostring.replace(0, tostring.length(), sumaUnos(tostring,0).toString());//sumamos los unos de la primera fila
  47.         tostring.reverse();
  48.       }
  49.        
  50.       if (j>0)
  51.         tostring.append("/");
  52.     }
  53.  
  54.     return tostring.toString();
  55.   }
  56.  
  57.   StringBuilder sumaUnos(StringBuilder tostring, int cont)
  58.   {
  59.     StringBuilder auxsb2 = new StringBuilder();
  60.    
  61.     for(int i=0;i<8;i++)
  62.     {
  63.       if (tostring.charAt(i)=='1'){      
  64.         cont++;
  65.         if (i==7)
  66.           auxsb2.append(cont);
  67.       }
  68.       else{
  69.         if (cont>0){
  70.           auxsb2.append(cont);
  71.           cont=0;
  72.         }
  73.         auxsb2.append(tostring.charAt(i));
  74.       }
  75.     }
  76.     return auxsb2;
  77.   }
Advertisement
Add Comment
Please, Sign In to add comment