示例#1
0
        internal void Select(ChessBoard board, SquareCoordinates coord)
        {
            ChessPiece selectedPiece = board.GetPiece(coord);
            if (moves == null)
            {
                if (selectedPiece != null && selectedPiece.White == white)
                {
                    firstSelection = coord;
                    moves = GameLogic.MoveSet(board, coord, this);
                    board.HighlightSquare(coord, Color.Yellow);
                    FirstSquareSelected(board, coord, moves);
                }
            }
            else
            {
                if (coord == firstSelection)
                {
                    MoveSet prevMoves = moves;
                    ResetSelected();
                    board.UnhighlightSquare(coord);
                    Unselected(board, coord, prevMoves);
                }
                else
                {
                    Move m = GetMove(moves, coord);
                    if (m != null)
                    {
                        ChessPiece movedPiece, takenPiece = null;
                        MoveSet prevMoves = moves;
                        ResetSelected();
                        board.UnhighlightSquare(firstSelection);
                        if (m.Overtaken)
                            takenPiece = board.ClearSquare(m.TakenPiece);

                        movedPiece = board.ClearSquare(firstSelection);
                        movedPiece.LastSquare = firstSelection;
                        movedPiece.LastMove = board.CurrentMove;
                        board.DrawPiece(coord, movedPiece);
                        material.MovePiece(movedPiece.Type, firstSelection, coord);

                        MadeMove(board, movedPiece, firstSelection, coord, prevMoves);
                        board.UpdateLastMove(firstSelection, m, movedPiece, takenPiece);
                        board.NextTurn();
                    }
                    else if (moves.CheckableMoves.Contains(coord) && !board.ShowCheckedMoves)
                    {
                        MessageBox.Show(board, "You may not place the selected piece there\nsince it will either place your king in check\nor will leave your king in check.", "Illegal Move", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
            }
        }
示例#2
0
        internal void Castle(ChessBoard board, bool kingSide)
        {
            MoveSet prevMoves = moves;
            if (moves != null)
            {
                ResetSelected();
                board.UnhighlightSquare(firstSelection);
            }

            SquareCoordinates kingStart, kingEnd, rookStart, rookEnd;
            ChessPiece piece;

            int rank = white ? 0 : 7;
            kingStart = new SquareCoordinates(4, rank);
            if (kingSide)
            {
                kingEnd = new SquareCoordinates(6, rank);
                rookStart = new SquareCoordinates(7, rank);
                rookEnd = new SquareCoordinates(5, rank);
            }
            else
            {
                kingEnd = new SquareCoordinates(2, rank);
                rookStart = new SquareCoordinates(0, rank);
                rookEnd = new SquareCoordinates(3, rank);
            }
            //rookEnd = new SquareCoordinates((kingStart.File + kingEnd.File) / 2, rank);

            piece = board.ClearSquare(kingStart);
            piece.LastSquare = kingStart;
            piece.LastMove = board.CurrentMove;
            board.DrawPiece(kingEnd, piece);
            material.MovePiece(PieceType.KING, kingStart, kingEnd);

            piece = board.ClearSquare(rookStart);
            piece.LastSquare = rookStart;
            piece.LastMove = board.CurrentMove;
            board.DrawPiece(rookEnd, piece);
            material.MovePiece(PieceType.ROOK, rookStart, rookEnd);

            PerformedCastle(board, kingSide, prevMoves);
            board.UpdateLastMoveFromCastle(kingSide);
            board.NextTurn();
        }
示例#3
0
 protected override void PerformedCastle(ChessBoard board, bool kingSide, MoveSet moves)
 {
     if (moves != null)
     {
         foreach (Move legalMove in moves.LegalMoves)
             board.UnhighlightSquare(legalMove.Coordinates);
         foreach (SquareCoordinates checkedMoves in moves.CheckableMoves)
             board.UnhighlightSquare(checkedMoves);
         foreach (SquareCoordinates checkedMoves in moves.BlockedSquareMoves)
             board.UnhighlightSquare(checkedMoves);
     }
     if (remoteOpponent != null)
         remoteOpponent.SendMessage(ChessPacketWriter.WriteCastle(kingSide));
 }
示例#4
0
 protected override void Unselected(ChessBoard board, SquareCoordinates coord, MoveSet moves)
 {
     foreach (Move legalMove in moves.LegalMoves)
         board.UnhighlightSquare(legalMove.Coordinates);
     foreach (SquareCoordinates checkedMoves in moves.CheckableMoves)
         board.UnhighlightSquare(checkedMoves);
     foreach (SquareCoordinates checkedMoves in moves.BlockedSquareMoves)
         board.UnhighlightSquare(checkedMoves);
     if (remoteOpponent != null)
         remoteOpponent.SendMessage(ChessPacketWriter.WriteSelectSquare(coord));
 }
示例#5
0
 protected override void MadeMove(ChessBoard board, ChessPiece movedPiece, SquareCoordinates from, SquareCoordinates to, MoveSet moves)
 {
     foreach (Move legalMove in moves.LegalMoves)
         board.UnhighlightSquare(legalMove.Coordinates);
     foreach (SquareCoordinates checkedMoves in moves.CheckableMoves)
         board.UnhighlightSquare(checkedMoves);
     foreach (SquareCoordinates checkedMoves in moves.BlockedSquareMoves)
         board.UnhighlightSquare(checkedMoves);
     PieceType promoted = PieceType.UNDEFINED;
     if (movedPiece.IsA(PieceType.PAWN) && (White && to.Rank == 7 || !White && to.Rank == 0))
         PromotePiece(board, to, promoted = new PromotionSelect().Prompt(board));
     if (remoteOpponent != null)
     {
         remoteOpponent.SendMessage(ChessPacketWriter.WriteSelectSquare(to));
         if (promoted != PieceType.UNDEFINED)
             remoteOpponent.SendMessage(ChessPacketWriter.WritePromotion(to, promoted));
     }
 }