Initial commit
This commit is contained in:
11
PkmnLib.Static/Utils/Errors/OutOfRange.cs
Normal file
11
PkmnLib.Static/Utils/Errors/OutOfRange.cs
Normal file
@@ -0,0 +1,11 @@
|
||||
using FluentResults;
|
||||
|
||||
namespace PkmnLib.Static.Utils.Errors;
|
||||
|
||||
public class OutOfRange : Error
|
||||
{
|
||||
public OutOfRange(string hint, int index, int max)
|
||||
: base($"{hint} index {index} is out of range. Must be between 0 and {max - 1}.")
|
||||
{
|
||||
}
|
||||
}
|
||||
43
PkmnLib.Static/Utils/Random.cs
Normal file
43
PkmnLib.Static/Utils/Random.cs
Normal file
@@ -0,0 +1,43 @@
|
||||
namespace PkmnLib.Static.Utils;
|
||||
|
||||
public interface IRandom
|
||||
{
|
||||
public int GetInt(int min, int max);
|
||||
public int GetInt(int max);
|
||||
public int GetInt();
|
||||
public float GetFloat();
|
||||
public float GetFloat(float min, float max);
|
||||
public bool GetBool();
|
||||
}
|
||||
|
||||
public class RandomImpl : IRandom
|
||||
{
|
||||
private Random _random;
|
||||
|
||||
public RandomImpl(Random random)
|
||||
{
|
||||
_random = random;
|
||||
}
|
||||
|
||||
public RandomImpl(int seed)
|
||||
{
|
||||
_random = new Random(seed);
|
||||
}
|
||||
|
||||
public RandomImpl()
|
||||
{
|
||||
_random = new Random();
|
||||
}
|
||||
|
||||
public int GetInt(int min, int max) => _random.Next(min, max);
|
||||
|
||||
public int GetInt(int max) => _random.Next(max);
|
||||
|
||||
public int GetInt() => _random.Next();
|
||||
|
||||
public float GetFloat() => (float)_random.NextDouble();
|
||||
|
||||
public float GetFloat(float min, float max) => (float)(_random.NextDouble() * (max - min) + min);
|
||||
|
||||
public bool GetBool() => _random.Next(2) == 1;
|
||||
}
|
||||
35
PkmnLib.Static/Utils/StringKey.cs
Normal file
35
PkmnLib.Static/Utils/StringKey.cs
Normal file
@@ -0,0 +1,35 @@
|
||||
using System;
|
||||
|
||||
namespace PkmnLib.Static.Utils;
|
||||
|
||||
/// <summary>
|
||||
/// A case-insensitive string key. We use this class for things like looking up data from dictionaries, etc.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// This is a struct, as it's effectively just a wrapper around a single reference object. Heap allocation would be silly.
|
||||
/// </remarks>
|
||||
public readonly record struct StringKey
|
||||
{
|
||||
private readonly string _key;
|
||||
|
||||
public StringKey(string key)
|
||||
{
|
||||
_key = key;
|
||||
}
|
||||
|
||||
public static implicit operator string(StringKey key) => key._key;
|
||||
public static implicit operator StringKey(string key) => new(key);
|
||||
|
||||
public override string ToString() => _key.ToLowerInvariant();
|
||||
|
||||
public bool Equals(StringKey other)
|
||||
{
|
||||
return string.Equals(_key, other._key, StringComparison.InvariantCultureIgnoreCase);
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return StringComparer.InvariantCultureIgnoreCase.GetHashCode(_key);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user