#Tetris thing

1 messages · Page 1 of 1 (latest)

scarlet cedar
#

thread

marsh shadow
#

Hey ...
Thanks you

scarlet cedar
#

so you're asking what Tiles[rotationState] means?

marsh shadow
#

Yes.
That's what I meant

scarlet cedar
#

so, Tiles is a Position[][]

marsh shadow
#

Yes.
a 2D array

scarlet cedar
#

more specifically, an array of arrays of positions

#

Position is a single position, Position[] is an array of positions, and Position[][] is an array of array of positions

#

and so on and so forth

#

if you index a Position[], you get a single Position

#

so what do you figure you get when you index a Position[][]?

marsh shadow
#

Like the location, a variable at i index and j index

scarlet cedar
#

just indexing it once

#
Position[][] tiles;
X result = tiles[RotationState];

what would you replace X with?

marsh shadow
#

Man I'm having a hard time translating my thoughts into English.
Um ...
I think X = tiles[i][RotationState] ?

scarlet cedar
#

I'm looking for a type: what the result looks like

#

like int, or float, or Position

marsh shadow
#

Position

#

It's a class

scarlet cedar
#

almost!

#

Position[]

#

tiles is a Position[][]

#

It is an array.

#

That array contains arrays. Those arrays contain Positions.

#

If you index an array, you get a single element from the array

#

if I index a int[], I get a int

marsh shadow
#

So it's an Array named tiles which contains arrays named positions ?

scarlet cedar
#

I wouldn't say the arrays it contains are called anything -- they have no name

#

It contains arrays that, themselves, contain Positions

#

You can try this for yourself by doing something like this:

#
int[][] bigArray;
int[] array = bigArray[0];
int element = array[0];
#

note that your code editor will probably complain that you didn't assign bigArray

marsh shadow
#

I'm trying this out

scarlet cedar
#

but the types -- the kinds of the variables -- will be correct

#

it's always very important to think about the types

#

one other thing: you can also do this

#

Position[,]

#

this is what I would call a 2D array. you index it with two values at the same time

#

it's not an array of arrays

#

it's just one big array that has some special syntax for indexing it

#
Position[,] positions;
positions[3,4];
marsh shadow
scarlet cedar
#

Position[][] allows for each sub-array to have a different length, and also requires you to create every sub-array separately. It also lets you index it in two separate steps

scarlet cedar
#

It's reasonable to call them both "2D arrays". I usually call Position[][] an array of arrays to help tell it apart from Position[,], though.

#

You can also call Position[][] a "ragged array" because the lengths of each sub-array can be different

#

if you were to draw it, it'd look like a rectangle with some pieces missing

marsh shadow
#

Ah ...
OK ...

scarlet cedar
#

you can read more about them here

marsh shadow
scarlet cedar
#

Right.

#

Each position in that Position[] you got by indexing a Position[][]

marsh shadow
#

This equals to a 2 layers-for loop right ?

scarlet cedar
#

If you wanted to go through every single Position, you could do two nested foreach loops, yes

marsh shadow
#

Like ``` for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++ {
}
}

scarlet cedar
#

if you used a for loop, you wouldn't use n in both loops

#
for (int i = 0; i < Tiles.length; ++i) {
  for (int j = 0; j < Tiles[i].length; ++j) {
     Position pos = Tiles[i][j];
  }
}
#

alternatively:

#
foreach (var positions in Tiles) {
  foreach (var position in positions) {
    //
  }
}
#

note that i'm not quite clear on what Tiles contains

#

it sounds like it's storing data for Tetris blocks

#

in different rotation states

marsh shadow
scarlet cedar
#

but if there were several kinds of blocks, then this wouldn't be enough to store all of their positions

scarlet cedar
#

remember that the length of each array on the inside can be different

#

so you have to check each array's length

#

even if they didn't vary, the inner arrays could still have a different length than the outer array!

marsh shadow
#

Ok I'll keep that in mind.

#

Thanks you

#

Now I gotta try to figure out the logic of the methods

marsh shadow
#

Excuse me here I saw he did Tiles.Length()
Is it a sum of all Position[].Length ?

scarlet cedar
#

do you mean just Tiles.Length?

#

I don't think there's a method named Length

marsh shadow
#

Yep just .Length

#

My bad

#

here they have these method that I'm trying to wrap my head around

        {
            rotationState = (rotationState + 1) % Tiles.Length;
        }

  public void RotateCCW()
        {
            if (rotationState == 0)
            {
                rotationState = Tiles.Length - 1;
            }
            else
            {
                rotationState--;
            }
        }
scarlet cedar
#

it sounds like rotationState is a number that tells you how many times the tetrominoes have been rotated

#

if you can rotate a tetromino four ways, then rotationState could be 0, 1, 2, or 3

#

if you go up to 4, then it should wrap around to 0

#

and if you go -1, it should wrap to 3

#

% is the remainder operator. It gives you the remainder after doing integer division

#

4 % 3 is 1

#

so you can just use that to wrap around when going forwards

#

annoyingly, it doesn't work when going backwards

#

-1 % 4 is just -1, not 3

#

so the RotateCCW function just checks if you're at 0

marsh shadow
#

So here I have 2 % (the number of ele in Tiles, the 1D Arrays) or do I have 2 % ( all the ele in each 1D array ) ?

scarlet cedar
#

The number of arrays that Tiles contains

#

(probably 4)

marsh shadow
scarlet cedar
#

Right.