public string BuildSimpleTestBoard(LocatedItem <ChessPieceEntity>[,] board) { var sb = new StringBuilder(); sb.AppendLine(" ABCDEFGH"); for (int rank = 7; rank >= 0; rank--) { sb.Append($"{rank+1} "); for (int file = 0; file < 8; file++) { var boardPiece = board[file, rank]; if (boardPiece == null) { sb.Append("."); } else { var entity = boardPiece.Item; var piece = entity.Piece == ChessPieceName.Knight ? "N" : entity.Piece.ToString().First().ToString(); sb.Append(entity.Player == Colours.White ? piece.ToUpper() : piece.ToLower()); } } sb.Append($" {rank + 1}"); sb.AppendLine(); } sb.AppendLine(" ABCDEFGH"); return(sb.ToString()); }
private static LocatedItem <TestBoardEntity> CreateItem(BoardMove move, TestBoardEntity entity) { LocatedItem <TestBoardEntity> locatedEntity = null; if (entity != null) { locatedEntity = new LocatedItem <TestBoardEntity>(move.From, entity, new Paths()); } return(locatedEntity); }
public void Setup() { _playerStateServiceMock = ChessTestFactory.ChessGameStateServiceMock(); _boardStateMock = new Mock <IBoardState <ChessPieceEntity> >(); _boardStateMock.Setup(mb => mb.Clone()).Returns(_boardStateMock.Object); _kingItem = new LocatedItem <ChessPieceEntity>( BoardLocation.At(1, 1), new PawnEntity(Colours.White), null); _boardStateMock.Setup(mb => mb.GetItems(It.IsAny <int>(), It.IsAny <int>())) .Returns(new List <LocatedItem <ChessPieceEntity> > { _kingItem }.AsEnumerable()); _moveServiceMock = ChessTestFactory.BoardMoveServiceMock(); }
private PlayerState CheckForCheckMate(IBoardState <ChessPieceEntity> boardState, LocatedItem <ChessPieceEntity> king, LocatedItem <ChessPieceEntity> attacker) { var state = PlayerState.Check; var clone = (IBoardState <ChessPieceEntity>)boardState.Clone(); // Find the attacking piece // Not sure why we need to refresh its piece paths here but I get problems with out it RefreshPiecePaths(clone, attacker); // find the path it's attacking on var attackPath = attacker.Paths.FirstOrDefault(p => p.ContainsTo(king.Location)); // Remove the king from the board so as to not have it's location block // attack paths from the new location clone.Remove(king.Location); var kingCannotMove = !king.Paths.Any() || king.Paths.FlattenMoves() .ToList() .All(m => IsLocationUnderCheck(clone, m.To, king.Item.Player).result); // // get all friendly pieces except king // refresh there paths var friendlyPieces = clone.GetItems(king.Item.Owner) .Where(p => p.Item.Piece != ChessPieceName.King) .OrderBy(p => p.Item.EntityType); // check if any friendly paths intersect the attackPath or attack the attacker var canBlock = friendlyPieces.Where(fp => fp.Paths.FlattenMoves() .Any(fm => attackPath .Any(am => am.To.Equals(fm.To) || am.From.Equals(fm.To) ) ) ); if (kingCannotMove && !canBlock.Any()) { return(PlayerState.Checkmate); } return(PlayerState.Check); }
protected void SetupGetNonOwnerEntities(BoardMove move, TestBoardEntity entity) { var from = BoardLocation.At(1, 1); var itemAttackingMoveToLocation = new LocatedItem <TestBoardEntity>( from, entity, new Paths { new Path { new BoardMove(from, move.To, (int)DefaultActions.MoveOrTake) } }); RoBoardStateMock.Setup(b => b.GetItems()) .Returns(new List <LocatedItem <TestBoardEntity> > { itemAttackingMoveToLocation }); }
private void RefreshPiecePaths(IBoardState <ChessPieceEntity> boardState, LocatedItem <ChessPieceEntity> piece) { var validatedPaths = _pathsValidator.GetValidatedPaths(boardState, piece.Item, piece.Location); piece.UpdatePaths(validatedPaths); }