diff --git a/Memory.Cmd/CmdGame.cs b/Memory.Cmd/CmdGame.cs index 283282e..d6b86bd 100644 --- a/Memory.Cmd/CmdGame.cs +++ b/Memory.Cmd/CmdGame.cs @@ -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; } } diff --git a/Memory.Cmd/Program.cs b/Memory.Cmd/Program.cs index 8b42f68..3b229c4 100644 --- a/Memory.Cmd/Program.cs +++ b/Memory.Cmd/Program.cs @@ -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()) { diff --git a/Memory.Data/ScoreHandler.cs b/Memory.Data/ScoreHandler.cs index 4115687..b403184 100644 --- a/Memory.Data/ScoreHandler.cs +++ b/Memory.Data/ScoreHandler.cs @@ -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; } } } diff --git a/Memory.Gui/MainWindow.xaml b/Memory.Gui/MainWindow.xaml index d689092..26473de 100644 --- a/Memory.Gui/MainWindow.xaml +++ b/Memory.Gui/MainWindow.xaml @@ -10,16 +10,19 @@ Height="600" ResizeMode="NoResize"> - -