From 48db9ba6de11f86f1e157f04861199c05f336643 Mon Sep 17 00:00:00 2001 From: Alexis DRAI Date: Wed, 21 Sep 2022 19:11:31 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=92=A1=E2=99=BB=EF=B8=8F=20Add=20to=20Gam?= =?UTF-8?q?e=20thanks=20to=20new=20classes,=20add=20fixes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Sources/Data/Stub.cs | 7 +- Sources/Model/Dice/Die.cs | 16 ++- Sources/Model/Dice/FavGroup.cs | 7 +- Sources/Model/Games/Game.cs | 130 ++++++++++++++++++++++--- Sources/Model/Players/PlayerManager.cs | 6 +- 5 files changed, 138 insertions(+), 28 deletions(-) diff --git a/Sources/Data/Stub.cs b/Sources/Data/Stub.cs index a8ea95a..39d29d5 100644 --- a/Sources/Data/Stub.cs +++ b/Sources/Data/Stub.cs @@ -15,12 +15,7 @@ namespace Data public GameRunner LoadApp() { // this doesn't do much for now, because the classes aren't coded - List games = new() - { - new Game("a"), - new Game("b"), - new Game("c") - }; + List games = new(); PlayerManager gpm = new(); gpm.Add(new Player("Alice")); diff --git a/Sources/Model/Dice/Die.cs b/Sources/Model/Dice/Die.cs index 5ba8c68..7f2314a 100644 --- a/Sources/Model/Dice/Die.cs +++ b/Sources/Model/Dice/Die.cs @@ -1,10 +1,22 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using Model.Dice.Faces; namespace Model.Dice { public class Die { - private IEnumerable faces; + private static readonly Random random = new Random(); + + private List faces; + + public AbstractDieFace Throw() + { + // next(x, y) --> [x, y[ + return faces[random.Next(0, faces.Count)]; + // replace with better algo later + } } + + } diff --git a/Sources/Model/Dice/FavGroup.cs b/Sources/Model/Dice/FavGroup.cs index 24bda81..b718e38 100644 --- a/Sources/Model/Dice/FavGroup.cs +++ b/Sources/Model/Dice/FavGroup.cs @@ -1,10 +1,13 @@ -using System.Collections.Generic; +using Model.Players; +using System.Collections.Generic; namespace Model.Dice { public class FavGroup { - private IEnumerable dice; + public IEnumerable Dice { get; private set; } + public string Name { get; private set; } + } } diff --git a/Sources/Model/Games/Game.cs b/Sources/Model/Games/Game.cs index 0bcc2c8..2ee0a41 100644 --- a/Sources/Model/Games/Game.cs +++ b/Sources/Model/Games/Game.cs @@ -1,17 +1,45 @@ using Model.Dice; +using Model.Dice.Faces; using Model.Players; +using System; using System.Collections.Generic; using System.Linq; +using System.Numerics; using System.Text; namespace Model.Games { public class Game { - public string Name { get; private set; } + /// + /// the name of the game 😎 + /// + 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; + /// + /// whether this game is new or not + /// public bool IsFirstTurn { get; private set; } = false; + /// + /// the turns that have been done so far + /// private readonly List turns; /// @@ -20,36 +48,112 @@ namespace Model.Games /// a readonly enumerable of all this game's turns public IEnumerable GetHistory() => turns.AsEnumerable(); + /// + /// the game's player manager, doing CRUD on players and switching whose turn it is + /// private readonly PlayerManager playerManager; + /// + /// the group of dice used for this game + /// private readonly FavGroup favGroup; - public Game(string name, IEnumerable turns, PlayerManager playerManager, FavGroup favGroup) + /// + /// constructs a Game with its own history of Turns. + /// If is null, starts a new history + /// + /// the name of the game 😎 + /// the turns that have been done so far + /// the game's player manager, doing CRUD on players and switching whose turn it is + /// the group of dice used for this game + public Game(string name, PlayerManager playerManager, FavGroup favGroup, IEnumerable turns) { Name = name; this.turns = turns.ToList() ?? new List(); - this.playerManager = playerManager ?? new PlayerManager(); - this.favGroup = favGroup ?? new FavGroup(); + this.playerManager = playerManager; + this.favGroup = favGroup; } - public Game(string name) - : this(name, null, null, null) + /// + /// constructs a Game with no history of turns. + /// + /// the name of the game 😎 + /// the game's player manager, doing CRUD on players and switching whose turn it is + /// the group of dice used for this game + public Game(string name, PlayerManager playerManager, FavGroup favGroup) + : this(name, playerManager, favGroup, null) { } - public void PerformTurn() + /// + /// performs a Turn, marks this Game as "started", and logs that Turn + /// + /// the player whose turn it is + public void PerformTurn(Player player) { - Player player = playerManager.WhoPlaysNow(IsFirstTurn); 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); + } + /// + /// finds whose turn it is + /// + /// the Player whose turn it is + public Player GetWhoPlaysNow() + { + return playerManager.WhoPlaysNow(IsFirstTurn); + } - // throw favGroup's dice and record it in a "faces" var + /// + /// throws all the Dice in FavGroup and returns a list of their Faces + /// + /// list of AbstractDieFaces after a throw + private List ThrowAll() + { + List 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" - playerManager.PrepareNextPlayer(player); - turns.Add(turn); + /// + /// asks the PlayerManager to prepare the next Player + /// + /// the Player whose turn it was + public void PrepareNextPlayer(Player currentPlayer) + { + playerManager.PrepareNextPlayer(currentPlayer); + } + + public Player AddPlayerToGame(Player player) + { + return playerManager.Add(player); + } + + public IEnumerable 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); + } + + /// + /// represents a Game in string format + /// + /// a Game in string format public override string ToString() { StringBuilder sb = new(); diff --git a/Sources/Model/Players/PlayerManager.cs b/Sources/Model/Players/PlayerManager.cs index 273d6de..fbdda57 100644 --- a/Sources/Model/Players/PlayerManager.cs +++ b/Sources/Model/Players/PlayerManager.cs @@ -79,8 +79,6 @@ namespace Model.Players /// /// finds and returns the player whose turn it is - ///
- /// SHOULD HAVE NO SIDE EFFECT ///
/// /// @@ -93,8 +91,7 @@ namespace Model.Players throw new Exception("you are exploring an empty collection\nthis should not have happened"); } - Player result = null; - + Player result; if (isFirstTurn) { result = players[0]; @@ -103,7 +100,6 @@ namespace Model.Players { result = players[NextIndex]; } - return result; }