简体   繁体   中英

Java Unicode character widths

I am attempting to program a chess game in Java that will display entirely in the console. I am using Unicode characters for this such as White Chess Queen U+2655. My problem is that I cannot find any whitespace characters that match the width of the chess pieces so I can't display the board correctly. Take a look at my output below, I moved the pawn from h2 to h4:

8 ♜♞♝♛♚♝♞♜
7 ♟♟♟♟♟♟♟♟
6                 
5                 
4               ♙
3                 
2 ♙♙♙♙♙♙♙  
1 ♖♘♗♕♔♗♘♖
  a b c d e f g h 

Edit: the problem is easier to point out with the following example:

♙♙♙♙♙♙♙♙
xxxxxxxx
iiiiiiii
||||||||

You can see that the chess pieces are slightly larger in width than the other characters below the pawns.

Here on this website, the chess pieces have slightly more than the width of a standard character. In my console in Eclipse, the chess pieces have a width that is slightly less than 2 standard characters.

I suppose I am requesting one of three things:

  • a whitespace character that matches the width of the Unicode chess pieces.
  • a Java method that returns the width of a character or string
  • a Java method that can set the width of a character or string to any arbitrary value.

Thanks for any help or advice you can offer.

Edit: turns out it was the consolas font that was causing me problems. Consolas, displays chess pieces with a very strange width, about 1.8 characters wide. I switched the font of my console to a few different monospaced fonts until I found one that correctly sized the chess pieces. Is there any way to ensure specific widths, regardless of the font?

Terminals (almost) always use monospaced fonts, so the space character is the same width as the piece characters.

Your problem is that you're rendering the board with spaces between columns, but you're not inserting them between your pieces.

public class TextChess {
    public static void main(String[] args) {
        char[][] board = new char[][] {
            {'♜', '♞', '♝', '♛', '♚', '♝', '♞', '♜'},
            {'♟', '♟', '♟', '♟', '♟', '♟', '♟', '♟'},
            {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
            {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
            {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
            {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
            {'♙', '♙', '♙', '♙', '♙', '♙', '♙', '♙'},
            {'♖', '♘', '♗', '♕', '♔', '♗', '♘', '♖'}
        };

        for (int i = 0; i < board.length; i++) {
            char[] pieces = board[i];
            System.out.print("" + (8 - i) + " ");
            for (char piece : pieces) {
                System.out.print(piece + " "); // you're missing this space
            }
            System.out.println();
        }
        System.out.println("  a b c d e f g h");
    }
}

Prints:

8 ♜ ♞ ♝ ♛ ♚ ♝ ♞ ♜ 
7 ♟ ♟ ♟ ♟ ♟ ♟ ♟ ♟ 
6                 
5                 
4                 
3                 
2 ♙ ♙ ♙ ♙ ♙ ♙ ♙ ♙ 
1 ♖ ♘ ♗ ♕ ♔ ♗ ♘ ♖ 
  a b c d e f g h  

If you're not using a monospaced font the width of the pieces will vary with the font. They might match one of the unicode spaces, perhaps the en-space ( \  ) or the em-space ( \  ). Any solution would be specific to your environment. Ref: unicode spaces .

Edit: I did some experiments. In most fonts (notably not my version of Helvetica tho') the chess glyphs are the same width as the em-quad ( \  ). You'll still run in to problems with your column labels. I noticed that the enclosed alphanumeric glyphs are mostly the same width as the chess pieces too, so you could label the columns ⒜-⒣ instead of ah (ie \⒜-\⒣ ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM