diff --git a/DomainLayer/Monopoly.DomainLayer.Domain/Chess.cs b/DomainLayer/Monopoly.DomainLayer.Domain/Chess.cs index 18190a2..afe0d77 100644 --- a/DomainLayer/Monopoly.DomainLayer.Domain/Chess.cs +++ b/DomainLayer/Monopoly.DomainLayer.Domain/Chess.cs @@ -12,9 +12,9 @@ public class Chess private int remainingSteps; public Chess(Player player, - string currentBlockId, - Direction currentDirection, - int remainingSteps) + string currentBlockId, + Direction currentDirection, + int remainingSteps) { this.player = player; this.currentBlockId = currentBlockId; @@ -37,7 +37,8 @@ private IEnumerable Move(Map map) { while (remainingSteps > 0) { - var nextBlock = map.FindBlockById(currentBlockId).GetDirectionBlock(CurrentDirection) ?? throw new Exception("找不到下一個區塊"); + var nextBlock = map.FindBlockById(currentBlockId).GetDirectionBlock(CurrentDirection) ?? + throw new Exception("找不到下一個區塊"); currentBlockId = nextBlock.Id; remainingSteps--; if (currentBlockId == "Start" && remainingSteps > 0) // 如果移動到起點,且還有剩餘步數,則獲得獎勵金 @@ -45,9 +46,13 @@ private IEnumerable Move(Map map) player.Money += 3000; yield return new ThroughStartEvent(player.Id, 3000, player.Money); } + var directions = DirectionOptions(map); + if (directions.Count > 1) { + yield return new ChessMovedEvent(player.Id, currentBlockId, currentDirection.ToString(), + remainingSteps); // 可選方向多於一個 // 代表棋子會停在這個區塊 yield return new PlayerNeedToChooseDirectionEvent( @@ -55,11 +60,14 @@ private IEnumerable Move(Map map) directions.Select(d => d.ToString()).ToArray()); yield break; } + // 只剩一個方向 // 代表棋子會繼續往這個方向移動 currentDirection = directions.First(); - yield return new ChessMovedEvent(player.Id, currentBlockId, currentDirection.ToString(), remainingSteps); + yield return new ChessMovedEvent(player.Id, currentBlockId, currentDirection.ToString(), + remainingSteps); } + map.FindBlockById(currentBlockId).DoBlockAction(player); var onBlockEvent = map.FindBlockById(currentBlockId).OnBlockEvent(player); if (onBlockEvent is not null) @@ -76,7 +84,7 @@ public IEnumerable Move(Map map, int steps) internal IEnumerable ChangeDirection(Map map, Direction direction) { - List events = new() { }; + List events = []; currentDirection = direction; events.Add(new PlayerChooseDirectionEvent(player.Id, direction.ToString())); events.AddRange(Move(map)); diff --git a/DomainLayer/Monopoly.DomainLayer.Domain/Events/PlayerNeedToChooseDirectionEvent.cs b/DomainLayer/Monopoly.DomainLayer.Domain/Events/PlayerNeedToChooseDirectionEvent.cs index 7280b6a..a43baa5 100644 --- a/DomainLayer/Monopoly.DomainLayer.Domain/Events/PlayerNeedToChooseDirectionEvent.cs +++ b/DomainLayer/Monopoly.DomainLayer.Domain/Events/PlayerNeedToChooseDirectionEvent.cs @@ -2,4 +2,22 @@ namespace Monopoly.DomainLayer.Domain.Events; -public record PlayerNeedToChooseDirectionEvent(string PlayerId, params string[] Directions) : DomainEvent; \ No newline at end of file +public record PlayerNeedToChooseDirectionEvent(string PlayerId, string[] Directions) : DomainEvent +{ + public override string ToString() + { + return $"{nameof(PlayerNeedToChooseDirectionEvent)} {{ PlayerId = {PlayerId}, Directions = [{string.Join(", ", Directions)}] }}"; + } + + public virtual bool Equals(PlayerNeedToChooseDirectionEvent? other) + { + if (ReferenceEquals(null, other)) return false; + if (ReferenceEquals(this, other)) return true; + return PlayerId == other.PlayerId && Directions.Order().SequenceEqual(other.Directions.Order()); + } + + public override int GetHashCode() + { + return HashCode.Combine(base.GetHashCode(), PlayerId, Directions); + } +} \ No newline at end of file diff --git a/tests/Monopoly.DomainLayer.Domain.Tests/Testcases/RollDiceTest.cs b/tests/Monopoly.DomainLayer.Domain.Tests/Testcases/RollDiceTest.cs index d74012a..58de90b 100644 --- a/tests/Monopoly.DomainLayer.Domain.Tests/Testcases/RollDiceTest.cs +++ b/tests/Monopoly.DomainLayer.Domain.Tests/Testcases/RollDiceTest.cs @@ -114,8 +114,8 @@ public void 玩家擲骰後移動棋子到需要選擇方向的地方() monopoly.DomainEvents .NextShouldBe(new PlayerRolledDiceEvent(player.Id, dicePoints)) - //.NextShouldBe(new ChessMovedEvent(player.Id, "ParkingLot", "Down", 1)) - .NextShouldBe(new PlayerNeedToChooseDirectionEvent(player.Id, "Left", "Right", "Down")) + .NextShouldBe(new ChessMovedEvent(player.Id, "ParkingLot", "Down", 1)) + .NextShouldBe(new PlayerNeedToChooseDirectionEvent(player.Id, ["Left", "Right", "Down"])) .NoMore(); } diff --git a/tests/Monopoly.DomainLayer.Domain.Tests/Testcases/SelectDirectionTest.cs b/tests/Monopoly.DomainLayer.Domain.Tests/Testcases/SelectDirectionTest.cs index d8ff413..9d161df 100644 --- a/tests/Monopoly.DomainLayer.Domain.Tests/Testcases/SelectDirectionTest.cs +++ b/tests/Monopoly.DomainLayer.Domain.Tests/Testcases/SelectDirectionTest.cs @@ -138,8 +138,8 @@ public void 玩家選擇方向後會繼續前進到需要選擇方向的地方() .NextShouldBe(new ChessMovedEvent(A.Id, "B6", "Left", 3)) .NextShouldBe(new ChessMovedEvent(A.Id, "B5", "Down", 2)) .NextShouldBe(new ChessMovedEvent(A.Id, "B4", "Down", 1)) - //.NextShouldBe(new ChessMovedEvent(A.Id, "Jail", "Down", 0)) - .NextShouldBe(new PlayerNeedToChooseDirectionEvent(A.Id, "Left", "Right", "Down")) + .NextShouldBe(new ChessMovedEvent(A.Id, "Jail", "Down", 0)) + .NextShouldBe(new PlayerNeedToChooseDirectionEvent(A.Id, ["Left", "Right", "Down"])) .NoMore(); } diff --git a/tests/Monopoly.InterfaceAdapterLayer.Server.Tests/AcceptanceTests/RollDiceTest.cs b/tests/Monopoly.InterfaceAdapterLayer.Server.Tests/AcceptanceTests/RollDiceTest.cs index 38ca662..bcbf029 100644 --- a/tests/Monopoly.InterfaceAdapterLayer.Server.Tests/AcceptanceTests/RollDiceTest.cs +++ b/tests/Monopoly.InterfaceAdapterLayer.Server.Tests/AcceptanceTests/RollDiceTest.cs @@ -128,7 +128,7 @@ public async Task 玩家擲骰後移動棋子到需要選擇方向的地方() VerifyChessMovedEvent(hub, "A", "A2", "Right", 4); VerifyChessMovedEvent(hub, "A", "A3", "Down", 3); VerifyChessMovedEvent(hub, "A", "A4", "Down", 2); - //VerifyChessMovedEvent(hub, "A", "ParkingLot", "Down", 1); + VerifyChessMovedEvent(hub, "A", "ParkingLot", "Down", 1); hub.FluentAssert.PlayerNeedToChooseDirectionEvent(new PlayerNeedToChooseDirectionEventArgs { PlayerId = a.Id,