示例#1
0
        private PieceClass movePiece(Element ele, PieceClass pieceToRemove)
        {
            foreach (TileClass tile in HighlightedTiles)
            {
                if (tile.element.Id == ele.Id)
                {
                    LocationPoint tileLoaction   = tile.element.Location as LocationPoint;
                    LocationPoint parentLoaction = tile.parentElement.Location as LocationPoint;
                    parentLoaction.Point = tileLoaction.Point;
                    PieceClass pawn = null;

                    foreach (PieceClass piece in PieceList)
                    {
                        if (piece.Col == tile.column && piece.Row == tile.row)
                        {
                            doc.Delete(piece.element.Id);
                            pieceToRemove = piece;
                        }

                        else if (piece.element.Id == tile.parentElement.Id)
                        {
                            if (turn == "White" && playVsCPU)
                            {
                                WhiteMoveForEngine = ChessEngine.generateMoveString(piece.Col, tile.column, piece.Row, tile.row);
                            }

                            piece.Col = tile.column;
                            piece.Row = tile.row;
                            if (checkPawnBecomesQueen(piece))
                            {
                                pawn = piece;
                            }
                        }
                    }

                    //Change turn
                    if (turn == "White")
                    {
                        turn = "Black";
                    }
                    else
                    {
                        turn = "White";
                    }

                    modelText.Text = turn + " To Move";

                    if (pawn != null)
                    {
                        doc.Delete(pawn.element.Id);
                        PieceList.Remove(pawn);
                        PieceList.Add(new Queen(pawn.Color, pawn.Row, pawn.Col, this));
                    }
                }
            }

            return(pieceToRemove);
        }
示例#2
0
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            WelcomeForm welcomeForm = new WelcomeForm();

            welcomeForm.ShowDialog();
            playVsCPU = welcomeForm.vsCPU;

            UIApplication uiapp         = commandData.Application;
            UIDocument    uidoc         = uiapp.ActiveUIDocument;
            PieceClass    pieceToRemove = null;

            doc = uidoc.Document;

            findExistingPieces();
            clearBoard(allFamilies);

            if (playVsCPU)
            {
                chessEngineProcess = ChessEngine.Start();
            }

            using (Transaction t = new Transaction(doc, "Set Board"))
            {
                t.Start();
                InitPieces();
                InitBoard();
                t.Commit();
            }

            while (true)
            {
                pieceToRemove = null;
                //isKingInCheck();

                using (Transaction t = new Transaction(doc, "Test"))
                {
                    t.Start();

                    Reference eleid = null;
                    Element   ele   = null;


                    //CPU Turn
                    if (playVsCPU && turn == "Black")
                    {
                        PieceClass piece = ChessEngine.CPUMove(WhiteMoveForEngine, chessEngineProcess, PieceList);
                        pieceToRemove = moveCPUPiece(piece, pieceToRemove);
                    }


                    else
                    {
                        //Get player to select an element
                        bool selectionFailed = selectPiece(uidoc, ref eleid, ref ele);

                        if (selectionFailed)
                        {
                            break;
                        }

                        //If chosen element is a chess piece then highlight its available moves
                        if ((ele as FamilyInstance).Symbol.Family.Name != "ChessTile")
                        {
                            deleteHighlightedTiles();
                            GetPieceClass(ele).HighlightMoveOptions();
                        }
                        //Otherwise element is a tile - therfore move chess piece to the location of the tile
                        else
                        {
                            pieceToRemove = movePiece(ele, pieceToRemove);
                            deleteHighlightedTiles();
                        }
                    }
                    t.Commit();
                    if (pieceToRemove != null)
                    {
                        PieceList.Remove(pieceToRemove);
                    }
                }
            }
            return(Result.Succeeded);
        }