0

我正在为家庭作业创建国际象棋游戏,但在创建网格时遇到了一些问题。我四处寻找,但一直找不到我特别要找的东西。

我已经成功地创建了我的棋盘和棋子,但是我需要用两个边框标记网格:一条水平的文本线(A 到 H)和一条垂直的线(8 到 1)。虽然我已经能够放置水平边框,但我无法让垂直边框与棋盘的适当行对齐。我会上传一张图片,但我目前确实有足够的声望点。请让我知道这是否有帮助,我可以解决一些问题。

正如您将在代码中看到的那样,我使用 2D 数组创建了网格,并通过枚举类分配了片段。我正在考虑在枚举类中添加边界值,然后将它们放在为棋子分配位置的循环中,但是我不认为这是我的讲师想要的。

任何建议或意见将不胜感激。

public class VirtualChess {

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){ //A method to print out chessboard
    System.out.println("\tA\tB\tC\tD\tE\tF\tG\tH"); //This the horizontal border of the grid
    System.out.println("1\n2\n3\n4\n5\n6\n7\n8"); //This the vertical border of the grid. This is faulty.
    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("\u2654" + "\t"); break; //This places the Unicode characters of the chess pieces
            case WHITE_QUEEN: System.out.print("\u2655"+ "\t"); break;
            case WHITE_ROOK: System.out.print("\u2656" + "\t"); break;
            case WHITE_BISHOP:  System.out.print("\u2657" + "\t"); break;
            case WHITE_KNIGHT: System.out.print("\u2658" + "\t"); break;
            case WHITE_PAWN: System.out.print("\u2659" + "\t"); break;
            case BLACK_KING: System.out.print("\u265A" + "\t"); break;
            case BLACK_QUEEN: System.out.print("\u265B" + "\t"); break;
            case BLACK_ROOK:  System.out.print("\u265C" + "\t"); break;
            case BLACK_BISHOP: System.out.print("\u265D" + "\t"); break;
            case BLACK_KNIGHT:  System.out.print("\u265E"+ "\t"); break;
            case BLACK_PAWN:  System.out.print("\u265F" + "\t"); break;
            default: System.out.print(" " + "\t"); break;         
            }
       }    

            System.out.println("");

    }
}

public static void main(String[] args) {

    Chessmen[][] chessboard = new Chessmen[8][8];

    for(int i = 0; i < 8; i ++){ //Making the 8 x 8 chessboard
        for (int j = 0; j < 8; j ++){
            if ( (i >= 2 && i <= 5) && (j <= 7)){
                chessboard [i] [j] = Chessmen.EMPTY;        //Assigning the chess pieces their starting positions
            } else {

            chessboard [0] [0] = Chessmen.BLACK_ROOK;
            chessboard [0] [7] = Chessmen.BLACK_ROOK;
            chessboard [7] [0] = Chessmen.WHITE_ROOK;
            chessboard [7] [7] = Chessmen.WHITE_ROOK;

            chessboard [0] [1] = Chessmen.BLACK_KNIGHT;
            chessboard [0] [6] = Chessmen.BLACK_KNIGHT;
            chessboard [7] [1] = Chessmen.WHITE_KNIGHT;
            chessboard [7] [6] = Chessmen.WHITE_KNIGHT;

            chessboard [0] [2] = Chessmen.BLACK_BISHOP;
            chessboard [0] [5] = Chessmen.BLACK_BISHOP;
            chessboard [7] [2] = Chessmen.WHITE_BISHOP;
            chessboard [7] [5] = Chessmen.WHITE_BISHOP;

            chessboard [0] [3] = Chessmen.BLACK_QUEEN;
            chessboard [7] [3] = Chessmen.WHITE_QUEEN;

            chessboard [0] [4] = Chessmen.BLACK_KING;
            chessboard [7] [4] = Chessmen.WHITE_KING;

            chessboard [1] [0] = Chessmen.BLACK_PAWN;
            chessboard [1] [1] = Chessmen.BLACK_PAWN;
            chessboard [1] [2] = Chessmen.BLACK_PAWN;
            chessboard [1] [3] = Chessmen.BLACK_PAWN;
            chessboard [1] [4] = Chessmen.BLACK_PAWN;
            chessboard [1] [5] = Chessmen.BLACK_PAWN;
            chessboard [1] [6] = Chessmen.BLACK_PAWN;
            chessboard [1] [7] = Chessmen.BLACK_PAWN;

            chessboard [6] [0] = Chessmen.WHITE_PAWN;
            chessboard [6] [1] = Chessmen.WHITE_PAWN;
            chessboard [6] [2] = Chessmen.WHITE_PAWN;
            chessboard [6] [3] = Chessmen.WHITE_PAWN;
            chessboard [6] [4] = Chessmen.WHITE_PAWN;
            chessboard [6] [5] = Chessmen.WHITE_PAWN;
            chessboard [6] [6] = Chessmen.WHITE_PAWN;
            chessboard [6] [7] = Chessmen.WHITE_PAWN;
            }
        }
    } 
    printBoard(chessboard);
}

}

4

1 回答 1

0

我正在做同样的任务,我发现的标签解决方案是:

    System.out.println("\tA\tB\tC\tD\tE\tF\tG\tH");

      for(int i = 0; i < chessboard.length; i ++){ 

            System.out.print((i+1) + "\t");

      for (int j = 0; j < chessboard[0].length; j ++){ 
于 2013-10-13T16:11:09.027 回答