Skip to content

Instantly share code, notes, and snippets.

@squareiguana
Created February 20, 2014 15:03
Show Gist options
  • Save squareiguana/9115655 to your computer and use it in GitHub Desktop.
Save squareiguana/9115655 to your computer and use it in GitHub Desktop.
Example Using While Loops - Using Coding 101 - Episode 02
using System;
namespace SquareIguana.Coding101.Example.E02
{
class Program
{
/* Example on what was learned on Coding 101 - Episode 02 - While Loops
* I made the example trying not to use "advanced" concepts, and only what the episode was trying to teach.
*
* The first thing I think when talking about while loops is animations and games. And for games we need
* at least some ifs... so animation its a cool demostration for this example.
*
* The most useful thing in the example, I think is the use of the While loop to make different kinds of
* animations, also the border can be more complicated once we know about loops.
*
* This was built with the knowledge learned from Episode 01 and 02 and my last Example: https://gist.github.com/squareiguana/9113733
*
* Arrays are mentioned on the code that Lou provided with a link to MSDN, I didn't include them in this example
* because I think that will be the topic of a future episode.
*
* I'm going out of my way to use only the simple concepts, this definitely needs a refactor once they go into
* more "advanced" topics like methods, functions, classes, constants, ifs, for loops, etc. maybe I'll do
* a refactor or several, after some of those topics to show a more cleaner code.
*
* Definition of Refactor: To rewrite existing source code in order to improve its readability, reusability or
* structure without affecting its meaning or behaviour.
*/
static void Main(string[] args)
{
int width = 80;
int height = 25;
int cursor = 0;
const string BorderCharacter = "▀▄";
int border = BorderCharacter.Length;
ConsoleColor white = ConsoleColor.White;
ConsoleColor black = ConsoleColor.Black;
ConsoleColor green = ConsoleColor.DarkGreen;
ConsoleColor red = ConsoleColor.DarkRed;
ConsoleColor blue = ConsoleColor.DarkBlue;
ConsoleColor yellow = ConsoleColor.DarkYellow;
Console.Title = "Coding 101 - C# E02 - Using While Loops";
Console.SetWindowSize(width, height);
Console.BufferWidth = width;
Console.BufferHeight = height;
Console.BackgroundColor = white;
Console.ForegroundColor = black;
Console.Clear();
Console.BackgroundColor = black;
Console.ForegroundColor = green;
while (cursor < width - border)
{
Console.SetCursorPosition(cursor, 0);
Console.Write(BorderCharacter);
Console.SetCursorPosition(cursor, height - 1);
Console.Write(BorderCharacter);
// Still no If..Else Statements, Oh well... This "hack" will have to do
// The code below ↓ will work only for a Lenght of 1 or 2 Characters in the var BorderCharacters...
Console.SetCursorPosition(cursor, border - 1);
Console.Write(BorderCharacter);
Console.SetCursorPosition(cursor, height - border);
Console.Write(BorderCharacter);
cursor = cursor + border;
//Console.Beep(37, 10); // I could still be using this like the last example, but I don't see the point on continuing this futile path.
System.Threading.Thread.Sleep(10); // This is a better way to pause than the code above ↑
}
cursor = 0;
while (cursor < height - 1)
{
Console.SetCursorPosition(0, cursor);
Console.Write(BorderCharacter);
Console.SetCursorPosition(width - border, cursor);
Console.Write(BorderCharacter);
cursor = cursor + 1;
System.Threading.Thread.Sleep(10);
}
// This is to write the right lower corner without going to the next line and loosing the first line
Console.BufferWidth = Console.BufferWidth + 1; // Enlarge buffer width in order to prevent it from going to the next line
Console.SetCursorPosition(width - border, height - 1);
Console.Write(BorderCharacter);
Console.BufferWidth = Console.BufferWidth - 1; // Restore buffer to the original size
string headerTop = "╔══════════════════════════════════════════════════════════╗";
string headerFill = "║ ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒ ║";
string headerBottom = "╚══════════════════════════════════════════════════════════╝";
int headerWidth = headerTop.Length;
int headerHeight = 5;
int padding = border;
int horizontal = ((width - (headerWidth + (border * 2))) / 2) + border;
int vertical = height - border - 1;
Console.BackgroundColor = black;
Console.ForegroundColor = green;
Console.SetCursorPosition(padding, vertical - 4);
Console.Write(headerTop);
Console.SetCursorPosition(padding, vertical - 3);
Console.Write(headerFill);
Console.SetCursorPosition(padding, vertical - 2);
Console.Write(headerFill);
Console.SetCursorPosition(padding, vertical - 1);
Console.Write(headerFill);
Console.SetCursorPosition(padding, vertical);
Console.Write(headerBottom);
Console.ForegroundColor = white;
string headerText = " CODING 101 - Example Using E02 "; // Empty spaces before and after text to make it look better
Console.SetCursorPosition(padding + (headerWidth / 2) - (headerText.Length / 2), vertical - 2);
Console.Write(headerText);
Console.BackgroundColor = white;
Console.ForegroundColor = black;
vertical = vertical - 4;
while (padding < horizontal)
{
Console.MoveBufferArea(padding, vertical, headerWidth, headerHeight, padding + 1, vertical);
padding = padding + 1;
System.Threading.Thread.Sleep(50);
}
while (vertical > border)
{
Console.MoveBufferArea(padding, vertical, headerWidth, headerHeight, padding, vertical - 1);
vertical = vertical - 1;
System.Threading.Thread.Sleep(35);
}
padding = horizontal;
Console.CursorVisible = false;
Console.SetCursorPosition(padding, border + headerHeight + 1);
int start = 0;
int percent = 0;
int characterStart = border;
int currentCursorLeft = Console.CursorLeft;
int currentCursorTop = Console.CursorTop;
while (start < headerWidth)
{
// No If...Else Statements Or No Arrays... Well then lets do it the hard way
Console.BackgroundColor = blue;
Console.ForegroundColor = white;
percent = Convert.ToInt32(start / 0.6); // I don't want doubles, just pure numbers
//percent = (int)(i / 0.6); // I could have used an Explicit Conversion or Cast directly, instead of the above ↑
Console.SetCursorPosition((headerWidth / 2) - (60 / 2) + padding, border + headerHeight + 2);
Console.Write("▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓ Example is " + percent + " Percent Done!! ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓");
Console.BackgroundColor = white;
Console.ForegroundColor = yellow;
Console.SetCursorPosition(characterStart, border + headerHeight + 4);
Console.Write(" █████ ");
Console.SetCursorPosition(characterStart, border + headerHeight + 5);
Console.Write(" ███████");
Console.SetCursorPosition(characterStart, border + headerHeight + 6);
Console.Write(" █████░██");
Console.SetCursorPosition(characterStart, border + headerHeight + 7);
Console.Write(" ███░░█░ ");
Console.SetCursorPosition(characterStart, border + headerHeight + 8);
Console.Write(" ██░░░█░ ");
Console.SetCursorPosition(characterStart, border + headerHeight + 9);
Console.Write(" ██░░░░ ");
Console.SetCursorPosition(characterStart, border + headerHeight + 10);
Console.Write(" ██░░ ");
Console.SetCursorPosition(characterStart, border + headerHeight + 11);
Console.Write(" █░░█░░█");
Console.SetCursorPosition(characterStart, border + headerHeight + 12);
Console.Write(" ██░░░░█░");
Console.SetCursorPosition(characterStart, border + headerHeight + 13);
Console.Write(" ██████░");
Console.SetCursorPosition(characterStart, border + headerHeight + 14);
Console.Write(" ░░░███ ");
Console.SetCursorPosition(characterStart, border + headerHeight + 15);
Console.Write(" ███ ");
Console.MoveBufferArea(characterStart, 11, 9, 12, characterStart, border + headerHeight + 4);
characterStart = characterStart + 1;
Console.BackgroundColor = red;
Console.ForegroundColor = white;
Console.SetCursorPosition(currentCursorLeft, currentCursorTop);
Console.Write("/");
System.Threading.Thread.Sleep(75);
Console.SetCursorPosition(currentCursorLeft, currentCursorTop);
Console.Write("-");
System.Threading.Thread.Sleep(75);
Console.SetCursorPosition(currentCursorLeft, currentCursorTop);
Console.Write("\\");
System.Threading.Thread.Sleep(75);
Console.SetCursorPosition(currentCursorLeft, currentCursorTop);
Console.Write("|");
System.Threading.Thread.Sleep(75);
Console.BackgroundColor = white;
Console.ForegroundColor = yellow;
Console.SetCursorPosition(characterStart, border + headerHeight + 4);
Console.Write(" █████ ");
Console.SetCursorPosition(characterStart, border + headerHeight + 5);
Console.Write(" ███████");
Console.SetCursorPosition(characterStart, border + headerHeight + 6);
Console.Write(" █████░██");
Console.SetCursorPosition(characterStart, border + headerHeight + 7);
Console.Write(" ███░░█░ ");
Console.SetCursorPosition(characterStart, border + headerHeight + 8);
Console.Write(" ██░░░█░ ");
Console.SetCursorPosition(characterStart, border + headerHeight + 9);
Console.Write(" ██░░░░ ");
Console.SetCursorPosition(characterStart, border + headerHeight + 10);
Console.Write(" ██░░ ");
Console.SetCursorPosition(characterStart, border + headerHeight + 11);
Console.Write(" █░░█░░█");
Console.SetCursorPosition(characterStart, border + headerHeight + 12);
Console.Write(" ██░░███░");
Console.SetCursorPosition(characterStart, border + headerHeight + 13);
Console.Write(" █░████░");
Console.SetCursorPosition(characterStart, border + headerHeight + 14);
Console.Write(" ███░░░ ");
Console.SetCursorPosition(characterStart, border + headerHeight + 15);
Console.Write(" ███ ");
Console.MoveBufferArea(characterStart, 11, 9, 12, characterStart, border + headerHeight + 4);
currentCursorLeft = currentCursorLeft + 1;
Console.BackgroundColor = red;
Console.ForegroundColor = white;
Console.SetCursorPosition(currentCursorLeft, currentCursorTop);
Console.Write("/");
System.Threading.Thread.Sleep(75);
Console.SetCursorPosition(currentCursorLeft, currentCursorTop);
Console.Write("-");
System.Threading.Thread.Sleep(75);
Console.SetCursorPosition(currentCursorLeft, currentCursorTop);
Console.Write("\\");
System.Threading.Thread.Sleep(75);
Console.SetCursorPosition(currentCursorLeft, currentCursorTop);
Console.Write("|");
System.Threading.Thread.Sleep(75);
currentCursorLeft = currentCursorLeft + 1;
start = start + 2;
characterStart = characterStart + 1;
}
Console.BackgroundColor = red;
Console.ForegroundColor = white;
Console.SetCursorPosition(padding + 2, border + 2);
string over = " EXAMPLE IS NOW OVER ";
Console.MoveBufferArea(padding + 2, border + 1, headerWidth - (2 * 2), 1, padding + 2, border + 2, '▒', green, black);
Console.SetCursorPosition((headerWidth / 2) - (over.Length / 2) + padding, border + 2);
Console.Write(over);
// This will "erase" the character so there are no artifacts left after the Words in ASCII
Console.BackgroundColor = white;
Console.ForegroundColor = white;
Console.MoveBufferArea(border, border + headerHeight + 4, 9, 12, width - padding - 9, border + headerHeight + 4);
Console.BackgroundColor = white;
Console.ForegroundColor = green;
padding = ((width - (68 + (border * 2))) / 2) + border;
Console.SetCursorPosition(padding, border + headerHeight + 1);
Console.Write(" ██████╗ ██████╗ ██████╗ ██╗███╗ ██╗ ██████╗ ██╗ ██████╗ ██╗");
Console.SetCursorPosition(padding, border + headerHeight + 2);
Console.Write("██╔════╝██╔═══██╗██╔══██╗██║████╗ ██║██╔════╝ ███║██╔═████╗███║");
Console.SetCursorPosition(padding, border + headerHeight + 3);
Console.Write("██║ ██║ ██║██║ ██║██║██╔██╗ ██║██║ ███╗ ╚██║██║██╔██║╚██║");
Console.SetCursorPosition(padding, border + headerHeight + 4);
Console.Write("██║ ██║ ██║██║ ██║██║██║╚██╗██║██║ ██║ ██║████╔╝██║ ██║");
Console.SetCursorPosition(padding, border + headerHeight + 5);
Console.Write("╚██████╗╚██████╔╝██████╔╝██║██║ ╚████║╚██████╔╝ ██║╚██████╔╝ ██║");
Console.SetCursorPosition(padding, border + headerHeight + 6);
Console.Write(" ╚═════╝ ╚═════╝ ╚═════╝ ╚═╝╚═╝ ╚═══╝ ╚═════╝ ╚═╝ ╚═════╝ ╚═╝");
Console.ForegroundColor = black;
padding = ((width - (65 + (border * 2))) / 2) + border;
Console.SetCursorPosition(padding, border + headerHeight + 8);
Console.Write("╦ ╦┬┌┬┐┬ ┬ ╔═╗┬─┐ ╦═╗┌─┐┌┐ ┌─┐┬─┐┌┬┐ ╔╗ ┌─┐┬ ┬ ┌─┐┌─┐┌─┐┬─┐");
Console.SetCursorPosition(padding, border + headerHeight + 9);
Console.Write("║║║│ │ ├─┤ ╠╣ ├┬┘ ╠╦╝│ │├┴┐├┤ ├┬┘ │ ╠╩╗├─┤│ │ ├┤ │ ├┤ ├┬┘");
Console.SetCursorPosition(padding, border + headerHeight + 10);
Console.Write("╚╩╝┴ ┴ ┴ ┴ ╚ ┴└─o ╩╚═└─┘└─┘└─┘┴└─ ┴ ╚═╝┴ ┴┴─┘┴─┘└─┘└─┘└─┘┴└─");
padding = ((width - (49 + (border * 2))) / 2) + border;
Console.SetCursorPosition(padding, border + headerHeight + 11);
Console.Write("┌─┐┌┐┌┌┬┐ ╔═╗┬ ┬┌─┐┌┐┌┌┐┌┌─┐┌┐┌ ╔╦╗┌─┐┬─┐┌─┐┌─┐");
Console.SetCursorPosition(padding, border + headerHeight + 12);
Console.Write("├─┤│││ ││ ╚═╗├─┤├─┤│││││││ ││││ ║║║│ │├┬┘└─┐├┤ ");
Console.SetCursorPosition(padding, border + headerHeight + 13);
Console.Write("┴ ┴┘└┘─┴┘ ╚═╝┴ ┴┴ ┴┘└┘┘└┘└─┘┘└┘ ╩ ╩└─┘┴└─└─┘└─┘");
Console.BackgroundColor = black;
Console.ForegroundColor = green;
padding = ((width - (61 + (border * 2))) / 2) + border;
Console.SetCursorPosition(padding, border + headerHeight + 15);
Console.Write(" /') _/_ ( /_ _ _ ' _ -- / /|/| _ _ _ _ _ _ ");
Console.SetCursorPosition(padding, border + headerHeight + 16);
Console.Write(" (__()(/(- |/|/(// / /()/ -- (__()(/ / |(// (-_) ( (/ ");
Console.BackgroundColor = white;
Console.ForegroundColor = white;
Console.SetCursorPosition(border, border + 19);
Console.Read(); //Pause, doesn't matter what is written is on a white line so it doesn't show, unless it's more than 80 characters
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment