2011-10-05 103 views
1

我有點卡住試圖爲我的棋盤做一個邊框,從上到下運行8-1和從左到右的a-h。我不太清楚如何去做。建議和幫助非常感謝!乾杯:)Java棋盤邊框?

輸出是目前:

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

} 
} 
+0

你是說你的輸出需要包括對方之間的所有空方塊? –

回答

2

在外部渲染循環之前和之後調用System.out.println("+--------------------------------+")

在內部渲染循環之前和之後調用System.out.print("|")

你需要稍微調整一下。

+0

嘿,謝謝你的回覆,我的意思是一個包含數字和字母的邊框。所以我可以調用座標。 – Hay1990

0

[編輯] 在回答您的評論...

我想你想要的其實是一個10×10網格,你需要你的邏輯調整僅打印遊戲區AS內部的8×8 10x10並使用外部正方形(不包括角落)來打印您的字母和數字。 [END編輯]

您的默認值不是空的,它只是沒有。趕上你的交換機中空箱體和打印標籤,你會看到,而不是

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; 
     } 
+0

嘿,謝謝你的回覆,我的意思是一個包含數字和字母的邊框。所以我可以調用座標。 – Hay1990

+0

啊......好的,看看我上面的編輯。但是,如上所述,您仍然需要修復您的EMPTY情況。 –

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'或沒有它告訴你,如果一個白嘴鴉可以城堡。一列中的箭頭'^'表示所述列中的棋子可以被視爲「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; 
    } 

我希望這有助於。

mp

0

我在Java中爲N皇后問題做了這個。因此,如果您想要打印除皇后外的其他作品,請相應地修改您的代碼。如果你不熟悉這個問題,那麼把N個皇后放在一個N×N的棋盤上是一個挑戰,因爲沒有兩個皇后相互檢查。我知道這是在你發佈這個問題後的幾年,但我想我會把代碼片段放在這裏供將來有人使用。

無論如何,第一步是創建我們的N×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]); 
} 

希望這有助於某人!