1

我有点坚持尝试为我的棋盘做一个边框,从上到下 8-1 从左到右运行啊。我不太确定该怎么做。非常感谢您的建议和帮助!干杯:)

目前的输出是:

BR BKn BB BK BQ BB BKn BR
BP BP BP BP BP BP BP BP

WP 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

4 回答 4

2

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

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

你需要稍微调整一下。

于 2011-10-05T16:16:30.990 回答
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

我认为它有一些有趣的功能。没有棋子的黑色方块的中心有一个点。棋盘底部或顶部的箭头告诉用户接下来要移动哪一边。一个“X”或没有它会告诉你一个车是否可以城堡。列下方的箭头“^”表示该列中的棋子可以“顺路”。

所以这里是代码(可能有错误,我只是翻译了法语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;
   }

我希望这有帮助。

mp

于 2011-10-07T11:57:49.110 回答
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;
        }
于 2011-10-05T16:21:11.680 回答
0

我为 Java 中的 N 个皇后问题做了这个。因此,如果您尝试打印除女王之外的其他作品,请相应地定制您的代码。如果你对这个问题不熟悉,那就是在 N x 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;

接下来,我们为棋盘空间本身创建字符串。由于我们的 board[][] 中已经有了这些,我们只需要格式化。

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]);
}

希望这对某人有帮助!

于 2014-03-20T01:09:55.953 回答