From 23ae45ba915308c8b6c04ea1b381931c26ca33dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A4seToatz?= Date: Wed, 6 Nov 2024 17:40:40 +0100 Subject: [PATCH] Added gui version and basic scorehandler --- Memory.Cmd/Memory.Cmd.csproj | 1 + Memory.Cmd/Program.cs | 42 +++++++++++------ Memory.Cmd/Renderer.cs | 19 ++++---- Memory.Data/Memory.Data.csproj | 13 ++++++ Memory.Data/ScoreHandler.cs | 9 ++++ Memory.Gui/MainWindow.xaml | 16 ++++++- Memory.Gui/MainWindow.xaml.cs | 85 ++++++++++++++++++++++++++++++---- Memory.Gui/Memory.Gui.csproj | 5 ++ Memory.Logic/Game.cs | 3 +- Memory.Logic/IScoreHandler.cs | 6 +++ Memory.sln | 12 +++-- 11 files changed, 171 insertions(+), 40 deletions(-) create mode 100644 Memory.Data/Memory.Data.csproj create mode 100644 Memory.Data/ScoreHandler.cs create mode 100644 Memory.Logic/IScoreHandler.cs diff --git a/Memory.Cmd/Memory.Cmd.csproj b/Memory.Cmd/Memory.Cmd.csproj index 4613569..a1237e7 100644 --- a/Memory.Cmd/Memory.Cmd.csproj +++ b/Memory.Cmd/Memory.Cmd.csproj @@ -8,6 +8,7 @@ + diff --git a/Memory.Cmd/Program.cs b/Memory.Cmd/Program.cs index bfe26a0..f13692a 100644 --- a/Memory.Cmd/Program.cs +++ b/Memory.Cmd/Program.cs @@ -1,4 +1,5 @@ -using Memory.Logic; +using Memory.Data; +using Memory.Logic; namespace Memory.Cmd { @@ -6,24 +7,37 @@ namespace Memory.Cmd { static void Main() { - Game game = new(); - Renderer renderer = new(game); - while (!game.IsFinished()) + while (true) { - renderer.Render(); - Console.Write("Enter card number: "); - try + Game game = new(new ScoreHandler()); + Renderer renderer = new(game); + while (!game.IsFinished()) { - game.ClickCard(game.Cards[int.Parse(Console.ReadLine()!) - 1]); - } - catch (Exception) - { - Console.WriteLine("Invalid card number given."); - Console.ReadLine(); + renderer.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(); + } } 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)) + { + break; + } } - renderer.Render(); } } } diff --git a/Memory.Cmd/Renderer.cs b/Memory.Cmd/Renderer.cs index 22008b6..b61a6f0 100644 --- a/Memory.Cmd/Renderer.cs +++ b/Memory.Cmd/Renderer.cs @@ -1,6 +1,4 @@ using Memory.Logic; -using System; -using System.Reflection; namespace Memory.Cmd { @@ -52,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 { @@ -70,8 +68,9 @@ namespace Memory.Cmd } } - public void Render() + public void Redraw() { + Console.Clear(); for (int i = 0; i < game.Cards.Count; i++) { Card card = game.Cards[i]; diff --git a/Memory.Data/Memory.Data.csproj b/Memory.Data/Memory.Data.csproj new file mode 100644 index 0000000..7507a42 --- /dev/null +++ b/Memory.Data/Memory.Data.csproj @@ -0,0 +1,13 @@ + + + + net8.0 + enable + enable + + + + + + + diff --git a/Memory.Data/ScoreHandler.cs b/Memory.Data/ScoreHandler.cs new file mode 100644 index 0000000..9062e5c --- /dev/null +++ b/Memory.Data/ScoreHandler.cs @@ -0,0 +1,9 @@ +using Memory.Logic; + +namespace Memory.Data +{ + public class ScoreHandler : IScoreHandler + { + + } +} diff --git a/Memory.Gui/MainWindow.xaml b/Memory.Gui/MainWindow.xaml index 19c03b9..c47524f 100644 --- a/Memory.Gui/MainWindow.xaml +++ b/Memory.Gui/MainWindow.xaml @@ -5,8 +5,20 @@ xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:Memory.Gui" mc:Ignorable="d" - Title="MainWindow" Height="450" Width="800"> + Title="Memory" + Width="800" + Height="600" + ResizeMode="NoResize"> - + + + + + + diff --git a/Memory.Gui/MainWindow.xaml.cs b/Memory.Gui/MainWindow.xaml.cs index 9cc8b5b..e27a2d3 100644 --- a/Memory.Gui/MainWindow.xaml.cs +++ b/Memory.Gui/MainWindow.xaml.cs @@ -1,24 +1,89 @@ -using System.Text; +using Memory.Data; +using Memory.Logic; using System.Windows; using System.Windows.Controls; -using System.Windows.Data; -using System.Windows.Documents; -using System.Windows.Input; using System.Windows.Media; -using System.Windows.Media.Imaging; -using System.Windows.Navigation; -using System.Windows.Shapes; namespace Memory.Gui { - /// - /// Interaction logic for MainWindow.xaml - /// public partial class MainWindow : Window { + private Game? game; + public MainWindow() { InitializeComponent(); + CreateGrid(); + } + + private void StartGame(object sender, RoutedEventArgs args) + { + game = new(new ScoreHandler()); + StartScreen.Visibility = Visibility.Hidden; + FinishScreen.Visibility = Visibility.Hidden; + GameScreen.Visibility = Visibility.Visible; + Redraw(); + } + + private void FinishGame() + { + GameScreen.Visibility = Visibility.Hidden; + FinishScreen.Visibility = Visibility.Visible; + } + + private void CreateGrid() + { + int columns = Game.GRIDSIZE; + int rows = Game.DECKSIZE * 2 / Game.GRIDSIZE; + for (int i = 0; i < columns; i++) + { + ColumnDefinition colDef = new() + { + Width = new(1, GridUnitType.Star) + }; + GameScreen.ColumnDefinitions.Add(colDef); + } + for (int i = 0; i < rows; i++) + { + RowDefinition rowDef = new() + { + Height = new(1, GridUnitType.Star) + }; + GameScreen.RowDefinitions.Add(rowDef); + } + } + + private void Redraw() + { + GameScreen.Children.Clear(); + for (int i = 0; i < game.Cards.Count; i++) + { + Card card = game.Cards[i]; + if (!card.Completed) + { + Button button = new() + { + Content = card.Selected() ? card.ID : null, + FontSize = 30, + Background = new SolidColorBrush(card.Selected() ? Color.FromRgb(0, 255, 0) : Color.FromRgb(255, 0, 0)) + }; + Grid.SetColumn(button, i % Game.GRIDSIZE); + Grid.SetRow(button, i / Game.GRIDSIZE); + button.Click += (object sender, RoutedEventArgs args) => + { + game.ClickCard(card); + if (!game.IsFinished()) + { + Redraw(); + } + else + { + FinishGame(); + } + }; + GameScreen.Children.Add(button); + } + } } } } \ No newline at end of file diff --git a/Memory.Gui/Memory.Gui.csproj b/Memory.Gui/Memory.Gui.csproj index e3e33e3..cda81cc 100644 --- a/Memory.Gui/Memory.Gui.csproj +++ b/Memory.Gui/Memory.Gui.csproj @@ -8,4 +8,9 @@ true + + + + + diff --git a/Memory.Logic/Game.cs b/Memory.Logic/Game.cs index e03e2a0..4d116cd 100644 --- a/Memory.Logic/Game.cs +++ b/Memory.Logic/Game.cs @@ -1,10 +1,11 @@ namespace Memory.Logic { - public class Game + public class Game(IScoreHandler scoreHandler) { public const int DECKSIZE = 10; public const int GRIDSIZE = 5; public List Cards { get; } = CreateDeck(DECKSIZE); + public IScoreHandler ScoreHandler { get; } = scoreHandler; private static List CreateDeck(int pairs) { diff --git a/Memory.Logic/IScoreHandler.cs b/Memory.Logic/IScoreHandler.cs new file mode 100644 index 0000000..ca7eb53 --- /dev/null +++ b/Memory.Logic/IScoreHandler.cs @@ -0,0 +1,6 @@ +namespace Memory.Logic +{ + public interface IScoreHandler + { + } +} diff --git a/Memory.sln b/Memory.sln index a76a0b0..a278b4b 100644 --- a/Memory.sln +++ b/Memory.sln @@ -3,11 +3,13 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 17 VisualStudioVersion = 17.9.34701.34 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Memory.Logic", "Memory.Logic\Memory.Logic.csproj", "{2490BF43-EFD6-4BE0-B231-860EB3508360}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Memory.Logic", "Memory.Logic\Memory.Logic.csproj", "{2490BF43-EFD6-4BE0-B231-860EB3508360}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Memory.Gui", "Memory.Gui\Memory.Gui.csproj", "{6E520079-0F1A-434A-82F8-56D3D5B57645}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Memory.Gui", "Memory.Gui\Memory.Gui.csproj", "{6E520079-0F1A-434A-82F8-56D3D5B57645}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Memory.Cmd", "Memory.Cmd\Memory.Cmd.csproj", "{36DBAAC0-3FEC-4C5C-8330-C1BD2D08BD05}" +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}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -27,6 +29,10 @@ Global {36DBAAC0-3FEC-4C5C-8330-C1BD2D08BD05}.Debug|Any CPU.Build.0 = Debug|Any CPU {36DBAAC0-3FEC-4C5C-8330-C1BD2D08BD05}.Release|Any CPU.ActiveCfg = Release|Any CPU {36DBAAC0-3FEC-4C5C-8330-C1BD2D08BD05}.Release|Any CPU.Build.0 = Release|Any CPU + {9ED8FC5D-4B8F-4FAA-AF87-087347548513}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {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 EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE