93 lines
3.0 KiB
C#
93 lines
3.0 KiB
C#
namespace Memory.Logic
|
|
{
|
|
public class Game(IScoreHandler scoreHandler, string player)
|
|
{
|
|
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 IScoreHandler ScoreHandler { get; } = scoreHandler;
|
|
public Score Scoring { get; set; } = new(player, STARTSCORE);
|
|
public long LastGuess { get; set; } = DateTimeOffset.Now.ToUnixTimeMilliseconds();
|
|
|
|
private static List<Card> CreateDeck(int pairs)
|
|
{
|
|
List<Card> cards = [];
|
|
for (int i = 1; i < pairs + 1; i++)
|
|
{
|
|
cards.Add(new(i));
|
|
cards.Add(new(i));
|
|
}
|
|
Random random = new();
|
|
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);
|
|
}
|
|
|
|
public Card? GetChoice2()
|
|
{
|
|
return Cards.FirstOrDefault(card => card.IsChoice2);
|
|
}
|
|
|
|
public bool IsFinished()
|
|
{
|
|
return Cards.All(card => card.Completed);
|
|
}
|
|
|
|
public void ClickCard(Card card)
|
|
{
|
|
Card? choice1 = GetChoice1();
|
|
Card? choice2 = GetChoice2();
|
|
if (!card.Completed)
|
|
{
|
|
if ((choice1 == null && choice2 == null) || (choice1 != null && choice2 != null))
|
|
{
|
|
if (choice1 != null)
|
|
{
|
|
choice1.IsChoice1 = false;
|
|
}
|
|
if (choice2 != null)
|
|
{
|
|
choice2.IsChoice2 = false;
|
|
}
|
|
card.IsChoice1 = true;
|
|
}
|
|
else if (choice1 != null && choice2 == null && choice1 != card)
|
|
{
|
|
card.IsChoice2 = true;
|
|
if (choice1.Matches(card))
|
|
{
|
|
choice1.Completed = true;
|
|
card.Completed = true;
|
|
IncreaseScore();
|
|
if (IsFinished())
|
|
{
|
|
ScoreHandler.WriteScore(Scoring);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
Scoring.Points -= 10;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|