borisjo
borisjo

Reputation: 178

Why is my array being reset to 0 after each loop?

I am trying to make a snake game but I run into a few problems. I have this thread which contains the game loop but it doesn't seem to work. This is a simple form of my loop, I simplified it because it didn't work at first but it doesn't still. What happens is that my state variable gets reset to 0 after the first loop and I really have no clue why. I tries so many ways to fix it but I can't seem to find the problem.

Oh yeah, also don't look at the sloppy start of my run method, I am trying to fix things and it gets rather ugly.

public void run() {
    alive = true;
    keyPressed = 2; // Right 
    keyPressedBefore = 2;
    field = Gui.getPanelArray();
    state = new int[20][20];
    newstate = new int[20][20];
    previous = new int[20][20];
    coordw = new int[20*20];
    coordh = new int[20*20];
    length = 2;
    width = height = 20;

    for (int w = 0; w < 20; w++) {
        for (int h = 0; h < 20; h++) {
            state[w][h] = 0;
        }
    }
    state[0][0] = 1;
    render(state,field);
    previous = state;
    newstate = state;

    try {
        Thread.sleep(2000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    loop();
}

private void loop() {
    while (alive) {
        for (int w = 0; w < 20; w++) {
            for (int h = 0; h < 20; h++) {
                newstate[w][h] = 0;
            }
        }

        if (keyPressed == 0) {
            for (int w = 0; w < 20; w++) {
                for (int h = 0; h < 20; h++) {
                    if (state[w][h] == 1) {
                        newstate[w][h-1] = 1;
                    }
                }
            }
        } else if (keyPressed == 1) {
            for (int w = 0; w < 20; w++) {
                for (int h = 0; h < 20; h++) {
                    if (state[w][h] == 1) {
                        newstate[w-1][h] = 1;
                    }
                }
            }
        } else if (keyPressed == 2) {               
            for (int w = 0; w < 20; w++) {
                for (int h = 0; h < 20; h++) {
                    if (state[w][h] == 1) {
                        newstate[w][h+1] = 1;
                    }
                }
            }
        } else if (keyPressed == 3) {
            for (int w = 0; w < 20; w++) {
                for (int h = 0; h < 20; h++) {
                    if (state[w][h] == 1) {
                        newstate[w+1][h] = 1;
                    }
                }
            }
        }
        render(newstate, field);
        state = newstate;
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        //keyPressedBefore = keyPressed;
    }

}

Upvotes: 0

Views: 1073

Answers (1)

Elliott Frisch
Elliott Frisch

Reputation: 201447

Because the first section of your loop explicitly sets the array to zero. Comment it out like

while (alive) {
    // for (int w = 0; w < 20; w++) {
    //    for (int h = 0; h < 20; h++) {
    //        newstate[w][h] = 0;
    //    }
    // }

Also, don't just assign the reference here

// state = newstate;
state = Arrays.copyOf(newstate, newstate.length);

Upvotes: 2

Related Questions