public void EndCastle( Point2D location ) { m_HasMoved = true; m_Move = new Move( this, location ); Point2D worldLocation = m_BChessboard.BoardToWorld( location ); m_Piece.GoTo( worldLocation ); }
/// <summary> /// This function is called when a move is completed, and the next step in game should be performed /// </summary> public void OnMoveOver( Move move, string whiteMsg, string blackMsg ) { m_Timer.OnMoveMade(); if ( move != null && move.Piece is Pawn && ( move.Piece as Pawn ).ShouldBePromoted ) { // A pawn should be promoted m_PromotedPawn = move.Piece as Pawn; if ( m_Status == GameStatus.BlackMoving ) { // Black m_Status = GameStatus.BlackPromotion; SendAllGumps( "Waiting for black to promote their pawn", "Please promote your pawn" ); m_Black.SendGump( new PawnPromotionGump( m_Black, this ) ); } else { // White m_Status = GameStatus.WhitePromotion; SendAllGumps( "Please promote your pawn", "Waiting for white to promote their pawn" ); m_White.SendGump( new PawnPromotionGump( m_White, this ) ); } return; } if ( m_Status == GameStatus.BlackMoving || m_Status == GameStatus.BlackPromotion ) { m_BlackTime += ( DateTime.Now.Subtract( m_MoveTime ) ); m_Status = GameStatus.WhiteToMove; } else if ( m_Status == GameStatus.WhiteMoving || m_Status == GameStatus.WhitePromotion ) { m_WhiteTime += ( DateTime.Now.Subtract( m_MoveTime ) ); m_Status = GameStatus.BlackToMove; } m_MoveTime = DateTime.Now; SendAllGumps( null, null ); }
/// <summary> /// Undos a move /// </summary> /// <param name="move">The move being reverted</param> private void UndoMove( Move move ) { m_Table.Remove( move.To ); m_Table.Add( move.From, move.Piece ); move.Piece.Position = move.From; if ( move.Capture ) { m_Table.Add( move.To, move.CapturedPiece ); move.CapturedPiece.Position = move.To; } }
/// <summary> /// Finalizes the move adding the captured piece to the captured pieces list /// </summary> /// <param name="move">The move performed</param> private void FinalizeMove( Move move ) { if ( ! move.Capture ) return; m_CapturedPieces.Add( move.CapturedPiece ); }
/// <summary> /// Applies a move to the BChessboard logic /// </summary> /// <param name="move">The move object</param> public void ApplyMove( Move move ) { if ( move.Capture ) { m_Table.Remove( move.CapturedPiece.Position ); } m_Table.Remove( move.From ); m_Table[ move.To ] = move.Piece; // This will automatically remove any piece stored before move.Piece.Position = move.To; }
/// <summary> /// Pushes forward the game, by allowing the next move to be executed and supplying information about the game status /// </summary> /// <param name="nextMoveColor">The color of the player making the next move</param> /// <param name="move">The last move made</param> private void PushGame( ChessColor nextMoveColor, Move move ) { // Move is over. Verify the following: // // 1. Opponent is checked // 2. Opponent is checkmated // 3. Opponent is stalled Status status = GetStatus( nextMoveColor ); switch ( status ) { case Status.Check: King k = GetKing( nextMoveColor ) as King; k.PlayCheck(); if ( nextMoveColor == ChessColor.White ) m_Game.OnMoveOver( move, "Your king is under check!", "You have the opponent's king under check!" ); else m_Game.OnMoveOver( move, "You have the opponent's kind under check!", "Your king is under check" ); break; case Status.CheckMate: King king = GetKing( nextMoveColor ) as King; king.PlayCheckMate(); m_Game.EndGame( nextMoveColor == ChessColor.White ? m_Black : m_White ); break; case Status.Stall: King wKing = GetKing( ChessColor.White ) as King; King bKing = GetKing( ChessColor.Black ) as King; wKing.PlayStaleMate(); bKing.PlayStaleMate(); m_Game.EndGame( null ); break; case Status.Normal: m_Game.OnMoveOver( move, null, null ); break; } }
/// <summary> /// A chess piece will call this function to notify the board that its movement is complete /// </summary> /// <param name="move">The move perfromed</param> public void OnMoveOver( Move move ) { ApplyMove( move ); FinalizeMove( move ); m_IsMoving = false; if ( m_DoRebuild ) Rebuild(); PushGame( move.EnemyColor, move ); }
/// <summary> /// Tries to perfrom a move. Will send any diagnostic messages to user if failed. /// Will perform the move if it's valid. /// </summary> /// <param name="err">This string will hold any error messages if the move fails</param> /// <param name="piece">The piece being moved</param> /// <param name="to">The world location of the move target</param> /// <returns>True if the move is legal</returns> public bool TryMove( ref string err, BaseChessPiece piece, Point2D to ) { Point2D p2 = WorldToBoard( to ); if ( piece == null ) { err = "You must select a piece for your move"; return false; } if ( ! piece.CanMoveTo( p2, ref err ) ) { return false; // Invalid move } if ( piece.IsCastle( p2 ) ) { // This move is making a castle. All needed verification is made by the AllowCastle() function. Rook rook = piece as Rook; if ( rook == null ) rook = this[ p2 ] as Rook; m_IsMoving = true; rook.Castle(); return true; } Move move = new Move( piece, p2 ); ApplyMove( move ); // bool ok = !IsCheck( piece.Color ) || IsCheckMate( piece.EnemyColor ); bool ok = !IsCheck( piece.Color ); UndoMove( move ); if ( ok ) { m_IsMoving = true; piece.MoveTo( move ); foreach( BaseChessPiece pass in m_Table.Values ) { if ( pass != piece ) pass.AllowEnPassantCapture = false; // Reset en passant allowance } } else { err = "That move would put your king under check"; } return ok; }
/// <summary> /// This function is called by the NPC when its move is over /// </summary> public virtual void OnMoveOver() { m_BChessboard.OnMoveOver( m_Move ); if ( m_Move.Capture ) { m_Piece.FixedParticles( m_BChessboard.CaptureEffect, 1, 15, 5012, SecondaryHue, 2, EffectLayer.Waist ); m_Move.CapturedPiece.Die( true ); } m_Move = null; m_Piece.Direction = Facing; }
/// <summary> /// Moves the chess piece to the specified position. This function assumes that a previous call /// to CanMoveTo() has been made and the move has been authorized. /// </summary> /// <param name="move">The move performed</param> public virtual void MoveTo( Move move ) { m_HasMoved = true; m_Move = move; Point2D worldLocation = m_BChessboard.BoardToWorld( move.To ); if ( move.Capture ) { m_BChessboard.PlaySound( m_Piece, m_CaptureSound ); // It's a capture, do an effect m_Piece.MovingParticles( move.CapturedPiece.m_Piece, m_BChessboard.AttackEffect, 5, 0, false, true, Hue, 2, 0, 1, 4006, EffectLayer.Waist, 0 ); } else { m_BChessboard.PlaySound( m_Piece, m_MoveSound ); } m_Piece.GoTo( worldLocation ); }
public override void MoveTo(Move move) { // Set En Passant flags int dy = Math.Abs( move.To.Y - m_Position.Y ); if ( ! m_HasMoved && dy == 2 ) { m_EnPassantRisk = true; } base.MoveTo (move); }
public void Castle() { m_Castle = true; int dx = 0; if ( m_Position.X == 0 ) dx = 3; else if ( m_Position.X == 7 ) dx = -2; Move move = new Move( this, new Point2D( m_Position.X + dx, m_Position.Y ) ); MoveTo( move ); }