public Piece(Square position, Board board, PieceColor color) { this.position = position; this.board = board; this.color = color; this.board.Squares[position] = this; }
public override bool CanMoveTo(Square square) { var moves = base.PossibleMoves().ToList(); var beatenSquares = this.board.Pieces.Where(x => x.Color != this.Color) .SelectMany(p => p.BeatenSquares()).ToList(); return moves.Except(beatenSquares).Any(x => x.Equals(square)); }
public void Whether_Board_AddsPieces_On_PutPiece() { var board = new Board(); var originalPosition = new Square(5, 4); var newPosition = new Square(4, 3); var bishop = new Bishop(originalPosition, board, PieceColor.White); var knight = new Bishop(newPosition, board, PieceColor.Black); Assert.That(board.Pieces != null); Assert.That(board.Pieces.Count() == 2); }
public void Whether_Board_InvokesOnPieceMoved_On_PieceMoveTo() { var board = new Board(); var pieceMoved = false; board.PieceMoved += (b, m) => pieceMoved = true; var originalPosition = new Square(5, 4); var newPosition = new Square(4, 3); var bishop = new Bishop(originalPosition, board, PieceColor.White); bishop.MoveTo(newPosition); Assert.That(pieceMoved); }
public void Whether_Board_InvokesPieceTaken_On_Caprute() { var board = new Board(); var pieceTaken = false; var originalPosition = new Square(5, 4); var newPosition = new Square(4, 3); var bishop = new Bishop(originalPosition, board, PieceColor.White); var knight = new Bishop(newPosition, board, PieceColor.Black); knight.PieceTaken += (s) => pieceTaken = true; var possibleMoves = bishop.MoveTo(newPosition); Assert.That(board.Squares[originalPosition] == null); Assert.That(board.Squares[newPosition] == bishop); Assert.That(pieceTaken); }
public void Whether_Game_NotifiesWhenPuzzleFailed_On_PieceMove() { var failed = false; var parser = new GameParser(); var result = parser.Parse(fenWhiteMove); var game = result.FirstOrDefault(); game.PuzzleFailed += (b, m) => failed = true; var targetSquare = new Square(7, 7); var rook = game.Board.Pieces.FirstOrDefault(x => x.Type == PieceType.Rook && x.PossibleMoves().Contains(targetSquare)); game.Board.PutPiece(targetSquare, rook); Assert.That(failed, Is.True); }
public void Whether_Board_DoesntInvokePieceMoved_On_MakeMove() { var board = new Board(); var pieceMoved = false; board.PieceMoved += (b, m) => pieceMoved = true; var originalPosition = new Square(5, 4); var newPosition = new Square(4, 3); var bishop = new Bishop(originalPosition, board, PieceColor.White); board.MakeMove(new Move() { Color = PieceColor.White, OriginalSquare = originalPosition, TargetSquare = newPosition, Piece = bishop.Type }); Assert.That(pieceMoved, Is.False); }
public void PutPiece(Square square, Piece piece, bool notifyPieceMoved = true) { if (this.squares[square] != null) { this.squares[square].TakePiece(); } // Put piece in a new position var oldPosition = piece.Position; this.squares[oldPosition] = null; this.squares[square] = piece; piece.Position = square; // Now it's opposite color to move this.IsWhiteMove = !this.IsWhiteMove; if (notifyPieceMoved) { this.FirePieceMoved(oldPosition, piece); } }
public void Whether_Piece_CantMoveWhenIsWhiteMove_On_MoveTo() { var board = new Board(); var newPos = new Square(1, 6); var oldPos = new Square(1, 1); var rook = new Rook(oldPos, board, PieceColor.Black); var hasMoved = rook.MoveTo(newPos); Assert.That(board.IsWhiteMove); Assert.That(rook.Position.Equals(oldPos)); Assert.That(!hasMoved); }
public King(Square square, Board board, PieceColor color) : base(square, board, color) { }
public bool MoveToSquare(Square square) { return this.SetPosition(square.GetPoint(this.piece.Board.Size)); }
public void Whether_Game_OpponentMakesNextMove_On_PieceMoved() { var pieceMoved = false; var parser = new GameParser(); var result = parser.Parse(fenGame); var game = result.FirstOrDefault(); var targetSquare = new Square(8, 3); var rook = game.Board.Pieces.FirstOrDefault(x => x.Type == PieceType.Rook && x.PossibleMoves().Contains(targetSquare) && x.Color == (game.Board.IsWhiteMove ? PieceColor.White : PieceColor.Black)); var whiteQueen = game.Board.Pieces.FirstOrDefault(x => x.Type == PieceType.Queen && x.Color == PieceColor.White); whiteQueen.PositionUpdated += (e, s) => pieceMoved = true; game.Board.PutPiece(targetSquare, rook); Assert.That(pieceMoved, Is.True); }
public void Whether_Game_SendsPuzzleSolvedMessage_On_PuzzleSolved() { var solved = false; var parser = new GameParser(); var result = parser.Parse(fenWhiteMove); var game = result.FirstOrDefault(); var targetSquare = new Square(7, 8); Messenger.Default.Register<PuzzleCompletedMessage>(this, (msg) => solved = msg.IsSolved); var rook = game.Board.Pieces.FirstOrDefault(x => x.Type == PieceType.Rook && x.PossibleMoves().Contains(targetSquare) && x.Color == (game.Board.IsWhiteMove ? PieceColor.White : PieceColor.Black)); game.Board.PutPiece(targetSquare, rook); Assert.That(solved, Is.True); }
private void FirePieceMoved(Square oldPosition, Piece piece) { if (this.PieceMoved != null) { var move = new Move() { Color = piece.Color, OriginalSquare = oldPosition, TargetSquare = piece.Position, Piece = piece.Type }; this.PieceMoved(this, move); } }
public Bishop(Square square, Board board, PieceColor color) : base(square, board, color) { }
public void Whether_Piece_UpdatesPosition_On_MoveTo() { var board = new Board(); var newPos = new Square(1, 6); var rook = new Rook(new Square(1, 1), board, PieceColor.White); rook.MoveTo(newPos); Assert.That(rook.Position.Equals(newPos)); }
public void Whether_Piece_PositionUpdatedRaised_On_PieceMoved() { var board = new Board(); var newPos = new Square(1, 6); var moved = false; var rook = new Rook(new Square(1, 1), board, PieceColor.White); rook.PositionUpdated += (s, p) => moved = true; rook.MoveTo(newPos); Assert.That(moved, Is.True); }
internal void MakeMove(Square originalSquare, Square targetSquare, bool notify) { var pieceToMove = this.squares[originalSquare]; this.PutPiece(targetSquare, pieceToMove, notify); }
internal void MakeMove(Square targetSquare, Piece pieceToMove) { this.PutPiece(targetSquare, pieceToMove, false); }
public virtual bool CanMoveTo(Square square) { return this.PossibleMoves().Any(x => x.Equals(square)); }
public bool IsEmpty(Square square) { return this.Squares.ContainsKey(square) && this.Squares[square] == null; }
public bool MoveTo(Square square) { if (this.CanMoveTo(square) && this.CanColorMove()) { this.board.PutPiece(square, this); return true; } return false; }
protected bool IsEmptySquare(Square sq) { return this.board.Squares.ContainsKey(sq) && this.board.Squares[sq] == null; }
protected bool IsOpponentsPiece(Square sq) { return this.board.Squares.ContainsKey(sq) && (this.board.Squares[sq] != null && this.board.Squares[sq].Color != this.Color); }
private void Touch(CCTouch touch, CCEvent e) { var square = touch.Location.GetSquare(this.piece.Board.Size); if (!square.Equals(this.lastMovePosition)) { drawNode.Clear(); this.lastMovePosition = square; if (this.possibleSquares.Any(x => x.Equals(square))) { var size = this.piece.Board.Size / 8; var color = new CCColor4B(0, 199, 90, 0x66); var point = square.GetPoint(this.piece.Board.Size); drawNode.DrawSolidCircle(point, size, color); drawNode.Visit(); } } this.Position = touch.Location; }
public void Whether_Board_UpdatesIsWhiteMove_On_MoveTo() { var board = new Board(); var newPos = new Square(1, 6); var oldPos = new Square(1, 1); var rook = new Rook(oldPos, board, PieceColor.White); var hasMoved = rook.MoveTo(newPos); Assert.That(rook.Position.Equals(newPos)); Assert.IsFalse(board.IsWhiteMove); Assert.That(hasMoved); }
/// <summary> /// Method to add max two possible takes of pawn (left&right) /// </summary> private void AddTakes(List<Square> moves, int verticalOffset) { var right = new Square(this.position.XPosition + 1, this.position.YPosition + verticalOffset); moves.Add(right); var left = new Square(this.position.XPosition - 1, this.position.YPosition + verticalOffset); moves.Add(left); }
public virtual IEnumerable<Square> BeatenSquares() { foreach (var offset in GetOffsets()) { for (int i = 1; i <= this.MaxRange; i++) { var square = new Square(this.position.XPosition + offset.Item1 * i, this.position.YPosition + offset.Item2 * i); yield return square; if (!this.IsEmptySquare(square) && !(this.board.Squares.ContainsKey(square) && this.board.Squares[square] is King)) { break; } } } }
public Queen(Square square, Board board, PieceColor color) : base(square, board, color) { }
public void Whether_Board_UpdatesPositionOnBoard_On_MoveTo() { var board = new Board(); var originalPosition = new Square(5, 4); var newPosition = new Square(4, 3); var bishop = new Bishop(originalPosition, board, PieceColor.White); var possibleMoves = bishop.MoveTo(newPosition); Assert.That(board.Squares[originalPosition] == null); Assert.That(board.Squares[newPosition] == bishop); }