Instantly share code, notes, and snippets.
Last active
June 22, 2020 18:02
-
Star
(0)
0
You must be signed in to star a gist -
Fork
(0)
0
You must be signed in to fork a gist
-
Save janell-baxter/b5d55dfda692d48a342401aa2ca8b8a5 to your computer and use it in GitHub Desktop.
Demo for Introduction to Programming: Repetitive Lyrics (conditional statements, loops, random, array)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using static System.Console; | |
using System; | |
namespace RepetitiveLyricsDemo | |
{ | |
//Practice refactoring on this code | |
//What are the redundencies? | |
//How could they be abstracted out into separate operations? | |
class Menu | |
{ | |
int originalWindowWidth = Console.WindowWidth; | |
int originalWindowHeight = Console.WindowHeight; | |
public Menu() | |
{ | |
//constructor | |
Title = "Repetitive Lyrics Demo"; | |
Run(); | |
} | |
private void Run() | |
{ | |
//NOTE: window width and size changes based on screen resolution | |
//uses character cell rows and columns, not pixels | |
//you can declare more than one variable of the same type like this: | |
int newWindowWidth, newWindowHeight; | |
newWindowWidth = originalWindowWidth / 2; | |
newWindowHeight = originalWindowHeight / 2; | |
//SetWindowSize (int width, int height); | |
SetWindowSize(newWindowWidth, newWindowHeight); | |
//set foreground and background colors | |
SetWindowColors(ConsoleColor.White, ConsoleColor.DarkCyan); | |
Clear(); | |
WriteLine("Welcome to the Repetitive Lyrics Demo."); | |
ListSongs(); | |
GetInput(); | |
//recursive function call | |
Run(); | |
} | |
private void ListSongs() | |
{ | |
WriteLine("We have lyrics to these songs:"); | |
WriteLine("a) Ten Bears"); | |
WriteLine("b) There Was an Old Lady Who Swallowed a Fly"); | |
} | |
private void GetInput() | |
{ | |
WriteLine("Which song would you like to see? Enter a or b:"); | |
string input = ReadLine(); | |
if (input.ToLower() == "a") | |
{ | |
BearSong(); | |
} | |
else if (input.ToLower() == "b") | |
{ | |
LadySong(); | |
} | |
else if (input.ToLower() == "exit") | |
{ | |
//this is a blunt-force way of closing the app | |
Environment.Exit(0); | |
} | |
else | |
{ | |
ShowCreature(); | |
} | |
} | |
#region Songs | |
/* | |
* A region is an area that you can expand/collapse | |
* It helps with organization of large files | |
*/ | |
private void BearSong() | |
{ | |
//LargestWindowWith and LargestWindowHeight | |
SetWindowSize(LargestWindowWidth - 140, LargestWindowHeight - 10); | |
//set foreground and background colors | |
SetWindowColors(ConsoleColor.Black, ConsoleColor.Green); | |
Clear(); | |
Title = "Ten Bears"; | |
WriteLine("................\n"); | |
WriteLine("Ten Bears"); | |
WriteLine("................\n"); | |
//loop | |
for (int currentBears = 10; currentBears > 1; currentBears--) | |
{ | |
//concatenation | |
WriteLine(currentBears + " bears in the bed and the little one said \"I'm crowded... roll over...\""); | |
WriteLine("So they all rolled over and one fell out...\n"); | |
} | |
WriteLine("One bear in the bed and the little one said \"I'm lonely\""); | |
string bearImage = @" | |
{''-''} | |
(o o) | |
,--`Y'--. | |
``: ;'' | |
/ _ \ | |
()' `() | |
"; | |
WriteLine(bearImage); | |
Pause(); | |
} | |
private void LadySong() | |
{ | |
Title = "There was an old lady..."; | |
Clear(); | |
//change just the height of the window | |
WindowHeight = LargestWindowHeight - 20; | |
string header = @" | |
The )'( | |
Old Lady .-. .-. . | |
Who... _.' `._.' `._.' | |
"; | |
ForegroundColor = ConsoleColor.Magenta; | |
WriteLine(header); | |
//set foreground color | |
ForegroundColor = ConsoleColor.Black; | |
string reason = "She swallowed the {0} to catch the {1}"; | |
string[] creatures = { "fly", "spider", "bird", "cat", "dog", "goat", "cow", "horse" }; | |
string[] comments = | |
{ | |
"I don't know why she swallowed that fly.\nDon't ask me why!\n", | |
"That wiggled and jiggled and tickled inside her", | |
"How absurd, to swallow a bird", | |
"Imagine that. She swallowed a cat", | |
"What a hog to swallow a dog", | |
"She just opened her throat and swallowed that goat", | |
"I don't know how she swallowed that cow", | |
"She's full of course!" | |
}; | |
int max = creatures.Length; | |
for (int i = 0; i < max; i++) | |
{ | |
//composite formatting | |
WriteLine("There was an old lady who swallowed a {0}", creatures[i]); | |
WriteLine(comments[i]); | |
for (int j = i; j > 0 && i < max - 1; j--) | |
{ | |
WriteLine(reason, creatures[j], creatures[j - 1]); | |
if (j == 1) | |
{ | |
WriteLine(comments[j - 1]); | |
} | |
} | |
Pause(); | |
} | |
} | |
#endregion | |
#region Creatures | |
private void ShowCreature() | |
{ | |
string bear = @" | |
{''-''} | |
(. .) | |
,--`-'--. | |
``: ;'' | |
/ _ \ | |
()' `() | |
"; | |
string lizard = @" | |
)/_ | |
_.--..---' -,--c_ | |
\L..' ._O__)_ | |
,-._.+ _ \..--( / a:f | |
`\.-''__.- ' \ ( \_ | |
`''' `\__ /\ | |
') | |
(lizard by Andreas Freise) | |
"; | |
string frog = @" | |
_e-e_ | |
_(-._.-)_ | |
.-( `---' )-. hjw | |
__\ \\\___/// /__ | |
'-._.'/M\ /M\`._,- | |
(frog by Hayley Jane Wakenshaw) | |
"; | |
string cat= @" | |
,_ _ | |
|\\_,-~/ | |
/ _ _ | ,--. | |
( @ @ ) / ,-' | |
\ _T_/-._( ( | |
/ `. \ | |
| _ \ | | |
\ \ , / | | |
|| |-_\__ / | |
((_/`(____,-' | |
"; | |
CreatureLoops(3, "cat", cat); | |
} | |
private void CreatureLoops(int numberToShow, string creatureName, string creatureImage) | |
{ | |
Title = $"Loop examples with a {creatureName}"; | |
SetWindowSize(LargestWindowWidth - 100, LargestWindowHeight - 10); | |
//set foreground and background colors | |
SetWindowColors(ConsoleColor.White, ConsoleColor.DarkBlue); | |
Clear(); | |
int totalShown = 0; | |
//WHILE LOOP | |
//interpolation | |
WriteLine($"WHILE LOOP\nA 'while' loop showing the {creatureName} while total shown is less than {numberToShow}."); | |
while (totalShown < numberToShow) | |
{ | |
WriteLine($"Times the {creatureName} has previously been shown: {totalShown}\n"); | |
WriteLine(creatureImage); | |
totalShown++; | |
Pause(); | |
} | |
// DO WHILE | |
WriteLine($"DO WHILE LOOP\nTimes the {creatureName} has been shown: {totalShown}"); | |
WriteLine($"A 'do while' loop showing {creatureName} while total shown is less than {numberToShow}...."); | |
do | |
{ | |
WriteLine(creatureImage); | |
totalShown++; | |
} while (totalShown < numberToShow); | |
WriteLine($"Why did the cat show again?\nEven if the total shown is already greater than one, a DO WHILE loop runs at least once.\n"); | |
Pause(); | |
//FOR LOOP | |
//Give the creature a random name | |
//array of names | |
string[] names = { "Jordan", "Cale", "Mohamad", "Hope", "Thi", "Laura", "Liselle" }; | |
WriteLine($"FOR LOOP\nSetting counter to 0."); | |
WriteLine($"A 'for' loop showing the {creatureName} x {numberToShow}.\nAlso showing an example of random information - in this case a name - pulled from an array."); | |
for (int counter = 0; counter < numberToShow; counter++) | |
{ | |
//Note the + sign used for two different purposes! | |
//first for a mathematical operation, then for concatenation | |
//do you know how the compiler can tell the difference? | |
WriteLine((counter + 1) + ") " + names[GetRandomNumber(names.Length)] + Environment.NewLine + creatureImage); | |
} | |
Pause(); | |
} | |
#endregion | |
#region Utility | |
private void Pause() | |
{ | |
WriteLine("Press any key to continue..."); | |
ReadKey(); | |
Clear(); | |
} | |
private void SetWindowColors(ConsoleColor background, ConsoleColor foreground) | |
{ | |
BackgroundColor = background; | |
ForegroundColor = foreground; | |
} | |
//Random | |
//note the instance instantiation is outside of the method | |
//try putting it inside the method and running the app | |
//to see the difference! | |
Random random = new Random(); | |
private int GetRandomNumber(int range) | |
{ | |
return (random.Next(range)); | |
} | |
#endregion | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Repetitive Lyrics Demo | |
Janell Baxter, 6/20/2020 | |
Demo for Introduction to Programming: Repetitive Lyrics (conditional statements, loops, random, array) | |
Incorporates code from: | |
10 Bears: http://programmingisfun.com/c-sharp-loops/ | |
Repetitive Lyrics: http://programmingisfun.com/repetitive-lyrics/ | |
*/ | |
namespace RepetitiveLyricsDemo | |
{ | |
class Program | |
{ | |
static void Main() | |
{ | |
new Menu(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment