Skip to content

Instantly share code, notes, and snippets.

@squareiguana
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save squareiguana/9254664 to your computer and use it in GitHub Desktop.
Save squareiguana/9254664 to your computer and use it in GitHub Desktop.
Example (Text to Morse Code) Using For Loops - Using Coding 101 - Episode 03
using System;
namespace SquareIguana.Coding101.Example.E03
{
/* Example on what was learned on Coding 101 - Episode 03 - For Loops
* I made the example trying not to use "advanced" concepts, and only what the episode was trying to teach.
*
* I couldn't think of something interesting using only for loops, I could have use the animations of my last
* example and use for loops instead of while loops, but that is not interesting, so I did a basic animation
* of text like we see in some movies that the computer writes text, like a human, like in the Matrix, with
* Wake Up, NEO... but that is not enough so I made a Text to Morse Code translator
*
* The most useful thing in the example, I think is the text animation using array, but a cooler thing is
* if you uncomment the If statements you can hear the translated Morse Code, I didn't include them by default
* in the code, because If statements where not discussed on this episode or on previous ones, but it is a cool
* thing to hear the morse code, and the if statement is not complicated.
*
* This was built with the knowledge learned from Episode 01-03 and my last Example: https://gist.github.com/squareiguana/9115655
*
* 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 functions, classes, if statemens, 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.
*/
class Program
{
static void Main(string[] args)
{
int width = 80;
int height = 25;
string BorderCharacter = "◄►";
int border = BorderCharacter.Length;
ConsoleColor white = ConsoleColor.White;
ConsoleColor black = ConsoleColor.Black;
ConsoleColor gray = ConsoleColor.Gray;
ConsoleColor cyan = ConsoleColor.DarkCyan;
ConsoleColor green = ConsoleColor.DarkGreen;
ConsoleColor red = ConsoleColor.DarkRed;
ConsoleColor blue = ConsoleColor.DarkBlue;
ConsoleColor yellow = ConsoleColor.DarkYellow;
Console.Title = "Coding 101 - C# E03 - Using For Loops And Arrays";
Console.SetWindowSize(width, height);
Console.BufferWidth = width;
Console.BufferHeight = height;
Console.BackgroundColor = white;
Console.ForegroundColor = white;
Console.Clear();
// Same code for Drawing the Border of last example, except using for instead of while, and removing Thread.Sleep
Console.BackgroundColor = gray;
Console.ForegroundColor = red;
for(int cursor = 0; cursor < width - border; cursor += 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);
}
for (int cursor = 0; cursor < height - 1; cursor++)
{
Console.SetCursorPosition(0, cursor);
Console.Write(BorderCharacter);
Console.SetCursorPosition(width - border, cursor);
Console.Write(BorderCharacter);
}
// 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 = ((width - (headerWidth + (border * 2))) / 2) + border;
Console.BackgroundColor = black;
Console.ForegroundColor = white;
Console.SetCursorPosition(padding, border);
Console.Write(headerTop);
Console.SetCursorPosition(padding, border + 1);
Console.Write(headerFill);
Console.SetCursorPosition(padding, border + 2);
Console.Write(headerFill);
Console.SetCursorPosition(padding, border + 3);
Console.Write(headerFill);
Console.SetCursorPosition(padding, border + 4);
Console.Write(headerBottom);
string headerText = " CODING 101 - Example Using E03 "; // Empty spaces before and after text to make it look better
Console.SetCursorPosition(padding + (headerWidth / 2) - (headerText.Length / 2), border + 2);
Console.Write(headerText);
Console.ForegroundColor = black;
Console.BackgroundColor = white;
Console.SetCursorPosition(padding, border + headerHeight + 1);
string firstText = "What Do You Want To Say In Morse Code: ";
for (int i = 0; i < firstText.Length; i++)
{
Console.Write(firstText[i]);
System.Threading.Thread.Sleep(65);
}
string textToConvert = Console.ReadLine();
// Morse Code Reference: https://en.wikipedia.org/wiki/Morse_code
Char[] characters = new Char[] {'A', 'B', 'C', 'D', 'E', 'F', 'G','H', 'I', 'J', 'K',
'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'.', ',', '?', '\'', '!', '/', '(', ')', '&', ':', ';',
'=', '+', '-', '_', '"', '$', '@', ' ',
'#', '%', '*', '<', '>', '[', '\\', ']', '^', '`', '{', '|', '}', '~'};
String[] morseCodeRepresentation = new String[] {"·–", "–···", "–·–·", "–··", "·", "··–·", "––·", "····", "··", "·–––", "–·–",
"·–··", "––", "–·", "–––", "·––·", "––·–", "·–·", "···", "–", "··–", "···–", "·––", "–··–", "–·––", "––··",
"–––––", "·––––", "··–––", "···––", "····–", "·····", "–····", "––···", "–––··", "––––·",
"·–·–·–", "––··––", "··––··", "·––––·", "–·–·––", "–··–·", "–·––·", "–·––·–", "·–···", "–––···", "–·–·–·",
"–···–", "·–·–·", "–····–", "··––·–", "·–··–·", "···–··–", "·––·–·", " ",
"NA", "NA", "NA", "NA", "NA", "NA", "NA", "NA", "NA", "NA", "NA", "NA", "NA", "NA"};
// NA is Not Available, this are some of the common characters that are not available on Morse Code,
// This was necessary to cover the basis because the program will crash if an input character is not in the
// characters array, the fix to this problem is a simple If statement, but that was not the topic of this episode
Console.SetCursorPosition(padding, Console.CursorTop);
Console.ForegroundColor = blue;
Console.CursorVisible = false;
string secondText = string.Format("We are about to translate '{0}' to Morse Code", textToConvert);
for (int i = 0; i < secondText.Length; i++)
{
Console.Write(secondText[i]);
System.Threading.Thread.Sleep(100);
}
Console.SetCursorPosition(padding, Console.CursorTop + 1);
Console.ForegroundColor = green;
Console.Write("The Morse Code is: ");
Console.BackgroundColor = black;
Console.ForegroundColor = white;
textToConvert = textToConvert.ToUpper();
string morseCodeForLetter = "";
int arrayIndex = -1; // -1 is Not Found for Array.IndexOf
int duration = 100;
for (int i = 0; i <= textToConvert.Length - 1; i++)
{
// This Will Crash if you input a character Not found in the character Array, I tried to cover the basis,
// but still, without if staments there is not much we can do, :
arrayIndex = Array.IndexOf(characters, textToConvert[i]);
// The potential Crash of the program can be fix if you uncomment the line below ↓
//if (arrayIndex == -1 || morseCodeRepresentation[arrayIndex].Equals("NA")) continue;
morseCodeForLetter = morseCodeRepresentation[arrayIndex];
// With If statments we can do more cool things like make the sounds
// Uncomment the code below ↓ to also do the classic morse code beeping sounds
//for (int x = 0; x <= morseCodeForLetter.Length - 1; x++)
//{
// if (morseCodeForLetter[x].Equals('·'))
// {
// Console.Beep(700, duration);
// }
// else if (morseCodeForLetter[x].Equals('–'))
// {
// Console.Beep(700, duration * 3);
// }
// else if (morseCodeForLetter[x].Equals(" "))
// {
// System.Threading.Thread.Sleep(duration * 7);
// }
// System.Threading.Thread.Sleep(duration);
//}
Console.Write(morseCodeForLetter);
}
System.Threading.Thread.Sleep(3000);
Console.BackgroundColor = red;
Console.ForegroundColor = white;
Console.SetCursorPosition(padding + 2, border + 2);
string over = " EXAMPLE IS NOW OVER ";
Console.MoveBufferArea(padding + 3, border + 1, headerWidth - (3 * 2), 1, padding + 3, border + 2, '▒', white, black);
Console.SetCursorPosition((headerWidth / 2) - (over.Length / 2) + padding, border + 2);
Console.Write(over);
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