Skip to content

Commit c5375d3

Browse files
Sullivan008kzk-ptr
authored andcommitted
Create Draw Method in RainOperation Class
1 parent 83cce7b commit c5375d3

15 files changed

+104
-5
lines changed

MatrixRain.Console/Program.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ private static void Main(string[] args)
1515
RainOperation rainOperation = new RainOperation(consoleOperation);
1616

1717
rainOperation.MatrixInitialize();
18-
19-
System.Console.ReadKey();
18+
rainOperation.Draw();
2019
}
2120
}
2221
}
0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.
1.5 KB
Binary file not shown.
2 KB
Binary file not shown.
Binary file not shown.
0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.

MatrixRain.Core/Operations/Console/ConsoleOperation.cs

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,23 @@ public class ConsoleOperation
1717

1818
private static IntPtr _thisConsole;
1919

20+
private readonly Random _random;
21+
22+
private char GetRandomChar { get { return GenerateRandomChar(); } }
23+
2024
public int ConsoleWidth { get { return System.Console.WindowWidth; } }
2125

2226
public int ConsoleHeight { get { return System.Console.WindowHeight; } }
2327

28+
public ConsoleColor SetConsoleForeground { set => System.Console.ForegroundColor = value; }
29+
30+
public ConsoleColor GetConsoleForeground { get => System.Console.ForegroundColor; }
31+
2432
public ConsoleOperation()
2533
{
2634
_thisConsole = GetConsoleWindow();
35+
36+
_random = new Random();
2737
}
2838

2939
public void Initialize()
@@ -40,13 +50,45 @@ public void Show()
4050
ShowWindow(_thisConsole, MAXIMIZE_CODE);
4151
}
4252

43-
public void SetCursorPosition()
53+
public void SetCursorPosition(int xAxisPosition, int yAxisPosition)
4454
{
55+
System.Console.SetCursorPosition(xAxisPosition, yAxisPosition);
56+
}
4557

58+
public void WriteCharacter(bool isRandom = false, bool isWhiteSpace = false)
59+
{
60+
if (isRandom && !isWhiteSpace)
61+
{
62+
System.Console.Write(GetRandomChar);
63+
}
64+
else if (!isRandom && isWhiteSpace)
65+
{
66+
System.Console.Write(' ');
67+
}
4668
}
4769

4870
#region PRIVATE Helper Methods
71+
private char GenerateRandomChar()
72+
{
73+
int randomCharKeyCode = _random.Next(10);
74+
75+
if (randomCharKeyCode <= 2)
76+
{
77+
return (char)('0' + _random.Next(10));
78+
}
4979

80+
if (randomCharKeyCode <= 4)
81+
{
82+
return (char)('a' + _random.Next(27));
83+
}
84+
85+
if (randomCharKeyCode <= 6)
86+
{
87+
return (char)('A' + _random.Next(27));
88+
}
89+
90+
return (char)(_random.Next(32, 255));
91+
}
5092
#endregion
5193
}
5294
}

MatrixRain.Core/Operations/Rain/RainOperation.cs

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using MatrixRain.Core.Operations.Console;
1+
using MatrixRain.Core.Common;
2+
using MatrixRain.Core.Operations.Console;
23
using System;
34

45
namespace MatrixRain.Core.Operations.Rain
@@ -32,13 +33,70 @@ public void MatrixInitialize()
3233
int heightFirstSection = _height / 2;
3334
int heightSecondSection = heightFirstSection / 2;
3435

35-
for(int i = 0; i < _width; i++)
36+
for (int i = 0; i < _width; i++)
3637
{
3738
_yAxisFirstSection[i] = _random.Next(_height);
3839
_yAxisSecondSection[i] =
3940
_random.Next(heightSecondSection * (i % 11 != 10 ? 2 : 1),
4041
heightFirstSection * (i % 11 != 10 ? 2 : 1));
4142
}
4243
}
44+
45+
public void Draw()
46+
{
47+
while (true)
48+
{
49+
DrawCharacters();
50+
}
51+
}
52+
53+
#region PRIVATE Helper Methods
54+
private void DrawCharacters()
55+
{
56+
for (int i = 0; i < _width; ++i)
57+
{
58+
if (i % 11 == 10)
59+
{
60+
_consoleOperation.SetConsoleForeground = ConsoleColors.DarkGray;
61+
}
62+
else
63+
{
64+
_consoleOperation.SetConsoleForeground = ConsoleColors.DarkGreen;
65+
}
66+
67+
_consoleOperation.SetCursorPosition(i,
68+
GetYAxisPosition(_yAxisFirstSection[i] - 2 - (_yAxisSecondSection[i] / 40 * 2), _height));
69+
70+
_consoleOperation.WriteCharacter(true, false);
71+
72+
if (_consoleOperation.GetConsoleForeground == ConsoleColors.DarkGreen)
73+
{
74+
_consoleOperation.SetConsoleForeground = ConsoleColors.Green;
75+
}
76+
else if (_consoleOperation.GetConsoleForeground == ConsoleColors.DarkGray)
77+
{
78+
_consoleOperation.SetConsoleForeground = ConsoleColors.White;
79+
}
80+
81+
_consoleOperation.SetCursorPosition(i, GetYAxisPosition(_yAxisFirstSection[i] + 1, _height));
82+
_consoleOperation.WriteCharacter(true, false);
83+
84+
_yAxisFirstSection[i] = GetYAxisPosition(_yAxisFirstSection[i] + 1, _height);
85+
86+
_consoleOperation.SetCursorPosition(i,
87+
GetYAxisPosition(_yAxisFirstSection[i] - _yAxisSecondSection[i], _height));
88+
_consoleOperation.WriteCharacter(false, true);
89+
90+
_consoleOperation.SetConsoleForeground = ConsoleColors.Green;
91+
}
92+
}
93+
94+
private int GetYAxisPosition(int yAxisPosition, int height)
95+
{
96+
yAxisPosition %= height;
97+
98+
return yAxisPosition < 0 ? (yAxisPosition + height) : yAxisPosition;
99+
}
100+
#endregion
43101
}
44102
}

0 commit comments

Comments
 (0)