added console application

This commit is contained in:
KäseToatz
2024-11-05 19:01:21 +01:00
parent d1bfecbfef
commit 754db562af
15 changed files with 292 additions and 26 deletions

View File

@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\Memory.Logic\Memory.Logic.csproj" />
</ItemGroup>
</Project>

29
Memory.Cmd/Program.cs Normal file
View File

@ -0,0 +1,29 @@
using Memory.Logic;
namespace Memory.Cmd
{
internal class Program
{
static void Main()
{
Game game = new();
Renderer renderer = new(game);
while (!game.IsFinished())
{
renderer.Render();
Console.Write("Enter card number: ");
try
{
game.ClickCard(game.Cards[int.Parse(Console.ReadLine()!) - 1]);
}
catch (Exception)
{
Console.WriteLine("Invalid card number given.");
Console.ReadLine();
}
Console.Clear();
}
renderer.Render();
}
}
}

94
Memory.Cmd/Renderer.cs Normal file
View File

@ -0,0 +1,94 @@
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;
}
}
}
}
}