简体   繁体   中英

by boolean, how to change existing chars to be true

My code is like this

 public class AsciiGrid
    {
        private static int ascrow = 0;
        private static int asccol = 0;
        private static char[][] myAsciiGrid; 
        private boolean [][] GridOccupied;
        private static char space = ' ';
.
.
.
.
.
        public String toString()
        {
                String[][] mystring = new String [ascrow+2][asccol+2];
                String space = " ";
                for (int i=0; i < ascrow+2; i++)
                {
                    for (int j = 0; j< asccol+2; j++)
                    {
                        if ((i==0)||(i==ascrow+1))
                            mystring[i][j] = "=";
                        else if ((j==0)||(j==asccol+1))
                            mystring[i][j] = "|";
                        else
                            mystring[i][j] = space;
                    }
                } 
                return mystring;
}
}

and this will return a grid like thing which I will place some image on there. the image will only be triangle and rectangle just like:

===========        ===========
|         |        |         |
|   *     |        |   **    |
|   **    |        |   **    |
===========        ===========

before i put the next image in there, i want to check if the space got already used by using boolean. I dont want them to get overlapped. so the code i wrote just like:

     private boolean placeable(int r,int c,int ascrow, int asccol){

}

but i dont know how to change my coped code to boolean and test if the space got something on there. this is my copyGrid:

private static char [][] copyGrid(char [][] myAsciiGrid)
    {
        char[][] newarray1 = new char [myAsciiGrid.length][myAsciiGrid[0].length];
        for (int i = 0; i < myAsciiGrid.length; i++)
            for(int j = 0; j < myAsciiGrid[0].length; j++)
                newarray1[i][j] = myAsciiGrid[i][j];
        return newarray1;
    }

im not really sure if this is right.

Can anyone help? thanks

hers how to directly check charters of the string

boolean filled;
String str;
if (str.charAt(character position starting at 0).equals(" "))
filled = false;
else
filled = true;

Your question is rather poorly worded, but from what I understand, you need a method to tell you if your "grid" has something in it other than space. Maybe something like the below could help:

private boolean isEmpty() {
    boolean empty = true;
    for (int i=1;i<myAsciiGrid.length-1;i++) {
        for (int j=1;j<myAsciiGrid[0].length-1;j++) {
            if (!myAsciiGrid[i][j].equals(" ")) {
                return false;
            }
        }
    }
    return empty;
}

But I could be wrong in my understanding and I suppose you would have figured out the above yourself

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