Created
November 9, 2019 22:48
-
-
Save MrDave1999/9afb8a4cc15538e1996e32eaf3d6713c to your computer and use it in GitHub Desktop.
Programa que muestra los posibles movimientos que puede tener un caballo en la tabla de ajedrez.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Piece | |
{ | |
//Coordenada del posible movimiento de una pieza | |
private int movX; | |
private int movY; | |
//Coordenada actual donde estará una pieza | |
private int posX; | |
private int posY; | |
private String msgError = "La posicion debe estar en el rango del 1 al 8"; | |
public boolean setPosX(int posX) | |
{ | |
if(!(posX >= 1 && posX <= 8)) | |
{ | |
System.out.println(msgError); | |
return true; | |
} | |
this.posX = posX; | |
return false; | |
} | |
public boolean setPosY(int posY) | |
{ | |
if(!(posY >= 1 && posY <= 8)) | |
{ | |
System.out.println(msgError); | |
return true; | |
} | |
this.posY = posY; | |
return false; | |
} | |
private boolean Valid(int i) | |
{ | |
switch(i) | |
{ | |
case 0: | |
movX = posX - 1; | |
movY = posY - 2; | |
break; | |
case 1: | |
movX = posX + 1; | |
movY = posY - 2; | |
break; | |
case 2: | |
movX = posX + 2; | |
movY = posY - 1; | |
break; | |
case 3: | |
movX = posX + 2; | |
movY = posY + 1; | |
break; | |
case 4: | |
movX = posX + 1; | |
movY = posY + 2; | |
break; | |
case 5: | |
movX = posX - 1; | |
movY = posY + 2; | |
break; | |
case 6: | |
movX = posX - 2; | |
movY = posY + 1; | |
break; | |
case 7: | |
movX = posX - 2; | |
movY = posY - 1; | |
break; | |
default: break; | |
} | |
return ((movX > 0 && movX < 9) && (movY > 0 && movY < 9) ? false : true); | |
} | |
//Imprime los posibles movimiento del caballo (usando función iterativa) | |
public void PrintMovHorse() | |
{ | |
for(int i = 0; i != 8; ++i) | |
{ | |
if(!Valid(i)) | |
System.out.println("(" + "" + movX + "," + movY + ")"); | |
} | |
} | |
//Imprime los posibles movimiento del caballo (usando función recursiva) | |
public void PrintMovHorse(int i) | |
{ | |
if(i != 8) | |
{ | |
if(!Valid(i)) | |
System.out.println("(" + "" + movX + "," + movY + ")"); | |
PrintMovHorse(++i); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Program | |
{ | |
public static void main(String[] args) | |
{ | |
Menu.ShowMenu(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment