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