45 lines
1.1 KiB
C#
45 lines
1.1 KiB
C#
using System.Text.Json;
|
|
using Lab1;
|
|
|
|
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<HighScore>? highScores = null;
|
|
const string FileName = "highscores.json";
|
|
if (File.Exists(FileName))
|
|
highScores =
|
|
JsonSerializer.Deserialize<List<HighScore>>(File.ReadAllText(FileName))
|
|
?? new List<HighScore>();
|
|
|
|
if (highScores == null)
|
|
highScores = new List<HighScore>();
|
|
|
|
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");
|
|
}
|