Skip to content

Instantly share code, notes, and snippets.

@Roberto24p
Last active November 12, 2019 02:33
Show Gist options
  • Save Roberto24p/ab611fc1306ca425ade330bc7578d3a3 to your computer and use it in GitHub Desktop.
Save Roberto24p/ab611fc1306ca425ade330bc7578d3a3 to your computer and use it in GitHub Desktop.
Un código relativamente sencillo que te pide las coordenas en X y en Y. Muestra por pantalla los posibles movimiento de la pieza caballo del juego ajedrez, se resolvio de 2 modos, iterativo y recursivo. Cualquier recomendación o bug, me serviria para mejorar.
import java.io.*;
public class piezaCa{
private int movX;
private int movY;
private String cadena;
private String cadenaR;
public piezaCa(int movX, int movY){
this.movX = movX;
this.movY = movY;
}
public int getMovx()
{return movX;}
public void setMovx(int movX)
{this.movX = movX;}
public int getMovy()
{return movY;}
public void setMovy(int movY)
{this.movY = movY;}
public String Movimiento()
{
int i = 0,j=1;
int x = 0,y = 0;
cadena = "";
while(i<8)
{
x=movX;
y=movY;
switch(i){
case 0: x= x-1;
y = y-2;
break;
case 1: x = x+1;
y = y-2;
break;
case 2: x = x+2;
y = y-1;
break;
case 3: x = x+2;
y = y+1;
break;
case 4: x = x+1;
y = y+2;
break;
case 5: x = x-1;
y = y+2;
break;
case 6: x = x-2;
y = y+1;
break;
case 7: x = x-2;
y = y-1;
break;
}
if((x>0&&x<9)&&(y>0&&y<9))
{
cadena = cadena+"\n"+j+")"+" X->"+x+" y->"+y+"\n";
j++;
}
i++;
}
return cadena;
}
public String reMovimiento(int i,int j)
{
int x = movX;
cadenaR = "";
int y = movY;
if (i<8)
{
switch(i)
{
case 0: x = x-1;
y = y-2;
break;
case 1: x = x+1;
y = y-2;
break;
case 2: x = x+2;
y = y-1;
break;
case 3: x = x+2;
y = y+1;
break;
case 4: x = x+1;
y = y+2;
break;
case 5: x = x-1;
y = y+2;
break;
case 6: x = x-2;
y = y+1;
break;
case 7: x = x-2;
y = y-1;
break;
}
if((x>0&&x<9)&&(y>0&&y<9))
{
cadenaR = "\n"+j+")"+" X->"+x+" y->"+y+"\n";
j=j+1;
i=i+1;
return cadenaR+reMovimiento(i,j);
}else
{
i=i+1;
return reMovimiento(i,j);
}
}else
return "";
}
}
import java.io.*;
public class program{
public static void main(String arg[])
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
piezaCa caballo; //Objeto caballo de tipo piezaCa;
int i=0,j=1, op = 0, val = 0, rep = 1; /*i = iterador que ayuda en la recursividad,j = ayuda a enumerar
tanto en la recursividad como en la resolución iterativa, op = opciones del menu, val = ayuda a reiniciar el programa,
rep = valida la entrada de las coordennadas*/
int x = 0, y = 0; //x, y = coordenadas ingresadas por teclado
try{
do{
do{
System.out.println("-----Menu de opciones-----");
System.out.println("1-Resolucion iterativa");
System.out.println("2-Resolucion recursiva");
op = Integer.parseInt(br.readLine());
}while(op<0||op>2);
do{
rep = 1;
System.out.println("Ingresa el punto en X");
x = Integer.parseInt(br.readLine());
System.out.println("Ingrese el punto en Y");
y = Integer.parseInt(br.readLine());
if((x>8||x<1)||(y>8||y<1))
{
System.out.println("----Ingrese coordenadas validas----");
rep = 0;
}
}while(rep==0);
caballo = new piezaCa(x,y);
switch(op){
case 1: System.out.println("---------------Resolucion iterativa---------\n");
System.out.println(caballo.Movimiento());
break;
case 2: System.out.println("---------------Resolucion Recursivo---------\n");
System.out.println(caballo.reMovimiento(i,j));
break;
}
System.out.println("Ingresar nuevas coordenadas: \n1-Si\n2-No");
val = Integer.parseInt(br.readLine());
}while(val==1);
}catch(Exception e)
{
System.out.println("Error: "+e.toString());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment