// (c) 2005 kp@kp73.com
using System;
using System.Globalization;
namespace KP.Tetris{
/// <summary>
/// A tetris game player.
/// </summary>
public class Player : IDisposable
{
/// <summary>
/// Number of players created.
/// </summary>
private static int m_playerCount;
private static object m_sync = new object();
/// <summary>
/// The game score for this player.
/// </summary>
private int m_score;
/// <summary>
/// Identifies this player.
/// </summary>
private string m_name;
/// <summary>
/// Initialises a new instance of the Player class.
/// </summary>
public Player() : this(Player.DefaultName)
{
}
/// <summary>
/// Initialises a new instance of the Player class.
/// </summary>
/// <param name="name"></param>
public Player(string name)
{
m_name = name;
lock(m_sync)
{
Player.m_playerCount++;
}
}
/// <summary>
/// Get the number of players created.
/// </summary>
internal static int Count { get { return m_playerCount; } }
/// <summary>
/// Get the game score for this player.
/// </summary>
public int Score { get { return m_score; } }
/// <summary>
/// Add points to the players score.
/// </summary>
/// <param name="level">The game level.</param>
/// <param name="freeFallCount">The number of rows the active piece has fallen.</param>
internal void AwardPoints(int level, int freeFallCount)
{
m_score += ((24 + (3 * level)) - freeFallCount);
}
/// <summary>
/// Get the default identity of this player.
/// </summary>
internal static string DefaultName
{
get
{
return string.Format(CultureInfo.CurrentUICulture, "Player {0}",
Player.Count.ToString(System.Globalization.CultureInfo.InvariantCulture));
}
}
#region IDisposable Members
/// <summary>
/// Clean up. Decreases player count.
/// </summary>
public void Dispose()
{
lock(m_sync)
{
m_playerCount--;
}
}
#endregion
}
}
|