2D array printing letters in middle of grid in forward direction

724 Views Asked by At
public void fill(ArrayList<String> a1) {
    int i = 0;
    while (i < a1.size()) {
        if (i == 0) {
            for (int j = 0; j < a1.get(i).length(); j++)
                crossword[ROWS / 2][(COLUMNS / 4) + j] = a1.get(i)
                        .charAt(j);
            i++;
        }
        if (i == 1) {
            outerloop: for (int t = 0; t < ROWS; t++)
                for (int s = 0; s < COLUMNS; s++)
                    for (int j = 0; j < a1.get(i).length(); j++)
                        if (crossword[t][s] == a1.get(i).charAt(j)) {
                            for (int z = 0; z < j; z++)
                                crossword[t - z - 1][s] = a1.get(i).charAt(
                                        z);
                            for (int h = j + 1; h < a1.get(i).length(); h++)
                                crossword[t + h - j][s] = a1.get(i).charAt(
                                        h);
                            crossword[t][s] = a1.get(i).charAt(j);
                            break outerloop;
                        }
            i++;
        }
    }
}

The above is my method to make the first two words of a list of words intersect each other on a crossword puzzle board. My question is for the part:

 for (int z = 0; z < j; z++)
 crossword[t - z - 1][s] = a1.get(i).charAt(z);

It takes the letters in front of the intersection point and prints them backwards above the intersection row. My brain is overloaded with different things right now and I can't seem to understand how to make the letters go in the right order. I can't attach an image to display my problem but for example the vertical word "throwing" which intersects with horizontal word "clowning" at the letter "o" prints out "rht" before the o (when it should be printing out "thr"). Could someone help? Would be much appreciated!

1

There are 1 best solutions below

0
On

This will do the trick: for (int z = 0; z < j; z++) crossword[t - j + z][s] = a1.get(i).charAt(z);

P.S. This question hasn't been closed yet (even though you did respond to your question). You should flag this answer (or, if you don't feel like giving me points, an answer that you put in) as correct, so that the question doesn't remain in the unanswered section!