Welcome back!
I hope you solved the challenge.
If we you didn't,
don't worry.
You can try it next time again.
Okay.
Let's check out my solution now. As I said I use a two dimensional bool slice to keep track of
the ball,
like so. This slice is going to store the rows and each row will be an inner slice.
Remember: For multidimensional slices, we also need to initialize each one of the inner slices.
Let's do that in a loop,
like so.
For example, board[4][2] allows me to access the cell in the fifth column and in the third row and so on.
By the way,
you know me, I don't like magical values in the code.
The dimensions of the board won't change.
So let's declare them using constants like so. These values are fine for me but you can change them if
you like.
I found these values by trial and error,
they are not magical values.
OK.
All right.
Now the board is ready.
So it's time to draw the board. Now I'm going to draw the board cells one by one. To do that, I need to
use inner loops to access the inner elements of the board like so. The parent loop will let us move over
the board rows and the inner loop will print the board columns. Here, I've picked the first element, it
will loop for ten times, because the height is ten. You know, each element of the board has the exact same
length, so I could have used any other element here as well. You will understand how this works in a second.
By the way, since each row and column have the same length, I could have used an array instead of a slice.
But, that's an optimization issue. Okay. I think it is better to see the board yourself. So, first let me
print a dummy character here, as an example.
Of course, it prints a lot of X characters in a single line. Of course, we need to print them in different
lines. To do that, after each row, I'm going to print a new line here. Now it prints a grid, cool! Each
X character here is an empty cell.
I'm going to remove them soon.
For now, they're there only for you to see where the empty cells are located, and how the program works.
I don't like the way the cells look because they are too close to each other.
So let me add a small space between them. Now it's okay.
As you can see, the algorithm prints a grid with a height of 10 and a width of 50.
Just like I want it.
Okay now it's time to print the ball in a specific cell.
Let's say the ball is in the top left corner of the board,
like so. Now, let's detect that and print the ball in that position.
To do that I need X and Y positions.
So where can I get them?
Remember I can get the positions through the range loop indexes.
Let me show you.
It is that simple.
Remember the parent loop moves over the board rows and the inner loop moves over the board columns.
So that is why the parent loop gives me the vertical positions and inner loops
give me the horizontal positions.
I loop over the vertical and after that I loop over the horizontal positions because I want to control
what to draw in a single line.
If I had first moved over the horizontal positions, I would need to print pixels vertically which is harder
for me to imagine in my mind.
However you can choose any way that is comfortable for you.
Now let's use the positions to check whether the ball is in the current X and Y positions like so.
If so, let's print the ball here. As you can see, there is a problem. For the first cell,
the program prints the ball, and the empty cell on top of each other.
I need to prevent that.
Let's start by putting the current cell value into a variable.
I have declared it as a rune variable because I'm going to use Unicode emojis. For example:
The ball is an emoji which can only be represented using multiple bytes, and a rune value can contain
multiple bytes, so it can store and emoji.
Let me first assign the empty cell value to the cell variable like so.
Remember single quote means a rune literal. Here,
I'm gonna set the current cell to the actual ball value like so.
And lastly let's print the cell value only once in here.
As you can see, it prints a lot of numbers instead of the ball and the cells. It's because, each cell here
is a rune which is a numeric type. To print the corresponding characters,
I need to convert it to a string, like so. You're going to learn more about this later on.
No worries.
Cool! Now it works. The program prints the ball at the top left corner of the board and the rest of the cells
are empty.
As an example let me print a smiley.
As you can see, I can't put the balls in any position on the board now.
It's like a drawing board.
I showed you this because I want you to have an intuition about how the animation is going to work. As
an exercise,
please pause the video, and try changing some of the pixels yourself. Instead of typing the cell values
manually,
here, let me also create a few constants for the cell values. So, I can change them anytime I like.
And I think that this makes the code easier to understand.
Let me also print the space character in a single function call,
right here. It's because, I don't want to call the print function unnecessarily.
It is an expensive operation. OK.
The program still works, awesome. While I'm talking about the expensive operations,
let me tell you something. Inside these loops,
I use a lot of Print function calls and doing so is not efficient.
In the next lecture, I'm also going to show you how to optimize this program.
All right.
That's all for now.
See in the next lecture! Bye!