Example #1
0
        public void ResetPieces(bool UserIsWhite)
        {
            //reset the board to to the default start positions
            ClearBoard();
            /*white moves first*/
            WhiteToMove = true;

            if (UserIsWhite)
            {
                imgSystemPawn = Image.FromFile("bp.png");
                imgUserPawn   = Image.FromFile("wp.png");
            }
            else
            {
                imgSystemPawn = Image.FromFile("wp.png");
                imgUserPawn   = Image.FromFile("bp.png");
            }

            chessSquares[0, 0].Image = imgUserPawn;
            chessSquares[1, 0].Image = imgUserPawn;
            chessSquares[2, 0].Image = imgUserPawn;
            chessSquares[3, 0].Image = imgUserPawn;
            chessSquares[4, 0].Image = imgUserPawn;
            chessSquares[5, 0].Image = imgUserPawn;

            chessSquares[0, 5].Image = imgSystemPawn;
            chessSquares[1, 5].Image = imgSystemPawn;
            chessSquares[2, 5].Image = imgSystemPawn;
            chessSquares[3, 5].Image = imgSystemPawn;
            chessSquares[4, 5].Image = imgSystemPawn;
            chessSquares[5, 5].Image = imgSystemPawn;


            if (!UserIsWhite)
            {
                /* As user is black the computer should make the first move*/
                Thread.Sleep(700);
                int        depth_to_search = (int)updAlphaBeta.Value;
                ChessBoard currentPos      = ConvertGraphicalRepresentationToBitboard(chessSquares);
                PawnChessEngine.MinMaxEx(currentPos, false, depth_to_search, depth_to_search, -PawnChessEngine.INFINITY, PawnChessEngine.INFINITY, new Evaluate(PawnChessEngine.EvaluatePosition));
                DisplayGraphicalRepresentationFromBitBoard(PawnChessEngine.ReplyMove, chessSquares);
                WhiteToMove = !WhiteToMove;
            }
        }
Example #2
0
        public bool ValidateUserMove(ChessBoard PositionBeforeMove, ChessBoard PositionAfterMove)
        {
            /*This method checks to see whether a user move is valid or not*/
            bool bRet = false;

            ChessBoard[] possibleReplies = PawnChessEngine.GenerateMoves(PositionBeforeMove, true);

            if (possibleReplies == null)
            {
                return(bRet);
            }

            for (int i = 0; i < possibleReplies.Length; i++)
            {
                if ((possibleReplies[i].SystemPawns == PositionAfterMove.SystemPawns) && (possibleReplies[i].UserPawns == PositionAfterMove.UserPawns))
                {
                    bRet = true;
                    break;
                }
            }

            return(bRet);
        }
Example #3
0
        void frmPawnChess_DragDrop(object sender, DragEventArgs e)
        {
            PictureBox picDestinaton = (PictureBox)sender;

            if (picDestinaton != picCurrentlySelected)
            {
                Image img = (Image)e.Data.GetData(DataFormats.Bitmap);
                /*User Move validation Code starts here*/

                /*get the bitboard representation of the board just before*/
                ChessBoard board_before_move = ConvertGraphicalRepresentationToBitboard(chessSquares);

                /* change the graphical board to reflect the user move*/
                Image temp_image = picDestinaton.Image;
                picDestinaton.Image        = img;
                picCurrentlySelected.Image = null;

                /*convert the graphical representation in to the bitboard after the move*/
                ChessBoard board_after_move = ConvertGraphicalRepresentationToBitboard(chessSquares);

                /*now validate!*/
                if (!ValidateUserMove(board_before_move, board_after_move))
                {
                    //move is invalid
                    MessageBox.Show("Illegal move!");
                    picDestinaton.Image        = temp_image;
                    picCurrentlySelected.Image = img;
                }
                else
                {
                    //Its the other player move.. so signal that
                    WhiteToMove = !WhiteToMove;


                    ChessBoard currentPos = ConvertGraphicalRepresentationToBitboard(chessSquares);
                    if (PawnChessEngine.DetectWinLoss(currentPos, false) == MOVE_STATUS.SYSTEM_WINS)
                    {
                        MessageBox.Show("System Wins!");
                        return;
                    }

                    if (PawnChessEngine.DetectWinLoss(currentPos, true) == MOVE_STATUS.USER_WINS)
                    {
                        MessageBox.Show("User Wins!");
                        return;
                    }



                    //ChessBoard currentPos = ConvertGraphicalRepresentationToBitboard(chessSquares);
                    int depth_to_search = (int)updAlphaBeta.Value;
                    PawnChessEngine.MinMaxEx(currentPos, false, depth_to_search, depth_to_search, -PawnChessEngine.INFINITY, PawnChessEngine.INFINITY, new Evaluate(PawnChessEngine.EvaluatePosition));
                    DisplayGraphicalRepresentationFromBitBoard(PawnChessEngine.ReplyMove, chessSquares);


                    if (PawnChessEngine.DetectWinLoss(PawnChessEngine.ReplyMove, false) == MOVE_STATUS.SYSTEM_WINS)
                    {
                        MessageBox.Show("System Wins!");
                        return;
                    }
                    if (PawnChessEngine.DetectWinLoss(PawnChessEngine.ReplyMove, false) == MOVE_STATUS.USER_WINS)
                    {
                        MessageBox.Show("User Wins!");
                        return;
                    }

                    WhiteToMove = !WhiteToMove;
                }
            }
        }