Skip to content

Commit

Permalink
WIP: StartGameTest and MoveChessTest
Browse files Browse the repository at this point in the history
  • Loading branch information
aa89227 committed Apr 23, 2024
1 parent 773cd03 commit f97b118
Show file tree
Hide file tree
Showing 5 changed files with 426 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Text;

namespace ChineseCheckers.ApiService;
namespace ChineseCheckers.Domain;

// Q1: 棋子要怎麼表示?
// 已知 棋子 有 6 種顏色,分別是 紅、橙、黃、綠、藍、紫
Expand Down Expand Up @@ -38,7 +38,7 @@ public class Board
(9,4),(10,4),(11,4),(12,4),
(10,5),(11,5),(12,5),
(11,6),(12,6),
(12,7),
(12,7),
],
topLeft = [
// x-y>=-4, y>=4, x<4
Expand Down Expand Up @@ -83,17 +83,20 @@ public class Board
]
;

public Board(int players)
public Board()
{
// 初始化棋盤
_board = new int[17, 17];

Initialize(players);
// 初始化棋盤
FillAll(PieceColor.Block);
}

public PieceColor this[int x, int y] => (PieceColor)_board[x, y];

public override string ToString()
{
StringBuilder sb = new StringBuilder();
var sb = new StringBuilder();

for (int x = 0; x < 17; x++)
{
Expand All @@ -113,11 +116,8 @@ private string GetPieceSymbol(PieceColor pieceColor)
return "*";
}

private void Initialize(int players)
public void Initialize(int players)
{
// 初始化棋盤
FillAll(PieceColor.Block);

// 中心區域
FillArea(center, PieceColor.None);

Expand All @@ -131,14 +131,19 @@ private void Initialize(int players)
FillArea(bottomRight, PieceColor.None);
FillArea(top, PieceColor.None);
FillArea(bottom, PieceColor.None);
FillArea([(4, 4), (4, 5), (4, 6), (4, 7), (4, 8)], PieceColor.Red);
FillArea([(8, 4), (9, 5), (10, 6), (11, 7), (12, 8)], PieceColor.Orange);
break;
case 3:
FillArea(topLeft, PieceColor.Red);
FillArea(topRight, PieceColor.Orange);
FillArea(bottomLeft, PieceColor.None);
FillArea(bottomRight, PieceColor.None);
FillArea(top, PieceColor.None);
FillArea(bottom, PieceColor.Purple);
FillArea(topLeft, PieceColor.None);
FillArea(topRight, PieceColor.None);
FillArea(bottomLeft, PieceColor.Yellow);
FillArea(bottomRight, PieceColor.Green);
FillArea(top, PieceColor.Red);
FillArea(bottom, PieceColor.None);
FillArea([(4, 4), (5, 4), (6, 4), (7, 4), (8, 4)], PieceColor.Red);
FillArea([(4, 8), (5, 9), (6, 10), (7, 11), (8, 12)], PieceColor.Yellow);
FillArea([(12, 8), (12, 9), (12, 10), (12, 11), (12, 12)], PieceColor.Green);
break;
case 4:
FillArea(topLeft, PieceColor.Red);
Expand Down
25 changes: 24 additions & 1 deletion src/ChineseCheckers.Domain/Game.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@

using System.Runtime.Serialization;

namespace ChineseCheckers.Domain;

public class Game
{
public bool IsStarted { get; private set; }
private readonly Board _board = new();
private readonly List<Player> _players = [];

public Board Board => _board;
public Game()
{
}
Expand All @@ -22,7 +27,25 @@ public void Start()

if (IsStarted == false)
{
// TODO: throw domain exception
return;
}

_board.Initialize(_players.Count);
}

public void MoveChess(int sx, int sy, int dx, int dy)
{
if (_board.IsValidMove(sx, sy, dx, dy) == false)
{
throw new InvalidMoveException("Invalid Move");
}

var tmp = _board.GetPieceColor(sx, sy);
_board.SetPieceColor(sx, sy, _board.GetPieceColor(dx, dy));
_board.SetPieceColor(dx, dy, tmp);
}
}

public class InvalidMoveException(string message) : Exception(message)
{
}
1 change: 1 addition & 0 deletions tests/ChineseCheckers.Tests/ChineseCheckers.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.0" />
<PackageReference Include="FluentAssertions" Version="6.12.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="8.0.3" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="MSTest.TestAdapter" Version="3.1.1" />
Expand Down
Loading

0 comments on commit f97b118

Please sign in to comment.