💡♻️ Add to Game thanks to new classes, add fixes
continuous-integration/drone/push Build is passing Details

pull/57/head
Alexis Drai 2 years ago
parent d744b07e3b
commit 48db9ba6de

@ -15,12 +15,7 @@ namespace Data
public GameRunner LoadApp() public GameRunner LoadApp()
{ {
// this doesn't do much for now, because the classes aren't coded // this doesn't do much for now, because the classes aren't coded
List<Game> games = new() List<Game> games = new();
{
new Game("a"),
new Game("b"),
new Game("c")
};
PlayerManager gpm = new(); PlayerManager gpm = new();
gpm.Add(new Player("Alice")); gpm.Add(new Player("Alice"));

@ -1,10 +1,22 @@
using System.Collections.Generic; using System;
using System.Collections.Generic;
using Model.Dice.Faces; using Model.Dice.Faces;
namespace Model.Dice namespace Model.Dice
{ {
public class Die public class Die
{ {
private IEnumerable<AbstractDieFace> faces; private static readonly Random random = new Random();
private List<AbstractDieFace> faces;
public AbstractDieFace Throw()
{
// next(x, y) --> [x, y[
return faces[random.Next(0, faces.Count)];
// replace with better algo later
}
} }
} }

@ -1,10 +1,13 @@
using System.Collections.Generic; using Model.Players;
using System.Collections.Generic;
namespace Model.Dice namespace Model.Dice
{ {
public class FavGroup public class FavGroup
{ {
private IEnumerable<Die> dice; public IEnumerable<Die> Dice { get; private set; }
public string Name { get; private set; } public string Name { get; private set; }
} }
} }

@ -1,17 +1,45 @@
using Model.Dice; using Model.Dice;
using Model.Dice.Faces;
using Model.Players; using Model.Players;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Numerics;
using System.Text; using System.Text;
namespace Model.Games namespace Model.Games
{ {
public class Game public class Game
{ {
public string Name { get; private set; } /// <summary>
/// the name of the game 😎
/// </summary>
public string Name
{
get
{
return name;
}
set // GameRunner will need to take care of forbidding
// (or allowing) having two Games with the same name etc.
{
if (string.IsNullOrWhiteSpace(value))
{
throw new ArgumentException("param should not be null or blank", nameof(value));
}
name = value;
}
}
private string name;
/// <summary>
/// whether this game is new or not
/// </summary>
public bool IsFirstTurn { get; private set; } = false; public bool IsFirstTurn { get; private set; } = false;
/// <summary>
/// the turns that have been done so far
/// </summary>
private readonly List<Turn> turns; private readonly List<Turn> turns;
/// </summary> /// </summary>
@ -20,36 +48,112 @@ namespace Model.Games
/// <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>
/// the game's player manager, doing CRUD on players and switching whose turn it is
/// </summary>
private readonly PlayerManager playerManager; private readonly PlayerManager playerManager;
/// <summary>
/// the group of dice used for this game
/// </summary>
private readonly FavGroup favGroup; private readonly FavGroup favGroup;
public Game(string name, IEnumerable<Turn> turns, PlayerManager playerManager, FavGroup favGroup) /// <summary>
/// constructs a Game with its own history of Turns.
/// If <paramref name="turns"/> is null, starts a new history
/// </summary>
/// <param name="name">the name of the game 😎</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="favGroup">the group of dice used for this game</param>
public Game(string name, PlayerManager playerManager, FavGroup favGroup, IEnumerable<Turn> turns)
{ {
Name = name; Name = name;
this.turns = turns.ToList() ?? new List<Turn>(); this.turns = turns.ToList() ?? new List<Turn>();
this.playerManager = playerManager ?? new PlayerManager(); this.playerManager = playerManager;
this.favGroup = favGroup ?? new FavGroup(); this.favGroup = favGroup;
} }
public Game(string name) /// <summary>
: this(name, null, null, null) /// constructs a Game with no history of turns.
/// </summary>
/// <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="favGroup">the group of dice used for this game</param>
public Game(string name, PlayerManager playerManager, FavGroup favGroup)
: this(name, playerManager, favGroup, null)
{ } { }
public void PerformTurn() /// <summary>
/// performs a Turn, marks this Game as "started", and logs that Turn
/// </summary>
/// <param name="player">the player whose turn it is</param>
public void PerformTurn(Player player)
{ {
Player player = playerManager.WhoPlaysNow(IsFirstTurn);
if (IsFirstTurn) { IsFirstTurn = false; } // only true one time (on the first turn...) if (IsFirstTurn) { IsFirstTurn = false; } // only true one time (on the first turn...)
Turn turn = Turn.CreateWithDefaultTime(
new Player(player),
ThrowAll() //using a copy so that next line doesn't "change history"
);
turns.Add(turn);
}
/// <summary>
/// finds whose turn it is
/// </summary>
/// <returns>the Player whose turn it is</returns>
public Player GetWhoPlaysNow()
{
return playerManager.WhoPlaysNow(IsFirstTurn);
}
// throw favGroup's dice and record it in a "faces" var /// <summary>
/// throws all the Dice in FavGroup and returns a list of their Faces
/// </summary>
/// <returns>list of AbstractDieFaces after a throw</returns>
private List<AbstractDieFace> ThrowAll()
{
List<AbstractDieFace> faces = new();
foreach (Die die in favGroup.Dice)
{
faces.Add(die.Throw());
}
return faces;
}
Turn turn = Turn.CreateWithDefaultTime(new Player(player)/*, faces*/); //using a copy so that next line doesn't "change history" /// <summary>
playerManager.PrepareNextPlayer(player); /// asks the PlayerManager to prepare the next Player
turns.Add(turn); /// </summary>
/// <param name="currentPlayer">the Player whose turn it was</param>
public void PrepareNextPlayer(Player currentPlayer)
{
playerManager.PrepareNextPlayer(currentPlayer);
}
public Player AddPlayerToGame(Player player)
{
return playerManager.Add(player);
}
public IEnumerable<Player> GetPlayersFromGame()
{
return playerManager.GetAll();
}
public Player UpdatePlayerInGame(Player oldPlayer, Player newPlayer)
{
return playerManager.Update(oldPlayer, newPlayer);
} }
// TODO test and debug public void RemovePlayerFromGame(Player player)
{
playerManager.Remove(player);
}
/// <summary>
/// represents a Game in string format
/// </summary>
/// <returns>a Game in string format</returns>
public override string ToString() public override string ToString()
{ {
StringBuilder sb = new(); StringBuilder sb = new();

@ -79,8 +79,6 @@ namespace Model.Players
/// <summary> /// <summary>
/// finds and returns the player whose turn it is /// finds and returns the player whose turn it is
/// <br/>
/// SHOULD HAVE NO SIDE EFFECT
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
/// <exception cref="Exception"></exception> /// <exception cref="Exception"></exception>
@ -93,8 +91,7 @@ namespace Model.Players
throw new Exception("you are exploring an empty collection\nthis should not have happened"); throw new Exception("you are exploring an empty collection\nthis should not have happened");
} }
Player result = null; Player result;
if (isFirstTurn) if (isFirstTurn)
{ {
result = players[0]; result = players[0];
@ -103,7 +100,6 @@ namespace Model.Players
{ {
result = players[NextIndex]; result = players[NextIndex];
} }
return result; return result;
} }

Loading…
Cancel
Save