Java棋盘边框?

时间:2011-10-05 16:09:27

标签: java loops chess

我有点卡住试图为我的棋盘制作一个边框,从上到下运行8-1,从左到右运行a-h。我不太清楚该怎么做。建议和帮助非常感谢!干杯:)

目前的输出是:

BR BKn BB BK BQ BB BKn BR
BP 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);

}
}

4 个答案:

答案 0 :(得分:2)

在外部渲染循环之前和之后调用System.out.println("+--------------------------------+")

在内部渲染循环之前和之后调用System.out.print("|")

你需要调整一下。

答案 1 :(得分:2)

正如其他人所说,你需要在一条线上显示空行和空方块。此外,你需要为每个方块打印相同数量的字符,所以我建议你使用BN进行黑夜,将BK用于黑人国王。 N用来表示代数国际象棋符号中的骑士,以区别于国王。

由于我已经在C ++中解决了这个问题,我将在下面发布我的代码。您可以将其用作算法来在Java中进行翻译。它将显示如下板:

  (Coups reversibles : 0)
  +-X-+---+---+---+---+---+---+-X-+
8 |=R=|=N=|=B=|=Q=|=K=|=B=|=N=|=R=|
  +---+---+---+---+---+---+---+---+
7 |=P=|=P=|=P=|=P=|=P=|=P=|=P=|=P=|
  +---+---+---+---+---+---+---+---+
6 |   | . |   | . |   | . |   | . |
  +---+---+---+---+---+---+---+---+
5 | . |   | . |   | . |   | . |   |
  +---+---+---+---+---+---+---+---+
4 |   | . |   | . |   | . |   | . |
  +---+---+---+---+---+---+---+---+
3 | . |   | . |   | . |   | . |   |
  +---+---+---+---+---+---+---+---+
2 | P | P | P | P | P | P | P | P |
  +---+---+---+---+---+---+---+---+
1 | R | N | B | Q | K | B | N | R |
=>+-X-+---+---+---+---+---+---+-X-+
    a   b   c   d   e   f   g   h

它有,我认为一些interesings功能。没有棋子的黑色正方形在它们的中心有一个圆点。电路板底部或顶部的箭头告诉用户下一步要移动哪一侧。一个'X'或没有它告诉你车是否可以城堡。列后面的箭头'^'表示所述列中的pawn可以被称为“en passant”。

所以这里是代码(可能有错误,我只是翻译了法语whitout的标识符和注释再次编译):

///////////////////////////////////////////////////////////////////////////
   std::ostream& operator << (std::ostream& out, const CPlanche& planche)
   {
      static const string piecesNames[] = {"   ","   ",
                                            " P ","=P=",
                                            " N ","=N=",
                                            " K ","=K=",
                                            "   ","   ",
                                            " B ","=B=",
                                            " R ","=R=",
                                            " Q ","=Q="};

      // We display how many revirsable moves have been playes
      out <<"  (Coups reversibles : " <<board.RecersiblesMoves() <<")\n";

      // If it's black to move we display an arrow
      if (board.ColorToMove() == BLACK)
      {
         out <<"=>";
      }
      else
      {
         out <<"  ";
      }

      // We display the top line
      out <<"+-" <<(Board.Castling(Black, QueenSideCastling)?'X':'-') <<"-+---+---+---+---+---+---+-" <<(board.Castling(Black, LingSideCastling)?'X':'-') <<"-+\n";

      // We display all 8 lines
      for (int line = 0; line < 8; line++)
      {
        out <<(char)('8' - line) <<' ';

        // for each column
        for (int column = 0; column < 8; column++)
        {
          out <<'|';

          if (board[56 - 8 * line + column] != EMPTY)
          {
             out <<piecesNames[board[56 - 8 * iLigne + column]];
          }
          else
          {
            // If both the line and column are even OR if both are odds
            if (!((line | column) & 1) || (line & column & 1))
            {
               out <<"   ";
            }
            else
            {
               out <<" . ";
            }
          }
        }

        out <<"|\n";

        if (line != 7)
        {
          out <<"  +---+---+---+---+---+---+---+---+\n";
        }
      }

      // If it's white to move we display an arrow
      if (board.ColorToMove() == WHITE)
      {
         out <<"=>";
      }
      else
      {
         out <<"  ";
      }

      // We display the bottom line
      out <<"+-" <<(planche.Castling(WHITE, CastlingQueenSide)?'X':'-') <<"-+---+---+---+---+---+---+-" <<(Board.Castling(WHITE, CastlingKingSide)?'X':'-') <<"-+\n";

      // Whe display the arrow for the prise en passant if there is one.
      if (board.ColumnPriseEnPassant() != NO_PRISE_EN_PASSANT)
      {
         for (int i = 0; i < (board.ColumnPriseEnPassant() + 1); i++)
         {
            out <<"    ";
         }
         out <<"^\n";
      }

      // We display the columns letters
      out <<"    a   b   c   d   e   f   g   h\n";

      return out;
   }

我希望这会有所帮助。

熔点

答案 2 :(得分:0)

[编辑] 回应你的评论...

我认为你想要的实际上是一个10x10网格,你需要调整你的逻辑,只在10x10内打印游戏区域为8x8,并使用外部方块(不包括角落)打印你的字母和数字。 [结束编辑]

你的默认值不是EMPTY它只是什么都没有。在您的交换机中捕获EMPTY案例并打印您的标签,您将看到所有空白区域

换句话说,而不是

case BLACK_PAWN:
            System.out.print("BP\t");
            break;
        default:
            System.out.print(" " + "\t");
            break;
        }

你想做的事:

case BLACK_PAWN:
            System.out.print("BP\t");
            break;
        case EMPTY:
            System.out.print(" " + "\t");
            break;
        }

答案 3 :(得分:0)

我为Java中的N皇后问题做了这个。所以,如果你试图打印除了女王之外的其他作品,那么相应地定制你的代码。如果您不熟悉这个问题,那么将N个皇后放置在N×N棋盘上是一项挑战,这样就不会有两个皇后互相检查。我知道这是你发布问题后的几年,但我想我会把代码片段放在这里供将来使用的人。

无论如何,第一步是创建我们的N x N数组,表示棋盘上的空格以及当前在棋盘上的棋子的价值。值得注意的是,由于这是Java,我们的电路板是基于0的(因为每行中的空格编号为0到7,而不是1到8):

public static final int N = 8;
public static char [][] board = new char[N][N];
//initialize every space to a space
for(int i = 0; i < N; i++){
    for(int j = 0; j < N; j++){
        board[i][j] = ' ';
    }
}

看到我们将每个点初始化为一个空格,这样我们就可以迭代我对于皇后位置的向量,并且只改变女王需要放置的位置。

以下是解决方案的示例向量:[1 6 2 5 7 4 0 3]。假设这个向量由int[] vector = new int[N]表示。元素的数量等于板中的行数。它的向量中的索引对应于行,并且值本身对应于后者中行后的列号。所以我们只需循环遍历这些并相应地更新电路板阵列:

for(int j = 0; j < N; j++){
    board[j][vector[j]] = 'Q';
}

如果您的问题涉及实际的国际象棋游戏,您需要遍历每一个棋子并以类似的方式更新其棋盘位置。

在我解释如何制作这个电路板之前,让我向您展示与前面提到的矢量相对应的电路板。另外,请注意没有皇后检查!我用遗传算法解决了这个问题。

+---+---+---+---+---+---+---+---+
|   | Q |   |   |   |   |   |   | 
+---+---+---+---+---+---+---+---+
|   |   |   |   |   |   | Q |   | 
+---+---+---+---+---+---+---+---+
|   |   | Q |   |   |   |   |   | 
+---+---+---+---+---+---+---+---+
|   |   |   |   |   | Q |   |   | 
+---+---+---+---+---+---+---+---+
|   |   |   |   |   |   |   | Q | 
+---+---+---+---+---+---+---+---+
|   |   |   |   | Q |   |   |   | 
+---+---+---+---+---+---+---+---+
| Q |   |   |   |   |   |   |   | 
+---+---+---+---+---+---+---+---+
|   |   |   | Q |   |   |   |   | 
+---+---+---+---+---+---+---+---+

我们的下一步是在行之间(以及在顶部和底部)创建边框。我将每一行视为格式化字符串。由于我们有空格行和边框行,因此我们的行数组大小为2 * N + 1。

String[] row = new String[2*N + 1];
String border = "";
//must be the length of board
for(int i = 0; i < N; i++){
    border += "+---";
}
border += "+";
//every other row is a border row
for(int i = 0; i < 2*N +1; i+=2){
    row[i] = border;
}
//must include the bottom
row[2*N] = border;

接下来,我们为电路板空间自己创建字符串。由于我们已经在我们的板[] []中有这些,我们只需要格式化。

for(int i = 0; i < N; i++){
    //left bar
    String spaces = "| ";
    //place enclosing right bar and spaces so next char goes in middle of next space
    for(int j = 0; j < N; j++){
        spaces += board[i][j];
        spaces += " | ";
    }
    //add the spaces string to the rows at the odd indices
    row[2*k +1 ] = spaces;

}

剩下的就是打印那块板子。

for(int i = 0; i < 2*N +1; i++){
    System.out.println(row[i]);
}

希望这有助于某人!