チェス盤の境界線を作ろうとして、上から下に 8-1、左から右に走るのにちょっと行き詰まりました。それを行う方法がよくわかりません。アドバイスとヘルプは大歓迎です!乾杯 :)
現在の出力は次のとおりです。
BR BKn BB BK BQ BB BKn BR
BP BP BP BP BP BP BP
WP WP WP WP WP WP WP WP
WR WKn WB WK WQ WB WKn WR
以下は Java コードです。
public class ex5 {
public enum Chessmen {
WHITE_KING,
WHITE_QUEEN,
WHITE_ROOK,
WHITE_BISHOP,
WHITE_KNIGHT,
WHITE_PAWN,
BLACK_KING,
BLACK_QUEEN,
BLACK_ROOK,
BLACK_BISHOP,
BLACK_KNIGHT,
BLACK_PAWN,
EMPTY
}
public static void printBoard (Chessmen [][] chessboard){
for (int i=0; i<chessboard.length;i++){
for (int j = 0; j<chessboard.length;j++){
switch (chessboard [i][j]){
case WHITE_KING:
System.out.print("WK\t");
break;
case WHITE_QUEEN:
System.out.print("WQ\t");
break;
case WHITE_ROOK:
System.out.print("WR\t");
break;
case WHITE_BISHOP:
System.out.print("WB\t");
break;
case WHITE_KNIGHT:
System.out.print("WKn\t");
break;
case WHITE_PAWN:
System.out.print("WP\t");
break;
case BLACK_KING:
System.out.print("BK\t");
break;
case BLACK_QUEEN:
System.out.print("BQ\t");
break;
case BLACK_ROOK:
System.out.print("BR\t");
break;
case BLACK_BISHOP:
System.out.print("BB\t");
break;
case BLACK_KNIGHT:
System.out.print("BKn\t");
break;
case BLACK_PAWN:
System.out.print("BP\t");
break;
default:
System.out.print(" " + "\t");
break;
}
}
System.out.println("");
}
}
public static void main(String[] args) {
int Rows = 8;
int Columns = 8;
Chessmen [][] chessboard = new Chessmen [Rows][Columns];
for (int i=0;i<chessboard.length; i++){
for (int j=0;j<chessboard[0].length;j++){
if (i==1){
chessboard [i][j]= Chessmen.BLACK_PAWN;
}else if (i==6){
chessboard [i][j]= Chessmen.WHITE_PAWN;
}else if ((i==0&&j==7)||(i==0&&j==0)){
chessboard [i][j]= Chessmen.BLACK_ROOK;
}else if ((i==0&&j==1)||(i==0&&j==6)){
chessboard [i][j] = Chessmen.BLACK_KNIGHT;
}else if ((i==0&&j==2)||(i==0&&j==5)){
chessboard [i][j] = Chessmen.BLACK_BISHOP;
}else if (i==0&&j==3){
chessboard [i][j] = Chessmen.BLACK_KING;
}else if (i==0&&j==4){
chessboard [i][j] = Chessmen.BLACK_QUEEN;
}else if ((i==7&&j==0)||(i==7&&j==7)){
chessboard [i][j]= Chessmen.WHITE_ROOK;
}else if ((i==7&&j==1)||(i==7&&j==6)){
chessboard [i][j] = Chessmen.WHITE_KNIGHT;
}else if ((i==7&&j==2)||(i==7&&j==5)){
chessboard [i][j] = Chessmen.WHITE_BISHOP;
}else if (i==7&&j==3){
chessboard [i][j] = Chessmen.WHITE_KING;
}else if (i==7&&j==4){
chessboard [i][j] = Chessmen.WHITE_QUEEN;
}else {
chessboard [i][j]= Chessmen.EMPTY;
}
}
}
printBoard (chessboard);
}
}