Finished scoring system and started tests

This commit is contained in:
KäseToatz
2024-11-06 21:46:37 +01:00
parent 23ae45ba91
commit af7457a9d9
13 changed files with 265 additions and 50 deletions

View File

@ -2,7 +2,7 @@
namespace Memory.Cmd
{
internal class Renderer(Game game)
internal class CmdGame(Game game)
{
private readonly Game game = game;
@ -26,7 +26,7 @@ namespace Memory.Cmd
for (int i = 0; i < Game.GRIDSIZE; i++)
{
Console.CursorLeft += (cardWidth + 1) * column;
Console.CursorTop = i + ((Game.GRIDSIZE + 1) * row);
Console.CursorTop = i + ((Game.GRIDSIZE + 1) * row) + 1;
for (int j = 0; j < cardWidth; j++)
{
if (i == 0)
@ -50,14 +50,14 @@ namespace Memory.Cmd
}
else if (i == Game.GRIDSIZE / 2 && j > 1 && j < cardWidth - 2)
{
//if (card.Selected())
//{
if (card.Selected())
{
Console.Write(num[j - 2]);
//}
//else
//{
// Console.Write('*');
//}
}
else
{
Console.Write('*');
}
}
else
{
@ -71,6 +71,7 @@ namespace Memory.Cmd
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];
@ -89,5 +90,44 @@ namespace Memory.Cmd
}
}
}
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;
}
}
}

View File

@ -7,36 +7,28 @@ namespace Memory.Cmd
{
static void Main()
{
string name = CmdGame.GetPlayerName();
while (true)
{
Game game = new(new ScoreHandler());
Renderer renderer = new(game);
Game game = new(new ScoreHandler(), name);
CmdGame cmdGame = new(game);
while (!game.IsFinished())
{
renderer.Redraw();
cmdGame.Redraw();
Console.Write("Enter card number: ");
try
{
game.ClickCard(game.Cards[int.Parse(Console.ReadLine()!) - 1]);
}
catch (Exception)
{
Console.Write("Invalid card number given.");
Console.ReadLine();
}
catch (Exception) {}
}
Console.Clear();
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))
cmdGame.WriteScores();
if (!CmdGame.ShouldReplay())
{
break;
}
}
}
}