Skip to content

Instantly share code, notes, and snippets.

@delucas
Created April 27, 2012 02:39
Show Gist options
  • Save delucas/2505253 to your computer and use it in GitHub Desktop.
Save delucas/2505253 to your computer and use it in GitHub Desktop.
Un pequeño ejemplo sobre cómo trabajar una matriz en Java (y su prueba correspondiente). En este caso, rotándola.
package ar.edu.untref.lp1.terreno;
public class Casa {
private boolean[][] plano;
private int orientacion;
public Casa(boolean[][] plano, int orientacion) {
this.plano = plano;
this.orientacion = orientacion;
}
public Casa girar(){
boolean[][] otroPlano = new boolean[this.plano[0].length][this.plano.length];
for (int i = 0; i < plano.length; i++){
for(int j = 0; j < plano[i].length; j++){
otroPlano[j][plano.length - 1 - i] = plano[i][j];
}
}
int nuevaOrientacion = (orientacion + 1) % 4;
return new Casa(otroPlano, nuevaOrientacion );
}
public boolean[][] getPlano() {
return plano;
}
public int getOrientacion() {
return orientacion;
}
}
package ar.edu.untref.lp1.terreno;
import org.junit.Assert;
import org.junit.Test;
public class CasaTests {
@Test
public void queLaCasaGireBien(){
boolean[][] plano = {{true, true, false},
{true, false, false},
{true, false, false}};
Casa casa = new Casa(plano, 0);
boolean[][] planoGirado = {{true, true, true},
{false, false, true},
{false, false, false}};
Casa casaGirada = casa.girar();
Assert.assertArrayEquals(planoGirado, casaGirada.getPlano());
Assert.assertEquals(1, casaGirada.getOrientacion());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment