简体   繁体   中英

printf of an UTF-8 character with indentation

I am writing a text interface of minesweeper as homework for an university project. I wanted to try to use this unicode character ⚑ (U+2691) as the flag to mark the mines, and it works, sometimes... this is the result. When there is a single flag, the matrix is well indented, but when two flags are nearby it's like if the spaces between them were smaller. I'm pretty sure that the console, after printing a flag, continues with a different encoding until it meets a different type (integer in my case). In any case, I'm new with java and inexperienced.

Two separated flags vs two contiguous flags: 图片

I have two matrices, a field, that holds all the bombs and numbers near them, and a state that tells if a cell has already been discovered or not: in particular, if a cell in the state matrix is == 0, it's available, if it's == 1, it was alredy discovered and if it's == 2 it means that the user put a flag in that cell. Leaving aside the code for the matrix generation, the recursive method to discover all the contiguous zeros, and the methods that enumerates rows and cols of the matrix, this is the interesting part of the code:

public class Minesweeper {

    //possible initial configuration
    private int [][] state = new int [10][10];
    private int [][] field = {  
        { 0, 1,-1,-1, 1, 0, 1, 1, 1, 0},
        { 0, 1, 2, 2, 1, 0, 1,-1, 1, 0},
        { 0, 1, 2, 3, 2, 1, 1, 1, 2, 1},
        { 0, 1,-1,-1,-1, 1, 0, 0, 2,-1},
        { 0, 1, 2, 3, 2, 1, 0, 0, 1,-1},
        { 0, 0, 0, 0, 1, 1, 2, 1, 3, 2},
        { 0, 0, 0, 0, 1,-1, 2,-1, 2,-1},
        { 1, 2, 1, 0, 1, 1, 3,-1, 2, 1},
        {-1, 3,-1, 2, 1, 2, 2, 2, 1, 0},
        {-1, 3, 1, 2,-1, 2,-1, 0, 0, 0}};

    public void printField() {

        String FLAG = "⚑";  //or "⚐"

        for (int row = 0; row < field.length; row++) {

            for (int col = 0; col < field[row].length; col++) {
                if (state[row][col] == 0)
                    System.out.printf("%4c", '-');
                if (state[row][col] == 1 && field[row][col] != -1)
                    System.out.printf("%4d", field[row][col]);
                if (field[row][col] == -1 && state[row][col] == 1)
                    System.err.printf("%4c", '*');
                if (state[row][col] == 2) {
                    System.err.printf("%4c" , FLAG);
                }
            }
            System.out.println();
        }

    }

    public static void main (String [] args) {
        Minesweeper campo = new Minesweeper ();
        campo.printField();
    }
}

Seems to work just fine if you use chars and the unicode value for the flag \⚑ ( See here )

Here is a simplified version:

private static int [][] field = {
    { 0, 1,-1,-1, 1, 0, 1, 1, 1, 0},
    { 0, 1, 2, 2, 1, 0, 1,-1, 1, 0},
    { 0, 1, 2, 3, 2, 1, 1, 1, 2, 1},
    { 0, 1,-1,-1,-1, 1, 0, 0, 2,-1},
    { 0, 1, 2, 3, 2, 1, 0, 0, 1,-1},
    { 0, 0, 0, 0, 1, 1, 2, 1, 3, 2},
    { 0, 0, 0, 0, 1,-1, 2,-1, 2,-1},
    { 1, 2, 1, 0, 1, 1, 3,-1, 2, 1},
    {-1, 3,-1, 2, 1, 2, 2, 2, 1, 0},
    {-1, 3, 1, 2,-1, 2,-1, 0, 0, 0}};

    public static void main (String[] args) {

    Character FLAG = '\u2691';

    for (int row = 0; row < field.length; row++) {
        for (int col = 0; col < field[row].length; col++) {
            int f = field[row][col];
            char c = '-';
            if (f == 1)
                c = Character.forDigit(f, 10);
            else if (f == -1)
                c = '*';
            else if (f == 2) {
                c = FLAG;
            }
            System.out.printf("%4c" , c);
        }
        System.out.println();
    }
}

I am getting that:

   -   1   *   *   1   -   1   1   1   -
   -   1   ⚑   ⚑   1   -   1   *   1   -
   -   1   ⚑   -   ⚑   1   1   1   ⚑   1
   -   1   *   *   *   1   -   -   ⚑   *
   -   1   ⚑   -   ⚑   1   -   -   1   *
   -   -   -   -   1   1   ⚑   1   -   ⚑
   -   -   -   -   1   *   ⚑   *   ⚑   *
   1   ⚑   1   -   1   1   -   *   ⚑   1
   *   -   *   ⚑   1   ⚑   ⚑   ⚑   1   -
   *   -   1   ⚑   *   ⚑   *   -   -   -

I ran it on linux. In IntelliJ console and on the terminal, both print well indented.

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