Files
Memory/Memory.Cmd/CmdGame.cs
KaseToatz1337 4c01b5ae79 Add comments
2024-11-07 10:18:20 +01:00

143 lines
5.1 KiB
C#

using Memory.Logic;
namespace Memory.Cmd
{
internal class CmdGame(Game game)
{
private readonly Game game = game;
// Add 0 padding to numbers so they have the same width
private string FormatNumber(int number)
{
string num = "";
int padding = game.DeckSize.ToString().Length - number.ToString().Length;
for (int i = 0; i < padding; i++)
{
num += '0';
}
num += number;
return num;
}
private void DrawCard(Card card, int index, int column, int row)
{
// Format both the card index and the card number
string num = FormatNumber(card.ID);
string cardNr = FormatNumber(index);
int cardWidth = game.DeckSize.ToString().Length + 4;
// Double for loop for drawing characters both vertically and horizontally
for (int i = 0; i < game.GridSize; i++)
{
// Set the cursor position so cards don't overlap
Console.CursorLeft += (cardWidth + 1) * column;
Console.CursorTop = i + (game.GridSize + 1) * row;
for (int j = 0; j < cardWidth; j++)
{
if (i == 0)
{
Console.Write('#');
}
else if (i == game.GridSize - 1)
{
if (j > 1 && j < cardWidth - 2)
{
Console.Write(cardNr[j - 2]);
}
else
{
Console.Write('#');
}
}
else if (j == 0 || j == cardWidth - 1)
{
Console.Write('#');
}
else if (i == game.GridSize / 2 && j > 1 && j < cardWidth - 2)
{
// Write the card number if it is selected otherwise hide it with star symbols
if (card.Selected())
{
Console.Write(num[j - 2]);
}
else
{
Console.Write('*');
}
}
else
{
Console.Write(' ');
}
}
Console.Write('\n');
}
}
public void Redraw()
{
Console.Clear();
// Loop over all cards to draw them individually
for (int i = 0; i < game.Cards.Count; i++)
{
Card card = game.Cards[i];
if (!card.Completed)
{
// Set the color of the card based on if it's selected or not
if (card.Selected())
{
Console.ForegroundColor = ConsoleColor.Green;
}
else
{
Console.ForegroundColor = ConsoleColor.Red;
}
DrawCard(game.Cards[i], i + 1, i % game.GridSize, i / game.GridSize);
Console.ForegroundColor = ConsoleColor.White;
}
}
}
public void WriteScores()
{
// Print the players score to the console then loop over highscores to print those aswell
Console.WriteLine($"Your score: {game.Scoring.Points}\nTop 10 Highscores:");
List<Score> highscores = game.ScoreHandler.GetTopScores();
for (int i = 0; i < highscores.Count; i++)
{
Score score = highscores[i];
Console.WriteLine($"{i+1}. {score.Name}: {score.Points}");
}
}
// Ask for the player's name with checks if the name is valid
public static string GetPlayerName()
{
Console.Write("Enter your name: ");
string? name = Console.ReadLine();
if (name == null || name.Length > 32 || name.Length < 2)
{
Console.Write("Name must be between 2 and 32 characters.\nEnter your name: ");
name = Console.ReadLine();
}
return name!;
}
// Ask if the player wants to play again
public static bool ShouldReplay()
{
Console.Write("Game Finished. Do you want to play again? (Y/N): ");
string? answer = Console.ReadLine();
while (answer == null || (!answer.Equals("y", StringComparison.CurrentCultureIgnoreCase) && !answer.Equals("n", StringComparison.CurrentCultureIgnoreCase)))
{
Console.Write("Invalid answer.\nDo you want to play again? (Y/N): ");
answer = Console.ReadLine();
}
if (answer.Equals("n", StringComparison.CurrentCultureIgnoreCase))
{
return false;
}
return true;
}
}
}