Beispiel #1
0
        private void handleMoveEnPassant(Position newMovePos, EnPassantedPosition opponentPos)
        {
            int xIncrement = 1;
            //int yIncrement = 1;
            int zIncrement = 1;

            if (piece.player is Player2)
            {
                xIncrement = -1;
                //yIncrement = -1;
                zIncrement = -1;
            }

            if (positionIsValid(newMovePos))
            {
                /*important: handle changing coords according to the player*/
                AbstractPiece opponent = null;
                switch (opponentPos)
                {
                case EnPassantedPosition.Left: opponent = Chessboard.getReference()[piece.position.x - xIncrement, piece.position.y, piece.position.z]; break;

                case EnPassantedPosition.Right: opponent = Chessboard.getReference()[piece.position.x + xIncrement, piece.position.y, piece.position.z]; break;

                case EnPassantedPosition.Up: opponent = Chessboard.getReference()[piece.position.x, piece.position.y + zIncrement, piece.position.z]; break;

                case EnPassantedPosition.Down: opponent = Chessboard.getReference()[piece.position.x, piece.position.y - zIncrement, piece.position.z]; break;
                }

                if (opponent == null)
                {
                    return;
                }

                if ((opponent.player == piece.player) || (opponent.name != ChessNames.Pawn))
                {
                    return;
                }

                if (!((Pawn)opponent).hasMovedTwoBlocks)
                {
                    return;
                }

                if (!History.getReference().peakPhase().oldPiece.Equals(opponent))
                {
                    return;
                }

                //Stack operations to check if it's the last piece moved!! //ATTENTION!

                //It's all ok! add it to available moves
                //opponent.isCapturable = true; //IMPORTANT FOR A.I.
                opponent.isCapturable = true;
                moves.Add(newMovePos);
            }
        }
Beispiel #2
0
        public HistoryPhase(AbstractPiece oldPiece, Position oldPos, AbstractPiece newPiece, Position newPos)
        {
            this.newPiece = newPiece;
            this.newPos   = newPos;
            newHasMoved   = (newPiece == null) ? false : newPiece.hasMoved;


            oldHasMoved   = oldPiece.hasMoved;
            this.oldPiece = oldPiece;
            this.oldPos   = oldPos;
        }
Beispiel #3
0
 public RookMoves(AbstractPiece piece) : base(piece)
 {
 }
Beispiel #4
0
 public BishopMoves(AbstractPiece piece) : base(piece)
 {
 }
Beispiel #5
0
        /* Chessboard[0,0,0] = King */
        //this is actually the moveTo facility
        /// <summary>
        /// A helper method 2 the moveTo() method,this where the actual moving happens .
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="z"></param>
        /// <returns></returns>
        public AbstractPiece this[int x, int y, int z]
        {
            get { return(matrix[x, y, z]); }
            set
            {
                if (value == null)
                {
                    matrix[x, y, z] = null;
                    return;
                }

                if (((AbstractPiece)value).name == ChessNames.King)
                {
                    int kingX = ((AbstractPiece)value).position.x;
                    int kingY = ((AbstractPiece)value).position.y;
                    int kingZ = ((AbstractPiece)value).position.z;

                    if ((kingX - x == -2) && (kingX == 3 || kingX == 4))
                    {
                        AbstractPiece rook = matrix[7, kingY, kingZ];
                        matrix[7, kingY, kingZ]         = null;
                        rook.position.x                 = kingX + 1;
                        matrix[kingX + 1, kingY, kingZ] = rook;
                    }
                    if (kingX - x == 2 && (kingX == 3 || kingX == 4))
                    {
                        AbstractPiece rook = matrix[0, kingY, kingZ];
                        matrix[0, kingY, kingZ]         = null;
                        rook.position.x                 = kingX - 1;
                        matrix[kingX - 1, kingY, kingZ] = rook;
                    }
                }

                if (((AbstractPiece)value).name == ChessNames.Pawn)
                {
                    int oldPosX = ((AbstractPiece)value).position.x;
                    int oldPosY = ((AbstractPiece)value).position.y;
                    int oldPosZ = ((AbstractPiece)value).position.z;

                    //handling first move (two blocks)
                    if ((Math.Abs(oldPosY - y) == 2) ||
                        (Math.Abs(oldPosZ - z) == 2))
                    {
                        //Do internal swap
                        AbstractPiece temp1 = (AbstractPiece)value;
                        //original position
                        matrix[temp1.position.x, temp1.position.y, temp1.position.z] = null;
                        //new position
                        ((Pawn)temp1).hasMovedTwoBlocks = true;
                        temp1.position.x = x;
                        temp1.position.y = y;
                        temp1.position.z = z;
                        matrix[x, y, z]  = value;
                        return;
                    }
                    else
                    {
                        ((Pawn)((AbstractPiece)value)).hasMovedTwoBlocks = false;
                    }
                    //handle unpassant. here only we will handle capturing the piece

                    //I'm not sure, but it looks something like this not the following:
                    //if((Math.Abs(x-oldPosX) != 0) || (Math.Abs(z-oldPosZ) != 0)) //it has captured something
                    //this is correct probably
                    if (Math.Abs(x - oldPosX) != 0)
                    {
                        if (Chessboard.getReference()[x, y, z] == null)
                        {//check if it has captured by unpassant move
                            if ((value.player is Player2) && (z - oldPosZ < 0))
                            {
                                Chessboard.getReference()[x, y, z + 1].IsCaptured = true;
                                Chessboard.getReference()[x, y, z + 1]            = null;
                            }
                            else
                            {
                                if ((value.player is Player1) && (z - oldPosZ > 0))
                                {
                                    Chessboard.getReference()[x, y, z - 1].IsCaptured = true;
                                    Chessboard.getReference()[x, y, z - 1]            = null;
                                }
                            }
                        }
                    }
                }

                AbstractPiece temp = (AbstractPiece)value;
                //original position
                matrix[temp.position.x, temp.position.y, temp.position.z] = null;
                //new position

                temp.position.x = x;
                temp.position.y = y;
                temp.position.z = z;
                matrix[x, y, z] = value;
            }
        }
Beispiel #6
0
        public void undo()
        {
            if (currentPhase < 0)
            {
                return;
            }
            //reverse camera and turn
            GameManager.getReference(null).toggleTurn(true);
            HistoryPhase currHP = history[currentPhase];

            if (currHP.newPiece == null) //we've moved to a null position
            {
                //check castling
                if (currHP.oldPiece.GetType() == typeof(King))
                {
                    if (Math.Abs(currHP.oldPos.x - currHP.newPos.x) > 1) //i.e. castling happened
                    {
                        //just restore the Rook as well
                        //we have to know where the rook is
                        if (currHP.newPos.x < currHP.oldPos.x) //rook is left
                        {
                            AbstractPiece curRook = vchessboard.ModelAt(currHP.newPos.x + 1, currHP.newPos.y, currHP.newPos.z).LogicalPieceRef;

                            Chessboard.getReference()[0, currHP.newPos.y, currHP.newPos.z] = curRook;
                            Console.WriteLine(curRook);
                        }
                        else
                        {
                            AbstractPiece curRook = vchessboard.ModelAt(currHP.newPos.x - 1, currHP.newPos.y, currHP.newPos.z).LogicalPieceRef;
                            Chessboard.getReference()[7, currHP.newPos.y, currHP.newPos.z] = curRook;
                        }
                    }
                }
                //check for unpassant move
                if (currHP is PawnHistoryPhase)
                {
                    //upassant = changing direction on X axis
                    if (Math.Abs(currHP.oldPos.x - currHP.newPos.x) == 1)
                    {
                        //Just restore the captured pawn, the rest of the code
                        //restores this pawn
                        AbstractPiece capturedPawn = vchessboard.LogicalModelAt(currHP.newPos.x, currHP.newPos.y, currHP.oldPos.z);
                        Chessboard.getReference()[currHP.newPos.x, currHP.newPos.y, currHP.oldPos.z] = capturedPawn;
                        capturedPawn.IsCaptured = false;
                        ((Pawn)capturedPawn).hasMovedTwoBlocks = true;
                    }
                }
                Chessboard.getReference()[currHP.oldPos.x, currHP.oldPos.y, currHP.oldPos.z] = currHP.oldPiece;
                currHP.oldPiece.position = currHP.oldPos;
                if (currHP is PawnHistoryPhase)
                {
                    //restore previous state (just to check if it has moved two steps forward
                    ((Pawn)currHP.oldPiece).hasMovedTwoBlocks = ((PawnHistoryPhase)currHP).hasMovesTwoBlocks;
                }
                currHP.oldPiece.hasMoved = currHP.oldHasMoved;
                currentPhase--;
            }
            else //we've captured something
            {
                Chessboard.getReference()[currHP.oldPos.x, currHP.oldPos.y, currHP.oldPos.z] =
                    currHP.oldPiece;
                currHP.oldPiece.position   = currHP.oldPos;
                currHP.oldPiece.isSelected = false;
                Chessboard.getReference()[currHP.newPos.x, currHP.newPos.y, currHP.newPos.z] =
                    currHP.newPiece;
                currHP.newPiece.IsCaptured = false; //for rendering
                currHP.newPiece.isSelected = false;
                currentPhase--;
            }
            PanelHistory.getReference().addMessage("Undo: " + GameManager.getReference(null).curPlayer().ToString() + " moved " + currHP.oldPiece.ToString() + " back from " + currHP.oldPos.ToString() + " to " + currHP.newPos.ToString(), GameManager.getReference(null).isPlayer1Turn());
        }
Beispiel #7
0
 public void pushPhase(AbstractPiece newPiece, Position newPos, AbstractPiece oldPiece, Position oldPos)
 {
     history.Add(new HistoryPhase(newPiece, newPos, oldPiece, oldPos));
     //PanelInfo event
 }
Beispiel #8
0
 public PawnMoves(AbstractPiece piece) : base(piece)
 {
 }
Beispiel #9
0
 public QueenMoves(AbstractPiece piece) : base(piece)
 {
     rookMoves   = new RookMoves(piece);
     bishopMoves = new BishopMoves(piece);
 }
Beispiel #10
0
 /// <summary>
 /// The constructor takes one parameter which is the moving AbstractPiece
 /// </summary>
 /// <param name="piece"></param>
 public AbstractMove(AbstractPiece piece)
 {
     this.piece = piece;
     moves      = new List <Position>();
 }
Beispiel #11
0
 public PawnHistoryPhase(Pawn oldPiece, Position oldPos, AbstractPiece newPiece, Position newPos)
     : base((AbstractPiece)oldPiece, oldPos, newPiece, newPos)
 {
     hasMovesTwoBlocks = oldPiece.hasMovedTwoBlocks;
 }
Beispiel #12
0
        private bool castlingIsOk(CastleAllignment castleAllign)
        {
            int rookX = ((castleAllign == CastleAllignment.Left) ? 0 : 7);

            AbstractPiece temp = Chessboard.getReference()[rookX, piece.position.y, piece.position.z];

            if (
                (temp != null) && (temp is Rook) &&
                (!temp.hasMoved) && (!piece.hasMoved) &&
                (!((King)piece).isThreatend)
                )
            {
                //check to see if the path is clear
                int kingX = piece.position.x;

                //check if there are any piece along the way
                //compare kingX with rookX to choose what pieces to check
                int xIncrement = (kingX > rookX ? -1 : 1);

                List <Position> freePieces = new List <Position>();
                freePieces.Add(new Position(kingX + xIncrement, piece.position.y, piece.position.z));
                freePieces.Add(new Position(kingX + (2 * xIncrement), piece.position.y, piece.position.z));

                if (xIncrement == -1)
                {
                    if (Chessboard.getReference()[kingX + (3 * xIncrement), piece.position.y, piece.position.z] != null)
                    {
                        return(false);
                    }
                    freePieces.Add(new Position(kingX + (3 * xIncrement), piece.position.y, piece.position.z));
                }


                //get enemy pieces
                List <AbstractPiece> enemyPieces =
                    Chessboard.getReference().getNotCapturedEnemies
                        (Chessboard.getReference()[piece.position.x, piece.position.y, piece.position.z].player);


                foreach (Position freePiece in freePieces)
                {
                    if (Chessboard.getReference()[freePiece.x, freePiece.y, freePiece.z] != null)
                    {
                        return(false);
                    }

                    foreach (AbstractPiece enemyPiece in enemyPieces)
                    {
                        List <Position> enemyAvailableMoves = enemyPiece.getAvailableMoves();
                        foreach (Position enemyAvailableMove in enemyAvailableMoves)
                        {
                            if (enemyAvailableMove.x == freePiece.x && enemyAvailableMove.y == freePiece.y && enemyAvailableMove.z == freePiece.z)
                            {
                                return(false);
                            }
                        }
                    }
                }
                ;
                return(true);
            }
            return(false);
        }
Beispiel #13
0
 public KingMoves(AbstractPiece piece)
     : base(piece)
 {
     //Empty Constructor
 }