5dplomacy/MultiversalDiplomacy/Model/Turn.cs

74 lines
2.3 KiB
C#
Raw Normal View History

2022-11-10 18:11:44 +00:00
using System.Diagnostics.CodeAnalysis;
namespace MultiversalDiplomacy.Model;
/// <summary>
/// A turn of a game. Essentially a wrapper around <see cref="int"/> for tracking the temporal dimension.
/// </summary>
public struct Turn
{
/// <summary>
/// The first turn number.
/// </summary>
public const int FIRST_TURN = 0;
/// <summary>
/// The first turn.
/// </summary>
public static readonly Turn First = new(FIRST_TURN);
/// <summary>
/// This turn's number.
/// </summary>
private readonly int number;
public Turn(int number)
{
if (number < FIRST_TURN)
{
throw new ArgumentException($"Invalid turn number: {number}", nameof(number));
}
this.number = number;
}
/// <summary>
/// Returns the turn's string representation as a Diplomacy season.
/// </summary>
/// <returns>The season and year as a string, e.g. "Spring 1901".</returns>
public override string ToString()
=> $"{(number % 2 == 0 ? "Spring" : "Fall")} {1901 + (number / 2)}";
/// <summary>
/// Returns a shorter string representation of the turn.
/// </summary>
/// <returns>The season and year as a string, e.g. "S'01".</returns>
public string ToShort()
=> $"{(number % 2 == 0 ? "S" : "F")}'{number / 2:D2}";
/// <summary>
/// Returns a value indicating whether this instance is equal to a specified <see cref="Turn"/> value.
/// </summary>
/// <returns>true if obj has the same value as this instance; otherwise, false.</returns>
public override bool Equals([NotNullWhen(true)] object? obj)
=> obj is Turn other
? number.Equals(other.number)
: false;
/// <summary>
/// Returns the hash code for this instance.
/// </summary>
/// <returns>A 32-bit signed integer hash code.</returns>
public override int GetHashCode()
=> number.GetHashCode();
public static int operator -(Turn first, Turn second)
=> Math.Abs(first.number - second.number);
public static bool operator ==(Turn first, Turn second)
=> first.number == second.number;
public static bool operator !=(Turn first, Turn second)
=> first.number != second.number;
public Turn Next => new Turn(number + 1);
}