Skip to content

Commit

Permalink
Merge RunMenu methods into one.
Browse files Browse the repository at this point in the history
  • Loading branch information
Fuinny committed Jul 11, 2023
1 parent d51b90e commit d2bbdcd
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 57 deletions.
59 changes: 4 additions & 55 deletions Oligopoly/Source/Menu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public Menu(string prompt, string[] options)
/// Runs menu with options.
/// </summary>
/// <returns>An integer, that represents selected option.</returns>
public int RunMenu()
public int RunMenu(string[] descriptions = default)
{
ConsoleKey keyPressed;

Expand All @@ -46,62 +46,11 @@ public int RunMenu()
}
}

ConsoleKeyInfo keyInfo = Console.ReadKey();
keyPressed = keyInfo.Key;

switch (keyPressed)
if (descriptions != null)
{
case ConsoleKey.UpArrow:
SelectedIndex--;
if (SelectedIndex == -1)
{
SelectedIndex = Options.Length - 1;
}
break;
case ConsoleKey.DownArrow:
SelectedIndex++;
if (SelectedIndex > Options.Length - 1)
{
SelectedIndex = 0;
}
break;
Console.WriteLine("\nDescription:");
Console.WriteLine(descriptions[SelectedIndex]);
}
} while (keyPressed != ConsoleKey.Enter);

return SelectedIndex;
}

/// <summary>
/// Runs menu with options.
/// When an option is selected, its description is displayed at the bottom of the menu.
/// </summary>
/// <param name="descriptions">Descriptions of options.</param>
/// <returns>An integer, that represents selected option.</returns>
public int RunMenuWithDescription(string[] descriptions)
{
ConsoleKey keyPressed;

do
{
Console.Clear();
Console.WriteLine(Prompt);

for (int i = 0; i < Options.Length; i++)
{
if (i == SelectedIndex)
{
(Console.ForegroundColor, Console.BackgroundColor) = (Console.BackgroundColor, Console.ForegroundColor);
Console.WriteLine($"[*] {Options[i]}");
Console.ResetColor();
}
else
{
Console.WriteLine($"[ ] {Options[i]}");
}
}

Console.WriteLine("\nDescription:");
Console.WriteLine(descriptions[SelectedIndex]);

ConsoleKeyInfo keyInfo = Console.ReadKey();
keyPressed = keyInfo.Key;
Expand Down
4 changes: 2 additions & 2 deletions Oligopoly/Source/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ private static void DisplayGameModeScreen()
"Want to go full random? In this mode, your money and company shares are randomly generated."
};
Menu gameModeMenu = new Menu(prompt, options);
switch (gameModeMenu.RunMenuWithDescription(descriptions))
switch (gameModeMenu.RunMenu(descriptions))
{
case 0:
GameMode = "default";
Expand All @@ -184,7 +184,7 @@ private static void DisplayDifficultiesScreen()
"You will have 5000$\nYou will lose if your net worth drop below 3000$\nYou will win if your net worth will be over 100000$"
};
Menu difficultiesMenu = new Menu(prompt, options);
switch (difficultiesMenu.RunMenuWithDescription(descriptions))
switch (difficultiesMenu.RunMenu(descriptions))
{
case 0:
Difficulty = "easy";
Expand Down

0 comments on commit d2bbdcd

Please sign in to comment.