Add comments

This commit is contained in:
KaseToatz1337
2024-11-07 10:18:20 +01:00
parent 32055107ab
commit 4c01b5ae79
5 changed files with 43 additions and 11 deletions

View File

@ -6,6 +6,7 @@ namespace Memory.Cmd
{
private readonly Game game = game;
// Add 0 padding to numbers so they have the same width
private string FormatNumber(int number)
{
string num = "";
@ -20,11 +21,14 @@ namespace Memory.Cmd
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++)
@ -50,6 +54,7 @@ namespace Memory.Cmd
}
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]);
@ -71,11 +76,13 @@ namespace Memory.Cmd
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;
@ -92,6 +99,7 @@ namespace Memory.Cmd
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++)
@ -101,6 +109,7 @@ namespace Memory.Cmd
}
}
// Ask for the player's name with checks if the name is valid
public static string GetPlayerName()
{
Console.Write("Enter your name: ");
@ -113,6 +122,7 @@ namespace Memory.Cmd
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): ");

View File

@ -12,6 +12,7 @@ namespace Memory.Cmd
{
Game game = new(new ScoreHandler(), name, 10);
CmdGame cmdGame = new(game);
// While the game isn't finished ask for a card index then attempt to click the card
while (!game.IsFinished())
{
cmdGame.Redraw();
@ -24,6 +25,7 @@ namespace Memory.Cmd
}
Console.Clear();
cmdGame.WriteScores();
// Ask if the player wants to play again, if not break out of the loop and the program is finished
if (!CmdGame.ShouldReplay())
{
break;