Skip to content

Instantly share code, notes, and snippets.

@DreamerDeLy
Last active March 21, 2020 11:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DreamerDeLy/31ad65b7f5ed133332ce943c035e6be2 to your computer and use it in GitHub Desktop.
Save DreamerDeLy/31ad65b7f5ed133332ce943c035e6be2 to your computer and use it in GitHub Desktop.
Task 8 [C++]
#include <iostream>
#include <cstdlib>
#include <string>
#include <Windows.h>
using namespace std;
// Array size
const int r = 10;
const int c = 10;
void print(int arr[r][c], int px = -1, int py = -1);
int main()
{
//--------------------------------------------------------------------------
int arr[r][c] =
{
{0, 0, 0, 0, 1, 1, 0, 0, 0, 1},
{0, 1, 0, 0, 0, 0, 0, 1, 0, 0},
{0, 1, 0, 1, 1, 1, 0, 1, 0, 1},
{0, 1, 0, 0, 0, 0, 0, 1, 0, 1},
{0, 1, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 1, 0, 0, 1, 0},
{0, 0, 0, 0, 0, 1, 0, 0, 1, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 1, 0},
{0, 0, 1, 1, 1, 1, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
};
//--------------------------------------------------------------------------
while (true)
{
print(arr);
int x = 0;
int y = 0;
cout << "x";
cin >> x;
cout << "y";
cin >> y;
//----------------------------------------------------------------------
if (arr[x][y] == 1)
{
bool is_left = (x > 0 && arr[x - 1][y] == 1);
bool is_right = (x < r-1 && arr[x + 1][y] == 1);
bool is_up = (y > 0 && arr[x][y - 1] == 1);
bool is_down = (y < c-1 && arr[x][y + 1] == 1);
bool is_horizontal = (is_left || is_right);
bool is_vertical = (is_up || is_down);
int tx = x;
int ty = y;
if (is_horizontal)
{
while (arr[tx-1][ty] == 1) tx--; // Move pointer
while (arr[tx][ty] == 1) // Go right
{
arr[tx][ty] = 2;
tx++;
}
}
else if (is_vertical)
{
while (arr[tx][ty-1] == 1) ty--; // Move pointer
while (arr[tx][ty] == 1) // Go down
{
arr[tx][ty] = 2;
ty++;
}
}
arr[x][y] = 2;
}
print(arr, x, y);
cout << endl;
}
system("pause");
}
void print(int arr[r][c], int px /* = -1 */, int py /* = -1 */)
{
for (int y = 0; y < r; y++)
{
for (int x = 0; x < c; x++)
{
if (!(x == px && y == py))
{
switch (arr[x][y])
{
case 0: cout << "-"; break;
case 1: cout << "o"; break;
case 2: cout << "x"; break;
default: cout << "-"; break;
}
}
else
{
switch (arr[x][y])
{
case 0: cout << "="; break;
case 1: cout << "O"; break;
case 2: cout << "X"; break;
default: cout << "="; break;
}
}
}
cout << endl;
}
}
C:\Users\dream\OneDrive\Documents\AP\Tasks_AP>task8_2.exe
----------
-oooo-----
--------o-
--o-----o-
o-o-----o-
o-o--oo-o-
----------
-ooo------
-----ooo--
o-oo------
x1
y1
----------
-Xxxx-----
--------o-
--o-----o-
o-o-----o-
o-o--oo-o-
----------
-ooo------
-----ooo--
o-oo------
----------
-xxxx-----
--------o-
--o-----o-
o-o-----o-
o-o--oo-o-
----------
-ooo------
-----ooo--
o-oo------
x8
y4
----------
-xxxx-----
--------x-
--o-----x-
o-o-----X-
o-o--oo-x-
----------
-ooo------
-----ooo--
o-oo------
----------
-xxxx-----
--------x-
--o-----x-
o-o-----x-
o-o--oo-x-
----------
-ooo------
-----ooo--
o-oo------
x3
y7
----------
-xxxx-----
--------x-
--o-----x-
o-o-----x-
o-o--oo-x-
----------
-xxX------
-----ooo--
o-oo------
----------
-xxxx-----
--------x-
--o-----x-
o-o-----x-
o-o--oo-x-
----------
-xxx------
-----ooo--
o-oo------
x
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment