Last active
August 3, 2022 22:37
-
-
Save ZacharyPatten/9d75e32e197e9f1f9eab2ab2861d77c5 to your computer and use it in GitHub Desktop.
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 System; | |
using System.Text; | |
using System.Linq; | |
Console.Clear(); | |
Console.WriteLine(); | |
Console.WriteLine(" Frog Jump"); | |
Console.WriteLine(); | |
Console.WriteLine(" In this game two frogs are placed in the middle"); | |
Console.WriteLine(" of a line of poles of randomly generated lengths"); | |
Console.WriteLine(" and each frog will jump towards their end of the"); | |
Console.WriteLine(" line of poles while the poles get shorter."); | |
Console.WriteLine(); | |
Console.Write(" Press any key to continue..."); | |
Console.ReadKey(); | |
Console.OutputEncoding = Encoding.UTF8; | |
int[] poleHeights = new int[10]; | |
for (int i = 0; i < poleHeights.Length; i++) | |
{ | |
poleHeights[i] = Random.Shared.Next(1, 7); | |
} | |
int frog1Position = poleHeights.Length / 2 - 1; | |
int frog2Position = frog1Position + 1; | |
while (true) | |
{ | |
Console.Clear(); | |
Console.WriteLine(); | |
Console.WriteLine(RenderState()); | |
Console.Write(" Press any key to continue..."); | |
Console.ReadKey(); | |
bool jumped = false; | |
if (frog1Position > 0 && poleHeights[frog1Position - 1] <= poleHeights[frog1Position]) | |
{ | |
frog1Position--; | |
jumped = true; | |
} | |
if (frog2Position < poleHeights.Length - 1 && poleHeights[frog2Position] >= poleHeights[frog2Position + 1]) | |
{ | |
frog2Position++; | |
jumped = true; | |
} | |
if (!jumped) | |
{ | |
break; | |
} | |
} | |
Console.Clear(); | |
Console.WriteLine(); | |
Console.WriteLine(RenderState()); | |
Console.Write("Game over..."); | |
Console.ReadKey(); | |
string RenderState(string indent = " ") | |
{ | |
string[] frogSprite = | |
{ | |
"╭───╮", | |
"│^_^│", | |
"╰───╯", | |
}; | |
StringBuilder sb = new(); | |
int rows = poleHeights.Max() + frogSprite.Length; | |
for (int i = rows; i > 0; i--) | |
{ | |
sb.Append(indent); | |
for (int j = 0; j < poleHeights.Length; j++) | |
{ | |
int height = poleHeights[j]; | |
if ((j == frog1Position || j == frog2Position) && i > height && i <= height + 3) | |
{ | |
sb.Append(frogSprite[height + 3 - i]); | |
} | |
else | |
{ | |
sb.Append(height >= i ? "█████" : " "); | |
} | |
sb.Append(' '); | |
} | |
sb.AppendLine(); | |
} | |
return sb.ToString(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment