Fixed all requirements
@ -6,10 +6,10 @@ namespace Memory.Cmd
|
|||||||
{
|
{
|
||||||
private readonly Game game = game;
|
private readonly Game game = game;
|
||||||
|
|
||||||
private static string FormatNumber(int number)
|
private string FormatNumber(int number)
|
||||||
{
|
{
|
||||||
string num = "";
|
string num = "";
|
||||||
int padding = Game.DECKSIZE.ToString().Length - number.ToString().Length;
|
int padding = game.DeckSize.ToString().Length - number.ToString().Length;
|
||||||
for (int i = 0; i < padding; i++)
|
for (int i = 0; i < padding; i++)
|
||||||
{
|
{
|
||||||
num += '0';
|
num += '0';
|
||||||
@ -18,22 +18,22 @@ namespace Memory.Cmd
|
|||||||
return num;
|
return num;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void DrawCard(Card card, int index, int column, int row)
|
private void DrawCard(Card card, int index, int column, int row)
|
||||||
{
|
{
|
||||||
string num = FormatNumber(card.ID);
|
string num = FormatNumber(card.ID);
|
||||||
string cardNr = FormatNumber(index);
|
string cardNr = FormatNumber(index);
|
||||||
int cardWidth = Game.DECKSIZE.ToString().Length + 4;
|
int cardWidth = game.DeckSize.ToString().Length + 4;
|
||||||
for (int i = 0; i < Game.GRIDSIZE; i++)
|
for (int i = 0; i < game.GridSize; i++)
|
||||||
{
|
{
|
||||||
Console.CursorLeft += (cardWidth + 1) * column;
|
Console.CursorLeft += (cardWidth + 1) * column;
|
||||||
Console.CursorTop = i + ((Game.GRIDSIZE + 1) * row) + 1;
|
Console.CursorTop = i + (game.GridSize + 1) * row;
|
||||||
for (int j = 0; j < cardWidth; j++)
|
for (int j = 0; j < cardWidth; j++)
|
||||||
{
|
{
|
||||||
if (i == 0)
|
if (i == 0)
|
||||||
{
|
{
|
||||||
Console.Write('#');
|
Console.Write('#');
|
||||||
}
|
}
|
||||||
else if (i == Game.GRIDSIZE - 1)
|
else if (i == game.GridSize - 1)
|
||||||
{
|
{
|
||||||
if (j > 1 && j < cardWidth - 2)
|
if (j > 1 && j < cardWidth - 2)
|
||||||
{
|
{
|
||||||
@ -48,7 +48,7 @@ namespace Memory.Cmd
|
|||||||
{
|
{
|
||||||
Console.Write('#');
|
Console.Write('#');
|
||||||
}
|
}
|
||||||
else if (i == Game.GRIDSIZE / 2 && j > 1 && j < cardWidth - 2)
|
else if (i == game.GridSize / 2 && j > 1 && j < cardWidth - 2)
|
||||||
{
|
{
|
||||||
if (card.Selected())
|
if (card.Selected())
|
||||||
{
|
{
|
||||||
@ -71,7 +71,6 @@ namespace Memory.Cmd
|
|||||||
public void Redraw()
|
public void Redraw()
|
||||||
{
|
{
|
||||||
Console.Clear();
|
Console.Clear();
|
||||||
Console.WriteLine($"Score: {game.Scoring.Points}");
|
|
||||||
for (int i = 0; i < game.Cards.Count; i++)
|
for (int i = 0; i < game.Cards.Count; i++)
|
||||||
{
|
{
|
||||||
Card card = game.Cards[i];
|
Card card = game.Cards[i];
|
||||||
@ -85,7 +84,7 @@ namespace Memory.Cmd
|
|||||||
{
|
{
|
||||||
Console.ForegroundColor = ConsoleColor.Red;
|
Console.ForegroundColor = ConsoleColor.Red;
|
||||||
}
|
}
|
||||||
DrawCard(game.Cards[i], i + 1, i % Game.GRIDSIZE, i / Game.GRIDSIZE);
|
DrawCard(game.Cards[i], i + 1, i % game.GridSize, i / game.GridSize);
|
||||||
Console.ForegroundColor = ConsoleColor.White;
|
Console.ForegroundColor = ConsoleColor.White;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -10,7 +10,7 @@ namespace Memory.Cmd
|
|||||||
string name = CmdGame.GetPlayerName();
|
string name = CmdGame.GetPlayerName();
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
Game game = new(new ScoreHandler(), name);
|
Game game = new(new ScoreHandler(), name, 10);
|
||||||
CmdGame cmdGame = new(game);
|
CmdGame cmdGame = new(game);
|
||||||
while (!game.IsFinished())
|
while (!game.IsFinished())
|
||||||
{
|
{
|
||||||
|
@ -7,6 +7,22 @@ namespace Memory.Data
|
|||||||
{
|
{
|
||||||
public const string URI = "Data Source=Scores.db;Version=3;";
|
public const string URI = "Data Source=Scores.db;Version=3;";
|
||||||
|
|
||||||
|
private bool IsTop10(int points)
|
||||||
|
{
|
||||||
|
using SQLiteConnection connection = new(URI);
|
||||||
|
connection.Open();
|
||||||
|
using SQLiteCommand command = new("SELECT Points FROM Scores ORDER BY Points DESC LIMIT 1 OFFSET 9", connection);
|
||||||
|
using SQLiteDataReader reader = command.ExecuteReader();
|
||||||
|
if (!reader.Read())
|
||||||
|
{
|
||||||
|
connection.Close();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
int lowest = reader.GetInt32(0);
|
||||||
|
connection.Close();
|
||||||
|
return points > lowest;
|
||||||
|
}
|
||||||
|
|
||||||
public ScoreHandler()
|
public ScoreHandler()
|
||||||
{
|
{
|
||||||
using SQLiteConnection connection = new(URI);
|
using SQLiteConnection connection = new(URI);
|
||||||
@ -22,18 +38,18 @@ namespace Memory.Data
|
|||||||
using SQLiteConnection connection = new(URI);
|
using SQLiteConnection connection = new(URI);
|
||||||
connection.Open();
|
connection.Open();
|
||||||
using SQLiteCommand command = new("SELECT Name, Points FROM Scores ORDER BY Points DESC LIMIT 10", connection);
|
using SQLiteCommand command = new("SELECT Name, Points FROM Scores ORDER BY Points DESC LIMIT 10", connection);
|
||||||
using (SQLiteDataReader reader = command.ExecuteReader())
|
using SQLiteDataReader reader = command.ExecuteReader();
|
||||||
{
|
|
||||||
while (reader.Read())
|
while (reader.Read())
|
||||||
{
|
{
|
||||||
scores.Add(new(reader.GetString(0), reader.GetInt32(1)));
|
scores.Add(new(reader.GetString(0), reader.GetInt32(1)));
|
||||||
}
|
}
|
||||||
}
|
|
||||||
connection.Close();
|
connection.Close();
|
||||||
return scores;
|
return scores;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void WriteScore(Score score)
|
public bool WriteScore(Score score)
|
||||||
|
{
|
||||||
|
if (IsTop10(score.Points))
|
||||||
{
|
{
|
||||||
using SQLiteConnection connection = new(URI);
|
using SQLiteConnection connection = new(URI);
|
||||||
connection.Open();
|
connection.Open();
|
||||||
@ -42,6 +58,9 @@ namespace Memory.Data
|
|||||||
command.Parameters.AddWithValue("@Points", score.Points);
|
command.Parameters.AddWithValue("@Points", score.Points);
|
||||||
command.ExecuteNonQuery();
|
command.ExecuteNonQuery();
|
||||||
connection.Close();
|
connection.Close();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -10,16 +10,19 @@
|
|||||||
Height="600"
|
Height="600"
|
||||||
ResizeMode="NoResize">
|
ResizeMode="NoResize">
|
||||||
<Grid>
|
<Grid>
|
||||||
<Grid Name="StartScreen">
|
<Grid Name="StartScreen" Visibility="Visible">
|
||||||
<Label Content="Memory" HorizontalAlignment="Center" VerticalAlignment="Top" FontSize="100px" Margin="0,140,0,0"/>
|
<Label Content="Memory" HorizontalAlignment="Center" VerticalAlignment="Top" FontSize="100px" Margin="0,120,0,0"/>
|
||||||
<Label Content="Name:" HorizontalAlignment="Left" VerticalAlignment="Top" FontSize="30px" Margin="220,275,0,0"></Label>
|
<Label Content="Name:" HorizontalAlignment="Left" VerticalAlignment="Top" FontSize="30px" Margin="220,250,0,0"></Label>
|
||||||
<TextBox HorizontalAlignment="Left" VerticalAlignment="Top" Margin="330,280,0,0" Width="280" Height="40" FontSize="24" Name="Name"></TextBox>
|
<TextBox HorizontalAlignment="Left" VerticalAlignment="Top" Margin="330,255,0,0" Width="280" Height="40" FontSize="24" Name="Name"></TextBox>
|
||||||
|
<Label Content="Pairs of cards in deck:" HorizontalAlignment="Left" VerticalAlignment="Top" FontSize="30px" Margin="120,300,0,0"></Label>
|
||||||
|
<Button Content="←" HorizontalAlignment="Left" VerticalAlignment="Top" FontSize="30px" Margin="470,300,0,0" Click="Decrease"></Button>
|
||||||
|
<Label Content="5" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="535,300,0,0" FontSize="30px" Name="DeckSize"></Label>
|
||||||
|
<Button Content="→" HorizontalAlignment="Left" VerticalAlignment="Top" FontSize="30px" Margin="600,300,0,0" Click="Increase"></Button>
|
||||||
<Label Content="Name must be between 2 and 32 characters." HorizontalAlignment="Center" VerticalAlignment="Top" Margin="0,320,0,0" FontSize="20" Foreground="Red" Name="ErrorLabel" Visibility="Hidden"></Label>
|
<Label Content="Name must be between 2 and 32 characters." HorizontalAlignment="Center" VerticalAlignment="Top" Margin="0,320,0,0" FontSize="20" Foreground="Red" Name="ErrorLabel" Visibility="Hidden"></Label>
|
||||||
<Button Content="Start" HorizontalAlignment="Center" VerticalAlignment="Top" FontSize="50px" Margin="0,360,0,0" Width="240" Height="80" Click="StartGame"></Button>
|
<Button Content="Start" HorizontalAlignment="Center" VerticalAlignment="Top" FontSize="50px" Margin="0,360,0,0" Width="240" Height="80" Click="StartGame"></Button>
|
||||||
</Grid>
|
</Grid>
|
||||||
<Grid Name="GameScreen" Visibility="Hidden">
|
<Grid Name="GameScreen" Visibility="Visible">
|
||||||
<Label Name="Score" Content="Score: 0" FontSize="50px" HorizontalAlignment="Center" Margin="0, 30, 0, 0"></Label>
|
<Grid Name="Cards" HorizontalAlignment="Center" VerticalAlignment="Center" Width="700" Height="500">
|
||||||
<Grid Name="Cards" Margin="50, 100, 50, 50">
|
|
||||||
</Grid>
|
</Grid>
|
||||||
</Grid>
|
</Grid>
|
||||||
<Grid Name="FinishScreen" Visibility="Hidden">
|
<Grid Name="FinishScreen" Visibility="Hidden">
|
||||||
|
@ -3,6 +3,8 @@ using Memory.Logic;
|
|||||||
using System.Windows;
|
using System.Windows;
|
||||||
using System.Windows.Controls;
|
using System.Windows.Controls;
|
||||||
using System.Windows.Media;
|
using System.Windows.Media;
|
||||||
|
using System.Windows.Data;
|
||||||
|
using System.Windows.Media.Imaging;
|
||||||
|
|
||||||
namespace Memory.Gui
|
namespace Memory.Gui
|
||||||
{
|
{
|
||||||
@ -12,11 +14,11 @@ namespace Memory.Gui
|
|||||||
public const int SCOREMARGIN = 30;
|
public const int SCOREMARGIN = 30;
|
||||||
|
|
||||||
private Game? game;
|
private Game? game;
|
||||||
|
private int deckSize = 5;
|
||||||
|
|
||||||
public MainWindow()
|
public MainWindow()
|
||||||
{
|
{
|
||||||
InitializeComponent();
|
InitializeComponent();
|
||||||
CreateGrid();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void StartGame(object sender, RoutedEventArgs args)
|
private void StartGame(object sender, RoutedEventArgs args)
|
||||||
@ -28,15 +30,28 @@ namespace Memory.Gui
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
game = new(new ScoreHandler(), name);
|
game = new(new ScoreHandler(), name, deckSize);
|
||||||
ErrorLabel.Visibility = Visibility.Hidden;
|
ErrorLabel.Visibility = Visibility.Hidden;
|
||||||
StartScreen.Visibility = Visibility.Hidden;
|
StartScreen.Visibility = Visibility.Hidden;
|
||||||
FinishScreen.Visibility = Visibility.Hidden;
|
FinishScreen.Visibility = Visibility.Hidden;
|
||||||
GameScreen.Visibility = Visibility.Visible;
|
GameScreen.Visibility = Visibility.Visible;
|
||||||
|
CreateGrid();
|
||||||
Redraw();
|
Redraw();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void Decrease(object sender, RoutedEventArgs args)
|
||||||
|
{
|
||||||
|
deckSize = Math.Max(deckSize - 1, 2);
|
||||||
|
DeckSize.Content = deckSize;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void Increase(object sender, RoutedEventArgs args)
|
||||||
|
{
|
||||||
|
deckSize = Math.Min(deckSize + 1, 10);
|
||||||
|
DeckSize.Content = deckSize;
|
||||||
|
}
|
||||||
|
|
||||||
private void DrawScores()
|
private void DrawScores()
|
||||||
{
|
{
|
||||||
Highscores.Children.Clear();
|
Highscores.Children.Clear();
|
||||||
@ -66,8 +81,11 @@ namespace Memory.Gui
|
|||||||
|
|
||||||
private void CreateGrid()
|
private void CreateGrid()
|
||||||
{
|
{
|
||||||
int columns = Game.GRIDSIZE;
|
Cards.ColumnDefinitions.Clear();
|
||||||
int rows = Game.DECKSIZE * 2 / Game.GRIDSIZE;
|
Cards.RowDefinitions.Clear();
|
||||||
|
Deck grid = Game.GetGridSize(deckSize);
|
||||||
|
int columns = grid.Columns;
|
||||||
|
int rows = grid.Rows;
|
||||||
for (int i = 0; i < columns; i++)
|
for (int i = 0; i < columns; i++)
|
||||||
{
|
{
|
||||||
ColumnDefinition colDef = new()
|
ColumnDefinition colDef = new()
|
||||||
@ -84,25 +102,40 @@ namespace Memory.Gui
|
|||||||
};
|
};
|
||||||
Cards.RowDefinitions.Add(rowDef);
|
Cards.RowDefinitions.Add(rowDef);
|
||||||
}
|
}
|
||||||
|
if (rows > columns)
|
||||||
|
{
|
||||||
|
Cards.Width /= (double)rows / columns;
|
||||||
|
}
|
||||||
|
else if (columns > rows)
|
||||||
|
{
|
||||||
|
Cards.Height /= (double)columns / rows;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void Redraw()
|
private void Redraw()
|
||||||
{
|
{
|
||||||
Cards.Children.Clear();
|
Cards.Children.Clear();
|
||||||
Score.Content = $"Score: {game!.Scoring.Points}";
|
Deck grid = Game.GetGridSize(deckSize);
|
||||||
for (int i = 0; i < game!.Cards.Count; i++)
|
for (int i = 0; i < game!.Cards.Count; i++)
|
||||||
{
|
{
|
||||||
Card card = game.Cards[i];
|
Card card = game.Cards[i];
|
||||||
if (!card.Completed)
|
if (!card.Completed)
|
||||||
{
|
{
|
||||||
|
Image image = new()
|
||||||
|
{
|
||||||
|
Source = new BitmapImage(new($"pack://application:,,,/images/{card.ID}.png")),
|
||||||
|
Stretch = Stretch.Fill,
|
||||||
|
};
|
||||||
Button button = new()
|
Button button = new()
|
||||||
{
|
{
|
||||||
Content = card.Selected() ? card.ID : null,
|
Content = card.Selected() ? image : null,
|
||||||
FontSize = 30,
|
FontSize = 30,
|
||||||
Background = new SolidColorBrush(card.Selected() ? Color.FromRgb(0, 255, 0) : Color.FromRgb(255, 0, 0))
|
Background = new SolidColorBrush(card.Selected() ? Color.FromRgb(0, 255, 0) : Color.FromRgb(255, 0, 0)),
|
||||||
|
Padding = new Thickness(0)
|
||||||
};
|
};
|
||||||
Grid.SetColumn(button, i % Game.GRIDSIZE);
|
|
||||||
Grid.SetRow(button, i / Game.GRIDSIZE);
|
Grid.SetColumn(button, i % grid.Columns);
|
||||||
|
Grid.SetRow(button, i / grid.Columns);
|
||||||
button.Click += (object sender, RoutedEventArgs args) =>
|
button.Click += (object sender, RoutedEventArgs args) =>
|
||||||
{
|
{
|
||||||
game.ClickCard(card);
|
game.ClickCard(card);
|
||||||
|
@ -8,9 +8,35 @@
|
|||||||
<UseWPF>true</UseWPF>
|
<UseWPF>true</UseWPF>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<None Remove="images\1.png" />
|
||||||
|
<None Remove="images\10.png" />
|
||||||
|
<None Remove="images\2.png" />
|
||||||
|
<None Remove="images\3.png" />
|
||||||
|
<None Remove="images\4.png" />
|
||||||
|
<None Remove="images\5.png" />
|
||||||
|
<None Remove="images\6.png" />
|
||||||
|
<None Remove="images\7.png" />
|
||||||
|
<None Remove="images\8.png" />
|
||||||
|
<None Remove="images\9.png" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ProjectReference Include="..\Memory.Data\Memory.Data.csproj" />
|
<ProjectReference Include="..\Memory.Data\Memory.Data.csproj" />
|
||||||
<ProjectReference Include="..\Memory.Logic\Memory.Logic.csproj" />
|
<ProjectReference Include="..\Memory.Logic\Memory.Logic.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<Resource Include="images\1.png" />
|
||||||
|
<Resource Include="images\10.png" />
|
||||||
|
<Resource Include="images\2.png" />
|
||||||
|
<Resource Include="images\3.png" />
|
||||||
|
<Resource Include="images\4.png" />
|
||||||
|
<Resource Include="images\5.png" />
|
||||||
|
<Resource Include="images\6.png" />
|
||||||
|
<Resource Include="images\7.png" />
|
||||||
|
<Resource Include="images\8.png" />
|
||||||
|
<Resource Include="images\9.png" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
BIN
Memory.Gui/images/1.png
Normal file
After Width: | Height: | Size: 47 KiB |
BIN
Memory.Gui/images/10.png
Normal file
After Width: | Height: | Size: 45 KiB |
BIN
Memory.Gui/images/2.png
Normal file
After Width: | Height: | Size: 43 KiB |
BIN
Memory.Gui/images/3.png
Normal file
After Width: | Height: | Size: 44 KiB |
BIN
Memory.Gui/images/4.png
Normal file
After Width: | Height: | Size: 31 KiB |
BIN
Memory.Gui/images/5.png
Normal file
After Width: | Height: | Size: 50 KiB |
BIN
Memory.Gui/images/6.png
Normal file
After Width: | Height: | Size: 41 KiB |
BIN
Memory.Gui/images/7.png
Normal file
After Width: | Height: | Size: 47 KiB |
BIN
Memory.Gui/images/8.png
Normal file
After Width: | Height: | Size: 30 KiB |
BIN
Memory.Gui/images/9.png
Normal file
After Width: | Height: | Size: 44 KiB |
8
Memory.Logic/Deck.cs
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
namespace Memory.Logic
|
||||||
|
{
|
||||||
|
public class Deck(int columns, int rows)
|
||||||
|
{
|
||||||
|
public int Columns { get; } = columns;
|
||||||
|
public int Rows { get; } = rows;
|
||||||
|
}
|
||||||
|
}
|
@ -1,20 +1,38 @@
|
|||||||
namespace Memory.Logic
|
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 MAXPOINTS = 100;
|
||||||
public const int MINPOINTS = 10;
|
public const int MINPOINTS = 10;
|
||||||
public const int MAXTIME = 10000;
|
public const int MAXTIME = 10000;
|
||||||
public const int STARTSCORE = 100;
|
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 IScoreHandler ScoreHandler { get; } = scoreHandler;
|
||||||
public Score Scoring { get; set; } = new(player, STARTSCORE);
|
public long StartTime { get; } = DateTimeOffset.Now.ToUnixTimeMilliseconds();
|
||||||
public long LastGuess { get; set; } = 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 = [];
|
List<Card> cards = [];
|
||||||
for (int i = 1; i < pairs + 1; i++)
|
for (int i = 1; i < pairs + 1; i++)
|
||||||
@ -26,15 +44,6 @@
|
|||||||
return [..cards.OrderBy(card => random.Next())];
|
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()
|
public Card? GetChoice1()
|
||||||
{
|
{
|
||||||
return Cards.FirstOrDefault(card => card.IsChoice1);
|
return Cards.FirstOrDefault(card => card.IsChoice1);
|
||||||
@ -71,20 +80,17 @@
|
|||||||
else if (choice1 != null && choice2 == null && choice1 != card)
|
else if (choice1 != null && choice2 == null && choice1 != card)
|
||||||
{
|
{
|
||||||
card.IsChoice2 = true;
|
card.IsChoice2 = true;
|
||||||
|
Attempts++;
|
||||||
if (choice1.Matches(card))
|
if (choice1.Matches(card))
|
||||||
{
|
{
|
||||||
choice1.Completed = true;
|
choice1.Completed = true;
|
||||||
card.Completed = true;
|
card.Completed = true;
|
||||||
IncreaseScore();
|
|
||||||
if (IsFinished())
|
if (IsFinished())
|
||||||
{
|
{
|
||||||
|
CalculateScore(DateTimeOffset.Now.ToUnixTimeMilliseconds());
|
||||||
ScoreHandler.WriteScore(Scoring);
|
ScoreHandler.WriteScore(Scoring);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
|
||||||
{
|
|
||||||
Scoring.Points -= 10;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
{
|
{
|
||||||
public interface IScoreHandler
|
public interface IScoreHandler
|
||||||
{
|
{
|
||||||
public void WriteScore(Score score);
|
public bool WriteScore(Score score);
|
||||||
public List<Score> GetTopScores();
|
public List<Score> GetTopScores();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -6,31 +6,39 @@ namespace Memory.Test
|
|||||||
[TestClass]
|
[TestClass]
|
||||||
public class GameTest
|
public class GameTest
|
||||||
{
|
{
|
||||||
|
[TestMethod]
|
||||||
|
public void CreateDeck_20pairs_ShouldEqual40RandomCards()
|
||||||
|
{
|
||||||
|
List<Card> deck1 = Game.CreateDeck(20);
|
||||||
|
List<Card> deck2 = Game.CreateDeck(20);
|
||||||
|
Assert.IsTrue(deck1.Count == 40 && deck2.Count == 40 && !deck1.SequenceEqual(deck2));
|
||||||
|
}
|
||||||
|
|
||||||
[TestMethod]
|
[TestMethod]
|
||||||
public void IncreaseScore_QuarterTime_ShouldEqual75PercentOfPoints()
|
public void IncreaseScore_QuarterTime_ShouldEqual75PercentOfPoints()
|
||||||
{
|
{
|
||||||
int score = (int)(Game.MAXPOINTS * 0.75);
|
int score = (int)(Game.MAXPOINTS * 0.75);
|
||||||
Game game = new(new ScoreHandler());
|
Game game = new(new ScoreHandler(), "test");
|
||||||
Thread.Sleep(Game.MAXTIME / 4);
|
Thread.Sleep(Game.MAXTIME / 4);
|
||||||
game.IncreaseScore();
|
game.IncreaseScore();
|
||||||
Assert.IsTrue(score - 1 <= game.Score && score + 1 >= game.Score);
|
Assert.IsTrue(score - 1 <= game.Scoring.Points && score + 1 >= game.Scoring.Points);
|
||||||
}
|
}
|
||||||
|
|
||||||
[TestMethod]
|
[TestMethod]
|
||||||
public void IncreaseScore_TooLong_ShouldEqualLowerBoundOfPoints()
|
public void IncreaseScore_TooLong_ShouldEqualLowerBoundOfPoints()
|
||||||
{
|
{
|
||||||
Game game = new(new ScoreHandler());
|
Game game = new(new ScoreHandler(), "test");
|
||||||
Thread.Sleep(Game.MAXTIME);
|
Thread.Sleep(Game.MAXTIME);
|
||||||
game.IncreaseScore();
|
game.IncreaseScore();
|
||||||
Assert.AreEqual(Game.MINPOINTS, game.Score);
|
Assert.AreEqual(Game.MINPOINTS, game.Scoring.Points);
|
||||||
}
|
}
|
||||||
|
|
||||||
[TestMethod]
|
[TestMethod]
|
||||||
public void IncreaseScore_Instant_ShouldEqualUpperBoundOfPoints()
|
public void IncreaseScore_Instant_ShouldEqualUpperBoundOfPoints()
|
||||||
{
|
{
|
||||||
Game game = new(new ScoreHandler());
|
Game game = new(new ScoreHandler(), "test");
|
||||||
game.IncreaseScore();
|
game.IncreaseScore();
|
||||||
Assert.AreEqual(Game.MAXPOINTS, game.Score);
|
Assert.AreEqual(Game.MAXPOINTS, game.Scoring.Points);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|