Fixed all requirements

This commit is contained in:
KäseToatz
2024-11-07 01:52:51 +01:00
parent af7457a9d9
commit 0121584708
20 changed files with 171 additions and 69 deletions

View File

@ -1,20 +1,38 @@
namespace Memory.Logic
{
public class Game(IScoreHandler scoreHandler, string player)
public class Game(IScoreHandler scoreHandler, string player, int deckSize)
{
public const int DECKSIZE = 10;
public const int GRIDSIZE = 5;
public const int MAXPOINTS = 100;
public const int MINPOINTS = 10;
public const int MAXTIME = 10000;
public const int STARTSCORE = 100;
public List<Card> Cards { get; } = CreateDeck(DECKSIZE);
public int DeckSize { get; } = deckSize;
public int GridSize { get; } = GetGridSize(deckSize).Rows;
public List<Card> Cards { get; } = CreateDeck(deckSize);
public IScoreHandler ScoreHandler { get; } = scoreHandler;
public Score Scoring { get; set; } = new(player, STARTSCORE);
public long LastGuess { get; set; } = DateTimeOffset.Now.ToUnixTimeMilliseconds();
public long StartTime { get; } = DateTimeOffset.Now.ToUnixTimeMilliseconds();
public int Attempts { get; set; } = 0;
public Score Scoring { get; } = new(player, 0);
private static List<Card> CreateDeck(int pairs)
public void CalculateScore(long endTime)
{
Scoring.Points = (int)(Math.Pow(DeckSize * 2, 2) / ((endTime - StartTime) / 1000.0 * Attempts) * 1000.0);
}
public static Deck GetGridSize(int deckSize)
{
for (int i = (int)Math.Sqrt(deckSize * 2); i > 0; i--)
{
if (deckSize * 2 % i == 0)
{
return new(i, deckSize * 2 / i);
}
}
return new(1, deckSize * 2);
}
public static List<Card> CreateDeck(int pairs)
{
List<Card> cards = [];
for (int i = 1; i < pairs + 1; i++)
@ -26,15 +44,6 @@
return [..cards.OrderBy(card => random.Next())];
}
public void IncreaseScore()
{
long curTime = DateTimeOffset.Now.ToUnixTimeMilliseconds();
double percentage = 1.0 - (double)(curTime - LastGuess) / MAXTIME;
int points = (int)(percentage * MAXPOINTS);
Scoring.Points += Math.Min(Math.Max(points, MINPOINTS), MAXPOINTS);
LastGuess = curTime;
}
public Card? GetChoice1()
{
return Cards.FirstOrDefault(card => card.IsChoice1);
@ -71,20 +80,17 @@
else if (choice1 != null && choice2 == null && choice1 != card)
{
card.IsChoice2 = true;
Attempts++;
if (choice1.Matches(card))
{
choice1.Completed = true;
card.Completed = true;
IncreaseScore();
if (IsFinished())
{
CalculateScore(DateTimeOffset.Now.ToUnixTimeMilliseconds());
ScoreHandler.WriteScore(Scoring);
}
}
else
{
Scoring.Points -= 10;
}
}
}
}