added console application
This commit is contained in:
14
Memory.Cmd/Memory.Cmd.csproj
Normal file
14
Memory.Cmd/Memory.Cmd.csproj
Normal file
@ -0,0 +1,14 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Memory.Logic\Memory.Logic.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
29
Memory.Cmd/Program.cs
Normal file
29
Memory.Cmd/Program.cs
Normal file
@ -0,0 +1,29 @@
|
||||
using Memory.Logic;
|
||||
|
||||
namespace Memory.Cmd
|
||||
{
|
||||
internal class Program
|
||||
{
|
||||
static void Main()
|
||||
{
|
||||
Game game = new();
|
||||
Renderer renderer = new(game);
|
||||
while (!game.IsFinished())
|
||||
{
|
||||
renderer.Render();
|
||||
Console.Write("Enter card number: ");
|
||||
try
|
||||
{
|
||||
game.ClickCard(game.Cards[int.Parse(Console.ReadLine()!) - 1]);
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
Console.WriteLine("Invalid card number given.");
|
||||
Console.ReadLine();
|
||||
}
|
||||
Console.Clear();
|
||||
}
|
||||
renderer.Render();
|
||||
}
|
||||
}
|
||||
}
|
94
Memory.Cmd/Renderer.cs
Normal file
94
Memory.Cmd/Renderer.cs
Normal file
@ -0,0 +1,94 @@
|
||||
using Memory.Logic;
|
||||
using System;
|
||||
using System.Reflection;
|
||||
|
||||
namespace Memory.Cmd
|
||||
{
|
||||
internal class Renderer(Game game)
|
||||
{
|
||||
private readonly Game game = game;
|
||||
|
||||
private static string FormatNumber(int number)
|
||||
{
|
||||
string num = "";
|
||||
int padding = Game.DECKSIZE.ToString().Length - number.ToString().Length;
|
||||
for (int i = 0; i < padding; i++)
|
||||
{
|
||||
num += '0';
|
||||
}
|
||||
num += number;
|
||||
return num;
|
||||
}
|
||||
|
||||
private static 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++)
|
||||
{
|
||||
Console.CursorLeft += (cardWidth + 1) * column;
|
||||
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)
|
||||
{
|
||||
if (j > 1 && j < cardWidth - 2)
|
||||
{
|
||||
Console.Write(cardNr[j - 2]);
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.Write('#');
|
||||
}
|
||||
}
|
||||
else if (j == 0 || j == cardWidth - 1)
|
||||
{
|
||||
Console.Write('#');
|
||||
}
|
||||
else if (i == Game.GRIDSIZE / 2 && j > 1 && j < cardWidth - 2)
|
||||
{
|
||||
if (card.Selected())
|
||||
{
|
||||
Console.Write(num[j - 2]);
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.Write('*');
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.Write(' ');
|
||||
}
|
||||
}
|
||||
Console.Write('\n');
|
||||
}
|
||||
}
|
||||
|
||||
public void Render()
|
||||
{
|
||||
for (int i = 0; i < game.Cards.Count; i++)
|
||||
{
|
||||
Card card = game.Cards[i];
|
||||
if (!card.Completed)
|
||||
{
|
||||
if (card.Selected())
|
||||
{
|
||||
Console.ForegroundColor = ConsoleColor.Green;
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.ForegroundColor= ConsoleColor.Red;
|
||||
}
|
||||
DrawCard(game.Cards[i], i + 1, i % Game.GRIDSIZE, i / Game.GRIDSIZE);
|
||||
Console.ForegroundColor = ConsoleColor.White;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
9
Memory.Gui/App.xaml
Normal file
9
Memory.Gui/App.xaml
Normal file
@ -0,0 +1,9 @@
|
||||
<Application x:Class="Memory.Gui.App"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:local="clr-namespace:Memory.Gui"
|
||||
StartupUri="MainWindow.xaml">
|
||||
<Application.Resources>
|
||||
|
||||
</Application.Resources>
|
||||
</Application>
|
14
Memory.Gui/App.xaml.cs
Normal file
14
Memory.Gui/App.xaml.cs
Normal file
@ -0,0 +1,14 @@
|
||||
using System.Configuration;
|
||||
using System.Data;
|
||||
using System.Windows;
|
||||
|
||||
namespace Memory.Gui
|
||||
{
|
||||
/// <summary>
|
||||
/// Interaction logic for App.xaml
|
||||
/// </summary>
|
||||
public partial class App : Application
|
||||
{
|
||||
}
|
||||
|
||||
}
|
10
Memory.Gui/AssemblyInfo.cs
Normal file
10
Memory.Gui/AssemblyInfo.cs
Normal file
@ -0,0 +1,10 @@
|
||||
using System.Windows;
|
||||
|
||||
[assembly: ThemeInfo(
|
||||
ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
|
||||
//(used if a resource is not found in the page,
|
||||
// or application resource dictionaries)
|
||||
ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
|
||||
//(used if a resource is not found in the page,
|
||||
// app, or any theme specific resource dictionaries)
|
||||
)]
|
@ -1,9 +1,9 @@
|
||||
<Window x:Class="Memory.MainWindow"
|
||||
<Window x:Class="Memory.Gui.MainWindow"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
xmlns:local="clr-namespace:Memory"
|
||||
xmlns:local="clr-namespace:Memory.Gui"
|
||||
mc:Ignorable="d"
|
||||
Title="MainWindow" Height="450" Width="800">
|
||||
<Grid>
|
@ -9,7 +9,7 @@ using System.Windows.Media.Imaging;
|
||||
using System.Windows.Navigation;
|
||||
using System.Windows.Shapes;
|
||||
|
||||
namespace Memory
|
||||
namespace Memory.Gui
|
||||
{
|
||||
/// <summary>
|
||||
/// Interaction logic for MainWindow.xaml
|
@ -1,13 +1,11 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<OutputType>WinExe</OutputType>
|
||||
<TargetFramework>net8.0-windows</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<UseWPF>true</UseWPF>
|
||||
<StartupObject>Memory.Program</StartupObject>
|
||||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
@ -1,6 +1,11 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<PropertyGroup />
|
||||
<ItemGroup>
|
||||
<ApplicationDefinition Update="App.xaml">
|
||||
<SubType>Designer</SubType>
|
||||
</ApplicationDefinition>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Page Update="MainWindow.xaml">
|
||||
<SubType>Designer</SubType>
|
20
Memory.Logic/Card.cs
Normal file
20
Memory.Logic/Card.cs
Normal file
@ -0,0 +1,20 @@
|
||||
namespace Memory.Logic
|
||||
{
|
||||
public class Card(int id)
|
||||
{
|
||||
public int ID { get; } = id;
|
||||
public bool IsChoice1 { get; set; } = false;
|
||||
public bool IsChoice2 { get; set; } = false;
|
||||
public bool Completed { get; set; } = false;
|
||||
|
||||
public bool Matches(Card card)
|
||||
{
|
||||
return ID == card.ID;
|
||||
}
|
||||
|
||||
public bool Selected()
|
||||
{
|
||||
return IsChoice1 || IsChoice2;
|
||||
}
|
||||
}
|
||||
}
|
67
Memory.Logic/Game.cs
Normal file
67
Memory.Logic/Game.cs
Normal file
@ -0,0 +1,67 @@
|
||||
namespace Memory.Logic
|
||||
{
|
||||
public class Game
|
||||
{
|
||||
public const int DECKSIZE = 10;
|
||||
public const int GRIDSIZE = 5;
|
||||
public List<Card> Cards { get; } = CreateDeck(DECKSIZE);
|
||||
|
||||
private static List<Card> CreateDeck(int pairs)
|
||||
{
|
||||
List<Card> cards = [];
|
||||
for (int i = 1; i < pairs + 1; i++)
|
||||
{
|
||||
cards.Add(new(i));
|
||||
cards.Add(new(i));
|
||||
}
|
||||
Random random = new();
|
||||
return [..cards.OrderBy(card => random.Next())];
|
||||
}
|
||||
|
||||
public Card? GetChoice1()
|
||||
{
|
||||
return Cards.FirstOrDefault(card => card.IsChoice1);
|
||||
}
|
||||
|
||||
public Card? GetChoice2()
|
||||
{
|
||||
return Cards.FirstOrDefault(card => card.IsChoice2);
|
||||
}
|
||||
|
||||
public bool IsFinished()
|
||||
{
|
||||
return Cards.All(card => card.Completed);
|
||||
}
|
||||
|
||||
public void ClickCard(Card card)
|
||||
{
|
||||
Card? choice1 = GetChoice1();
|
||||
Card? choice2 = GetChoice2();
|
||||
if (!card.Completed)
|
||||
{
|
||||
if ((choice1 == null && choice2 == null) || (choice1 != null && choice2 != null))
|
||||
{
|
||||
if (choice1 != null)
|
||||
{
|
||||
choice1.IsChoice1 = false;
|
||||
}
|
||||
if (choice2 != null)
|
||||
{
|
||||
choice2.IsChoice2 = false;
|
||||
}
|
||||
card.IsChoice1 = true;
|
||||
}
|
||||
else if (choice1 != null && choice2 == null && choice1 != card)
|
||||
{
|
||||
card.IsChoice2 = true;
|
||||
if (choice1.Matches(card))
|
||||
{
|
||||
choice1.Completed = true;
|
||||
card.Completed = true;
|
||||
// handle score etc
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
9
Memory.Logic/Memory.Logic.csproj
Normal file
9
Memory.Logic/Memory.Logic.csproj
Normal file
@ -0,0 +1,9 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
22
Memory.sln
22
Memory.sln
@ -3,7 +3,11 @@ 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", "src/Memory.csproj", "{8380EF7C-6F0D-488A-9B88-409696E4565F}"
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "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}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Memory.Cmd", "Memory.Cmd\Memory.Cmd.csproj", "{36DBAAC0-3FEC-4C5C-8330-C1BD2D08BD05}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
@ -11,10 +15,18 @@ Global
|
||||
Release|Any CPU = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||
{8380EF7C-6F0D-488A-9B88-409696E4565F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{8380EF7C-6F0D-488A-9B88-409696E4565F}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{8380EF7C-6F0D-488A-9B88-409696E4565F}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{8380EF7C-6F0D-488A-9B88-409696E4565F}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{2490BF43-EFD6-4BE0-B231-860EB3508360}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{2490BF43-EFD6-4BE0-B231-860EB3508360}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{2490BF43-EFD6-4BE0-B231-860EB3508360}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{2490BF43-EFD6-4BE0-B231-860EB3508360}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{6E520079-0F1A-434A-82F8-56D3D5B57645}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{6E520079-0F1A-434A-82F8-56D3D5B57645}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{6E520079-0F1A-434A-82F8-56D3D5B57645}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{6E520079-0F1A-434A-82F8-56D3D5B57645}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{36DBAAC0-3FEC-4C5C-8330-C1BD2D08BD05}.Debug|Any CPU.ActiveCfg = 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.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
|
@ -1,15 +0,0 @@
|
||||
using System.Windows;
|
||||
|
||||
namespace Memory
|
||||
{
|
||||
internal partial class Program
|
||||
{
|
||||
[STAThread]
|
||||
public static void Main()
|
||||
{
|
||||
Application app = new();
|
||||
MainWindow mainWindow = new();
|
||||
app.Run(mainWindow);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user