Fixed all requirements
@ -6,10 +6,10 @@ namespace Memory.Cmd
|
||||
{
|
||||
private readonly Game game = game;
|
||||
|
||||
private static string FormatNumber(int number)
|
||||
private string FormatNumber(int number)
|
||||
{
|
||||
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++)
|
||||
{
|
||||
num += '0';
|
||||
@ -18,22 +18,22 @@ namespace Memory.Cmd
|
||||
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 cardNr = FormatNumber(index);
|
||||
int cardWidth = Game.DECKSIZE.ToString().Length + 4;
|
||||
for (int i = 0; i < Game.GRIDSIZE; i++)
|
||||
int cardWidth = game.DeckSize.ToString().Length + 4;
|
||||
for (int i = 0; i < game.GridSize; i++)
|
||||
{
|
||||
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++)
|
||||
{
|
||||
if (i == 0)
|
||||
{
|
||||
Console.Write('#');
|
||||
}
|
||||
else if (i == Game.GRIDSIZE - 1)
|
||||
else if (i == game.GridSize - 1)
|
||||
{
|
||||
if (j > 1 && j < cardWidth - 2)
|
||||
{
|
||||
@ -48,7 +48,7 @@ namespace Memory.Cmd
|
||||
{
|
||||
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())
|
||||
{
|
||||
@ -71,7 +71,6 @@ 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];
|
||||
@ -83,9 +82,9 @@ namespace Memory.Cmd
|
||||
}
|
||||
else
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
@ -10,7 +10,7 @@ namespace Memory.Cmd
|
||||
string name = CmdGame.GetPlayerName();
|
||||
while (true)
|
||||
{
|
||||
Game game = new(new ScoreHandler(), name);
|
||||
Game game = new(new ScoreHandler(), name, 10);
|
||||
CmdGame cmdGame = new(game);
|
||||
while (!game.IsFinished())
|
||||
{
|
||||
|
@ -7,6 +7,22 @@ namespace Memory.Data
|
||||
{
|
||||
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()
|
||||
{
|
||||
using SQLiteConnection connection = new(URI);
|
||||
@ -22,26 +38,29 @@ namespace Memory.Data
|
||||
using SQLiteConnection connection = new(URI);
|
||||
connection.Open();
|
||||
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();
|
||||
return scores;
|
||||
}
|
||||
|
||||
public void WriteScore(Score score)
|
||||
public bool WriteScore(Score score)
|
||||
{
|
||||
using SQLiteConnection connection = new(URI);
|
||||
connection.Open();
|
||||
using SQLiteCommand command = new("INSERT INTO Scores(Name, Points) VALUES(@Name, @Points)", connection);
|
||||
command.Parameters.AddWithValue("@Name", score.Name);
|
||||
command.Parameters.AddWithValue("@Points", score.Points);
|
||||
command.ExecuteNonQuery();
|
||||
connection.Close();
|
||||
if (IsTop10(score.Points))
|
||||
{
|
||||
using SQLiteConnection connection = new(URI);
|
||||
connection.Open();
|
||||
using SQLiteCommand command = new("INSERT INTO Scores(Name, Points) VALUES(@Name, @Points)", connection);
|
||||
command.Parameters.AddWithValue("@Name", score.Name);
|
||||
command.Parameters.AddWithValue("@Points", score.Points);
|
||||
command.ExecuteNonQuery();
|
||||
connection.Close();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -10,16 +10,19 @@
|
||||
Height="600"
|
||||
ResizeMode="NoResize">
|
||||
<Grid>
|
||||
<Grid Name="StartScreen">
|
||||
<Label Content="Memory" HorizontalAlignment="Center" VerticalAlignment="Top" FontSize="100px" Margin="0,140,0,0"/>
|
||||
<Label Content="Name:" HorizontalAlignment="Left" VerticalAlignment="Top" FontSize="30px" Margin="220,275,0,0"></Label>
|
||||
<TextBox HorizontalAlignment="Left" VerticalAlignment="Top" Margin="330,280,0,0" Width="280" Height="40" FontSize="24" Name="Name"></TextBox>
|
||||
<Grid Name="StartScreen" Visibility="Visible">
|
||||
<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,250,0,0"></Label>
|
||||
<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>
|
||||
<Button Content="Start" HorizontalAlignment="Center" VerticalAlignment="Top" FontSize="50px" Margin="0,360,0,0" Width="240" Height="80" Click="StartGame"></Button>
|
||||
</Grid>
|
||||
<Grid Name="GameScreen" Visibility="Hidden">
|
||||
<Label Name="Score" Content="Score: 0" FontSize="50px" HorizontalAlignment="Center" Margin="0, 30, 0, 0"></Label>
|
||||
<Grid Name="Cards" Margin="50, 100, 50, 50">
|
||||
<Grid Name="GameScreen" Visibility="Visible">
|
||||
<Grid Name="Cards" HorizontalAlignment="Center" VerticalAlignment="Center" Width="700" Height="500">
|
||||
</Grid>
|
||||
</Grid>
|
||||
<Grid Name="FinishScreen" Visibility="Hidden">
|
||||
|
@ -3,6 +3,8 @@ using Memory.Logic;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Media;
|
||||
using System.Windows.Data;
|
||||
using System.Windows.Media.Imaging;
|
||||
|
||||
namespace Memory.Gui
|
||||
{
|
||||
@ -12,11 +14,11 @@ namespace Memory.Gui
|
||||
public const int SCOREMARGIN = 30;
|
||||
|
||||
private Game? game;
|
||||
private int deckSize = 5;
|
||||
|
||||
public MainWindow()
|
||||
{
|
||||
InitializeComponent();
|
||||
CreateGrid();
|
||||
}
|
||||
|
||||
private void StartGame(object sender, RoutedEventArgs args)
|
||||
@ -28,15 +30,28 @@ namespace Memory.Gui
|
||||
}
|
||||
else
|
||||
{
|
||||
game = new(new ScoreHandler(), name);
|
||||
game = new(new ScoreHandler(), name, deckSize);
|
||||
ErrorLabel.Visibility = Visibility.Hidden;
|
||||
StartScreen.Visibility = Visibility.Hidden;
|
||||
FinishScreen.Visibility = Visibility.Hidden;
|
||||
GameScreen.Visibility = Visibility.Visible;
|
||||
CreateGrid();
|
||||
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()
|
||||
{
|
||||
Highscores.Children.Clear();
|
||||
@ -66,8 +81,11 @@ namespace Memory.Gui
|
||||
|
||||
private void CreateGrid()
|
||||
{
|
||||
int columns = Game.GRIDSIZE;
|
||||
int rows = Game.DECKSIZE * 2 / Game.GRIDSIZE;
|
||||
Cards.ColumnDefinitions.Clear();
|
||||
Cards.RowDefinitions.Clear();
|
||||
Deck grid = Game.GetGridSize(deckSize);
|
||||
int columns = grid.Columns;
|
||||
int rows = grid.Rows;
|
||||
for (int i = 0; i < columns; i++)
|
||||
{
|
||||
ColumnDefinition colDef = new()
|
||||
@ -84,25 +102,40 @@ namespace Memory.Gui
|
||||
};
|
||||
Cards.RowDefinitions.Add(rowDef);
|
||||
}
|
||||
if (rows > columns)
|
||||
{
|
||||
Cards.Width /= (double)rows / columns;
|
||||
}
|
||||
else if (columns > rows)
|
||||
{
|
||||
Cards.Height /= (double)columns / rows;
|
||||
}
|
||||
}
|
||||
|
||||
private void Redraw()
|
||||
{
|
||||
Cards.Children.Clear();
|
||||
Score.Content = $"Score: {game!.Scoring.Points}";
|
||||
Deck grid = Game.GetGridSize(deckSize);
|
||||
for (int i = 0; i < game!.Cards.Count; i++)
|
||||
{
|
||||
Card card = game.Cards[i];
|
||||
if (!card.Completed)
|
||||
{
|
||||
Image image = new()
|
||||
{
|
||||
Source = new BitmapImage(new($"pack://application:,,,/images/{card.ID}.png")),
|
||||
Stretch = Stretch.Fill,
|
||||
};
|
||||
Button button = new()
|
||||
{
|
||||
Content = card.Selected() ? card.ID : null,
|
||||
Content = card.Selected() ? image : null,
|
||||
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) =>
|
||||
{
|
||||
game.ClickCard(card);
|
||||
|
@ -8,9 +8,35 @@
|
||||
<UseWPF>true</UseWPF>
|
||||
</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>
|
||||
<ProjectReference Include="..\Memory.Data\Memory.Data.csproj" />
|
||||
<ProjectReference Include="..\Memory.Logic\Memory.Logic.csproj" />
|
||||
</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>
|
||||
|
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
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -2,7 +2,7 @@
|
||||
{
|
||||
public interface IScoreHandler
|
||||
{
|
||||
public void WriteScore(Score score);
|
||||
public bool WriteScore(Score score);
|
||||
public List<Score> GetTopScores();
|
||||
}
|
||||
}
|
||||
|
@ -6,31 +6,39 @@ namespace Memory.Test
|
||||
[TestClass]
|
||||
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]
|
||||
public void IncreaseScore_QuarterTime_ShouldEqual75PercentOfPoints()
|
||||
{
|
||||
int score = (int)(Game.MAXPOINTS * 0.75);
|
||||
Game game = new(new ScoreHandler());
|
||||
Game game = new(new ScoreHandler(), "test");
|
||||
Thread.Sleep(Game.MAXTIME / 4);
|
||||
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]
|
||||
public void IncreaseScore_TooLong_ShouldEqualLowerBoundOfPoints()
|
||||
{
|
||||
Game game = new(new ScoreHandler());
|
||||
Game game = new(new ScoreHandler(), "test");
|
||||
Thread.Sleep(Game.MAXTIME);
|
||||
game.IncreaseScore();
|
||||
Assert.AreEqual(Game.MINPOINTS, game.Score);
|
||||
Assert.AreEqual(Game.MINPOINTS, game.Scoring.Points);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void IncreaseScore_Instant_ShouldEqualUpperBoundOfPoints()
|
||||
{
|
||||
Game game = new(new ScoreHandler());
|
||||
Game game = new(new ScoreHandler(), "test");
|
||||
game.IncreaseScore();
|
||||
Assert.AreEqual(Game.MAXPOINTS, game.Score);
|
||||
Assert.AreEqual(Game.MAXPOINTS, game.Scoring.Points);
|
||||
}
|
||||
}
|
||||
}
|