Edit: Please check my reply below for the current code.
I'm not having too much luck with this. Think of Tic Tac Toe, and trying to find if the angle contains all of the same value (such as 1). I'm operating on a dynamic, 2D array.
bool isAngleWithValue(const int xPosFrom, const int yPosFrom,
const int xPosTo, const int yPosTo, const T &value)
{
bool blnGreatSuccess = true;
int width, height;
// ... removed validation code for demostration
// get width and height
width = (xPosTo - xPosFrom);
height = (yPosTo - yPosFrom);
for (int y = yPosFrom; y < height; y++)
{
for (int x = xPosFrom; x < width; x++)
{
if (x == y)
{
if (m_array[y * m_width + x] != value)
blnGreatSuccess = false;
}
}
}
return blnGreatSuccess;
}
For example, this works:
Array2D<int> a(3, 3);
a.fill(0);
a.get(0, 0) = 1;
a.get(1, 1) = 1;
a.get(2, 2) = 1;
if (a.isAngleWithValue(0, 0, 2, 2, 1))
cout << "Yes!" << endl;
else
cout << "No!" << endl;
But when I change a.get(2,2) to a.get(1, 2), it's still outputting "Yes!", which is not functioning correctly.
I tried researching various sites, but I can't seem to relate it to this problem. A general algorithm can be helpful too for checking diagonally.