Add unit model and unit tests

This commit is contained in:
Jaculabilis 2022-02-18 12:13:23 -08:00
parent b284f1acc6
commit 031d1b60bd
5 changed files with 157 additions and 0 deletions

View File

@ -0,0 +1,17 @@
namespace MultiversalDiplomacy.Model;
/// <summary>
/// One of the rival nations vying for control of the map.
/// </summary>
public class Power
{
/// <summary>
/// The power's name.
/// </summary>
public string Name { get; }
public Power(string name)
{
this.Name = name;
}
}

View File

@ -0,0 +1,54 @@
namespace MultiversalDiplomacy.Model;
/// <summary>
/// Represents a unit at a particular point in time.
/// </summary>
public class Unit
{
/// <summary>
/// The previous iteration of a unit. This is null if the unit was just built.
/// </summary>
public Unit? Past { get; }
/// <summary>
/// The location on the map where the unit is.
/// </summary>
public Location Location { get; }
/// <summary>
/// The season in time when the unit is.
/// </summary>
public Season Season { get; }
/// <summary>
/// The allegiance of the unit.
/// </summary>
public Power Power { get; }
/// <summary>
/// The type of unit.
/// </summary>
public UnitType Type { get; }
private Unit(Unit? past, Location location, Season season, Power power, UnitType type)
{
this.Past = past;
this.Location = location;
this.Season = season;
this.Power = power;
this.Type = type;
}
/// <summary>
/// Create a new unit. No validation is performed; the adjudicator should only call this
/// method after accepting a build order.
/// </summary>
public static Unit Build(Location location, Season season, Power power, UnitType type)
=> new Unit(past: null, location, season, power, type);
/// <summary>
/// Advance this unit's timeline to a new location and season.
/// </summary>
public Unit Next(Location location, Season season)
=> new Unit(past: this, location, season, this.Power, this.Type);
}

View File

@ -0,0 +1,17 @@
namespace MultiversalDiplomacy.Model;
/// <summary>
/// The type of a unit.
/// </summary>
public enum UnitType
{
/// <summary>
/// A unit that moves on land.
/// </summary>
Army = 0,
/// <summary>
/// A unit that moves in oceans and along coasts and can convoy armies.
/// </summary>
Fleet = 1,
}

View File

@ -0,0 +1,32 @@
using MultiversalDiplomacy.Map;
using MultiversalDiplomacy.Model;
namespace MultiversalDiplomacyTests;
public class TestMap : Map
{
public override IEnumerable<Province> Provinces { get; }
public static TestMap Instance { get; } = new TestMap();
private TestMap()
{
Provinces = new List<Province>()
{
Province.Supply("Left", "LEF")
.AddLandLocation(),
Province.Supply("Right", "RIG")
.AddLandLocation(),
Province.Empty("Center", "CEN")
.AddLandLocation(),
};
Land("LEF").AddBorder(Land("RIG"));
Land("LEF").AddBorder(Land("CEN"));
Land("RIG").AddBorder(Land("LEF"));
Land("RIG").AddBorder(Land("CEN"));
Land("CEN").AddBorder(Land("LEF"));
Land("CEN").AddBorder(Land("RIG"));
}
}

View File

@ -0,0 +1,37 @@
using MultiversalDiplomacy.Map;
using MultiversalDiplomacy.Model;
using NUnit.Framework;
namespace MultiversalDiplomacyTests;
public class UnitTests
{
[Test]
public void MovementTest()
{
Map map = TestMap.Instance;
Location left = map.Land("LEF"), right = map.Land("RIG"), center = map.Land("CEN");
Power pw1 = new Power("First");
Season s1 = Season.MakeRoot();
Unit u1 = Unit.Build(left, s1, pw1, UnitType.Army);
Season s2 = s1.MakeNext();
Unit u2 = u1.Next(right, s2);
Season s3 = s2.MakeNext();
Unit u3 = u2.Next(center, s3);
Assert.That(u3.Past, Is.EqualTo(u2), "Missing unit past");
Assert.That(u2.Past, Is.EqualTo(u1), "Missing unit past");
Assert.That(u1.Past, Is.Null, "Unexpected unit past");
Assert.That(u1.Season, Is.EqualTo(s1), "Unexpected unit season");
Assert.That(u2.Season, Is.EqualTo(s2), "Unexpected unit season");
Assert.That(u3.Season, Is.EqualTo(s3), "Unexpected unit season");
Assert.That(u1.Location, Is.EqualTo(left), "Unexpected unit location");
Assert.That(u2.Location, Is.EqualTo(right), "Unexpected unit location");
Assert.That(u3.Location, Is.EqualTo(center), "Unexpected unit location");
}
}