array use?

Previous topic - Next topic

cruelcynic

For my falling blocks game I figure I can store the block locations for my seven shapes in a 4x4 array. Do I need to make an array for all seven or is their a more efficient way to store the data?

MrTAToad

Technically you could use one 4 x 4 and then just copy the shape into the array

cruelcynic

OK. Another question if you don't mind. I have obrick[4][4] I plan to fill it like...
0000
0110
0110
0000

Is their a faster way to do that than using:
obrick[0][0]=0
obrick[0][1]=0
...

Thanks for the help.

cruelcynic

Well I found i bit of Basic code to do what I want but I can't seem to get it to work. Here is what I have. Any ideas?

Code (glbasic) Select
GLOBAL BlockColor[];DIM BlockColor[7][3]//rgb colors(1-3) for each of the seven pieces

FOR iter = 0 TO 6 //loop through seven pieces
READ BlockColor[iter][0]//read red amount
READ BlockColor[iter][1]//read green amount
READ BlockColor[iter][2]//read blue amount
NEXT

DATA 255,255,0//yellow square
DATA 0,128,255//turquoise line
DATA 0,0,255//blue left L
DATA 255,128,0//orange right L
DATA 255,0,0//red left S
DATA 0,255,0//green right S
DATA 128,0,255//purple T

erico

Quote from: cruelcynic on 2012-Jun-08
...
Is their a faster way to do that than using:
obrick[0][0]=0
obrick[0][1]=0
...

You can use a for next loop to cycle through it enough times.
Here is one I did to read DATA for a maps but here adapted to a 4x4 block.

There might be others and better ways.
Code (glbasic) Select
// --------------------------------- //
// Project: SOLARIS block adapt
// Start: Friday, June 08, 2012
// IDE Version: 10.283

// DECLARE

DIM m[12][12]
d=0
c=0

// LOAD BLOCO

RESTORE BLOC
FOR d=0 TO 4
FOR c=0 TO 4
READ m[c][d]
NEXT
NEXT

// BLOCO DATA

STARTDATA BLOC:

DATA  0, 0, 0, 0
DATA  0, 1, 1, 0
DATA  0, 1, 1, 0
DATA  0, 0, 0, 0

ENDDATA

backslider

Quote from: cruelcynic on 2012-Jun-07
For my falling blocks game I figure I can store the block locations for my seven shapes in a 4x4 array. Do I need to make an array for all seven or is their a more efficient way to store the data?

If I understand correctly, you want to store the block settings while runtime, right?
Then you could use a Type to store your data.

Example
Code (glbasic) Select

TYPE TBlock
posX%
posY%
//some other attributes
ENDTYPE

LOCAL blocks[] AS TBlock

FOR i%=0 TO 10
    FOR j%=0 TO 10
        LOCAL block AS TBlock
        block.posX = i * 32
        block.posY = j * 32
        DIMPUSH blocks[], block
    NEXT
NEXT

WHILE TRUE
FOREACH B IN blocks[]
    PRINT B.posX + ", " + B.posY, B.posX, B.posY
NEXT
SHOWSCREEN
WEND


I can't test the code, but it should work.

kanonet

DIMDATA can do exactly what you want, at least for one dimension arrays, not sure about about multi dimension arrays.

Btw you dont need to store the value '0', all arrays (and other variables) automatically have a '0' value when you define them.
Lenovo Thinkpad T430u: Intel i5-3317U, 8GB DDR3, NVidia GeForce 620M, Micron RealSSD C400 @Win7 x64

FutureCow

Sorry I'm in a rush so don't have time to write much about this now, but if may be more efficient to use bitwise operations. Eg. A standard character is made up of 8 bits = 2 x 4 lines. Get the first line by bitwise anding the number with 11110000 and the lower by anding with 00001111. Therefore 2 lines per ascii character, that's 2 ascii characters to store each block. 7 blocks worth of shapes can be stored in 14 alphanumeric characters.
Depending on how much programming experience you've had it might be a bit hard to get your head around / bug fix bitwise operations though. Also, although it's very efficient storage, it's often much wiser to compromise efficiency. Nowadays computers have cpu cycles and memory to burn. I'm not saying you should intentionally waste resources, but your "time" resource to write and bug fix "efficient" code to save a couple of CPU cycles and a few KB of RAM may be effort better spent elsewhere.
Bitwise calculations on each line of blocks will allow you to quickly determine which lines are full and which have holes in them, whether a block can fit in a hole etc.
I wouldn't necessarily recommend you head this way, it's just an idea.

Another thought is to store your data as a 1D array. The first 4 bits of data are the first line, the next 4 the second line etc. It only takes a for loop or addition statement to find the right bit of data.

In regards to kanonet's post, my personal preference is to err on the side of caution and explicitly set arrays and variables to known values (in that case, 0). That way you future-proof your code in case, for example, the language changes and all variables get whatever random value is in that section of memory at the time of creation until it's set otherwise. It also guarantees you know exactly what everything's set to at that point in the code in case you accidently reset the variable to another value somewhere else.

cruelcynic

Thanks for the posts. The help is much appreciated!