Added gui version and basic scorehandler

This commit is contained in:
KäseToatz
2024-11-06 17:40:40 +01:00
parent 754db562af
commit 23ae45ba91
11 changed files with 171 additions and 40 deletions

View File

@ -8,6 +8,7 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\Memory.Data\Memory.Data.csproj" />
<ProjectReference Include="..\Memory.Logic\Memory.Logic.csproj" /> <ProjectReference Include="..\Memory.Logic\Memory.Logic.csproj" />
</ItemGroup> </ItemGroup>

View File

@ -1,4 +1,5 @@
using Memory.Logic; using Memory.Data;
using Memory.Logic;
namespace Memory.Cmd namespace Memory.Cmd
{ {
@ -6,24 +7,37 @@ namespace Memory.Cmd
{ {
static void Main() static void Main()
{ {
Game game = new(); while (true)
Renderer renderer = new(game);
while (!game.IsFinished())
{ {
renderer.Render(); Game game = new(new ScoreHandler());
Console.Write("Enter card number: "); Renderer renderer = new(game);
try while (!game.IsFinished())
{ {
game.ClickCard(game.Cards[int.Parse(Console.ReadLine()!) - 1]); renderer.Redraw();
} Console.Write("Enter card number: ");
catch (Exception) try
{ {
Console.WriteLine("Invalid card number given."); game.ClickCard(game.Cards[int.Parse(Console.ReadLine()!) - 1]);
Console.ReadLine(); }
catch (Exception)
{
Console.Write("Invalid card number given.");
Console.ReadLine();
}
} }
Console.Clear(); 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();
} }
} }
} }

View File

@ -1,6 +1,4 @@
using Memory.Logic; using Memory.Logic;
using System;
using System.Reflection;
namespace Memory.Cmd namespace Memory.Cmd
{ {
@ -52,14 +50,14 @@ namespace Memory.Cmd
} }
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())
{ //{
Console.Write(num[j - 2]); Console.Write(num[j - 2]);
} //}
else //else
{ //{
Console.Write('*'); // Console.Write('*');
} //}
} }
else 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++) for (int i = 0; i < game.Cards.Count; i++)
{ {
Card card = game.Cards[i]; Card card = game.Cards[i];

View File

@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\Memory.Logic\Memory.Logic.csproj" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,9 @@
using Memory.Logic;
namespace Memory.Data
{
public class ScoreHandler : IScoreHandler
{
}
}

View File

@ -5,8 +5,20 @@
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Memory.Gui" xmlns:local="clr-namespace:Memory.Gui"
mc:Ignorable="d" mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800"> Title="Memory"
Width="800"
Height="600"
ResizeMode="NoResize">
<Grid> <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>
</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>
</Grid>
</Grid> </Grid>
</Window> </Window>

View File

@ -1,24 +1,89 @@
using System.Text; using Memory.Data;
using Memory.Logic;
using System.Windows; using System.Windows;
using System.Windows.Controls; using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media; using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace Memory.Gui namespace Memory.Gui
{ {
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window public partial class MainWindow : Window
{ {
private Game? game;
public MainWindow() public MainWindow()
{ {
InitializeComponent(); 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);
}
}
} }
} }
} }

View File

@ -8,4 +8,9 @@
<UseWPF>true</UseWPF> <UseWPF>true</UseWPF>
</PropertyGroup> </PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\Memory.Data\Memory.Data.csproj" />
<ProjectReference Include="..\Memory.Logic\Memory.Logic.csproj" />
</ItemGroup>
</Project> </Project>

View File

@ -1,10 +1,11 @@
namespace Memory.Logic namespace Memory.Logic
{ {
public class Game public class Game(IScoreHandler scoreHandler)
{ {
public const int DECKSIZE = 10; public const int DECKSIZE = 10;
public const int GRIDSIZE = 5; public const int GRIDSIZE = 5;
public List<Card> Cards { get; } = CreateDeck(DECKSIZE); public List<Card> Cards { get; } = CreateDeck(DECKSIZE);
public IScoreHandler ScoreHandler { get; } = scoreHandler;
private static List<Card> CreateDeck(int pairs) private static List<Card> CreateDeck(int pairs)
{ {

View File

@ -0,0 +1,6 @@
namespace Memory.Logic
{
public interface IScoreHandler
{
}
}

View File

@ -3,11 +3,13 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17 # Visual Studio Version 17
VisualStudioVersion = 17.9.34701.34 VisualStudioVersion = 17.9.34701.34
MinimumVisualStudioVersion = 10.0.40219.1 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 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 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 EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution 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}.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.ActiveCfg = Release|Any CPU
{36DBAAC0-3FEC-4C5C-8330-C1BD2D08BD05}.Release|Any CPU.Build.0 = 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 EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE