C#:
public class LimUniqueResponder
{
private List<string> responses;
private UniqueRandomGenerator urg = new UniqueRandomGenerator(0);
private readonly int lim;
// Constructor
public LimUniqueResponder(int lim)
{
responses = new List<string>();
this.lim = lim;
}
// Method to get a response
public string GetAResponse()
{
if (responses.Count == 0)
{
return "";
}
return responses[urg.GetUniqueRandom()];
}
// Method to check if responses contain a string
public bool ResponsesContainsStr(string item)
{
return responses.Contains(item);
}
// Method to check if a string contains any response
public bool StrContainsResponse(string item)
{
foreach (string response in responses)
{
if (string.IsNullOrEmpty(response))
{
continue;
}
if (item.Contains(response))
{
return true;
}
}
return false;
}
// Method to add a response
public void AddResponse(string s1)
{
if (this.responses.Count > lim - 1)
{
responses.RemoveAt(0);
}
if (!responses.Contains(s1))
{
responses.Add(s1);
urg = new UniqueRandomGenerator(responses.Count);
}
}
// Method to add multiple responses
public void AddResponses(params string[] replies)
{
foreach (string value in replies)
{
AddResponse(value);
}
}
// Method to get a savable string
public string GetSavableStr()
{
return string.Join("_", responses);
}
// Method to get the last item
public string GetLastItem()
{
if (responses.Count == 0)
{
return "";
}
return responses[responses.Count - 1];
}
}