commit 8ce1137eb716a8cea5952c927d1883fa24c72daa Author: Marcin Badurowicz Date: Fri Oct 11 10:32:33 2024 +0200 initial diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3afdc66 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +bin/ +obj/ +.vscode/ \ No newline at end of file diff --git a/HighScore.cs b/HighScore.cs new file mode 100644 index 0000000..f8d487a --- /dev/null +++ b/HighScore.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Lab1 +{ + internal class HighScore + { + public string Name { get; set; } + public int Trials { get; set; } + } +} diff --git a/Lab1.csproj b/Lab1.csproj new file mode 100644 index 0000000..74abf5c --- /dev/null +++ b/Lab1.csproj @@ -0,0 +1,10 @@ + + + + Exe + net6.0 + enable + enable + + + diff --git a/Lab1.sln b/Lab1.sln new file mode 100644 index 0000000..6dfecda --- /dev/null +++ b/Lab1.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.2.32602.215 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lab1", "Lab1.csproj", "{32FE7F03-E9ED-411A-9046-4E691EF040BA}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {32FE7F03-E9ED-411A-9046-4E691EF040BA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {32FE7F03-E9ED-411A-9046-4E691EF040BA}.Debug|Any CPU.Build.0 = Debug|Any CPU + {32FE7F03-E9ED-411A-9046-4E691EF040BA}.Release|Any CPU.ActiveCfg = Release|Any CPU + {32FE7F03-E9ED-411A-9046-4E691EF040BA}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {326F17E7-2CD2-4C6A-8484-8D0952D2FECE} + EndGlobalSection +EndGlobal diff --git a/Program.cs b/Program.cs new file mode 100644 index 0000000..b7d0e80 --- /dev/null +++ b/Program.cs @@ -0,0 +1,43 @@ +using Lab1; +using System.Text.Json; + +var rand = new Random(); +var value = rand.Next(1, 100); + +int guess; +int trials = 0; +do +{ + Console.Write("Wprowadź wartość: "); + guess = Convert.ToInt32(Console.ReadLine()); + + if (guess > value) + Console.WriteLine("Za dużo!"); + else if (guess < value) + Console.WriteLine("Za mało!"); + + trials++; +} +while (guess != value); + +Console.WriteLine($"Wygrana w {trials} próbie!"); +Console.Write("Podaj swoje imię: "); +var name = Console.ReadLine(); + +var hs = new HighScore { Name = name, Trials = trials }; + +List highScores = null; +const string FileName = "highscores.json"; +if (File.Exists(FileName)) + highScores = JsonSerializer.Deserialize>(File.ReadAllText(FileName)); + +if (highScores == null) + highScores = new List(); + +highScores.Add(hs); +File.WriteAllText(FileName, JsonSerializer.Serialize(highScores)); + +foreach (var item in highScores.OrderBy(x => x.Trials)) +{ + Console.WriteLine($"{item.Name} -- {item.Trials} prób"); +} \ No newline at end of file