public class ChessBoard {
int[][] Board;
public ChessBoard() {
Board = new int[8][8];
for(int i=0; i<8; i++)
for(int j=0; j<8; j++)
Board[i][j]='E';//Empty
}
public void KnightMove(int x, int y) {
if(x>-1 && x<8 && y>-1 && y<8)
Board[x][y]='K';//Knight
else {
System.out.println("Error: Not valid Points");
return;
}
int a=x+2;int b=y+1;
if(a>-1 && a<8 && b>-1 && b<8)
Board[a][b]='T';//Threten
System.out.println(a+" "+b);
a=x+2;b=y-1;
if(a>-1 && a<8 && b>-1 && b<8)
Board[a][b]='T';//Threten
a=x-2;b=y+1;
if(a>-1 && a<8 && b>-1 && b<8)
Board[a][b]='T';//Threten
a=x-2;b=y-1;
if(a>-1 && a<8 && b>-1 && b<8)
Board[a][b]='T';//Threten
a=x+1;b=y+2;
if(a>-1 && a<8 && b>-1 && b<8)
Board[a][b]='T';//Threten
a=x-1;b=y+2;
if(a>-1 && a<8 && b>-1 && b<8)
Board[a][b]='T';//Threten
a=x+1;b=y-2;
if(a>-1 && a<8 && b>-1 && b<8)
Board[a][b]='T';//Threten
a=x-1;b=y-2;
if(a>-1 && a<8 && b>-1 && b<8)
Board[a][b]='T';//Threten
printBoard();
}
public void printBoard() {
for(int i=0; i<8; i++) {
StringBuffer sb = new StringBuffer();
for(int j=0; j<8; j++)
sb.append((char)Board[i][j]).append(", ");
System.out.println(sb.toString());
}
}
}
No comments:
Post a Comment