C# – Eggcellent – Painting on the console
Sometimes, you may see something that seems rather easy to be achieved, you have a vision in your mind regarding it and it still takes 5 times more time and patience to get it. Today, after taking a look at what seemed to be rather easy task with printing on the console an egg, I was pretty sure that I can come up with a working solution within 15 minutes.
The task was similar to the one with the boats, its logic and methodology was exactly the same, but still I needed about 1.5 hours of what seemed to be an easily digestible task. Quite funny, eh?
The question is why? First, I had problems with the fact that the vertical lines of the egg were not defined in size. I created some easy formula to define them, but it did not work with more than 3 of the types. Then, it seemed that it is not that easy to reverse the first part of the egg, and after some 30 minutes of trying to do it, I simply decided to build it up again from scratch. Thus, the code written by me became 144 lines, with about 60% of it a redundant one.
Considering the fact, that I have already spent some hours writing the code, I have decided to publish it anyhow, without further edition. Yes, it can be upgraded and improved, but for the sake of the fact that it gives 100% on the tests, it is quite OK. Brace yourself, here it comes:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
class Egg
{
static void Main()
{
int N = int.Parse(Console.ReadLine());
int height = 2 * N;
int Eggwidth = 3 * N - 1;
int drawingAreaWidth = 3 * N + 1;
int counterHeight = 0;
//Top
Console.Write(new string('.', 1));
Console.Write(new string('.', N));
Console.Write(new string('*', Eggwidth - N * 2));
Console.Write(new string('.', N));
Console.Write(new string('.', 1));
counterHeight++;
Console.WriteLine();
//print slope top side:
int loopSize = N - 2;
int mask = N;
if (loopSize >= 1)
{
for (int i = 0; i < loopSize; i++)
{
if (mask < 1)
{
break;
}
Console.Write(new string('.', 1));
Console.Write(new string('.', mask - 2));
Console.Write(new string('*', 1));
Console.Write(new string('.', Eggwidth - mask * 2 + 2));
Console.Write(new string('*', 1));
Console.Write(new string('.', mask - 2));
Console.Write(new string('.', 1));
mask -= 2;
counterHeight++;
Console.WriteLine();
}
}
//print top sides:
int maskWidth = (N * 2 - (counterHeight * 2)-2)/2;
if (maskWidth > 0)
{
for (int i = 0; i < maskWidth; i++)
{
Console.Write(new string('.', 1));
Console.Write(new string('*', 1));
Console.Write(new string('.', Eggwidth - 2));
Console.Write(new string('*', 1));
Console.Write(new string('.', 1));
counterHeight++;
Console.WriteLine();
}
}
//Printing the middle:
Console.Write(new string('.', 1));
Console.Write(new string('*', 1));
for (int i = 0; i < Eggwidth - 2; i++)
{
if (i % 2 == 0)
{
Console.Write('@');
}
else
{
Console.Write('.');
}
}
Console.Write(new string('*', 1));
Console.Write(new string('.', 1));
counterHeight++;
Console.WriteLine();
//Second line of the middle:
Console.Write(new string('.', 1));
Console.Write(new string('*', 1));
for (int i = 0; i < Eggwidth - 2; i++)
{
if (i % 2 == 1)
{
Console.Write('@');
}
else
{
Console.Write('.');
}
}
Console.Write(new string('*', 1));
Console.Write(new string('.', 1));
counterHeight++;
Console.WriteLine();
//print bottom:
//print bottom sides:
for (int i = 0; i < maskWidth; i++)
{
Console.Write(new string('.', 1));
Console.Write(new string('*', 1));
Console.Write(new string('.', Eggwidth - 2));
Console.Write(new string('*', 1));
Console.Write(new string('.', 1));
counterHeight++;
Console.WriteLine();
}
int loopsLeft = height - counterHeight - 1;
int z = 0;
for (int i = 0; i < loopsLeft; i++)
{
Console.Write(new string('.', 1));
Console.Write(new string('.', z));
Console.Write(new string('*', 1));
Console.Write(new string('.', Eggwidth-2*z-2));
Console.Write(new string('*', 1));
Console.Write(new string('.', z));
Console.Write(new string('.', 1));
z += 2;
Console.WriteLine();
}
Console.Write(new string('.', 1));
Console.Write(new string('.', N));
Console.Write(new string('*', Eggwidth - N * 2));
Console.Write(new string('.', N));
Console.Write(new string('.', 1));
counterHeight++;
Console.WriteLine();
}
}
So, that’s all 🙂

