added console application
This commit is contained in:
20
Memory.Logic/Card.cs
Normal file
20
Memory.Logic/Card.cs
Normal file
@ -0,0 +1,20 @@
|
||||
namespace Memory.Logic
|
||||
{
|
||||
public class Card(int id)
|
||||
{
|
||||
public int ID { get; } = id;
|
||||
public bool IsChoice1 { get; set; } = false;
|
||||
public bool IsChoice2 { get; set; } = false;
|
||||
public bool Completed { get; set; } = false;
|
||||
|
||||
public bool Matches(Card card)
|
||||
{
|
||||
return ID == card.ID;
|
||||
}
|
||||
|
||||
public bool Selected()
|
||||
{
|
||||
return IsChoice1 || IsChoice2;
|
||||
}
|
||||
}
|
||||
}
|
67
Memory.Logic/Game.cs
Normal file
67
Memory.Logic/Game.cs
Normal file
@ -0,0 +1,67 @@
|
||||
namespace Memory.Logic
|
||||
{
|
||||
public class Game
|
||||
{
|
||||
public const int DECKSIZE = 10;
|
||||
public const int GRIDSIZE = 5;
|
||||
public List<Card> Cards { get; } = CreateDeck(DECKSIZE);
|
||||
|
||||
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 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;
|
||||
// handle score etc
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
9
Memory.Logic/Memory.Logic.csproj
Normal file
9
Memory.Logic/Memory.Logic.csproj
Normal file
@ -0,0 +1,9 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
Reference in New Issue
Block a user