So in the last videos, we saw how arrays work and one of the next projects will be tic tac toe.
And in order to understand tic tac toe, we need to make sure that we use arrays the two dimensionally.
Okay.
So we saw how to create a one dimensional array.
But now we're going to look at how to create two dimensional arrays.
So in order to declare a two dimensional array you again use the square brackets.
But then you add a comma in between.
So this one will be array 2D and declaration for example.
And then we can just say new int.
And here I'm just going to use let's say a three by three multi-dimensional array.
So how this is going to look like is going to be something like let me show you here.
So we have a few entries here okay.
So this two dimensional array will now look like this.
It has two dimensions.
It goes into the x direction as well as the y direction okay.
So we have those here.
So at position zero zero we have the entry of zero at the position of zero one.
So line zero row one we have zero as well.
But that's just because by default all the values will be set to zero.
But that is the content of our current 2D array.
Now we can use this and create a 3D array as well by adding another comma in here.
And let's call this one 3D declaration.
But then we also need to make sure that we now add another dimension.
So this one would be a cube of three by three by three.
So three in the x direction, three in the y direction as well as three into the z direction.
So this one will be a three dimensional array.
And I cannot display a three dimensional array on a 2D field.
I would need a three dimensional cube for that.
How do I now initialize such an array straight away?
So let me show you.
And I'm using int again here to keep it simple.
And this one will be array to the initialized.
And here we can use the query brackets.
So how this works is you have multiple dimensions and you need to have surrounding curly brackets.
And then for each of the dimension that you want to use curly brackets again.
So how this would work is I would have here, let's say I assign a value of one and a value of two.
And then this one of three and four.
So now I have a two dimensional array with two values.
So here we saw it three okay.
And this one will just have two values in the x axis as well as the y axis.
So if we compare it to what we did there, now we have assigned values directly or initialized values
directly.
So this one would be one and this one would be two.
This one would be three and that one would be four.
So you see this is what we did here.
We said in the first row this is the first row.
We have the values one at position zero and the value two at position one at index one.
And then in the second row we have three and four okay.
So let's try and get access to items inside of this array.
Initialize.
And therefore I'm going to get rid of all the other code that we had before.
Just going to leave the read key in here.
And let's say I want to access array 2D initialized at the position zero zero.
Okay I cannot say zero, but I need to have both dimensions here because it is a two dimensional array.
I need to separate it with a comma and access it like this.
Okay, so this is how I access the variable.
And this is also how I can overwrite the value.
So let's say first we access zero zero and then we overwrite zero zero with let's say five.
And then we access it again.
So we display it once again.
So let me show you what this will look like.
So we will see that we get one and then we get five.
Why do we get one.
Well because we initialized the entry at zero zero as one.
And then we overrode it with five.
So now how would you access this here this three here okay I hope you pause the video and tried to think
about it.
So in order to access this three I need to say 10Y10 because the first entry is the row and the second
entry is the column.
So this here is in row one row one.
And this here is row.
Zero.
So if I want to access something from here, I need to treat it like a normal array.
But in row one, so to speak.
Right.
So I'm saying this is index zero and this is index one but it is for row one.
So I need to use one as the row and zero as the column.
So now this will allow me to access this three okay.
Let me get rid of this line and see if we actually get the three.
And there we are.
We get the number three here.
All right okay.
So let's create another array.
But this time we create a multi-dimensional array that has the capability of storing a three by three
field or the information of a three by three field, like in tic tac toe where you can have an X or
you can have an O.
And this means we need to use strings for that.
So therefore let's pause the video.
Or you can pause the video and try that for yourself just for a second okay I hope you tried it.
So what I am going to do here is I'm going to create another array, this time of strings, and it's
going to be multi-dimensional.
And I'm going to call this one let's say tic tac toe field.
And this one will contain a multi-dimensional string array.
So here I need to add the semicolon at the end because this is still a declaration of an array.
And here I can now go ahead and create the entries.
So it's going to be like so.
And then another one like so and a third one like so.
So what should be in there.
So let's say the first entry is O like so the second entry is an x.
The third entry is an x as well.
And then here oh and here we have O again.
And here we have x.
And now finally the third row it's going to be x comma x and o because we need a winner.
So who won in this case O one.
Whoever player had O as their sign so to speak.
Because if we cross it you see the player has won.
So this is how a tic tac toe field could look like.
Now you could also use chars here because it's really just one character in there.
But I'm just using strings for simplicity.
So this is what a tic tac toe field would look like.
How do I or let's, let's say I want to display tic tac toe field at position.
And now it's going to get interesting.
One two.
So what am I going to get.
Just think about it.
Pause the video real quick and think about it.
What am I going to get.
So I'm going to get row one which means this row here.
And I'm going to get the index two entry which is going to be this x here.
So let's run this real quick and you will see we do in fact get x okay.
So I quickly created this understanding index a string multi-dimensional array for you to just understand
what the indexes look like.
So if you have a three dimensional array, if you want to access this item you need to access zero comma
zero.
Or we can go even further and like add zero comma zero and two comma.
And then maybe it's going to be even easier for you to understand.
So these are the indexes if you want to access them.
So if you want to access the top left item you need to use zero zero.
If you want to use this item here you need to use one one as the index.
So let me just quickly show you if I access one two that I'm actually going to get this one comma two
as my value here.
Okay.
So that is generally speaking how you can use multidimensional arrays.
At least we used two dimensional arrays here.
Now this works the same way for three dimensional arrays, only that you then obviously need to add
even more to it.
So let's look at three dimensional arrays in the next video real quick.
See you there.