Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public String toString()
- {
- StringBuilder tostring = new StringBuilder();
- for (int j=7;j>=0;j--)
- {
- StringBuilder auxsb = new StringBuilder();
- StringBuilder auxsb2 = new StringBuilder();
- for (int i=7;i>=0;i--)
- {
- if (tablero[i][j]!=null)
- {
- if (tablero[i][j].getTipo()==Tipo.TORRE && tablero[i][j].getColor()==Color.BLANCO) {tostring.append("T");}
- else if (tablero[i][j].getTipo()==Tipo.TORRE && tablero[i][j].getColor()==Color.NEGRO) {tostring.append("t");}
- else if (tablero[i][j].getTipo()==Tipo.CABALLO && tablero[i][j].getColor()==Color.BLANCO) {tostring.append("C");}
- else if (tablero[i][j].getTipo()==Tipo.CABALLO && tablero[i][j].getColor()==Color.NEGRO) {tostring.append("c");}
- else if (tablero[i][j].getTipo()==Tipo.ALFIL && tablero[i][j].getColor()==Color.BLANCO) {tostring.append("A");}
- else if (tablero[i][j].getTipo()==Tipo.ALFIL && tablero[i][j].getColor()==Color.NEGRO) {tostring.append("a");}
- else if (tablero[i][j].getTipo()==Tipo.DAMA && tablero[i][j].getColor()==Color.BLANCO) {tostring.append("D");}
- else if (tablero[i][j].getTipo()==Tipo.DAMA && tablero[i][j].getColor()==Color.NEGRO) {tostring.append("d");}
- else if (tablero[i][j].getTipo()==Tipo.REY && tablero[i][j].getColor()==Color.BLANCO) {tostring.append("R");}
- else if (tablero[i][j].getTipo()==Tipo.REY && tablero[i][j].getColor()==Color.NEGRO) {tostring.append("r");}
- else if (tablero[i][j].getTipo()==Tipo.PEON && tablero[i][j].getColor()==Color.BLANCO) {tostring.append("P");}
- else if (tablero[i][j].getTipo()==Tipo.PEON && tablero[i][j].getColor()==Color.NEGRO) {tostring.append("p");}
- }
- else
- tostring.append(1); //añadimos 1 en las casillas nulas
- }
- if (j<7) //invertimos cada fila del tablero
- {
- auxsb.append(tostring.substring(tostring.lastIndexOf("/"), tostring.length()));
- tostring.insert(tostring.lastIndexOf("/")+1,auxsb.reverse().toString());
- tostring.delete(tostring.lastIndexOf("/"), tostring.length());//borramos la fila no invertida
- // --
- //reemplazamos los unos utilizando la función sumaUnos
- auxsb2.append(tostring.substring(tostring.lastIndexOf("/")+1, tostring.length()));
- tostring.replace(tostring.lastIndexOf("/")+1,tostring.length(),sumaUnos(auxsb2,0).toString());
- }
- else
- {
- tostring.replace(0, tostring.length(), sumaUnos(tostring,0).toString());//sumamos los unos de la primera fila
- tostring.reverse();
- }
- if (j>0)
- tostring.append("/");
- }
- return tostring.toString();
- }
- StringBuilder sumaUnos(StringBuilder tostring, int cont)
- {
- StringBuilder auxsb2 = new StringBuilder();
- for(int i=0;i<8;i++)
- {
- if (tostring.charAt(i)=='1'){
- cont++;
- if (i==7)
- auxsb2.append(cont);
- }
- else{
- if (cont>0){
- auxsb2.append(cont);
- cont=0;
- }
- auxsb2.append(tostring.charAt(i));
- }
- }
- return auxsb2;
- }
Advertisement
Add Comment
Please, Sign In to add comment