为数组中所有位置的值读零

时间:2014-08-02 15:58:54

标签: java arrays swing

我正在使用Swing在Java中制作游戏。我正在使用2D数组来存储我的世界。我有一个文本命令系统,当它从数组中调用一个变量时,它会在除0,0之外的每个位置返回0。这是我的代码:

生成数组的类:

package finalproject;

public class WorldGen {

public int length = 20;
public int width = 20;
public double[][] world = new double[length][width];

public WorldGen(int seed) {

    worldBase(seed);

}

public void worldBase(int seed) {

    if(seed == 1) {

        world[0][0] = 1;

    } else if (seed == 2) {

        world[0][0] = 2;

    } else if (seed == 3) {

        world[0][0] = 3;

    } else if (seed == 4) {

        world[0][0] = 4;

    } else if (seed == 5) {

        world[0][0] = 5;

    } else if (seed == 0) {

        world[0][0] = 1 + (Math.floor(Math.random() * 5));

    }

    worldSmooth(0, 0);

}


public void worldSmooth(int posX, int posY) {

    double tempChunkType;

    for(int x = 1; x < length; x ++) {

        for(int y = 1; y < width; y ++) {

            if(world[posX][posY] == 1) {

                tempChunkType = 1 + (Math.floor(Math.random() * 10));
                if(tempChunkType == 1 || tempChunkType == 2 || tempChunkType == 3 || tempChunkType == 4 || tempChunkType == 5) {

                    world[x][y] = 1;

                } else if(tempChunkType == 6 || tempChunkType == 7 || tempChunkType == 8) {

                    world[x][y] = 2;

                } else if(tempChunkType == 9) {

                    world[x][y] = 3;

                } else if(tempChunkType == 10) {

                    world[x][y] = 4;

                }

            } else if(world[posX][posY] == 2) {

                tempChunkType = 1 + (Math.floor(Math.random() * 10));
                if(tempChunkType == 1 || tempChunkType == 2 || tempChunkType == 3 || tempChunkType == 4 || tempChunkType == 5) {

                    world[x][y] = 2;

                } else if(tempChunkType == 6 || tempChunkType == 7 || tempChunkType == 8) {

                    world[x][y] = 1;

                } else if(tempChunkType == 9) {

                    world[x][y] = 3;

                } else if(tempChunkType == 10) {

                    world[x][y] = 4;

                }

            } else if(world[posX][posY] == 3) {

                tempChunkType = 1 + (Math.floor(Math.random() * 10));
                if(tempChunkType == 1 || tempChunkType == 2 || tempChunkType == 3 || tempChunkType == 4 || tempChunkType == 5) {

                    world[x][y] = 3;

                } else if(tempChunkType == 6 || tempChunkType == 7 || tempChunkType == 8) {

                    world[x][y] = 2;

                } else if(tempChunkType == 9) {

                    world[x][y] = 1;

                } else if(tempChunkType == 10) {

                    world[x][y] = 4;

                }

            } else if(world[posX][posY] == 4) {

                tempChunkType = 1 + (Math.floor(Math.random() * 10));
                if(tempChunkType == 1 || tempChunkType == 2 || tempChunkType == 3 || tempChunkType == 4 || tempChunkType == 5) {

                    world[x][y] = 4;

                } else if(tempChunkType == 6 || tempChunkType == 7 || tempChunkType == 8) {

                    world[x][y] = 3;

                } else if(tempChunkType == 9) {

                    world[x][y] = 5;

                } else if(tempChunkType == 10) {

                    world[x][y] = 2;

                }

            } else if(world[posX][posY] == 5) {

                tempChunkType = 1 + (Math.floor(Math.random() * 10));
                if(tempChunkType == 1 || tempChunkType == 2 || tempChunkType == 3 || tempChunkType == 4 || tempChunkType == 5) {

                    world[x][y] = 5;

                } else if(tempChunkType == 6 || tempChunkType == 7 || tempChunkType == 8) {

                    world[x][y] = 4;

                } else if(tempChunkType == 9) {

                    world[x][y] = 3;

                } else if(tempChunkType == 10) {

                    world[x][y] = 2;

                }

            }

            posX = x;
            posY = y;

        }

    }


}

}

命令类:

package finalproject;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class ControlPanel extends JPanel implements ActionListener {

public String chunkType;
public WorldDisp world = new WorldDisp();
public double[][] worldOb = new double[world.world.length][world.world.width];
public double[][] heightOb = new double[world.world.length][world.world.width];
public int posX = 0;
public int posY = 0;
public int columns = 20;
public int rows = 5;
public JTextField textField = new JTextField(columns);
public JTextArea textArea = new JTextArea(rows, columns);
public JScrollPane scrollPane = new JScrollPane(textArea);
public String newline = "\n";
public String input;

public ControlPanel() {

    setLayout(new BorderLayout());

    scrollPane.setPreferredSize(new Dimension(400, 50));
    textArea.setLineWrap(true);
    textArea.setEditable(false);

    add(textField, BorderLayout.NORTH);
    add(scrollPane, BorderLayout.CENTER);

    textField.addActionListener(this);

    setOb();

}

@Override
public void actionPerformed(ActionEvent e) {
    // TODO Auto-generated method stub

    textArea.setText(null);

    input = textField.getText();

    textArea.append(textField.getText());
    textField.selectAll();

    commandCheck(input);



}

public void displayPosAndChunk(int posX, int posY, String chunkType) {

    textArea.setText(null);

    textArea.append("X: " + posX + " Y: " + posY + newline);
    System.out.println("You are in a " + chunkType);
    textArea.append("You are in a " + chunkType);

}

public void commandCheck(String input) {

    if(input.equalsIgnoreCase("right")) {

        posX ++;
        System.out.println("Go right");

    } else if(input.equalsIgnoreCase("left")) {

        posX --;
        System.out.println("Go left");

    } else if(input.equalsIgnoreCase("down")) {

        posY ++;
        System.out.println("Go down");

    } else if(input.equalsIgnoreCase("up")) {

        posY --;
        System.out.println("Go up");

    } else {

        //Send text to textArea in ControlPanel

    }

    System.out.println("At call for displayPosAndChunk. Before this is all fine. X: " + posX + " Y: " + posY);

    System.out.println(worldOb[posX][posY]);

    displayPosAndChunk(posX, posY, chunkIntToString(worldOb[posX][posY]));

}

public String chunkIntToString(double chunkTypeInt) {

    System.out.println(chunkTypeInt);

    if(chunkTypeInt == 1) {

        chunkType = "desert";

    } else if(chunkTypeInt == 2) {

        chunkType = "plains";

    } else if(chunkTypeInt == 3) {

        chunkType = "lightForest";

    } else if(chunkTypeInt == 4) {

        chunkType = "darkForest";

    } else if(chunkTypeInt == 5) {

        chunkType = "pineForest";

    } else {

        chunkType = "nothing. Something is wrong.";

    }

    System.out.println(chunkType);

    return chunkType;

}

public void setOb() {

    worldOb = world.world.world;

}

}

我的代码出了什么问题?我试过改变我调用数组的方式。还有一点需要注意,我知道数组生成正常,因为我有一个类(WorldDisp),它使用WorldGen生成一个数组并完美地显示它。那么,它发生了什么?

2 个答案:

答案 0 :(得分:0)

所有这些位置都从零开始,然后你过多的if语句检查几乎所有为零。

答案 1 :(得分:0)

WorldGen.worldSmooth(int posX, int posY)未将块类型分配给第一行或第一列。您应该将for循环设置为0(但忽略0,0)。由于0不是有效的块类型,因此沿着这些边缘始终存在“有问题”的空间。

commandCheck(String input)没有处理走出世界的情况(例如从开始左起或上升将进入负指数)。因此,访问worldOb[posX][posY]可能会抛出一个未被处理的异常。也许您应该检查一次移动是否会将您带到一个有效的位置并阻止无效的移动,或者转移到另一侧。

世界是否需要属于double类型?您可以将它们表示为intenum

同时仔细检查您是否传递了有效的种子。无效的种子(如-1)将导致数组为0.0s。

否则,由于您已确认正确生成了数组,因此您的代码中的其他位置可能正在访问其他实例。