This site requires JavaScript, please enable it in your browser!
Greenfoot back
Xolkiyr
Xolkiyr wrote ...

2018/2/16

Inserting an array into an array?

1
2
Xolkiyr Xolkiyr

2018/2/16

#
NOTE: Using psuedo-code, not actual java or php for example. I know its possible in php to take an array like
array1 = {{1,2,3},{1,2,3},{1,2,3}}
and place it within another array into an, example a 5x5 multidimensional, array like so
array2[0][0] = array1
Is it possible in java and more specifically Greenfoot?
danpost danpost

2018/2/16

#
Greenfoot uses java. Whatever is possible in java, as far as arrays are concerned, is possible in greenfoot. If I understand what you are asking properly, I would say that it is possible, provided the declared array types are not violated.
Xolkiyr Xolkiyr

2018/2/16

#
Variable Declarations:
/** Initiate World Variable  */
int[][] worldChunk = new int[5][5];
int[][] worldChunkOre = new int[12][3];
int curChunkX = Greenfoot.getRandomNumber(4);
int curChunkY = Greenfoot.getRandomNumber(4);
The Chunk Update code:
    /**
     * Updates the World Chunk
     */
    public void updateChunk(){
        worldChunk[curChunkY][curChunkX] = worldChunkOre;
    }
Xolkiyr Xolkiyr

2018/2/16

#
Forgot to add the error I get: incompatible types: int cannot be converted to int
Xolkiyr Xolkiyr

2018/2/16

#
Oh and the bit that adds ore to the worldChunkOre:
    /**
     * Add Ore listing to worldChunkOre
     */
    public void addOre(int x, int y, float ore, int oreCount){
        worldChunkOre[oreCount][0] = x;
        worldChunkOre[oreCount][1] = y;
        worldChunkOre[oreCount][2] = (int)ore;
    }
Xolkiyr Xolkiyr

2018/2/16

#
Forums messed up my error code
incompatible types: int[][] cannot be converted to int
danpost danpost

2018/2/16

#
You cannot assign a two-dimensional array to an element of a two-dimensional array -- you can only set a array of two-dimensions to an array of two dimensions. That is:
// with this
int[][][] array;

// you have this
array = new int[1][1][1]; // 'array' is a 3D array (an 'int[][][]')
array[0] = new int[1][1]; // 'array[0]' is a 2D array (an 'int[][]')
array[0][0] = new int[1]; // 'array[0][0]' is a 1D array (an 'int[]')
array[0][0][0] = 0; // 'array[0][0][0]' is a 0D array (an 'int')

//  you cannot do these
array[0][0][0] = new int[1][1][1]; // cannot assign an 'int[][][]' to something that is to hold an 'int'
array = 0; // cannot assign an 'int' to something that is to hold an 'int[][][]'
Line 11 is an example of what you are trying to do in your 'updateChunk' method. I wonder, however, if all this (what you are trying to do) is necessary. What is the purpose in all the arrays?
Xolkiyr Xolkiyr

2018/2/16

#
The exact of it is that the worldChunks are each World that makes up the bigger game world(a 5x5 area). They're created in the player class because its the only thing that stays constant. The worldChunks are, at the moment, supposed to hold the locations of each ore vein as well as the amount of ore in that vein. Each Chunk will spawn ore veins unless they've already been loaded into the worldChunk array. The array is meant to load in the ore veins where they were recorded and with how much ore they had when you left the Chunk. That makes sense, right?
Xolkiyr Xolkiyr

2018/2/16

#
Eventually I plan to expand it to 11x11.
danpost danpost

2018/2/17

#
Okay -- tell me where I am wrong (if anywhere): 1) The array holds the data from which you build your worlds; 2) The array also saves the worlds as you traverse among them; 3) You have 25 worlds arranged in a 5x5 (eventually 11x11); 4) All worlds have ore that can be anywhere within a 12x3 area; 5) You currently have only two types of ore -- copper and iron;
Xolkiyr Xolkiyr

2018/2/17

#
The ore can be anywhere in an 1180x600 area. But there can only 12 ore veins per world. I noticed one flaw even if my code did work, I was only saving the x, y, and ore amount of each vein. It wouldn't have been able to tell what type of ore it was. Can I save an object in an array?
Vercility Vercility

2018/2/17

#
Xolkiyr wrote...
The ore can be anywhere in an 1180x600 area. But there can only 12 ore veins per world. I noticed one flaw even if my code did work, I was only saving the x, y, and ore amount of each vein. It wouldn't have been able to tell what type of ore it was. Can I save an object in an array?
You can save anything in an array as long as the type stays the same (or you use wrapper classes)
Xolkiyr Xolkiyr

2018/2/17

#
Okay so what is the first step I'd need to do?
Vercility Vercility

2018/2/17

#
I dont really understand what your problem about that is. If you want to put an object of Type XYZ into an array you just do it like you would with a variable
XYZ object = new XYZ();
XYZ[] array = new XYZ[dimension];

array[0] = object;
danpost danpost

2018/2/17

#
If you only have two types of ore, then you can distinguish between them within the array by using negative quantities for one of them.
There are more replies on the next page.
1
2