Exemplo n.º 1
0
        private void DrawSquare(System.Windows.Forms.PaintEventArgs e, Graphics surface,
                                BoardLocation boardLocation, int x, int y)
        {
            //draw bounding black square
            surface.DrawRectangle(PenBorder, x, y, 64, 64);

            //draw light or dark square background
            surface.FillRectangle(
                boardLocation.Color() == BoardLocationColor.Light ? LightSquareBrush: DarkSquareBrush,
                x + 1,
                y + 1,
                63, 63);

            //draw square as a valid move square
            if (MovingPiece != ChessPiece.None)
            {
                if (IsValidMove(boardLocation))
                {
                    surface.FillRectangle(LegalMoveBrush, x + 1, y + 1, 63, 63);
                }
            }

            //Draw the legend for squares (a-h and 1-8)
            if (boardLocation.Column() == 0)
            {
                surface.DrawString(Convert.ToString((boardLocation.Row() + 1)), Font,
                                   BlackBoardTextbrush, x + 3, y + 25);
            }
            if (boardLocation.Row() == 0)
            {//ascii A=65
                surface.DrawString(Convert.ToChar(65 + boardLocation.Column()).ToString(), Font,
                                   BlackBoardTextbrush, x + 27, y + 48);
            }

            //dont draw piece if its being moved...
            if ((MovingPiece != ChessPiece.None) &&
                (boardLocation == MovingPieceBoardLocation))
            {
                return;
            }

            //draw the icon on the board..
            if (Game.Board[boardLocation] != ChessPiece.None)
            {
                surface.DrawImage(
                    imageList32.Images[ToImageIndex(boardLocation)],
                    x + 16, y + 16, 32, 32);
            }
        }
Exemplo n.º 2
0
        public static BoardLocation?South(this BoardLocation currentLocation)
        {
            var val = currentLocation.ToByte() - 1;

            //if we hop columns we are out of bounds
            if (val / 8 != currentLocation.Column())
            {
                return(null);
            }

            return((val >= 0) ? (BoardLocation)val : default(BoardLocation?));
        }
Exemplo n.º 3
0
        public static BoardLocation?North(this BoardLocation currentLocation)
        {
            var val = currentLocation.ToByte() + 1;

            //if we hop columns we are out of bounds
            if (val / 8 != currentLocation.Column())
            {
                return(null);
            }

            return((val < Constants.BoardSize) ? (BoardLocation)val : default(BoardLocation?));
        }
Exemplo n.º 4
0
        public static BoardLocation?SouthEast(this BoardLocation currentLocation)
        {
            var val = currentLocation.ToByte() + 7;

            //if we are not the next column this is invalid
            if ((val / 8) == currentLocation.Column())
            {
                return(null);
            }

            return((val < Constants.BoardSize) ? (BoardLocation)val : default(BoardLocation?));
        }
Exemplo n.º 5
0
        public static BoardLocation?SouthWest(this BoardLocation currentLocation)
        {
            var val = currentLocation.ToByte() - 9;

            //if we are not the next column this is invalid
            if ((val / 8) + 1 != currentLocation.Column())
            {
                return(null);
            }

            return((val >= 0) ? (BoardLocation)val : default(BoardLocation?));
        }
Exemplo n.º 6
0
        public static BoardLocation?NorthWest(this BoardLocation currentLocation)
        {
            var val = currentLocation.ToByte() - 7;

            //if we DONT hop columns we are out of bounds
            if (val / 8 == currentLocation.Column())
            {
                return(null);
            }

            return((val >= 0) ? (BoardLocation)val : default(BoardLocation?));
        }
Exemplo n.º 7
0
        private bool LastMoveAllowsEnPassantFor(BoardLocation pawnLocation, Direction pawnCaptureDirection)
        {
            var lastMove = _moveHistory.LastOrDefault();

            if (lastMove == null)
            {
                return(false);
            }

            var pawnLocationColumn     = pawnLocation.Column();
            int lastMoveExpectedColumn = 0;

            switch (pawnCaptureDirection)
            {
            case Direction.NorthEast:
            case Direction.SouthEast:
                lastMoveExpectedColumn = pawnLocationColumn + 1;
                break;

            case Direction.NorthWest:
            case Direction.SouthWest:
                lastMoveExpectedColumn = pawnLocationColumn - 1;
                break;
            }

            //If (and ONLY if):
            //-: the last move was a pawn move AND
            //-: the last move was two spaces AND
            //-: we can thus infer it belongs to the opposite player AND
            //-: the last move was right next to the given pawn
            //-: the last move was in the right column (based on capture direction)
            //(Jesus christ almighty this was not easy)
            if ((_board.ContainsPawn(lastMove.Destination)) &&
                (Math.Abs(lastMove.Destination.ToByte() - lastMove.Source.ToByte()) == 2) &&
                (lastMove.Destination.Row() == pawnLocation.Row()) &&
                (lastMove.Destination.Column() == lastMoveExpectedColumn))
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
Exemplo n.º 8
0
 public static bool IsOnSameColumnAs(this BoardLocation source, BoardLocation target)
 {
     return(source.Column() == target.Column());
 }
Exemplo n.º 9
0
 public static BoardLocationColor Color(this BoardLocation boardLocation)
 {
     return(((boardLocation.ToByte() + boardLocation.Column() % 2) % 2 == 0) ? BoardLocationColor.Dark : BoardLocationColor.Light);
 }