48 lines
1.1 KiB
C#
48 lines
1.1 KiB
C#
using System.Text.Json;
|
|
using Lab1;
|
|
|
|
const string EnterValue = "Enter value:";
|
|
|
|
var rand = new Random();
|
|
var value = rand.Next(1, 100);
|
|
|
|
int guess;
|
|
int trials = 0;
|
|
do
|
|
{
|
|
Console.Write(EnterValue);
|
|
guess = Convert.ToInt32(Console.ReadLine());
|
|
|
|
if (guess > value)
|
|
Console.WriteLine("Too high!");
|
|
else if (guess < value)
|
|
Console.WriteLine("Too low!");
|
|
|
|
trials++;
|
|
} while (guess != value);
|
|
|
|
Console.WriteLine($"Win in {trials}th trial!");
|
|
Console.Write("Enter your name: ");
|
|
var name = Console.ReadLine();
|
|
|
|
if (string.IsNullOrEmpty(name))
|
|
return;
|
|
|
|
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));
|
|
|
|
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} trials");
|
|
}
|