public bool CheckMoveForCheck(Move move) { //var newBoard = Copy(); var from = GetPiece(move.From.X, move.From.Y); var fromMoved = from != null ? from.HasMoved : false; var from2 = default(IPiece); var from2Moved = default(bool); if (move.From2 != null) { from2 = GetPiece(move.From2.X, move.From2.Y); from2Moved = from2 != null ? from2.HasMoved : false; } var to = GetPiece(move.To.X, move.To.Y); var toMoved = to != null ? to.HasMoved : false; var to2 = default(IPiece); var to2Moved = default(bool); if(move.To2 != null) { to2 = GetPiece(move.To2.X, move.To2.Y); to2Moved = to2 != null ? to2.HasMoved : false; } var currentPlayer = PlayerTurn; MovePiece(move); var result = PlayerHasCheck(currentPlayer); PlayerTurn = currentPlayer; if(from != null) from.HasMoved = fromMoved; if(from2 != null)from2.HasMoved = from2Moved; if(to != null)to.HasMoved = toMoved; if(to2 != null)to2.HasMoved = to2Moved; SetPiece(move.From.X, move.From.Y, from); if(move.From2 != null) SetPiece(move.From2.X, move.From2.Y, from2); SetPiece(move.To.X, move.To.Y, to); if(move.To2 != null) SetPiece(move.To2.X, move.To2.Y, to2); return result; }
public TreeNode(Move move, Double score) { Score = score; Move = move; }
protected MoveResult TryAttackAlongSlope(Board board, Move slope) { var curX = slope.From.X; var curY = slope.From.Y; var result = default(MoveResult); do { curX += slope.To.X; curY += slope.To.Y; result = TryAttackSquare(board, new Move(slope.From, new Coordinate(curX, curY))); } while (result.Type == MoveResultType.Move); return result; }
protected MoveResult TryMoveToSquare(Board board, List<Move> possibleMoves, Move move, MoveType moveType) { if (move.To.IsValid) { var targetPiece = board.GetPiece(move.To.X, move.To.Y); if (moveType != MoveType.MustAttack && targetPiece == null) { if(!board.CheckMoveForCheck(move)) { possibleMoves.Add(move); } return new MoveResult() { Type = MoveResultType.Move }; } else if(moveType != MoveType.CannotAttack && targetPiece != null && targetPiece.Player != Player) { if(!board.CheckMoveForCheck(move)) { possibleMoves.Add(move); } return new MoveResult() { Type = MoveResultType.Attack, CapturedPiece = targetPiece }; } } return new MoveResult() { Type = MoveResultType.Invalid }; }
protected void TryMoveAlongSlope(Board board, List<Move> possibleMoves, Move slope) { var curX = slope.From.X; var curY = slope.From.Y; do { curX += slope.To.X; curY += slope.To.Y; } while (TryMoveToSquare(board, possibleMoves, new Move(slope.From, new Coordinate(curX, curY)), MoveType.CanAttack).Type == MoveResultType.Move); }
protected MoveResult TryAttackSquare(Board board, Move move) { if (move.To.IsValid) { var targetPiece = board.GetPiece(move.To.X, move.To.Y); if(targetPiece != null) { if(targetPiece.Player != Player) { return new MoveResult() { Type = MoveResultType.Attack, CapturedPiece = targetPiece }; } } else { return new MoveResult() { Type = MoveResultType.Move }; } } return new MoveResult() { Type = MoveResultType.Invalid }; }
private void MovePiece(Move mv) { var movedpiece = GetPiece(mv.From.X, mv.From.Y); var capturedPiece = GetPiece(mv.To.X, mv.To.Y); if(capturedPiece != null) { if(capturedPiece.PieceType == PieceType.King) { throw new System.Exception("Cannot capture the king."); } if(capturedPiece.Player == Player.Dark) _darkPieces.Remove(capturedPiece); else _lightPieces.Remove(capturedPiece); } if(movedpiece != null) { movedpiece.HasMoved = true; } SetPiece(mv.To.X, mv.To.Y, movedpiece); SetPiece(mv.From.X, mv.From.Y, null); PlayerTurn = OpposingPlayer(); }
public void MakeMove(Move mv) { MovePiece(mv); }