Skip to content

Commit

Permalink
Merge pull request #4 from Fuinny/pause-menu
Browse files Browse the repository at this point in the history
  • Loading branch information
Fuinny committed Jul 12, 2023
2 parents d2bbdcd + edf7662 commit 7a53009
Show file tree
Hide file tree
Showing 2 changed files with 208 additions and 21 deletions.
63 changes: 47 additions & 16 deletions Oligopoly/Source/Menu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,17 @@ public Menu(string prompt, string[] options)
SelectedIndex = 0;
}

// <summary>
// Runs menu with options.
// </summary>
// <returns>An integer, that represents selected option.</returns>
/// <summary>
/// Runs menu with options.
/// </summary>
/// <param name="isPausable">Determines whether the menu can be paused. The default is false.</param>
/// <param name="descriptions">Contains all option descriptions. The default is null.</param>
/// <returns>An integer, that represents selected option.</returns>
public int RunMenu(string[] descriptions = default)
public int RunMenu(string[] descriptions = default, bool isPausable = false)
{
ConsoleKey keyPressed;

Expand Down Expand Up @@ -55,22 +61,47 @@ public int RunMenu(string[] descriptions = default)
ConsoleKeyInfo keyInfo = Console.ReadKey();
keyPressed = keyInfo.Key;

switch (keyPressed)
if (isPausable)
{
case ConsoleKey.UpArrow:
SelectedIndex--;
if (SelectedIndex == -1)
{
SelectedIndex = Options.Length - 1;
}
break;
case ConsoleKey.DownArrow:
SelectedIndex++;
if (SelectedIndex > Options.Length - 1)
{
SelectedIndex = 0;
}
break;
switch (keyPressed)
{
case ConsoleKey.P:
return -1;
case ConsoleKey.UpArrow:
SelectedIndex--;
if (SelectedIndex == -1)
{
SelectedIndex = Options.Length - 1;
}
break;
case ConsoleKey.DownArrow:
SelectedIndex++;
if (SelectedIndex > Options.Length - 1)
{
SelectedIndex = 0;
}
break;
}
}
else
{
switch (keyPressed)
{
case ConsoleKey.UpArrow:
SelectedIndex--;
if (SelectedIndex == -1)
{
SelectedIndex = Options.Length - 1;
}
break;
case ConsoleKey.DownArrow:
SelectedIndex++;
if (SelectedIndex > Options.Length - 1)
{
SelectedIndex = 0;
}
break;
}
}
} while (keyPressed != ConsoleKey.Enter);

Expand Down
166 changes: 161 additions & 5 deletions Oligopoly/Source/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ public class Program
/// </summary>
public static void Main()
{
if (!Directory.Exists("Saves"))
{
Directory.CreateDirectory("Saves");
}
if (OperatingSystem.IsWindows())
{
Console.CursorVisible = false;
Expand Down Expand Up @@ -96,6 +100,111 @@ private static void LoadEmbeddedResources()
}
}

/// <summary>
/// Saves the current stage of the game.
/// </summary>
private static void SaveGame()
{
int fileCounter = 1;
string fileName = $"Save_{fileCounter}.xml";
string filePath = Path.Combine("Saves", fileName);

try
{
while (File.Exists(filePath))
{
fileName = $"Save_{fileCounter}.xml";
filePath = Path.Combine("Saves", fileName);
fileCounter++;
}
XDocument saveFile = new XDocument(
new XElement("SaveFile",
new XElement("GameMode", GameMode),
new XElement("Difficulty", Difficulty),
new XElement("CurrentTurn", TurnCounter),
new XElement("Money", Money),
new XElement("SharePrices", Companies.Select(company => new XElement($"{company.Name.Replace(" ", "_")}", company.SharePrice))),
new XElement("BuyedShares", Companies.Select(company => new XElement($"{company.Name.Replace(" ", "_")}", company.NumberOfShares)))
)
);
saveFile.Save(filePath);
}
catch (Exception ex)
{
Console.WriteLine($"Error! \nDetails: {ex.Message}");
Console.WriteLine("Press any key to exit the menu...");
Console.ReadKey(true);
}

Console.WriteLine($"\nYour file was successfully saved with the name {fileName}");
Console.WriteLine("You can find all of your save files in Saves folder.");
Console.WriteLine("\nPress any key to exit the menu...");
Console.ReadKey(true);
}

/// <summary>
/// Loads the game from already created saves.
/// </summary>
private static bool LoadGame()
{
string[] saveFiles = Directory.GetFiles("Saves", "*.xml");

if (saveFiles.Length == 0)
{
Console.Clear();
Console.WriteLine("No save files found.");
Console.WriteLine("\nPress any key to exit the menu...");
Console.ReadKey(true);
return false;
}

Menu loadMenu = new Menu("Select file to load: ", saveFiles);
int selectedFile = loadMenu.RunMenu();

try
{
XDocument saveFile = XDocument.Load(saveFiles[selectedFile]);
GameMode = saveFile.Element("SaveFile").Element("GameMode").Value;
Difficulty = saveFile.Element("SaveFile").Element("Difficulty").Value;
TurnCounter = int.Parse(saveFile.Element("SaveFile").Element("CurrentTurn").Value);
Money = decimal.Parse(saveFile.Element("SaveFile").Element("Money").Value);
var sharePrices = saveFile.Element("SaveFile").Element("SharePrices").Elements();
var buyedShares = saveFile.Element("SaveFile").Element("BuyedShares").Elements();

foreach (var companyElement in sharePrices)
{
string companyName = companyElement.Name.LocalName.Replace("_", " ");
decimal sharePrice = decimal.Parse(companyElement.Value);

Company company = Companies.FirstOrDefault(c => c.Name == companyName);
if (company != null)
{
company.SharePrice = sharePrice;
}
}
foreach (var companyElement in buyedShares)
{
string companyName = companyElement.Name.LocalName.Replace("_", " ");
int numberOfShares = int.Parse(companyElement.Value);

Company company = Companies.FirstOrDefault(c => c.Name == companyName);
if (company != null)
{
company.NumberOfShares = numberOfShares;
}
}
}
catch (Exception ex)
{
Console.WriteLine($"Error! \nDetails: {ex.Message}");
Console.WriteLine("Press any key to exit the menu...");
Console.ReadKey(true);
return false;
}

return true;
}

/// <summary>
/// Displays main menu to the console.
/// </summary>
Expand All @@ -110,7 +219,7 @@ private static void DisplayMainMenuScreen()
╚═════╝ ╚══════╝╚═╝ ╚═════╝ ╚═════╝ ╚═╝ ╚═════╝ ╚══════╝╚═╝
Use up and down arrow keys to select an option
";
string[] options = { "Play", "About", "Exit" };
string[] options = { "Play", "Load", "About", "Exit" };
Menu mainMenu = new Menu(prompt, options);
while (true)
{
Expand Down Expand Up @@ -139,15 +248,59 @@ private static void DisplayMainMenuScreen()
GameLoop();
break;
case 1:
DisplayAboutGameMenu();
LoadEmbeddedResources();
if (LoadGame())
{
switch (GameMode)
{
case "default":
InitializeGame();
break;
case "random":
LosingNetWorth = 2000.00M;
WinningNetWorth = 50000.00M;
break;
}
GameLoop();
}
break;
case 2:
DisplayAboutGameMenu();
break;
case 3:
DisplayExitMenu();
break;
}
}
}

private static void DisplayPauseMenu()
{
string prompt = @"
██████╗ █████╗ ███╗ ███╗███████╗ ██████╗ ███╗ ██╗ ██████╗ █████╗ ██╗ ██╗███████╗███████╗
██╔════╝ ██╔══██╗████╗ ████║██╔════╝ ██╔═══██╗████╗ ██║ ██╔══██╗██╔══██╗██║ ██║██╔════╝██╔════╝
██║ ███╗███████║██╔████╔██║█████╗ ██║ ██║██╔██╗ ██║ ██████╔╝███████║██║ ██║███████╗█████╗
██║ ██║██╔══██║██║╚██╔╝██║██╔══╝ ██║ ██║██║╚██╗██║ ██╔═══╝ ██╔══██║██║ ██║╚════██║██╔══╝
╚██████╔╝██║ ██║██║ ╚═╝ ██║███████╗ ╚██████╔╝██║ ╚████║ ██║ ██║ ██║╚██████╔╝███████║███████╗
╚═════╝ ╚═╝ ╚═╝╚═╝ ╚═╝╚══════╝ ╚═════╝ ╚═╝ ╚═══╝ ╚═╝ ╚═╝ ╚═╝ ╚═════╝ ╚══════╝╚══════╝
Use up and down arrow keys to select an option
";
string[] options = { "Save", "Load", "Exit" };
Menu pauseMenu = new Menu(prompt, options);
switch (pauseMenu.RunMenu(default, true))
{
case 0:
SaveGame();
break;
case 1:
LoadGame();
break;
case 2:
DisplayExitMenu();
break;
}
}

/// <summary>
/// Displays game mode screen.
/// </summary>
Expand Down Expand Up @@ -241,8 +394,11 @@ private static void GameLoop()

Menu gameMenu = new Menu(prompt.ToString(), options);

switch (gameMenu.RunMenu())
switch (gameMenu.RunMenu(default, true))
{
case -1:
DisplayPauseMenu();
continue;
case 0:
UpdateMarketPrices();
GenerateEvent();
Expand Down Expand Up @@ -281,12 +437,12 @@ private static void GameLoop()
/// </summary>
private static void UpdateMarketPrices()
{
for (int i = 0; i < Companies.Count; i++)
for (int i = 0; i < Companies.Count; i++)
{
Random random = new Random();
int effect = random.Next(0, 2);

switch (effect)
switch (effect)
{
case 0:
Companies[i].SharePrice += Companies[i].SharePrice * Random.Shared.Next(1, 4) / 100;
Expand Down

0 comments on commit 7a53009

Please sign in to comment.