Output on screen.
Output from results.txt file.
I want only print out the high score from the file. Only the best result from each person on the screen. In other words, no duplicates of names with scores.
Hope you can help me.
Score.cs
C#:
namespace GuessTheNumber
{
class Score : IComparable<Score>
{
public string PlayerName { get; set; }
public int GuessCount { get; set; }
public int CompareTo(Score other)
{
if (other == null) return 1;
if (this.GuessCount == other.GuessCount)
{
return this.PlayerName.CompareTo(other.PlayerName);
}
return this.GuessCount.CompareTo(other.GuessCount);
}
}
}
HandleHighScore.cs
C#:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace GuessTheNumber
{
class HandleHighScore
{
private string filePath = "results.txt";
public bool SaveOrUpdateHighScore(Score score)
{
List<Score> scores = FetchHighScore();
scores.Add(score);
// scores.Sort();
return SaveAllScores(scores);
}
public bool SaveAllScores(List<Score> scores)
{
try
{
using (StreamWriter sWriter = new StreamWriter(filePath, false))
{
foreach (var score in scores)
{
sWriter.WriteLine($"{score.PlayerName}: {score.GuessCount} gissningar");
}
}
return true;
}
catch
{
Console.WriteLine("Ett fel inträffade vid sparningen.");
return false;
}
}
public List<Score> FetchHighScore()
{
List<Score> scores = new List<Score>();
if (File.Exists(filePath))
{
using (StreamReader sReader = new StreamReader(filePath))
{
string line;
while ((line = sReader.ReadLine()) != null)
{
string[] data = line.Split(':');
if (data.Length == 2 && int.TryParse(data[1].Trim().Split(' ')[0], out int guessCount))
{
scores.Add(new Score { PlayerName = data[0], GuessCount = guessCount });
}
}
}
scores.Sort();
}
return scores;
}
}
}
GuessNumberGame.cs
C#:
using System;
using System.Collections.Generic;
namespace GuessTheNumber
{
class GuessNumberGame
{
private bool runGame;
private HandleHighScore saveScoreList;
public GuessNumberGame()
{
saveScoreList = new HandleHighScore();
}
public void PlayGame()
{
do
{
List<int> guesses = new List<int>();
Random random = new Random();
int numberToGuess = random.Next(1, 101);
Console.WriteLine("Start av nytt spel.");
while (true)
{
Console.Write("Gissa på ett tal mellan 1 och 100: ");
int userGuess;
while (!int.TryParse(Console.ReadLine(), out userGuess))
{
Console.Write("Ogiltigt tecken. ");
}
guesses.Add(userGuess);
if (userGuess == numberToGuess)
{
Console.WriteLine("Grattis, du gissade rätt nummer.");
Console.WriteLine($"Det tog dig {guesses.Count} gissningar.");
Console.Write("Ange ditt namn: ");
string playerName = Console.ReadLine();
Score gameScore = new Score
{
PlayerName = playerName,
GuessCount = guesses.Count
};
saveScoreList.SaveOrUpdateHighScore(gameScore);
List<Score> sortedScores = saveScoreList.FetchHighScore();
Console.WriteLine("High Scores:");
int test;
foreach (var score in sortedScores)
{
{
Console.WriteLine($"{score.PlayerName}: {score.GuessCount} gissningar");
}
}
break;
}
else if (userGuess < numberToGuess)
{
Console.WriteLine(userGuess + " är för lågt! Försök igen.");
}
else
{
Console.WriteLine(userGuess + " är för högt! Prova igen.");
}
}
Console.Write("Vill du spela igen? (ja/nej): ");
runGame = WillPlay(Console.ReadLine());
} while (runGame);
Console.WriteLine("Thank you for playing!");
}
private bool WillPlay(string response)
{
return response.Equals("ja", StringComparison.OrdinalIgnoreCase);
}
}
}
Last edited: