Snake/StepResult.cs

Defines StepOutcome enum for snake game tick results and a readonly record struct StepResult holding outcome, positions, direction, growth flag and score gained, with convenience properties IsTerminal and IsDeath.

namespace Coilgarden;

/// <summary>What one logical tick did.</summary>
public enum StepOutcome
{
	/// <summary>Moved into an empty cell.</summary>
	Moved,

	/// <summary>Moved into the apple. The snake grew and the score went up.</summary>
	Ate,

	/// <summary>Moved into a wall. The run is over.</summary>
	HitWall,

	/// <summary>Moved into its own body. The run is over.</summary>
	HitSelf,

	/// <summary>Ate the last apple the arena had room for. The run is won.</summary>
	FilledArena
}

/// <summary>
/// The outcome of one tick, in enough detail that the presentation layer never has to
/// re-derive anything the simulation already knew.
/// </summary>
public readonly record struct StepResult(
	StepOutcome Outcome,
	GridPos From,
	GridPos To,
	Direction Direction,
	bool Grew,
	int ScoreGained )
{
	/// <summary>True when this tick ended the run, whether won or lost.</summary>
	public bool IsTerminal => Outcome is StepOutcome.HitWall or StepOutcome.HitSelf or StepOutcome.FilledArena;

	/// <summary>True when the run ended badly, as opposed to filling the arena.</summary>
	public bool IsDeath => Outcome is StepOutcome.HitWall or StepOutcome.HitSelf;
}