1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
|
#include <iostream>
#include <iomanip>
#include <string>
#include <limits>
constexpr int MAXROWS{ 3 }, MAXCOLS{ 10 };
bool connectThree(char grid[][MAXCOLS])
{
for (int column = 0; column < 10; column++)
{
if (grid[0][column] == 'T' && grid[1][column] == 'T' && grid[2][column] == 'T')
return true;
}
for (int col = 0, row = 0; col < MAXCOLS - 3; col ++)
{
if (grid[row][col] == 'T' && grid[row + 1][col + 1] == 'T' && grid[row + 2][col + 2] == 'T')
return true;
}
return false;
}
int main()
{
char grid[MAXROWS][MAXCOLS] // <--- This grid will return false.
{
{ 'T', 'F', 'T', 'T', 'F', 'F', 'T', 'F', 'F', 'F' },
{ 'F', 'F', 'F', 'T', 'F', 'T', 'F', 'T', 'F', 'T' },
{ 'T', 'F', 'T', 'F', 'F', 'F', 'T', 'F', 'F', 'F' }
};
//char grid[MAXROWS][MAXCOLS] // <--- For Testing vertical. // <--- This grid will return true.
//{
// { 'T', 'F', 'T', 'T', 'F', 'F', 'T', 'F', 'F', 'F' },
// { 'F', 'F', 'F', 'T', 'F', 'T', 'T', 'T', 'F', 'T' },
// { 'T', 'F', 'T', 'F', 'F', 'F', 'T', 'F', 'F', 'F' }
//};
//char grid[MAXROWS][MAXCOLS] // <--- For Testing diagional. // <--- This grid will return true.
//{
// { 'T', 'F', 'T', 'T', 'F', 'F', 'T', 'F', 'F', 'F' },
// { 'F', 'F', 'F', 'T', 'F', 'T', 'F', 'T', 'F', 'T' },
// { 'T', 'F', 'T', 'F', 'F', 'F', 'T', 'F', 'F', 'F' }
//};
std::cout << "\n " << std::boolalpha << connectThree(grid) << '\n';
// <--- Keeps console window open when running in debug mode on Visual Studio. Or a good way to pause the program.
// The next line may not be needed. If you have to press enter to see the prompt it is not needed.
//std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>.
std::cout << "\n\n Press Enter to continue: ";
std::cin.get();
return 0; // <--- Not required, but makes a good break point.
}
|