using System;
using System.Collections.Generic;
namespace Balloons_Pops_Game_Variant_Four
{
public class ScoreboardInitialization
{
private static List<Player> playersScoreboard;
private static int bestPlayerScore = 0;
private static int worstPlayerScore = int.MaxValue;
static ScoreboardInitialization()
{
playersScoreboard = new List<Player>();
}
public static void Plot()
{
if (playersScoreboard.Count == 0)
{
ConfigurateEmptyLinesOfMessages.WriteWithEmptyLine(GameMessages.emptyScoreboard);
return;
}
ConfigurateEmptyLinesOfMessages.WriteEmptyLine(); Console.WriteLine();
ConfigurateEmptyLinesOfMessages.WriteWithEmptyLine(GameMessages.scoreboardMessage);
List<KeyValuePair<String, Int32>> scoreboardList = Sort();
int index = 1;
foreach (KeyValuePair<String, Int32> item in scoreboardList)
{
Console.WriteLine(" {0} {1} --> {2} moves ", index, item.Key, item.Value);
index++;
}
Console.WriteLine();
}
private static List<KeyValuePair<String, Int32>> Sort()
{
List<KeyValuePair<String, Int32>> scoreboardList = new List<KeyValuePair<String,Int32>>();
foreach (Player p in playersScoreboard)
{
KeyValuePair<String, Int32> item = new KeyValuePair<string,int>(p.Name, p.Score);
scoreboardList.Add(item);
}
scoreboardList.Sort(delegate(KeyValuePair<String, Int32> x,
KeyValuePair<String, Int32> y) { return x.Value.CompareTo(y.Value); });
return scoreboardList;
}
public static bool ShouldBeRefreshed(Player player)
{
return player.Score <= worstPlayerScore;
}
public static void Refresh(Player player)
{
if (!ShouldBeRefreshed(player))
return;
if (playersScoreboard.Count == 5)
DeleteTheWorstPlayer();
playersScoreboard.Add(player);
RefreshScores(player);
}
private static void DeleteTheWorstPlayer()
{
Player player = new Player();
player.Score = worstPlayerScore;
foreach (Player players in playersScoreboard)
{
if(players.Score == worstPlayerScore)
{
player.Name = players.Name;
break;
}
}
playersScoreboard.Remove(player);
}
private static void RefreshScores(Player player)
{
if (player.Score < bestPlayerScore)
bestPlayerScore = player.Score;
if (playersScoreboard.Count < 5)
worstPlayerScore = int.MaxValue;
else
worstPlayerScore = player.Score;
foreach (Player p in playersScoreboard)
{
if (p.Score > worstPlayerScore)
worstPlayerScore = p.Score;
}
}
}
}