Exemple #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);
        }
Exemple #2
0
        bool checkPawnBecomesQueen(PieceClass piece)
        {
            if (piece is Pawn)
            {
                if (piece.Color == "White" && piece.Row == 1)
                {
                    return(true);
                }
                else if (piece.Color == "Black" && piece.Row == 8)
                {
                    return(true);
                }
            }

            return(false);
        }
Exemple #3
0
        private PieceClass moveCPUPiece(PieceClass piece, PieceClass pieceToRemove)
        {
            LocationPoint pieceOriginalLocation = piece.element.Location as LocationPoint;
            var           updatedPoint          = new XYZ(piece.Col, piece.Row, 0);

            pieceOriginalLocation.Point = updatedPoint;

            PieceClass pawn = null;

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

            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);
        }
Exemple #4
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);
        }
Exemple #5
0
        private void isKingInCheck()
        {
            using (Transaction t = new Transaction(doc, "CheckCheck"))
            {
                t.Start();

                List <PieceClass> allEnemyPieces = null;

                if (turn != "White")
                {
                    allEnemyPieces = PieceList.FindAll(x => x.Color == "White");
                    foreach (PieceClass piece in allEnemyPieces)
                    {
                        piece.HighlightMoveOptions();
                    }
                }
                else
                {
                    allEnemyPieces = PieceList.FindAll(x => x.Color == "Black");
                    foreach (PieceClass piece in allEnemyPieces)
                    {
                        piece.HighlightMoveOptions();
                    }
                }

                PieceClass king = null;
                foreach (PieceClass _king in PieceList)
                {
                    if (_king.Color == turn)
                    {
                        king = _king;
                    }
                }



                foreach (TileClass tile in HighlightedTiles)
                {
                    if (tile.column == king.Col && tile.row == king.Row)
                    {
                        TaskDialog.Show("Check", turn + " King is in check.");
                    }
                }

                List <TileClass> tilesToRemove = new List <TileClass>();

                foreach (TileClass tile in HighlightedTiles)
                {
                    string color = GetPieceClass(tile.parentElement).Color;
                    if (color != turn)
                    {
                        doc.Delete(tile.element.Id);
                        tilesToRemove.Add(tile);
                    }
                }
                foreach (TileClass tile in tilesToRemove)
                {
                    HighlightedTiles.Remove(tile);
                }
                t.Commit();
            }
        }