PingPong/PingPong.GameEngine/Components/Score.cs

212 lines
4.7 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
namespace PingPong.GameEngine.Components
{
public class Score
{
#region Members
/// <summary>
/// The score history.
/// </summary>
private Stack<int> history;
/// <summary>
/// The score value.
/// </summary>
private Lazy<int> value;
#endregion
#region Properties
/// <summary>
/// Gets the score value.
/// </summary>
/// <value>
/// The value.
/// </value>
public int Value
{
get
{
return this.value.Value;
}
private set
{
history.Push(this.Value);
this.value = new Lazy<int>(() => value);
}
}
/// <summary>
/// Gets the increment expression.
/// </summary>
/// <value>
/// The increment expression.
/// </value>
public string IncrementExpression { get; private set; }
/// <summary>
/// Gets the decrement expression.
/// </summary>
/// <value>
/// The decrement expression.
/// </value>
public string DecrementExpression { get; private set; }
#endregion
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="PingPong.GameEngine.Score"/> class.
/// </summary>
public Score()
{
this.history = new Stack<int>();
this.value = new Lazy<int>(() => default(int));
}
#endregion
#region Methods
/// <summary>
/// Sets the increment expression.
/// </summary>
/// <returns>
/// The increment expression.
/// </returns>
/// <param name='expression'>
/// Expression.
/// </param>
public Score SetIncrementExpression(string expression)
{
if (string.IsNullOrWhiteSpace(expression))
{
throw new ArgumentNullException("expression");
}
this.IncrementExpression = expression;
return this;
}
/// <summary>
/// Sets the decrement expression.
/// </summary>
/// <returns>
/// The decrement expression.
/// </returns>
/// <param name='expression'>
/// Expression.
/// </param>
public Score SetDecrementExpression(string expression)
{
this.DecrementExpression = expression;
return this;
}
/// <summary>
/// Sets the score.
/// </summary>
/// <returns>
/// The score.
/// </returns>
/// <param name='expression'>
/// Expression.
/// </param>
public Score SetScore(string expression)
{
if (string.IsNullOrWhiteSpace(expression))
{
throw new ArgumentNullException("expression");
}
Engine.ExpressionParser.Interpret(expression);
this.Value = Engine.ExpressionParser.Int;
return this;
}
/// <summary>
/// Sets the score.
/// </summary>
/// <returns>
/// The score.
/// </returns>
/// <param name='score'>
/// Score.
/// </param>
public Score SetScore(int score)
{
this.Value = score;
return this;
}
/// <summary>
/// Increment this instance.
/// </summary>
public int Increment()
{
if (string.IsNullOrWhiteSpace(this.IncrementExpression))
{
throw new NotImplementedException("No increment expression set.");
}
Engine.ExpressionParser.Interpret(this.IncrementExpression);
this.Value += Engine.ExpressionParser.Int;
return this.Value;
}
/// <summary>
/// Decrement this instance.
/// </summary>
public int Decrement()
{
if (string.IsNullOrWhiteSpace(this.DecrementExpression))
{
Reverse();
}
else
{
throw new NotImplementedException("No decrement expression set.");
}
Engine.ExpressionParser.Interpret(this.DecrementExpression);
this.Value += Engine.ExpressionParser.Int;
return this.Value;
}
/// <summary>
/// Reverse the last action.
/// </summary>
public int Reverse()
{
if (!this.history.Any())
{
throw new ArgumentOutOfRangeException("No history found.");
}
this.value = new Lazy<int>(() => this.history.Pop());
return this.Value;
}
#endregion
#region Operators
/// <param name='score'>
/// Score.
/// </param>
public static implicit operator int(Score score)
{
return score.Value;
}
#endregion
}
}