示例#1
0
        private static void AddPawnMoves(List <ChessMoveData> moveList, ChessBoard board, int x, int y, ChessColor pieceColor)
        {
            var startLocation = new ChessLocation(x, y);
            var canJump       = (pieceColor == ChessColor.Black && y == 1) || (pieceColor == ChessColor.White && y == 6);
            var yForward      = (pieceColor == ChessColor.Black) ? 1 : -1;

            if (TileIsValid(x, y + yForward) && TileIsFree(board, x, y + yForward))
            {
                moveList.Add(NewChessMove(board, startLocation, new ChessLocation(x, y + yForward), pieceColor));
                if (canJump)
                {
                    if (TileIsValid(x, y + yForward * 2) && TileIsFree(board, x, y + yForward * 2))
                    {
                        moveList.Add(NewChessMove(board, startLocation, new ChessLocation(x, y + yForward * 2), pieceColor));
                    }
                }
            }

            if (TileIsValid(x + 1, y + yForward) && TileHasColor(board, x + 1, y + yForward, EnemyColor(pieceColor)))
            {
                moveList.Add(NewChessMove(board, startLocation, new ChessLocation(x + 1, y + yForward), pieceColor));
            }

            if (TileIsValid(x - 1, y + yForward) && TileHasColor(board, x - 1, y + yForward, EnemyColor(pieceColor)))
            {
                moveList.Add(NewChessMove(board, startLocation, new ChessLocation(x - 1, y + yForward), pieceColor));
            }
        }
示例#2
0
        private void OnMouseDown(object sender, MouseEventArgs e)
        {
            if ((!IsLocked) &&
                (e.Button == MouseButtons.Left) &&
                (e.X > AdjustedVerticalBorderWidth) &&
                (e.Y > AdjustedHorizontalBorderHeight) &&
                (e.X < this.Width) &&
                (e.Y < this.Height))
            {
                _mouseX = e.X;
                _mouseY = e.Y;

                _pieceBeingMovedLocation = new ChessLocation((_mouseX - AdjustedVerticalBorderWidth) / AdjustedTileWidth,
                                                             (_mouseY - AdjustedHorizontalBorderHeight) / AdjustedTileHeight);

                _pieceBeingMoved = Board[_pieceBeingMovedLocation];

                if (_pieceBeingMoved != ChessPiece.Empty)
                {
                    // Only say the mouse is down if they clicked on a non-empty board square
                    _isDraggingPiece = true;

                    Board[_pieceBeingMovedLocation] = ChessPiece.Empty;
                    _boardChanged = true;

                    this.Invalidate();
                }
            }
        }
示例#3
0
        public void GetChessLocationTest2()
        {
            var       chessBoard    = new ChessBoard();
            ILocation chessLocation = new ChessLocation("h8");

            Assert.AreEqual(7, chessLocation.x);
            Assert.AreEqual(0, chessLocation.y);
        }
示例#4
0
        private void OnMouseUp(object sender, MouseEventArgs e)
        {
            if ((e.Button == MouseButtons.Left) &&
                (_isDraggingPiece))
            {
                _isDraggingPiece = false;
                _mouseX          = e.X;
                _mouseY          = e.Y;

                if ((_mouseX > AdjustedVerticalBorderWidth) &&
                    (_mouseY > AdjustedHorizontalBorderHeight) &&
                    (_mouseX < this.Width) &&
                    (_mouseY < this.Height))
                {
                    ChessLocation newLoc = new ChessLocation((_mouseX - AdjustedVerticalBorderWidth) / AdjustedTileWidth,
                                                             (_mouseY - AdjustedHorizontalBorderHeight) / AdjustedTileHeight);

                    if (_pieceBeingMovedLocation == newLoc)
                    {
                        // They picked up a piece and put it back down
                        Board[_pieceBeingMovedLocation] = _pieceBeingMoved;
                        _boardChanged = true;
                        this.Invalidate();
                    }
                    else if (PieceMovedByHuman != null)
                    {
                        // Someone has subscribed to the event, so it's their
                        // responsibility to call my ResetBoard methods to update the board
                        // Fire the event, and forget
                        ChessMove humanMove = new ChessMove(_pieceBeingMovedLocation, newLoc);

                        //Set the chess flag from the cmbChessFlag
                        humanMove.Flag = (ChessFlag)cmbchessflags.SelectedItem;
                        cmbchessflags.SelectedIndex = 0; //reset flag to NoFlag

                        PieceMovedByHuman(humanMove);
                    }
                    else
                    {
                        // At this point, they didn't just pick up a piece and put it back down,
                        // _and_ noone is subscribed to the event, so I have to update the
                        // board myself.
                        // This should never really happen 'cause when the game is stopped,
                        // WinGui subs to this event.
                        Board[newLoc] = _pieceBeingMoved;
                        _boardChanged = true;
                        this.Invalidate();
                    }
                }
                else
                {
                    // they drug a piece off the board. Put it back to where it was.
                    Board[_pieceBeingMovedLocation] = _pieceBeingMoved;
                    _boardChanged = true;
                    this.Invalidate();
                }
            }
        }
示例#5
0
        WhatWhere AssignPosition(string pieceType, string location)
        {
            var assignment = new WhatWhere();

            assignment.PieceType = pieceType;
            ILocation chessLocation = new ChessLocation(location);

            assignment.Location = chessLocation;
            return(assignment);
        }
示例#6
0
        public void ReqChessLocation(int placeX, int placeY)
        {
            ChessLocation msgData = new ChessLocation
            {
                x = (uint)placeX,
                y = (uint)placeY
            };

            NetManager.SendMsg(ProtoDefine.ChessLocation, msgData);
        }
示例#7
0
        /// <summary>
        /// gets move for a specfic piece
        /// </summary>
        public static List <ChessMove> getmovesofpiece(StudentAI ai, ChessColor color, ChessBoard board, ChessLocation location)
        {
            List <ChessMove> piecemoves = new List <ChessMove>();
            ChessMove        move       = new ChessMove(location, location);

            switch (board[location])
            {
            case ChessPiece.BlackPawn:
                ChessMove bdiag1 = new ChessMove(location, new ChessLocation((location.X) - 1, (location.Y) + 1));
                ChessMove bdown  = new ChessMove(location, new ChessLocation((location.X), (location.Y) + 1));
                ChessMove bdown2 = new ChessMove(location, new ChessLocation((location.X), (location.Y) + 2));
                ChessMove bdiag2 = new ChessMove(location, new ChessLocation((location.X) + 1, (location.Y) + 1));
                if (ai.IsValidMove(board, bdiag1, color))
                {
                    piecemoves.Add(bdiag1);
                }
                if (ai.IsValidMove(board, bdown, color))
                {
                    piecemoves.Add(bdown);
                }
                if (ai.IsValidMove(board, bdiag2, color))
                {
                    piecemoves.Add(bdiag2);
                }
                if (ai.IsValidMove(board, bdown2, color))
                {
                    piecemoves.Add(bdown2);
                }
                break;

            case ChessPiece.WhitePawn:
                ChessMove wdiag1 = new ChessMove(location, new ChessLocation((location.X) - 1, (location.Y) - 1));
                ChessMove wup    = new ChessMove(location, new ChessLocation((location.X), (location.Y) - 1));
                ChessMove wup2   = new ChessMove(location, new ChessLocation((location.X), (location.Y) - 2));
                ChessMove wdiag2 = new ChessMove(location, new ChessLocation((location.X) + 1, (location.Y) - 1));
                if (ai.IsValidMove(board, wdiag1, color))
                {
                    piecemoves.Add(wdiag1);
                }
                if (ai.IsValidMove(board, wup, color))
                {
                    piecemoves.Add(wup);
                }
                if (ai.IsValidMove(board, wdiag2, color))
                {
                    piecemoves.Add(wdiag2);
                }
                if (ai.IsValidMove(board, wup2, color))
                {
                    piecemoves.Add(wup2);
                }
                break;

            case ChessPiece.BlackKing:
                for (int i = -1; i < 2; i++)
                {
                    for (int j = -1; j < 2; j++)
                    {
                        move = new ChessMove(location, new ChessLocation(location.X + i, location.Y + j));
                        if (ai.IsValidMove(board, move, color))
                        {
                            piecemoves.Add(move);
                        }
                    }
                }
                break;

            case ChessPiece.WhiteKing:
                for (int i = -1; i < 2; i++)
                {
                    for (int j = -1; j < 2; j++)
                    {
                        move = new ChessMove(location, new ChessLocation(location.X + i, location.Y + j));
                        if (ai.IsValidMove(board, move, color))
                        {
                            piecemoves.Add(move);
                        }
                    }
                }
                break;

            case ChessPiece.BlackKnight:
                move = new ChessMove(location, new ChessLocation(location.X + 2, location.Y + 1));
                if (ai.IsValidMove(board, move, color))
                {
                    piecemoves.Add(move);
                }
                move = new ChessMove(location, new ChessLocation(location.X + 2, location.Y + -1));
                if (ai.IsValidMove(board, move, color))
                {
                    piecemoves.Add(move);
                }
                move = new ChessMove(location, new ChessLocation(location.X + 1, location.Y + 2));
                if (ai.IsValidMove(board, move, color))
                {
                    piecemoves.Add(move);
                }
                move = new ChessMove(location, new ChessLocation(location.X + 1, location.Y + -2));
                if (ai.IsValidMove(board, move, color))
                {
                    piecemoves.Add(move);
                }
                move = new ChessMove(location, new ChessLocation(location.X + -2, location.Y + 1));
                if (ai.IsValidMove(board, move, color))
                {
                    piecemoves.Add(move);
                }
                move = new ChessMove(location, new ChessLocation(location.X + -2, location.Y + -1));
                if (ai.IsValidMove(board, move, color))
                {
                    piecemoves.Add(move);
                }
                move = new ChessMove(location, new ChessLocation(location.X + -1, location.Y + 2));
                if (ai.IsValidMove(board, move, color))
                {
                    piecemoves.Add(move);
                }
                move = new ChessMove(location, new ChessLocation(location.X + -1, location.Y + -2));
                if (ai.IsValidMove(board, move, color))
                {
                    piecemoves.Add(move);
                }
                break;

            case ChessPiece.WhiteKnight:
                move = new ChessMove(location, new ChessLocation(location.X + 2, location.Y + 1));
                if (ai.IsValidMove(board, move, color))
                {
                    piecemoves.Add(move);
                }
                move = new ChessMove(location, new ChessLocation(location.X + 2, location.Y + -1));
                if (ai.IsValidMove(board, move, color))
                {
                    piecemoves.Add(move);
                }
                move = new ChessMove(location, new ChessLocation(location.X + 1, location.Y + 2));
                if (ai.IsValidMove(board, move, color))
                {
                    piecemoves.Add(move);
                }
                move = new ChessMove(location, new ChessLocation(location.X + 1, location.Y + -2));
                if (ai.IsValidMove(board, move, color))
                {
                    piecemoves.Add(move);
                }
                move = new ChessMove(location, new ChessLocation(location.X + -2, location.Y + 1));
                if (ai.IsValidMove(board, move, color))
                {
                    piecemoves.Add(move);
                }
                move = new ChessMove(location, new ChessLocation(location.X + -2, location.Y + -1));
                if (ai.IsValidMove(board, move, color))
                {
                    piecemoves.Add(move);
                }
                move = new ChessMove(location, new ChessLocation(location.X + -1, location.Y + 2));
                if (ai.IsValidMove(board, move, color))
                {
                    piecemoves.Add(move);
                }
                move = new ChessMove(location, new ChessLocation(location.X + -1, location.Y + -2));
                if (ai.IsValidMove(board, move, color))
                {
                    piecemoves.Add(move);
                }
                break;

            case ChessPiece.BlackBishop:
            case ChessPiece.WhiteBishop:
                bool flag = true;
                int  x    = 1;
                while (flag)
                {
                    move = new ChessMove(location, new ChessLocation(location.X + x, location.Y + x));
                    if (ai.IsValidMove(board, move, color))
                    {
                        piecemoves.Add(move);
                    }
                    else
                    {
                        flag = false;
                    }
                    x++;
                }
                x    = 1;
                flag = true;
                while (flag)
                {
                    move = new ChessMove(location, new ChessLocation(location.X + x, location.Y - x));
                    if (ai.IsValidMove(board, move, color))
                    {
                        piecemoves.Add(move);
                    }
                    else
                    {
                        flag = false;
                    }
                    x++;
                }
                x    = 1;
                flag = true;
                while (flag)
                {
                    move = new ChessMove(location, new ChessLocation(location.X - x, location.Y - x));
                    if (ai.IsValidMove(board, move, color))
                    {
                        piecemoves.Add(move);
                    }
                    else
                    {
                        flag = false;
                    }
                    x++;
                }
                x    = 1;
                flag = true;
                while (flag)
                {
                    move = new ChessMove(location, new ChessLocation(location.X - x, location.Y + x));
                    if (ai.IsValidMove(board, move, color))
                    {
                        piecemoves.Add(move);
                    }
                    else
                    {
                        flag = false;
                    }
                    x++;
                }
                break;

            case ChessPiece.BlackRook:
            case ChessPiece.WhiteRook:
                flag = true;
                x    = 1;
                while (flag)
                {
                    move = new ChessMove(location, new ChessLocation(location.X + x, location.Y));
                    if (ai.IsValidMove(board, move, color))
                    {
                        piecemoves.Add(move);
                    }
                    else
                    {
                        flag = false;
                    }
                    x++;
                }
                x    = 1;
                flag = true;
                while (flag)
                {
                    move = new ChessMove(location, new ChessLocation(location.X - x, location.Y));
                    if (ai.IsValidMove(board, move, color))
                    {
                        piecemoves.Add(move);
                    }
                    else
                    {
                        flag = false;
                    }
                    x++;
                }
                x    = 1;
                flag = true;
                while (flag)
                {
                    move = new ChessMove(location, new ChessLocation(location.X, location.Y + x));
                    if (ai.IsValidMove(board, move, color))
                    {
                        piecemoves.Add(move);
                    }
                    else
                    {
                        flag = false;
                    }
                    x++;
                }
                x    = 1;
                flag = true;
                while (flag)
                {
                    move = new ChessMove(location, new ChessLocation(location.X, location.Y - x));
                    if (ai.IsValidMove(board, move, color))
                    {
                        piecemoves.Add(move);
                    }
                    else
                    {
                        flag = false;
                    }
                    x++;
                }
                break;

            case ChessPiece.BlackQueen:
            case ChessPiece.WhiteQueen:
                flag = true;
                x    = 1;
                while (flag)
                {
                    move = new ChessMove(location, new ChessLocation(location.X + x, location.Y + x));
                    if (ai.IsValidMove(board, move, color))
                    {
                        piecemoves.Add(move);
                    }
                    else
                    {
                        flag = false;
                    }
                    x++;
                }
                x    = 1;
                flag = true;
                while (flag)
                {
                    move = new ChessMove(location, new ChessLocation(location.X + x, location.Y - x));
                    if (ai.IsValidMove(board, move, color))
                    {
                        piecemoves.Add(move);
                    }
                    else
                    {
                        flag = false;
                    }
                    x++;
                }
                x    = 1;
                flag = true;
                while (flag)
                {
                    move = new ChessMove(location, new ChessLocation(location.X - x, location.Y - x));
                    if (ai.IsValidMove(board, move, color))
                    {
                        piecemoves.Add(move);
                    }
                    else
                    {
                        flag = false;
                    }
                    x++;
                }
                x    = 1;
                flag = true;
                while (flag)
                {
                    move = new ChessMove(location, new ChessLocation(location.X - x, location.Y + x));
                    if (ai.IsValidMove(board, move, color))
                    {
                        piecemoves.Add(move);
                    }
                    else
                    {
                        flag = false;
                    }
                    x++;
                }
                x    = 1;
                flag = true;
                while (flag)
                {
                    move = new ChessMove(location, new ChessLocation(location.X + x, location.Y));
                    if (ai.IsValidMove(board, move, color))
                    {
                        piecemoves.Add(move);
                    }
                    else
                    {
                        flag = false;
                    }
                    x++;
                }
                x    = 1;
                flag = true;
                while (flag)
                {
                    move = new ChessMove(location, new ChessLocation(location.X - x, location.Y));
                    if (ai.IsValidMove(board, move, color))
                    {
                        piecemoves.Add(move);
                    }
                    else
                    {
                        flag = false;
                    }
                    x++;
                }
                x    = 1;
                flag = true;
                while (flag)
                {
                    move = new ChessMove(location, new ChessLocation(location.X, location.Y + x));
                    if (ai.IsValidMove(board, move, color))
                    {
                        piecemoves.Add(move);
                    }
                    else
                    {
                        flag = false;
                    }
                    x++;
                }
                x    = 1;
                flag = true;
                while (flag)
                {
                    move = new ChessMove(location, new ChessLocation(location.X, location.Y - x));
                    if (ai.IsValidMove(board, move, color))
                    {
                        piecemoves.Add(move);
                    }
                    else
                    {
                        flag = false;
                    }
                    x++;
                }
                break;

            default:
                return(piecemoves);
            }
            return(piecemoves);
        }
示例#8
0
        private Bitmap GetBoardImage(ChessBoard board)
        {
            Bitmap boardBitmap = new Bitmap(_boardWidth, _boardHeight);

            Graphics boardGraphics = Graphics.FromImage(boardBitmap);

            boardGraphics.DrawImage(_cornerBorder, 0, 0);

            for (int ix = 0; ix < ChessBoard.NumberOfColumns; ix++)
            {
                int curPos = (ix * _tileWidth) + _verticalBorderWidth;

                boardGraphics.DrawImage(_horizontalBorderBitmaps[ix], curPos, 0);
                boardGraphics.DrawImage(_verticalBorderBitmaps[ix], 0, curPos);
            }

            bool lightSquare = true;

            for (int y = 0; y < ChessBoard.NumberOfRows; y++)
            {
                int curY = (y * _tileHeight) + _horizontalBorderHeight;

                for (int x = 0; x < ChessBoard.NumberOfColumns; x++)
                {
                    int curX = (x * _tileWidth) + _verticalBorderWidth;

                    if (lightSquare)
                    {
                        boardGraphics.DrawImage(_lightTile, curX, curY);
                    }
                    else
                    {
                        boardGraphics.DrawImage(_darkTile, curX, curY);
                    }

                    if ((_lastFewMoves != null) && (_lastFewMoves.Count > 0))
                    {
                        ChessLocation curLoc = new ChessLocation(x, y);

                        for (int ix = 0; ix < _lastFewMoves.Count; ix++)
                        {
                            if ((_lastFewMoves[ix].From == curLoc) ||
                                (_lastFewMoves[ix].To == curLoc))
                            {
                                boardGraphics.DrawImage(_tileHighlightBitmaps[ix % _tileHighlightBitmaps.Length], curX, curY);
                            }
                        }
                    }

                    if (board[x, y] != ChessPiece.Empty)
                    {
                        boardGraphics.DrawImage(_pieceBitmaps[(int)board[x, y]], curX, curY);
                    }

                    lightSquare = !lightSquare;
                }

                lightSquare = !lightSquare;
            }

            boardGraphics.Dispose();
            boardGraphics = null;

            return(boardBitmap);
        }
示例#9
0
        private static ChessMoveData NewChessMove(ChessBoard board, ChessLocation from, ChessLocation to, ChessColor color)
        {
            var move = new ChessMove(from, to);

            return(new ChessMoveData(board, move, color));
        }
示例#10
0
        private void OnMouseUp(object sender, MouseEventArgs e)
        {
            if ( (e.Button == MouseButtons.Left) &&
                 (_isDraggingPiece) )
            {
                _isDraggingPiece = false;
                _mouseX = e.X;
                _mouseY = e.Y;

                if ( (_mouseX > AdjustedVerticalBorderWidth) &&
                     (_mouseY > AdjustedHorizontalBorderHeight) &&
                     (_mouseX < this.Width) &&
                     (_mouseY < this.Height))
                {
                    ChessLocation newLoc = new ChessLocation((_mouseX - AdjustedVerticalBorderWidth) / AdjustedTileWidth,
                                                             (_mouseY - AdjustedHorizontalBorderHeight) / AdjustedTileHeight);

                    if (_pieceBeingMovedLocation == newLoc)
                    {
                        // They picked up a piece and put it back down
                        Board[_pieceBeingMovedLocation] = _pieceBeingMoved;
                        _boardChanged = true;
                        this.Invalidate();
                    }
                    else if (PieceMovedByHuman != null)
                    {
                        // Someone has subscribed to the event, so it's their
                        // responsibility to call my ResetBoard methods to update the board
                        // Fire the event, and forget
                        ChessMove humanMove = new ChessMove(_pieceBeingMovedLocation, newLoc);

                        //Set the chess flag from the cmbChessFlag
                        humanMove.Flag = (ChessFlag) cmbchessflags.SelectedItem;
                        cmbchessflags.SelectedIndex = 0; //reset flag to NoFlag

                        PieceMovedByHuman(humanMove);
                    }
                    else
                    {
                        // At this point, they didn't just pick up a piece and put it back down,
                        // _and_ noone is subscribed to the event, so I have to update the
                        // board myself.
                        // This should never really happen 'cause when the game is stopped,
                        // WinGui subs to this event.
                        Board[newLoc] = _pieceBeingMoved;
                        _boardChanged = true;
                        this.Invalidate();
                    }
                }
                else
                {
                     // they drug a piece off the board. Put it back to where it was.
                     Board[_pieceBeingMovedLocation] = _pieceBeingMoved;
                     _boardChanged = true;
                     this.Invalidate();
                }
            }
        }
示例#11
0
        private void OnMouseDown(object sender, MouseEventArgs e)
        {
            if ( (! IsLocked) &&
                 (e.Button == MouseButtons.Left) &&
                 (e.X > AdjustedVerticalBorderWidth) &&
                 (e.Y > AdjustedHorizontalBorderHeight) &&
                 (e.X < this.Width) &&
                 (e.Y < this.Height))
            {
                _mouseX = e.X;
                _mouseY = e.Y;

                _pieceBeingMovedLocation = new ChessLocation((_mouseX - AdjustedVerticalBorderWidth) / AdjustedTileWidth,
                                                             (_mouseY - AdjustedHorizontalBorderHeight) / AdjustedTileHeight);

                _pieceBeingMoved = Board[_pieceBeingMovedLocation];

                if (_pieceBeingMoved != ChessPiece.Empty)
                {
                    // Only say the mouse is down if they clicked on a non-empty board square
                    _isDraggingPiece = true;

                    Board[_pieceBeingMovedLocation] = ChessPiece.Empty;
                    _boardChanged = true;

                    this.Invalidate();
                }
            }
        }
示例#12
0
        private Bitmap GetBoardImage(ChessBoard board)
        {
            Bitmap boardBitmap = new Bitmap(_boardWidth, _boardHeight);

            Graphics boardGraphics = Graphics.FromImage(boardBitmap);

            boardGraphics.DrawImage(_cornerBorder, 0, 0);

            for (int ix = 0; ix < ChessBoard.NumberOfColumns; ix++)
            {
                int curPos = (ix * _tileWidth) + _verticalBorderWidth;

                boardGraphics.DrawImage(_horizontalBorderBitmaps[ix], curPos, 0);
                boardGraphics.DrawImage(_verticalBorderBitmaps[ix], 0, curPos);
            }

            bool lightSquare = true;
            for (int y = 0; y < ChessBoard.NumberOfRows; y++)
            {
                int curY = (y * _tileHeight) + _horizontalBorderHeight;

                for (int x = 0; x < ChessBoard.NumberOfColumns; x++)
                {
                    int curX = (x * _tileWidth) + _verticalBorderWidth;

                    if (lightSquare)
                    {
                        boardGraphics.DrawImage(_lightTile, curX, curY);
                    }
                    else
                    {
                        boardGraphics.DrawImage(_darkTile, curX, curY);
                    }

                    if ((_lastFewMoves != null) && (_lastFewMoves.Count > 0))
                    {
                        ChessLocation curLoc = new ChessLocation(x, y);

                        for (int ix = 0; ix < _lastFewMoves.Count; ix++)
                        {
                            if ((_lastFewMoves[ix].From == curLoc) ||
                                (_lastFewMoves[ix].To == curLoc))
                            {
                                boardGraphics.DrawImage(_tileHighlightBitmaps[ix % _tileHighlightBitmaps.Length], curX, curY);
                            }
                        }
                    }

                    if (board[x, y] != ChessPiece.Empty)
                    {
                        boardGraphics.DrawImage(_pieceBitmaps[(int)board[x, y]], curX, curY);
                    }

                    lightSquare = !lightSquare;
                }

                lightSquare = !lightSquare;
            }

            boardGraphics.Dispose();
            boardGraphics = null;

            return boardBitmap;
        }
示例#13
0
 public ChessBoardSquare(ChessLocation location)
 {
     this.Location      = location;
     this.OccupiedBy    = null;
     this.IsUnderAttack = false;
 }