134 lines
4.5 KiB
C#
134 lines
4.5 KiB
C#
using Memory.Logic;
|
|
|
|
namespace Memory.Cmd
|
|
{
|
|
internal class CmdGame(Game game)
|
|
{
|
|
private readonly Game game = game;
|
|
|
|
private static 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 static void DrawCard(Card card, int index, int column, int row)
|
|
{
|
|
string num = FormatNumber(card.ID);
|
|
string cardNr = FormatNumber(index);
|
|
int cardWidth = Game.DECKSIZE.ToString().Length + 4;
|
|
for (int i = 0; i < Game.GRIDSIZE; i++)
|
|
{
|
|
Console.CursorLeft += (cardWidth + 1) * column;
|
|
Console.CursorTop = i + ((Game.GRIDSIZE + 1) * row) + 1;
|
|
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)
|
|
{
|
|
if (card.Selected())
|
|
{
|
|
Console.Write(num[j - 2]);
|
|
}
|
|
else
|
|
{
|
|
Console.Write('*');
|
|
}
|
|
}
|
|
else
|
|
{
|
|
Console.Write(' ');
|
|
}
|
|
}
|
|
Console.Write('\n');
|
|
}
|
|
}
|
|
|
|
public void Redraw()
|
|
{
|
|
Console.Clear();
|
|
Console.WriteLine($"Score: {game.Scoring.Points}");
|
|
for (int i = 0; i < game.Cards.Count; i++)
|
|
{
|
|
Card card = game.Cards[i];
|
|
if (!card.Completed)
|
|
{
|
|
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()
|
|
{
|
|
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}");
|
|
}
|
|
}
|
|
|
|
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!;
|
|
}
|
|
|
|
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;
|
|
}
|
|
}
|
|
}
|