Finished scoring system and started tests
This commit is contained in:
@ -2,7 +2,7 @@
|
||||
|
||||
namespace Memory.Cmd
|
||||
{
|
||||
internal class Renderer(Game game)
|
||||
internal class CmdGame(Game game)
|
||||
{
|
||||
private readonly Game game = game;
|
||||
|
||||
@ -26,7 +26,7 @@ namespace Memory.Cmd
|
||||
for (int i = 0; i < Game.GRIDSIZE; i++)
|
||||
{
|
||||
Console.CursorLeft += (cardWidth + 1) * column;
|
||||
Console.CursorTop = i + ((Game.GRIDSIZE + 1) * row);
|
||||
Console.CursorTop = i + ((Game.GRIDSIZE + 1) * row) + 1;
|
||||
for (int j = 0; j < cardWidth; j++)
|
||||
{
|
||||
if (i == 0)
|
||||
@ -50,14 +50,14 @@ namespace Memory.Cmd
|
||||
}
|
||||
else if (i == Game.GRIDSIZE / 2 && j > 1 && j < cardWidth - 2)
|
||||
{
|
||||
//if (card.Selected())
|
||||
//{
|
||||
if (card.Selected())
|
||||
{
|
||||
Console.Write(num[j - 2]);
|
||||
//}
|
||||
//else
|
||||
//{
|
||||
// Console.Write('*');
|
||||
//}
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.Write('*');
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -71,6 +71,7 @@ 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];
|
||||
@ -89,5 +90,44 @@ namespace Memory.Cmd
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void WriteScores()
|
||||
{
|
||||
Console.WriteLine($"Your score: {game.Scoring.Points}\nTop 10 Highscores:");
|
||||
List<Score> highscores = game.ScoreHandler.GetTopScores();
|
||||
for (int i = 0; i < highscores.Count; i++)
|
||||
{
|
||||
Score score = highscores[i];
|
||||
Console.WriteLine($"{i+1}. {score.Name}: {score.Points}");
|
||||
}
|
||||
}
|
||||
|
||||
public static string GetPlayerName()
|
||||
{
|
||||
Console.Write("Enter your name: ");
|
||||
string? name = Console.ReadLine();
|
||||
if (name == null || name.Length > 32 || name.Length < 2)
|
||||
{
|
||||
Console.Write("Name must be between 2 and 32 characters.\nEnter your name: ");
|
||||
name = Console.ReadLine();
|
||||
}
|
||||
return name!;
|
||||
}
|
||||
|
||||
public static bool ShouldReplay()
|
||||
{
|
||||
Console.Write("Game Finished. Do you want to play again? (Y/N): ");
|
||||
string? answer = Console.ReadLine();
|
||||
while (answer == null || (!answer.Equals("y", StringComparison.CurrentCultureIgnoreCase) && !answer.Equals("n", StringComparison.CurrentCultureIgnoreCase)))
|
||||
{
|
||||
Console.Write("Invalid answer.\nDo you want to play again? (Y/N): ");
|
||||
answer = Console.ReadLine();
|
||||
}
|
||||
if (answer.Equals("n", StringComparison.CurrentCultureIgnoreCase))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
@ -7,36 +7,28 @@ namespace Memory.Cmd
|
||||
{
|
||||
static void Main()
|
||||
{
|
||||
string name = CmdGame.GetPlayerName();
|
||||
while (true)
|
||||
{
|
||||
Game game = new(new ScoreHandler());
|
||||
Renderer renderer = new(game);
|
||||
Game game = new(new ScoreHandler(), name);
|
||||
CmdGame cmdGame = new(game);
|
||||
while (!game.IsFinished())
|
||||
{
|
||||
renderer.Redraw();
|
||||
cmdGame.Redraw();
|
||||
Console.Write("Enter card number: ");
|
||||
try
|
||||
{
|
||||
game.ClickCard(game.Cards[int.Parse(Console.ReadLine()!) - 1]);
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
Console.Write("Invalid card number given.");
|
||||
Console.ReadLine();
|
||||
}
|
||||
catch (Exception) {}
|
||||
}
|
||||
Console.Clear();
|
||||
Console.Write("Game Finished. Do you want to play again? (Y/N): ");
|
||||
string? answer = Console.ReadLine();
|
||||
while (answer == null || (!answer.Equals("y", StringComparison.CurrentCultureIgnoreCase) && !answer.Equals("n", StringComparison.CurrentCultureIgnoreCase)))
|
||||
{
|
||||
Console.Write("Invalid answer.\nDo you want to play again? (Y/N): ");
|
||||
answer = Console.ReadLine();
|
||||
}
|
||||
if (answer.Equals("n", StringComparison.CurrentCultureIgnoreCase))
|
||||
cmdGame.WriteScores();
|
||||
if (!CmdGame.ShouldReplay())
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -6,6 +6,10 @@
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="System.Data.SQLite" Version="1.0.119" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Memory.Logic\Memory.Logic.csproj" />
|
||||
</ItemGroup>
|
||||
|
@ -1,9 +1,47 @@
|
||||
using Memory.Logic;
|
||||
using System.Data.SQLite;
|
||||
|
||||
namespace Memory.Data
|
||||
{
|
||||
public class ScoreHandler : IScoreHandler
|
||||
{
|
||||
public const string URI = "Data Source=Scores.db;Version=3;";
|
||||
|
||||
public ScoreHandler()
|
||||
{
|
||||
using SQLiteConnection connection = new(URI);
|
||||
connection.Open();
|
||||
using SQLiteCommand command = new("CREATE TABLE IF NOT EXISTS Scores(ID INTEGER PRIMARY KEY, Name TEXT, Points INTEGER)", connection);
|
||||
command.ExecuteNonQuery();
|
||||
connection.Close();
|
||||
}
|
||||
|
||||
public List<Score> GetTopScores()
|
||||
{
|
||||
List<Score> scores = [];
|
||||
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())
|
||||
{
|
||||
while (reader.Read())
|
||||
{
|
||||
scores.Add(new(reader.GetString(0), reader.GetInt32(1)));
|
||||
}
|
||||
}
|
||||
connection.Close();
|
||||
return scores;
|
||||
}
|
||||
|
||||
public void 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,14 +1,8 @@
|
||||
using System.Configuration;
|
||||
using System.Data;
|
||||
using System.Windows;
|
||||
using System.Windows;
|
||||
|
||||
namespace Memory.Gui
|
||||
{
|
||||
/// <summary>
|
||||
/// Interaction logic for App.xaml
|
||||
/// </summary>
|
||||
public partial class App : Application
|
||||
{
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -11,14 +11,23 @@
|
||||
ResizeMode="NoResize">
|
||||
<Grid>
|
||||
<Grid Name="StartScreen">
|
||||
<Label Content="Memory" HorizontalAlignment="Center" VerticalAlignment="Top" FontSize="100px" Margin="0,180,0,0"/>
|
||||
<Button Content="Start" HorizontalAlignment="Center" VerticalAlignment="Top" FontSize="50px" Margin="0,320,0,0" Width="240" Height="80" Click="StartGame"></Button>
|
||||
<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>
|
||||
<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>
|
||||
<Grid Name="GameScreen" Visibility="Hidden" Margin="50, 100, 50, 50">
|
||||
</Grid>
|
||||
<Grid Name="FinishScreen" Visibility="Hidden">
|
||||
<Label Content="Game Finished" HorizontalAlignment="Center" VerticalAlignment="Top" FontSize="100px" Margin="0,180,0,0"/>
|
||||
<Button Content="Restart" HorizontalAlignment="Center" VerticalAlignment="Top" FontSize="50px" Margin="0,320,0,0" Width="240" Height="80" Click="StartGame"></Button>
|
||||
<Label Content="Game Finished" HorizontalAlignment="Center" VerticalAlignment="Top" FontSize="40px" Margin="0,10,0,0"/>
|
||||
<Label Content="Your score: 0" HorizontalAlignment="Center" VerticalAlignment="Top" FontSize="30px" Margin="0,50,0,0" Name="OwnScore"/>
|
||||
<Label Content="Top 10 Highscores:" HorizontalAlignment="Center" VerticalAlignment="Top" FontSize="50px" Margin="0,100,0,0"/>
|
||||
<Grid Name="Highscores"></Grid>
|
||||
<Button Content="Restart" HorizontalAlignment="Center" VerticalAlignment="Top" FontSize="50px" Margin="0,480,0,0" Width="240" Height="80" Click="StartGame"></Button>
|
||||
</Grid>
|
||||
</Grid>
|
||||
</Window>
|
||||
|
@ -8,6 +8,9 @@ namespace Memory.Gui
|
||||
{
|
||||
public partial class MainWindow : Window
|
||||
{
|
||||
public const int SCOREPADDING = 160;
|
||||
public const int SCOREMARGIN = 30;
|
||||
|
||||
private Game? game;
|
||||
|
||||
public MainWindow()
|
||||
@ -18,15 +21,45 @@ namespace Memory.Gui
|
||||
|
||||
private void StartGame(object sender, RoutedEventArgs args)
|
||||
{
|
||||
game = new(new ScoreHandler());
|
||||
string name = Name.Text;
|
||||
if (name.Length > 32 || name.Length < 2)
|
||||
{
|
||||
ErrorLabel.Visibility = Visibility.Visible;
|
||||
}
|
||||
else
|
||||
{
|
||||
game = new(new ScoreHandler(), name);
|
||||
ErrorLabel.Visibility = Visibility.Hidden;
|
||||
StartScreen.Visibility = Visibility.Hidden;
|
||||
FinishScreen.Visibility = Visibility.Hidden;
|
||||
GameScreen.Visibility = Visibility.Visible;
|
||||
Redraw();
|
||||
}
|
||||
}
|
||||
|
||||
private void DrawScores()
|
||||
{
|
||||
Highscores.Children.Clear();
|
||||
OwnScore.Content = $"Your score: {game!.Scoring.Points}";
|
||||
List<Score> highscores = game.ScoreHandler.GetTopScores();
|
||||
for (int i = 0; i < highscores.Count; i++)
|
||||
{
|
||||
Score score = highscores[i];
|
||||
Label label = new()
|
||||
{
|
||||
Content = $"{i+1}. {score.Name}: {score.Points}",
|
||||
HorizontalAlignment = HorizontalAlignment.Center,
|
||||
VerticalAlignment = VerticalAlignment.Top,
|
||||
FontSize = 30,
|
||||
Margin = new Thickness(0, SCOREPADDING + SCOREMARGIN * i, 0, 0)
|
||||
};
|
||||
Highscores.Children.Add(label);
|
||||
}
|
||||
}
|
||||
|
||||
private void FinishGame()
|
||||
{
|
||||
DrawScores();
|
||||
GameScreen.Visibility = Visibility.Hidden;
|
||||
FinishScreen.Visibility = Visibility.Visible;
|
||||
}
|
||||
@ -41,7 +74,7 @@ namespace Memory.Gui
|
||||
{
|
||||
Width = new(1, GridUnitType.Star)
|
||||
};
|
||||
GameScreen.ColumnDefinitions.Add(colDef);
|
||||
Cards.ColumnDefinitions.Add(colDef);
|
||||
}
|
||||
for (int i = 0; i < rows; i++)
|
||||
{
|
||||
@ -49,14 +82,15 @@ namespace Memory.Gui
|
||||
{
|
||||
Height = new(1, GridUnitType.Star)
|
||||
};
|
||||
GameScreen.RowDefinitions.Add(rowDef);
|
||||
Cards.RowDefinitions.Add(rowDef);
|
||||
}
|
||||
}
|
||||
|
||||
private void Redraw()
|
||||
{
|
||||
GameScreen.Children.Clear();
|
||||
for (int i = 0; i < game.Cards.Count; i++)
|
||||
Cards.Children.Clear();
|
||||
Score.Content = $"Score: {game!.Scoring.Points}";
|
||||
for (int i = 0; i < game!.Cards.Count; i++)
|
||||
{
|
||||
Card card = game.Cards[i];
|
||||
if (!card.Completed)
|
||||
@ -81,7 +115,7 @@ namespace Memory.Gui
|
||||
FinishGame();
|
||||
}
|
||||
};
|
||||
GameScreen.Children.Add(button);
|
||||
Cards.Children.Add(button);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,11 +1,18 @@
|
||||
namespace Memory.Logic
|
||||
{
|
||||
public class Game(IScoreHandler scoreHandler)
|
||||
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)
|
||||
{
|
||||
@ -19,6 +26,15 @@
|
||||
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);
|
||||
@ -59,7 +75,15 @@
|
||||
{
|
||||
choice1.Completed = true;
|
||||
card.Completed = true;
|
||||
// handle score etc
|
||||
IncreaseScore();
|
||||
if (IsFinished())
|
||||
{
|
||||
ScoreHandler.WriteScore(Scoring);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Scoring.Points -= 10;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -2,5 +2,7 @@
|
||||
{
|
||||
public interface IScoreHandler
|
||||
{
|
||||
public void WriteScore(Score score);
|
||||
public List<Score> GetTopScores();
|
||||
}
|
||||
}
|
||||
|
8
Memory.Logic/Score.cs
Normal file
8
Memory.Logic/Score.cs
Normal file
@ -0,0 +1,8 @@
|
||||
namespace Memory.Logic
|
||||
{
|
||||
public class Score(string name, int points)
|
||||
{
|
||||
public string Name { get; } = name;
|
||||
public int Points { get; set; } = points;
|
||||
}
|
||||
}
|
36
Memory.Test/GameTest.cs
Normal file
36
Memory.Test/GameTest.cs
Normal file
@ -0,0 +1,36 @@
|
||||
using Memory.Logic;
|
||||
using Memory.Data;
|
||||
|
||||
namespace Memory.Test
|
||||
{
|
||||
[TestClass]
|
||||
public class GameTest
|
||||
{
|
||||
[TestMethod]
|
||||
public void IncreaseScore_QuarterTime_ShouldEqual75PercentOfPoints()
|
||||
{
|
||||
int score = (int)(Game.MAXPOINTS * 0.75);
|
||||
Game game = new(new ScoreHandler());
|
||||
Thread.Sleep(Game.MAXTIME / 4);
|
||||
game.IncreaseScore();
|
||||
Assert.IsTrue(score - 1 <= game.Score && score + 1 >= game.Score);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void IncreaseScore_TooLong_ShouldEqualLowerBoundOfPoints()
|
||||
{
|
||||
Game game = new(new ScoreHandler());
|
||||
Thread.Sleep(Game.MAXTIME);
|
||||
game.IncreaseScore();
|
||||
Assert.AreEqual(Game.MINPOINTS, game.Score);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void IncreaseScore_Instant_ShouldEqualUpperBoundOfPoints()
|
||||
{
|
||||
Game game = new(new ScoreHandler());
|
||||
game.IncreaseScore();
|
||||
Assert.AreEqual(Game.MAXPOINTS, game.Score);
|
||||
}
|
||||
}
|
||||
}
|
28
Memory.Test/Memory.Test.csproj
Normal file
28
Memory.Test/Memory.Test.csproj
Normal file
@ -0,0 +1,28 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
|
||||
<IsPackable>false</IsPackable>
|
||||
<IsTestProject>true</IsTestProject>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="coverlet.collector" Version="6.0.0" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
|
||||
<PackageReference Include="MSTest.TestAdapter" Version="3.1.1" />
|
||||
<PackageReference Include="MSTest.TestFramework" Version="3.1.1" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Memory.Data\Memory.Data.csproj" />
|
||||
<ProjectReference Include="..\Memory.Logic\Memory.Logic.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Using Include="Microsoft.VisualStudio.TestTools.UnitTesting" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
@ -9,7 +9,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Memory.Gui", "Memory.Gui\Me
|
||||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Memory.Cmd", "Memory.Cmd\Memory.Cmd.csproj", "{36DBAAC0-3FEC-4C5C-8330-C1BD2D08BD05}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Memory.Data", "Memory.Data\Memory.Data.csproj", "{9ED8FC5D-4B8F-4FAA-AF87-087347548513}"
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Memory.Data", "Memory.Data\Memory.Data.csproj", "{9ED8FC5D-4B8F-4FAA-AF87-087347548513}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Memory.Test", "Memory.Test\Memory.Test.csproj", "{57EE3199-A942-4250-AD1E-B3EFBAEC89FF}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
@ -33,6 +35,10 @@ Global
|
||||
{9ED8FC5D-4B8F-4FAA-AF87-087347548513}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{9ED8FC5D-4B8F-4FAA-AF87-087347548513}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{9ED8FC5D-4B8F-4FAA-AF87-087347548513}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{57EE3199-A942-4250-AD1E-B3EFBAEC89FF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{57EE3199-A942-4250-AD1E-B3EFBAEC89FF}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{57EE3199-A942-4250-AD1E-B3EFBAEC89FF}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{57EE3199-A942-4250-AD1E-B3EFBAEC89FF}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
|
Reference in New Issue
Block a user