You've learned about making one direction checking. From your current position, you can find out something in one block to the right. If you can't move anymore or you get the gold, you will return. That's what we're talking about in the previous post.
Now, I will continue about making more than one direction checking. How if we have a map that looks like this.
The Dog | Gold | |
# | # | |
Gold | # | # |
In this situation, we can get the gold by moving right or down. There are 2 alternatives. How do we make the dog to check both right and down?
In the previous post you'll find code like this
if(j < 3) // to check whether the current position is not bigger than the number of column
{
if(array[j] == 1) // to check whether the current position has the gold or not
return 1;
else
find_1(j+1); // to move to the next position, (j + 1) means move to the right.
}
We need to check not only the columns, but also the rows. So we need not only j but also i to check the rows. We can start making the down checking by adding the parameter.
Change this bool find_1(int j){} into bool find_1(int i, int j){}
i for the rows
j for the columns
Then, change the array, from 1 dimension into 2 dimensions. From array[j] into array[i][j]
Wait, don't forget to change the calling of the method find_1. From find_1(j+1) into find(i, j+1)
Now we're getting into the answer of the question. To make the checking for both direction, you can just simply add another if block.
Column check:
Row check:
Next Post -> Self-playing pacman. Using recursive, we can make a pacman moves and search its food in the entire area.
We need to check not only the columns, but also the rows. So we need not only j but also i to check the rows. We can start making the down checking by adding the parameter.
Change this bool find_1(int j){} into bool find_1(int i, int j){}
i for the rows
j for the columns
Then, change the array, from 1 dimension into 2 dimensions. From array[j] into array[i][j]
Wait, don't forget to change the calling of the method find_1. From find_1(j+1) into find(i, j+1)
Now we're getting into the answer of the question. To make the checking for both direction, you can just simply add another if block.
Column check:
if(j < 3)
{
if(array[i][j] == 1)
return 1;
else
find_1(i, j+1);
}
Row check:
if(i < 3)
{
if(array[i][j] == 1)
return 1;
else
find_1(i+1,j);
}
But there's a problem.
1. It would only check the column. Because when the column check fails, it directly return 0 to the main.
Solution : use a variable to keep the status whether there is gold or not from the right.
2. If we have add a variable flag to solve the 1st problem, there's another problem. In the row check, there would be a moment when j == 3. This shouldn't happen.
Solution: remove the if(j < 3) and if(i < 3). Replace then with this.
if(j >= 3 || i >=3){return 0}
This is the complete code. Try it.
source codeNext Post -> Self-playing pacman. Using recursive, we can make a pacman moves and search its food in the entire area.