• Dear Cerberus X User!

    As we prepare to transition the forum ownership from Mike to Phil (TripleHead GmbH), we need your explicit consent to transfer your user data in accordance with our amended Terms and Rules in order to be compliant with data protection laws.

    Important: If you accept the amended Terms and Rules, you agree to the transfer of your user data to the future forum owner!

    Please read the new Terms and Rules below, check the box to agree, and click "Accept" to continue enjoying your Cerberus X Forum experience. The deadline for consent is April 5, 2024.

    Do not accept the amended Terms and Rules if you do not wish your personal data to be transferred to the future forum owner!

    Accepting ensures:

    - Continued access to your account with a short break for the actual transfer.

    - Retention of your data under the same terms.

    Without consent:

    - You don't have further access to your forum user account.

    - Your account and personal data will be deleted after April 5, 2024.

    - Public posts remain, but usernames indicating real identity will be anonymized. If you disagree with a fictitious name you have the option to contact us so we can find a name that is acceptable to you.

    We hope to keep you in our community and see you on the forum soon!

    All the best

    Your Cerberus X Team

Multi dimensional array question

Never played Robot Ralley. But I am a huge fan of Board Games and good computer conversions of them. Are you planning a multiplayer mode?
I already have multi-player in terms of hot seat, 2 or more players on 1 device. True online MP is something I will have a look at once I'm done with the base game. To be honest, online multiplayer seems like a pretty daunting challenge.
 
I know I'm very late for this discussion, but this is what I do (and Its what I do since the 8 bits day):

map_array:Int[144] - to get a coordinate, just calc x + y * 12. Lets say you want x:5, y:8 - its map_array[5 + 8 * 12]
The actual formula is X + Y * MAP_WIDTH. (MAP_WIDTH being the number of tiles on each row of the tilemap)
 
I know I'm very late for this discussion, but this is what I do (and Its what I do since the 8 bits day):

map_array:Int[144] - to get a coordinate, just calc x + y * 12. Lets say you want x:5, y:8 - its map_array[5 + 8 * 12]
The actual formula is X + Y * MAP_WIDTH. (MAP_WIDTH being the number of tiles on each row of the tilemap)
That's the exact method that I use to create my map and it works great. In the game, my robots have an x,y tile position on the map. So x is 1 to 12 and y is 1 to 12. So I was looking to for a more intuitive way to see the data. If you know what I mean. Robot.x = 4; robot.y =3 is easier to understand than robot.position = 137

I don't know if I'm explaining that in a way that's understandable.
 
You can still have robot.x = 4, robot.y = 3. Just transform this when necessary.
For example, if you want to test the tile your robot is, then you do something like:

Local mapPos:int = robot.x + robot.y * map.width
Local currentTile:int = map.data[mapPos]
 
By the way is it possible to make this so arr3d is: Int, Int, String? Or is mixing Int and String not possible?
If you have few unique strings that you want to use you can set a few constants and enumerate them.

grassplayfield = 1
frontlayer = 2
hiddenwalls = 4
etc


This way you can use strings to identify parts of the map.

You could also go the extra mile and use hash to identify any string if that is something you really want.
We can let those be a part of some other scheme in your code, or we could add a whole new dimension for them if you want like this.
Here I use an 1-dimensional array to mimic not 2D but even 3D.

Code:
Local arr1d:Int[512*512*512] ' Let's say we want these dimensions
Local d:Int, x:Int, y:Int, z:Int, width:Int, depth:Int

' 3D-to-1D (x,y,z to d)
d = (y * width) + (z * width * depth) + x

' 1D-to-3D (d to x,y,z)
z = d / (width * depth)
d = d - (z * width * depth)
y = d / width
x = d Mod width
 
Last edited:
By the way is it possible to make this so arr3d is: Int, Int, String? Or is mixing Int and String not possible?


Local arr2d:Int[][]

Code:
Class MyCustomTile
   Field x:Int, y:Int
   Field name:String
End Class

Local map:MyCustomTile[]

map = new MyCustomTile[12*12]
 
Btw if you wanna avoid Mod but still be flexible about the size you can do this trick instead of shifting:

Code:
' Change the line

' x = d Mod width

' Into these two lines

' d = d - (y * width)
' x = d

(This code is made into two lines to make it easy to see the pattern that is going on)
 
Last edited:
Thanks lads for putting in the time to answer this. I am mucking around with your code and it's saving me a ton of time.
 
I know I'm very late for this discussion, but this is what I do (and Its what I do since the 8 bits day):

map_array:Int[144] - to get a coordinate, just calc x + y * 12. Lets say you want x:5, y:8 - its map_array[5 + 8 * 12]
The actual formula is X + Y * MAP_WIDTH. (MAP_WIDTH being the number of tiles on each row of the tilemap)
This is fantastic! I'm using this now. As the very top left of my map is 1, 1 and not 0, 0, I've had to change this to x + y *12 -12. Thanks again.
 
Bit late to the party, but arrays in CX are similar to Java - a multi-dimensional array is an array of arrays. (Of course you can also make a C-style 1D array and index it yourself.)

I use the following code for handy array allocations. CX doesn't have generic functions so I made a class called Generic to contain the ones I use a lot. Then Generic is kind of like a keyword for generic functions. Using it, I can allocate a 10x10 int array like so:

Local arr:Int[][] = Generic< Int >.AllocateArray( 10, 10 )


Cerberus:
' Class for generic functions
Class Generic< T >
  
    ' Allocate a 1D array
    Function AllocateArray:T[]( i:Int )
        Local arr:T[] = New T[ i ]
        Return arr
    End
  
  
    ' Allocate a 2D array
    Function AllocateArray:T[][]( i:Int, j:Int )
        Local arr:T[][] = New T[ i ][]
        For Local index:Int = 0 Until i
            arr[ index ] = AllocateArray( j )
        Next
        Return arr
    End
   
  
    ' Allocate a 3D array
    Function AllocateArray:T[][][]( i:Int, j:Int, k:int )
        Local arr:T[][][] = New T[ i ][][]
        For Local index:Int = 0 Until i
            arr[ index ] = AllocateArray( j, k )
        Next
        Return arr
    End

End
 
Back
Top Bottom