Ensure unicity within Game.turns

pull/88/head
Alexis Drai 3 years ago
parent 2556b46192
commit a03c1f4526

@ -1,209 +1,212 @@
using Model.Dice; using Model.Dice;
using Model.Dice.Faces; using Model.Dice.Faces;
using Model.Players; using Model.Players;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Numerics; using System.Numerics;
using System.Text; using System.Text;
namespace Model.Games namespace Model.Games
{ {
public class Game public class Game
{ {
/// <summary> /// <summary>
/// the name of the game 😎 /// the name of the game 😎
/// </summary> /// </summary>
public string Name public string Name
{ {
get get
{ {
return name; return name;
} }
set // GameRunner will need to take care of forbidding set // GameRunner will need to take care of forbidding
// (or allowing) having two Games with the same name etc. // (or allowing) having two Games with the same name etc.
{ {
if (string.IsNullOrWhiteSpace(value)) if (string.IsNullOrWhiteSpace(value))
{ {
throw new ArgumentException("param should not be null or blank", nameof(value)); throw new ArgumentException("param should not be null or blank", nameof(value));
} }
name = value; name = value;
} }
} }
private string name; private string name;
/// <summary> /// <summary>
/// references the position in list of the current player, for a given game. /// references the position in list of the current player, for a given game.
/// </summary> /// </summary>
private int nextIndex; private int nextIndex;
/// <summary> /// <summary>
/// the turns that have been done so far /// the turns that have been done so far
/// </summary> /// </summary>
private readonly List<Turn> turns; private readonly List<Turn> turns;
/// </summary> /// </summary>
/// get a READ ONLY enumerable of all turns belonging to this game /// get a READ ONLY enumerable of all turns belonging to this game
/// </summary> /// </summary>
/// <returns>a readonly enumerable of all this game's turns</returns> /// <returns>a readonly enumerable of all this game's turns</returns>
public IEnumerable<Turn> GetHistory() => turns.AsEnumerable(); public IEnumerable<Turn> GetHistory() => turns.AsEnumerable();
/// <summary> /// <summary>
/// the game's player manager, doing CRUD on players and switching whose turn it is /// the game's player manager, doing CRUD on players and switching whose turn it is
/// </summary> /// </summary>
private readonly IManager<Player> playerManager; private readonly IManager<Player> playerManager;
/// <summary> /// <summary>
/// the group of dice used for this game /// the group of dice used for this game
/// </summary> /// </summary>
public IEnumerable<AbstractDie<AbstractDieFace>> Dice => dice; public IEnumerable<AbstractDie<AbstractDieFace>> Dice => dice;
private readonly IEnumerable<AbstractDie<AbstractDieFace>> dice; private readonly IEnumerable<AbstractDie<AbstractDieFace>> dice;
/// <summary> /// <summary>
/// constructs a Game with its own history of Turns. /// constructs a Game with its own history of Turns.
/// If <paramref name="turns"/> is null, starts a new history /// If <paramref name="turns"/> is null, starts a new history
/// </summary> /// </summary>
/// <param name="name">the name of the game 😎</param> /// <param name="name">the name of the game 😎</param>
/// <param name="turns">the turns that have been done so far</param> /// <param name="turns">the turns that have been done so far</param>
/// <param name="playerManager">the game's player manager, doing CRUD on players and switching whose turn it is</param> /// <param name="playerManager">the game's player manager, doing CRUD on players and switching whose turn it is</param>
/// <param name="favGroup">the group of dice used for this game</param> /// <param name="favGroup">the group of dice used for this game</param>
public Game(string name, IManager<Player> playerManager, IEnumerable<AbstractDie<AbstractDieFace>> dice, IEnumerable<Turn> turns) public Game(string name, IManager<Player> playerManager, IEnumerable<AbstractDie<AbstractDieFace>> dice, IEnumerable<Turn> turns)
{ {
Name = name; Name = name;
this.turns = turns is null ? new List<Turn>() : turns.ToList(); this.turns = turns is null ? new List<Turn>() : turns.ToList();
this.playerManager = playerManager; this.playerManager = playerManager;
this.dice = dice; this.dice = dice;
this.nextIndex = 0; this.nextIndex = 0;
} }
/// <summary> /// <summary>
/// constructs a Game with no history of turns. /// constructs a Game with no history of turns.
/// </summary> /// </summary>
/// <param name="name">the name of the game 😎</param> /// <param name="name">the name of the game 😎</param>
/// <param name="playerManager">the game's player manager, doing CRUD on players and switching whose turn it is</param> /// <param name="playerManager">the game's player manager, doing CRUD on players and switching whose turn it is</param>
/// <param name="favGroup">the group of dice used for this game</param> /// <param name="favGroup">the group of dice used for this game</param>
public Game(string name, IManager<Player> playerManager, IEnumerable<AbstractDie<AbstractDieFace>> dice) public Game(string name, IManager<Player> playerManager, IEnumerable<AbstractDie<AbstractDieFace>> dice)
: this(name, playerManager, dice, null) : this(name, playerManager, dice, null)
{ } { }
/// <summary> /// <summary>
/// performs a Turn, marks this Game as "started", and logs that Turn /// performs a Turn, marks this Game as "started", and logs that Turn
/// </summary> /// </summary>
/// <param name="player">the player whose turn it is</param> /// <param name="player">the player whose turn it is</param>
public void PerformTurn(Player player) public void PerformTurn(Player player)
{ {
Turn turn = Turn.CreateWithDefaultTime( Turn turn = Turn.CreateWithDefaultTime(
player, player,
ThrowAll() ThrowAll()
); );
turns.Add(turn); if(turn != null && !(turns.Contains(turn)))
} {
turns.Add(turn);
/// <summary> }
/// finds and returns the player whose turn it is }
/// </summary>
/// <returns></returns> /// <summary>
/// <exception cref="Exception"></exception> /// finds and returns the player whose turn it is
public Player GetWhoPlaysNow() /// </summary>
{ /// <returns></returns>
if (!playerManager.GetAll().Any()) /// <exception cref="Exception"></exception>
{ public Player GetWhoPlaysNow()
throw new MemberAccessException("you are exploring an empty collection\nthis should not have happened"); {
} if (!playerManager.GetAll().Any())
return playerManager.GetAll().ElementAt(nextIndex); {
} throw new MemberAccessException("you are exploring an empty collection\nthis should not have happened");
}
/// <summary> return playerManager.GetAll().ElementAt(nextIndex);
/// this feels very dirty }
/// </summary>
/// <param name="current">the current player</param> /// <summary>
/// <exception cref="MemberAccessException"></exception> /// this feels very dirty
/// <exception cref="ArgumentNullException"></exception> /// </summary>
/// <exception cref="ArgumentException"></exception> /// <param name="current">the current player</param>
public void PrepareNextPlayer(Player current) /// <exception cref="MemberAccessException"></exception>
{ /// <exception cref="ArgumentNullException"></exception>
if (!playerManager.GetAll().Any()) /// <exception cref="ArgumentException"></exception>
{ public void PrepareNextPlayer(Player current)
throw new MemberAccessException("you are exploring an empty collection\nthis should not have happened"); {
} if (!playerManager.GetAll().Any())
if (current == null) {
{ throw new MemberAccessException("you are exploring an empty collection\nthis should not have happened");
throw new ArgumentNullException(nameof(current), "param should not be null"); }
} if (current == null)
if (!playerManager.GetAll().Contains(current)) {
{ throw new ArgumentNullException(nameof(current), "param should not be null");
throw new ArgumentException("param could not be found in this collection\n did you forget to add it?", nameof(current)); }
} if (!playerManager.GetAll().Contains(current))
if (playerManager.GetAll().Last() == current) {
{ throw new ArgumentException("param could not be found in this collection\n did you forget to add it?", nameof(current));
// if we've reached the last player, we need the index to loop back around }
nextIndex = 0; if (playerManager.GetAll().Last() == current)
} {
else // if we've reached the last player, we need the index to loop back around
{ nextIndex = 0;
// else we can just move up by one from the current index }
nextIndex++; else
} {
} // else we can just move up by one from the current index
nextIndex++;
/// <summary> }
/// throws all the Dice in FavGroup and returns a list of their Faces }
/// </summary>
/// <returns>list of AbstractDieFaces after a throw</returns> /// <summary>
private Dictionary<AbstractDie<AbstractDieFace>, AbstractDieFace> ThrowAll() /// throws all the Dice in FavGroup and returns a list of their Faces
{ /// </summary>
Dictionary<AbstractDie<AbstractDieFace>, AbstractDieFace> faces = new(); /// <returns>list of AbstractDieFaces after a throw</returns>
foreach (AbstractDie<AbstractDieFace> die in dice) private Dictionary<AbstractDie<AbstractDieFace>, AbstractDieFace> ThrowAll()
{ {
faces.Add(die, die.GetRandomFace()); Dictionary<AbstractDie<AbstractDieFace>, AbstractDieFace> faces = new();
} foreach (AbstractDie<AbstractDieFace> die in dice)
return faces; {
} faces.Add(die, die.GetRandomFace());
}
public Player AddPlayerToGame(Player player) return faces;
{ }
return playerManager.Add(player);
} public Player AddPlayerToGame(Player player)
{
public IEnumerable<Player> GetPlayersFromGame() return playerManager.Add(player);
{ }
return playerManager.GetAll();
} public IEnumerable<Player> GetPlayersFromGame()
{
public Player UpdatePlayerInGame(Player oldPlayer, Player newPlayer) return playerManager.GetAll();
{ }
return playerManager.Update(oldPlayer, newPlayer);
} public Player UpdatePlayerInGame(Player oldPlayer, Player newPlayer)
{
public void RemovePlayerFromGame(Player player) return playerManager.Update(oldPlayer, newPlayer);
{ }
playerManager.Remove(player);
} public void RemovePlayerFromGame(Player player)
{
/// <summary> playerManager.Remove(player);
/// represents a Game in string format }
/// </summary>
/// <returns>a Game in string format</returns> /// <summary>
public override string ToString() /// represents a Game in string format
{ /// </summary>
StringBuilder sb = new(); /// <returns>a Game in string format</returns>
sb.Append($"Game: {Name}"); public override string ToString()
{
sb.Append("\nPlayers:"); StringBuilder sb = new();
foreach (Player player in GetPlayersFromGame()) sb.Append($"Game: {Name}");
{
sb.Append($" {player.ToString()}"); sb.Append("\nPlayers:");
} foreach (Player player in GetPlayersFromGame())
{
sb.Append($"\nNext: {GetWhoPlaysNow()}"); sb.Append($" {player.ToString()}");
}
sb.Append("\nLog:\n");
foreach (Turn turn in this.turns) sb.Append($"\nNext: {GetWhoPlaysNow()}");
{
sb.Append($"\t{turn.ToString()}\n"); sb.Append("\nLog:\n");
} foreach (Turn turn in this.turns)
{
return sb.ToString(); sb.Append($"\t{turn.ToString()}\n");
} }
}
} return sb.ToString();
}
}
}

Loading…
Cancel
Save