-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
105 lines (100 loc) · 3.75 KB
/
Program.cs
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
namespace RandomImageGenerator
{
internal class Program
{
private static void ScanSavesDirectory(ref string path, ref int id)
{
string[] files = Directory.GetFiles(path);
if (files.Length == 0) return;
for (int i = 0; i < files.Length; i++)
{
string filename = Path.GetFileName(files[i]);
try
{
if (filename.Split('.')[1] == "txt")
{
int value = Convert.ToInt32(filename.Split('.')[0]);
if (value >= id) id = value + 1;
}
}
catch {}
}
}
private static void SaveImage(char[] img)
{
string path = AppDomain.CurrentDomain.BaseDirectory + "/saved_images/";
int id = 1;
if (!Directory.Exists(path)) Directory.CreateDirectory(path);
else ScanSavesDirectory(ref path, ref id);
string imageString = "";
for (int i = 0; i < img.Length;i++)
{
if ((i + 1) % 120 == 0 & i + 1 < img.Length)
{
imageString += $"{img[i]}\n";
}
else imageString += img[i];
}
File.WriteAllText($"{path}/{id}.txt", imageString);
}
private static void RotateImage(ref char[] img)
{
char[] tmp = new char[img.Length];
Array.Copy(img, tmp, img.Length);
for (int i = 0, j = tmp.Length - 1; i < tmp.Length; i++, j--) img[i] = tmp[j];
}
private static void InvertImage(ref char[] img)
{
for (int i = 0; i < img.Length; i++)
{
if (img[i] == ' ') img[i] = '#';
else img[i] = ' ';
}
}
private static char[] NewImage()
{
return new RandomImageGenerator().image;
}
static void Main(string[] args)
{
Console.Title = "Random image generator";
Console.TreatControlCAsInput = false;
Console.Write($"* Random (abstract) image generator by HardcoreMagazine\n" +
$"* Github: https://github.com/HardcoreMagazine/RandomImageGenerator\n\n" +
$"* Controls:\n" +
$"** RIGHT ARROW - create new image\n" +
$"** S\t - save current image in file\n" +
$"** I\t - invert image colors\n" +
$"** R\t - rotate image 180 degrees\n" +
$"** ESC\t - exit program\n\n" +
$"* Images saved as text files under current program directory in 'saved_images' subfolder\n\n" +
$"* Press any key continue...");
Console.ReadKey(true);
char[] img = NewImage();
while (true)
{
Console.Clear();
Console.Write(img);
switch (Console.ReadKey(true).Key)
{
case ConsoleKey.RightArrow:
img = NewImage();
break;
case ConsoleKey.S:
SaveImage(img);
break;
case ConsoleKey.I:
InvertImage(ref img);
break;
case ConsoleKey.R:
RotateImage(ref img);
break;
case ConsoleKey.Escape:
return;
default:
break;
}
}
}
}
}