Last active
March 21, 2020 11:30
-
-
Save DreamerDeLy/879f6982da8886181621941e8cee3f96 to your computer and use it in GitHub Desktop.
Task 8 [C#]
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace Task_8 | |
{ | |
class Program | |
{ | |
// Array size | |
const int r = 10; | |
const int c = 10; | |
//---------------------------------------------------------------------+ | |
// Main loop | |
//---------------------------------------------------------------------+ | |
static void Main(string[] args) | |
{ | |
int[,] array = new int[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(array); | |
int x = 0; | |
int y = 0; | |
Console.WriteLine("x"); | |
x = Int32.Parse(Console.ReadLine()); | |
Console.WriteLine("y"); | |
y = Int32.Parse(Console.ReadLine()); | |
//-------------------------------------------------------------- | |
if (array[x, y] == 1) | |
{ | |
bool is_left = (x > 0 && array[x - 1, y] == 1); | |
bool is_right = (x < r - 1 && array[x + 1, y] == 1); | |
bool is_up = (y > 0 && array[x, y - 1] == 1); | |
bool is_down = (y < c - 1 && array[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 (array[tx-1, ty] == 1) tx--; | |
while (array[tx,ty] == 1) // Go right | |
{ | |
array[tx,ty] = 2; | |
tx++; | |
} | |
} | |
else if (is_vertical) | |
{ | |
while (array[tx, ty-1] == 1) ty--; // Move pointer | |
while (array[tx,ty] == 1) // Go down | |
{ | |
array[tx,ty] = 2; | |
ty++; | |
} | |
} | |
array[x,y] = 2; | |
} | |
Print(array, x, y); | |
Console.WriteLine(); | |
} | |
} | |
//---------------------------------------------------------------------+ | |
// Print array to console | |
//---------------------------------------------------------------------+ | |
static void Print(int[,] arr, 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: Console.Write("-"); break; | |
case 1: Console.Write("o"); break; | |
case 2: Console.Write("x"); break; | |
default: Console.Write("-"); break; | |
} | |
} | |
else | |
{ | |
switch (arr[x, y]) | |
{ | |
case 0: Console.Write("="); break; | |
case 1: Console.Write("O"); break; | |
case 2: Console.Write("X"); break; | |
default: Console.Write("="); break; | |
} | |
} | |
} | |
Console.WriteLine(); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
C:\Users\dream\OneDrive\Documents\AP\Task8>dotnet run | |
---------- | |
-oooo----- | |
--------o- | |
--o-----o- | |
o-o-----o- | |
o-o--oo-o- | |
---------- | |
-ooo------ | |
-----ooo-- | |
o-oo------ | |
x | |
1 | |
y | |
1 | |
---------- | |
-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------ | |
x | |
8 | |
y | |
5 | |
---------- | |
-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------ | |
x | |
0 | |
y | |
9 | |
---------- | |
-xxxx----- | |
--------x- | |
--o-----x- | |
o-o-----x- | |
o-o--oo-x- | |
---------- | |
-ooo------ | |
-----ooo-- | |
X-oo------ | |
---------- | |
-xxxx----- | |
--------x- | |
--o-----x- | |
o-o-----x- | |
o-o--oo-x- | |
---------- | |
-ooo------ | |
-----ooo-- | |
x-oo------ | |
x | |
2 | |
y | |
5 | |
---------- | |
-xxxx----- | |
--------x- | |
--x-----x- | |
o-x-----x- | |
o-X--oo-x- | |
---------- | |
-ooo------ | |
-----ooo-- | |
x-oo------ | |
---------- | |
-xxxx----- | |
--------x- | |
--x-----x- | |
o-x-----x- | |
o-x--oo-x- | |
---------- | |
-ooo------ | |
-----ooo-- | |
x-oo------ | |
x |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment