95 lines
2.9 KiB
C#
95 lines
2.9 KiB
C#
using Memory.Logic;
|
|
using System;
|
|
using System.Reflection;
|
|
|
|
namespace Memory.Cmd
|
|
{
|
|
internal class Renderer(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);
|
|
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 Render()
|
|
{
|
|
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;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|