mardi 27 octobre 2020

how to stop getting errors when trying to turn all certain squares dark blue on a grid

I'm not sure how to fix this issue. my program is basically randomly generated green blocks on a 32 by 32 grid(it does not fill the whole grid). if i click on an empty spot, it turns blue and is supposed to spread to all other empty spots but not changing green(land). if I click on an area that is connected to the edge of the screen, it will turn a dark green and also spread to all other empty spots. my problem is that if i click on a square that is connected to the edge of the screen, i just get an error and although it spreads, it doesn't always spread to every spot, and it's not the same color

this is the code to make it spread

    void findLakes(int x, int y) {
    if (board[x][y] != EMPTY)
        return;
    board[x][y] = LAKE;
    if (y > 0 && board[x][y - 1] == EMPTY)
        findLakes(x, y - 1);
    if (y < GRID - 1 && board[x][y + 1] == EMPTY)
        findLakes(x, y + 1);
    if (x > 0 && board[x - 1][y] == EMPTY)
        findLakes(x - 1, y);
    if (x < GRID - 1 && board[x + 1][y] == EMPTY)
        findLakes(x + 1, y);
    if (x == 0) findOceans(x,y);
}
// if (... square is on the edge of the board) findOceans(x,y);
void findOceans(int x, int y) {
            if (board[x][y] == LAKE) board [x][y] = OCEAN;

            if (((y > 0) && (board[x][y - 1] == LAKE)) ||  (board[x][y - 1] == EMPTY ))
                findOceans(x, y - 1);
            if (((y < GRID - 1) && (board[x][y + 1] == LAKE)) ||  (board[x][y + 1] == EMPTY))
                findOceans(x, y + 1);
            if (((x > 0) && (board[x - 1][y] == LAKE)) || (board[x][x - 1] == EMPTY))
                findOceans(x - 1, y);
            if (((x < GRID - 1 ) && (board[x + 1][y] == LAKE)) ||  (board[x][x + 1] == EMPTY))
                findOceans(x + 1, y);
}

and if you wanted, this is how i generate my map

void makeRandomMap() {
    int i, j;
    boolean done = false;
    int landTiles = 0;
    while (!done) {
        i = (int) (Math.random() * GRID);
        j = (int) (Math.random() * GRID);
        if (board[i][j] == EMPTY) {
            board[i][j] = LAND;
            landTiles++;
            if (landTiles == NUM_LAND)
                done = true;
        }
    }
}

randomly generated map

its kind of confusing how I described it so if you have any questions, I can try and answer them!

Aucun commentaire:

Enregistrer un commentaire