#Tetris thing
1 messages · Page 1 of 1 (latest)
Hey ...
Thanks you
so you're asking what Tiles[rotationState] means?
Yes.
That's what I meant
so, Tiles is a Position[][]
Yes.
a 2D array
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[][]?
Like the location, a variable at i index and j index
just indexing it once
Position[][] tiles;
X result = tiles[RotationState];
what would you replace X with?
Man I'm having a hard time translating my thoughts into English.
Um ...
I think X = tiles[i][RotationState] ?
I'm looking for a type: what the result looks like
like int, or float, or Position
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
So it's an Array named tiles which contains arrays named positions ?
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
I'm trying this out
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];
This is a 2D array also right ?
But the length of each ele is equal ?
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
Yep!
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
Ah ...
OK ...
you can read more about them here
So in this case ...
tiles[RotationState] = Position[] ?
foreach (Position p in Tiles[rotationState])
means select each ele in that Position[] ?
This equals to a 2 layers-for loop right ?
If you wanted to go through every single Position, you could do two nested foreach loops, yes
Like ``` for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++ {
}
}
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
Oh wow I was taught to use n in both loop @@
but if there were several kinds of blocks, then this wouldn't be enough to store all of their positions
this would be fair if both the outer and inner arrays were both of the same size
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!
Ok I'll keep that in mind.
Thanks you
Now I gotta try to figure out the logic of the methods
Excuse me here I saw he did Tiles.Length()
Is it a sum of all Position[].Length ?
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--;
}
}
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
So let's say the current state is 1, 1 + 1 = 2 % Tiles.Length
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 ) ?
4 states of rotation right ?
Right.