Ejemplo n.º 1
0
        protected List <Coordinate> removeImpossibleMoves(Piece[,] board, List <Coordinate> possibleMoves,
                                                          Coordinate position, bool kingIsWhite)
        {
            //creating simulated board of all possible moves

            for (short index = 0; index < possibleMoves.Count; index++)
            {
                Coordinate i = possibleMoves[index];
                Piece[,] simulatedBoard  = (Piece[, ])board.Clone();
                simulatedBoard[i.X, i.Y] = simulatedBoard[position.X, position.Y];
                simulatedBoard[position.X, position.Y] = null;

                (King, Coordinate)king = findKing(simulatedBoard, kingIsWhite);

                if (king.Item1.isCheck(simulatedBoard, king.Item2))
                {
                    Coordinate move = possibleMoves[index];
                    possibleMoves.RemoveAt(index);
                    index--;
                }
            }


            return(possibleMoves);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Metod som kollar om draget man gjort kommer orsaka schack för sin egna kung.
        /// </summary>
        /// <param name="Pieces">Spelbrädet som används.</param>
        /// <param name="xIndex">X koordinaten för den valda pjäsen.</param>
        /// <param name="yIndex">Y koordinaten för den valda pjäsen.</param>
        /// <param name="xTarget">X koordinaten för dit den valda pjäsen ska flytta till.</param>
        /// <param name="yTarget">Y koordinaten för dit den valda pjäsen ska flytta till.</param>
        /// <returns>Returnerar om det draget man gjort kommer schacka sin kung.</returns>
        public static bool WillMoveCauseCheck(Piece[,] Pieces, int xIndex, int yIndex, int xTarget, int yTarget)
        {
            Piece[,] tempBoard = (Piece[, ])Pieces.Clone();
            var piece = tempBoard[xIndex, yIndex];

            tempBoard[xIndex, yIndex]   = null;
            tempBoard[xTarget, yTarget] = piece;

            return(Check(tempBoard));
        }
Ejemplo n.º 3
0
        public bool MovePutsPlayerInCheck(Player player, Square from, Square to)
        {
            Piece[,] newBoard = (Piece[, ])board.Clone();

            Board workingBoard = new Board(player, newBoard);

            workingBoard.MovePiece(from, to);

            return(workingBoard.PlayerIsInCheck(player));
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Gets all moves the king can make
        /// </summary>
        /// <param name="currentState">The game's current state</param>
        /// <param name="currentPosition">The king's current position</param>
        /// <returns>a list of the valid moves</returns>
        public override List <PotentialMove> GetAllValidMoves(Piece[,] currentState, int[] currentPosition)
        {
            List <PotentialMove> possibleMoves = new List <PotentialMove>();

            Piece[,] tempState = new Piece[8, 8];
            int newX, newY;

            newX      = currentPosition[0] - 1;
            newY      = currentPosition[1] - 1;
            tempState = (Piece[, ])currentState.Clone();
            TestValidMove(currentState, currentPosition, ref possibleMoves, tempState, newX, newY);

            newX      = currentPosition[0];
            newY      = currentPosition[1] - 1;
            tempState = (Piece[, ])currentState.Clone();
            TestValidMove(currentState, currentPosition, ref possibleMoves, tempState, newX, newY);

            newX      = currentPosition[0] + 1;
            newY      = currentPosition[1] - 1;
            tempState = (Piece[, ])currentState.Clone();
            TestValidMove(currentState, currentPosition, ref possibleMoves, tempState, newX, newY);

            newX      = currentPosition[0] - 1;
            newY      = currentPosition[1];
            tempState = (Piece[, ])currentState.Clone();
            TestValidMove(currentState, currentPosition, ref possibleMoves, tempState, newX, newY);

            newX      = currentPosition[0] + 1;
            newY      = currentPosition[1];
            tempState = (Piece[, ])currentState.Clone();
            TestValidMove(currentState, currentPosition, ref possibleMoves, tempState, newX, newY);

            newX      = currentPosition[0] - 1;
            newY      = currentPosition[1] + 1;
            tempState = (Piece[, ])currentState.Clone();
            TestValidMove(currentState, currentPosition, ref possibleMoves, tempState, newX, newY);

            newX      = currentPosition[0];
            newY      = currentPosition[1] + 1;
            tempState = (Piece[, ])currentState.Clone();
            TestValidMove(currentState, currentPosition, ref possibleMoves, tempState, newX, newY);

            newX      = currentPosition[0] + 1;
            newY      = currentPosition[1] + 1;
            tempState = (Piece[, ])currentState.Clone();
            TestValidMove(currentState, currentPosition, ref possibleMoves, tempState, newX, newY);

            return(possibleMoves);
        }
Ejemplo n.º 5
0
    //evaluate current board
    public Board(Piece [,] basePieces)
    {
        //set default values
        pieces         = (Piece[, ])basePieces.Clone();
        sideTable      = new SideTable(BoardManager.GetInstance().table1);
        playerOneMoves = new List <Square> ();
        playerTwoMoves = new List <Square> ();
        eval           = 0;

        playerOneMoves = GetAllLegalMoves(isPlayerOnesTurn: true);
        playerTwoMoves = GetAllLegalMoves(isPlayerOnesTurn: false);
        eval           = Evaluate();
    }
Ejemplo n.º 6
0
        public Piece[,] GetBoardAfterMove(Move move)
        {
            var newBoard = (Piece[, ])Board.Clone();
            var position = GetPiecePosition(move.Piece);

            var oldId = move.Piece.Id;

            if (move.Piece is Pawn && move.Piece.Color == Color.White && move.TargetPosition.Y == 0)
            {
                move.Piece = new Queen(move.Piece.Color, oldId);
            }

            if (move.Piece is Pawn && move.Piece.Color == Color.Black && move.TargetPosition.Y == 7)
            {
                move.Piece = new Queen(move.Piece.Color, oldId);
            }

            newBoard[position.Y, position.X] = null;
            newBoard[move.TargetPosition.Y, move.TargetPosition.X] = move.Piece;

            return(newBoard);
        }
Ejemplo n.º 7
0
        public void MovePiece(Square from, Square to)
        {
            var movingPiece = board[from.Row, from.Col];

            if (movingPiece == null)
            {
                return;
            }

            if (movingPiece.Player != CurrentPlayer)
            {
                throw new ArgumentException("The supplied piece does not belong to the current player.");
            }

            //If the space we're moving to is occupied, we need to mark it as captured.
            if (board[to.Row, to.Col] != null)
            {
                OnPieceCaptured(board[to.Row, to.Col]);
            }
            if (movingPiece.GetType() == typeof(Pawn))
            {
                if (to.Col != from.Col && GetPiece(to) == null)
                {
                    OnPieceCaptured(board[from.Row, to.Col]);
                    board[from.Row, to.Col] = null;
                }
            }

            PreviousBoard = (Piece[, ])board.Clone();

            //Move the piece and set the 'from' square to be empty.
            board[to.Row, to.Col]     = board[from.Row, from.Col];
            board[from.Row, from.Col] = null;

            CurrentPlayer = movingPiece.Player == Player.White ? Player.Black : Player.White;
            OnCurrentPlayerChanged(CurrentPlayer);
        }
Ejemplo n.º 8
0
 protected Piece[,] Clone(Piece[,] board)
 {
     return((Piece[, ])board.Clone());           // Shallow Copy
 }
        private bool AddMove(Player player, string pos, out int errorInPosString)
        {
            Color  curPlayerColor = player.GetColorEmum();
            string from;
            string to;

            string[] subStr = pos.Split(" ");
            if (subStr.Length != 2)
            {
                errorInPosString = -1;
                return(false);
            }
            from = subStr[0].Trim().ToUpper();
            to   = subStr[1].Trim().ToUpper();
            if (!posMapStr.ContainsKey(from))
            {
                errorInPosString = 1;
                return(false);
            }
            if (!posMapStr.ContainsKey(to))
            {
                errorInPosString = 2;
                return(false);
            }
            int   posFrom   = posMapStr[from];
            Piece pieceFrom = cellPiece(posFrom);

            if (pieceFrom == null || (pieceFrom != null && pieceFrom.GetColor() != curPlayerColor))
            {
                errorInPosString = 1;
                return(false);
            }
            int   posTo   = posMapStr[to];
            Piece pieceTo = cellPiece(posTo);

            if (pieceTo != null && pieceTo.GetColor() == curPlayerColor)
            {
                errorInPosString = 2;
                return(false);
            }
            var tupleFrom = PosFrom1DTo2D(posFrom);
            var tupleTo   = PosFrom1DTo2D(posTo);

            if (pieceFrom.IsPossibleMove(tupleFrom.Item1, tupleFrom.Item2, tupleTo.Item1, tupleTo.Item2))
            {
                if (pieceFrom.GetPieceType() == PieceType.King && IsInCheckmate(pieceFrom.GetOtherColor(), tupleTo.Item1, tupleTo.Item2))
                {
                    Console.WriteLine("Invalid move it will be CheckMate!");
                    errorInPosString = 2;
                    return(false);
                }

                // Makes a copy of the board.
                var boardTmp = board.Clone();

                // Make Move;
                board[tupleTo.Item1, tupleTo.Item2]     = pieceFrom;
                board[tupleFrom.Item1, tupleFrom.Item2] = null;

                // Update All references in the peaces to the board.
                foreach (Piece p in board)
                {
                    if (p != null)
                    {
                        p.SetBoard(this);
                    }
                }

                // Get my king position.
                int kingRow = -1;
                int kingCol = -1;
                for (int row = 0; row < 8; row++)
                {
                    for (int col = 0; col < 8; col++)
                    {
                        Piece piece = board[row, col];
                        if (piece != null && piece.GetColor() == curPlayerColor && piece.GetPieceType() == PieceType.King)
                        {
                            kingRow = row;
                            kingCol = col;
                        }
                    }
                }

                if (IsInCheckmate(pieceFrom.GetOtherColor(), kingRow, kingCol))
                {
                    // Reverts the play.
                    board = (Piece[, ])boardTmp;

                    // Update All references in the peaces to the board.
                    foreach (Piece p in board)
                    {
                        if (p != null)
                        {
                            p.SetBoard(this);
                        }
                    }

                    Console.WriteLine("Invalid move it will be CheckMate!");
                    errorInPosString = 1;
                    return(false);
                }

                errorInPosString = -1;
                return(true);
            }
            errorInPosString = 2;
            return(false);
        }
Ejemplo n.º 10
0
        public Board Move(IntPoint location, IntPoint targetLocation, out Piece eatenPiece)
        {
            Debug.Assert(board[location.Y, location.X] != null, $"想要从({location.X},{location.Y})走子,但该位置没有棋子。");


            var newBoard = (Piece[, ])board.Clone();

            eatenPiece = newBoard[targetLocation.Y, targetLocation.X];

            newBoard[targetLocation.Y, targetLocation.X] = newBoard[location.Y, location.X];
            newBoard[location.Y, location.X]             = null;

            var b = new Board(newBoard);

            b.redXEqualityRepresentation   = redXEqualityRepresentation;
            b.redYEqualityRepresentation   = redYEqualityRepresentation;
            b.blackXEqualityRepresentation = blackXEqualityRepresentation;
            b.blackYEqualityRepresentation = blackYEqualityRepresentation;

            var movingColor = board[location.Y, location.X].Color;
            var movingId    = board[location.Y, location.X].ID;

            if (movingColor == ChessColor.Red)
            {
                b.redXEqualityRepresentation -= GetEqualityDigit(movingId, location.X);
                b.redYEqualityRepresentation -= GetEqualityDigit(movingId, location.Y);

                b.redXEqualityRepresentation += GetEqualityDigit(movingId, targetLocation.X);
                b.redYEqualityRepresentation += GetEqualityDigit(movingId, targetLocation.Y);

                if (eatenPiece != null)
                {
                    b.blackXEqualityRepresentation -= GetEqualityDigit(eatenPiece.ID, targetLocation.X);
                    b.blackYEqualityRepresentation -= GetEqualityDigit(eatenPiece.ID, targetLocation.Y);
                }
            }
            else
            {
                b.blackXEqualityRepresentation -= GetEqualityDigit(movingId, location.X);
                b.blackYEqualityRepresentation -= GetEqualityDigit(movingId, location.Y);

                b.blackXEqualityRepresentation += GetEqualityDigit(movingId, targetLocation.X);
                b.blackYEqualityRepresentation += GetEqualityDigit(movingId, targetLocation.Y);

                if (eatenPiece != null)
                {
                    b.redXEqualityRepresentation -= GetEqualityDigit(eatenPiece.ID, targetLocation.X);
                    b.redYEqualityRepresentation -= GetEqualityDigit(eatenPiece.ID, targetLocation.Y);
                }
            }

#if DEBUG
            ulong rx = b.redXEqualityRepresentation,
                  ry = b.redYEqualityRepresentation,
                  bx = b.blackXEqualityRepresentation,
                  by = b.blackYEqualityRepresentation;

            b.InitilizeEqualityRepresentation();

            Debug.Assert(b.redXEqualityRepresentation == rx);
            Debug.Assert(b.redYEqualityRepresentation == ry);
            Debug.Assert(b.blackXEqualityRepresentation == bx);
            Debug.Assert(b.blackYEqualityRepresentation == by);
#endif

            return(b);
        }
Ejemplo n.º 11
0
    private double Minimax(Piece[,] board, int depth, bool whiteSide, bool maximizingPlayer, double alpha, double beta)
    {
        double initial = 0;

        Piece[,] tempBoard;

        if (depth == 0)
        {
            return(GetHeuristic(board, whiteSide));
        }

        // Volver a calcular los movimientos posibles
        List <Move>    possibleMoves = new List <Move>();
        List <Vector2> forcedPieces  = new List <Vector2>();

        // Mirar si es obligatorio matar con alguna pieza
        forcedPieces = FindForcedPieces(board, whiteSide);

        // Si hay alguna incluir los movimientos posibles de ellas
        if (forcedPieces.Count > 0)
        {
            possibleMoves = GetForcedMoves(board, forcedPieces);
        }
        // Si no incluir todos los movimientos posibles
        else if (forcedPieces.Count == 0)
        {
            possibleMoves = GetAllValidMoves(board, whiteSide);
        }

        // Si el jugador busca MAX
        if (maximizingPlayer)
        {
            initial = double.NegativeInfinity;
            for (int i = 0; i < possibleMoves.Count; i++)
            {
                tempBoard = (Piece[, ])board.Clone();
                MakeMove(tempBoard, possibleMoves[i]);

                double result = Minimax(tempBoard, depth - 1, !whiteSide, !maximizingPlayer, alpha, beta);

                if (result > initial)
                {
                    initial = result;
                }
                if (initial > alpha)
                {
                    alpha = initial;
                }

                if (alpha >= beta)
                {
                    break;
                }
            }
        }
        // Si busca MIN
        else
        {
            initial = double.PositiveInfinity;
            for (int i = 0; i < possibleMoves.Count; i++)
            {
                tempBoard = (Piece[, ])board.Clone();
                MakeMove(tempBoard, possibleMoves[i]);

                double result = Minimax(tempBoard, depth - 1, !whiteSide, !maximizingPlayer, alpha, beta);

                if (result < initial)
                {
                    initial = result;
                }
                if (initial < alpha)
                {
                    alpha = initial;
                }

                if (alpha >= beta)
                {
                    break;
                }
            }
        }

        return(initial);
    }
Ejemplo n.º 12
0
    private Move MinimaxStart(Piece[,] board, int depth, bool whiteSide, bool maximizingPlayer)
    {
        double alpha = double.NegativeInfinity;
        double beta  = double.PositiveInfinity;

        List <Move>    possibleMoves = new List <Move>();
        List <Vector2> forcedPieces  = new List <Vector2>();

        // Mirar si es obligatorio matar con alguna pieza y guardar sus x e y
        forcedPieces = FindForcedPieces(board, whiteSide);

        // Si hay alguna incluir los movimientos posibles de ellas
        if (forcedPieces.Count > 0)
        {
            possibleMoves = GetForcedMoves(board, forcedPieces);
        }

        // Si no incluir todos los movimientos posibles
        else if (forcedPieces.Count == 0)
        {
            possibleMoves = GetAllValidMoves(board, whiteSide);
        }

        // Si no hay movimientos posibles return null
        if (possibleMoves.Count == 0)
        {
            Debug.Log("NO HAY MOVIMIENTOS");
            return(null);
        }

        // Crear la lista para guardar los valores de los estados
        List <double> heuristics = new List <double>();

        // Crear un tablero temporal
        Piece[,] tempBoard;

        // Por cada uno de los movimientos posibles
        for (int i = 0; i < possibleMoves.Count; i++)
        {
            tempBoard = (Piece[, ])board.Clone();
            // Hacer el movimiento en el tablero temporal
            MakeMove(tempBoard, possibleMoves[i]);
            // Hacer el calculo
            heuristics.Add(Minimax(tempBoard, depth - 1, !whiteSide, !maximizingPlayer, alpha, beta));
        }

        // Buscar el valor maximo en la lista de heuristicas
        double maxHeuristics = double.NegativeInfinity;

        for (int i = heuristics.Count - 1; i >= 0; i--)
        {
            if (heuristics[i] >= maxHeuristics)
            {
                maxHeuristics = heuristics[i];
            }
        }

        // Filtrar segun resultado dejando solo los movimientos mas valiosos
        for (int i = 0; i < heuristics.Count; i++)
        {
            if (heuristics[i] < maxHeuristics)
            {
                heuristics.RemoveAt(i);
                possibleMoves.RemoveAt(i);
                i--;
            }
        }

        // Devolver aleatoriamente un movimiento de entre los de igual valia
        int rand = (int)Random.Range(0, possibleMoves.Count);

        return(possibleMoves[rand]);
    }
Ejemplo n.º 13
0
        /// <summary>
        /// Gets all valid moves
        /// </summary>
        /// <param name="currentState">The current state of the game</param>
        /// <param name="currentPosition">the pawn's current position</param>
        /// <returns>the list of valid moves</returns>
        public override List <PotentialMove> GetAllValidMoves(Piece[,] currentState, int[] currentPosition)
        {
            List <PotentialMove> possibleMoves = new List <PotentialMove>();

            Piece[,] tempState = new Piece[8, 8];
            int newX, newY;

            if (PieceTeam == true)
            {
                newX = currentPosition[0];
                newY = currentPosition[1];
                newY++;
                if (ValidatePawnMove(currentPosition, new[] { newX, newY }, currentState))
                {
                    tempState             = (Piece[, ])currentState.Clone();
                    tempState[newX, newY] = tempState[currentPosition[0], currentPosition[1]];
                    tempState[currentPosition[0], currentPosition[1]] = null;
                    possibleMoves.Add(new PotentialMove(currentPosition, new[] { newX, newY }, tempState));
                }

                if (ValidatePawnMove(currentPosition, new[] { newX + 1, newY }, currentState))
                {
                    tempState = (Piece[, ])currentState.Clone();
                    tempState[newX + 1, newY] = tempState[currentPosition[0], currentPosition[1]];
                    tempState[currentPosition[0], currentPosition[1]] = null;
                    possibleMoves.Add(new PotentialMove(currentPosition, new[] { newX + 1, newY }, tempState));
                }

                if (ValidatePawnMove(currentPosition, new[] { newX - 1, newY }, currentState))
                {
                    tempState = (Piece[, ])currentState.Clone();
                    tempState[newX - 1, newY] = tempState[currentPosition[0], currentPosition[1]];
                    tempState[currentPosition[0], currentPosition[1]] = null;
                    possibleMoves.Add(new PotentialMove(currentPosition, new[] { newX - 1, newY }, tempState));
                }

                if (ValidatePawnMove(currentPosition, new[] { newX, newY + 1 }, currentState))
                {
                    tempState = (Piece[, ])currentState.Clone();
                    tempState[newX, newY + 1] = tempState[currentPosition[0], currentPosition[1]];
                    tempState[currentPosition[0], currentPosition[1]] = null;
                    possibleMoves.Add(new PotentialMove(currentPosition, new[] { newX, newY + 1 }, tempState));
                }
            }
            else if (PieceTeam == false)
            {
                newX = currentPosition[0];
                newY = currentPosition[1];
                newY--;
                if (ValidatePawnMove(currentPosition, new[] { newX, newY }, currentState))
                {
                    tempState             = (Piece[, ])currentState.Clone();
                    tempState[newX, newY] = tempState[currentPosition[0], currentPosition[1]];
                    tempState[currentPosition[0], currentPosition[1]] = null;
                    possibleMoves.Add(new PotentialMove(currentPosition, new[] { newX, newY }, tempState));
                }

                if (ValidatePawnMove(currentPosition, new[] { newX + 1, newY }, currentState))
                {
                    tempState = (Piece[, ])currentState.Clone();
                    tempState[newX + 1, newY] = tempState[currentPosition[0], currentPosition[1]];
                    tempState[currentPosition[0], currentPosition[1]] = null;
                    possibleMoves.Add(new PotentialMove(currentPosition, new[] { newX + 1, newY }, tempState));
                }

                if (ValidatePawnMove(currentPosition, new[] { newX - 1, newY }, currentState))
                {
                    tempState = (Piece[, ])currentState.Clone();
                    tempState[newX - 1, newY] = tempState[currentPosition[0], currentPosition[1]];
                    tempState[currentPosition[0], currentPosition[1]] = null;
                    possibleMoves.Add(new PotentialMove(currentPosition, new[] { newX - 1, newY }, tempState));
                }

                if (ValidatePawnMove(currentPosition, new[] { newX, newY - 1 }, currentState))
                {
                    tempState = (Piece[, ])currentState.Clone();
                    tempState[newX, newY - 1] = tempState[currentPosition[0], currentPosition[1]];
                    tempState[currentPosition[0], currentPosition[1]] = null;
                    possibleMoves.Add(new PotentialMove(currentPosition, new[] { newX, newY - 1 }, tempState));
                }
            }

            return(possibleMoves);
        }
Ejemplo n.º 14
0
 public Piece[,] GetTable()
 {
     return((Piece[, ])table.Clone());
 }
Ejemplo n.º 15
0
        /// <summary>
        /// Gets all rook valid moves
        /// </summary>
        /// <param name="currentState">The game's current state</param>
        /// <param name="currentPosition">The rook's current position</param>
        /// <returns>A list of all valid moves found</returns>
        public List <PotentialMove> GetAllValidRookMoves(Piece[,] currentState, int[] currentPosition)
        {
            List <PotentialMove> possibleMoves = new List <PotentialMove>();

            Piece[,] tempState = new Piece[8, 8];
            int newX, newY;

            newX = currentPosition[0];
            newY = currentPosition[1];

            tempState = (Piece[, ])currentState.Clone();
            newX++;

            while (TestValidMove(new[] { newX, newY }, currentPosition, PieceTeam, currentState))
            {
                tempState = (Piece[, ])currentState.Clone();

                tempState[newX, newY] = tempState[currentPosition[0], currentPosition[1]];
                tempState[currentPosition[0], currentPosition[1]] = null;
                possibleMoves.Add(new PotentialMove(currentPosition, new[] { newX, newY }, tempState));
                if (currentState[newX, newY] != null && currentState[newX, newY].PieceTeam != currentState[currentPosition[0], currentPosition[1]].pieceTeam)
                {
                    break;
                }

                newX++;
            }

            newX = currentPosition[0];
            newY = currentPosition[1];

            tempState = (Piece[, ])currentState.Clone();
            newY++;

            while (TestValidMove(new[] { newX, newY }, currentPosition, PieceTeam, currentState))
            {
                tempState = (Piece[, ])currentState.Clone();

                tempState[newX, newY] = tempState[currentPosition[0], currentPosition[1]];
                tempState[currentPosition[0], currentPosition[1]] = null;
                possibleMoves.Add(new PotentialMove(currentPosition, new[] { newX, newY }, tempState));
                if (currentState[newX, newY] != null && currentState[newX, newY].PieceTeam != currentState[currentPosition[0], currentPosition[1]].pieceTeam)
                {
                    break;
                }

                newY++;
            }

            newX = currentPosition[0];
            newY = currentPosition[1];

            tempState = (Piece[, ])currentState.Clone();
            newX--;

            while (TestValidMove(new[] { newX, newY }, currentPosition, PieceTeam, currentState))
            {
                tempState = (Piece[, ])currentState.Clone();

                tempState[newX, newY] = tempState[currentPosition[0], currentPosition[1]];
                tempState[currentPosition[0], currentPosition[1]] = null;
                possibleMoves.Add(new PotentialMove(currentPosition, new[] { newX, newY }, tempState));
                if (currentState[newX, newY] != null && currentState[newX, newY].PieceTeam != currentState[currentPosition[0], currentPosition[1]].pieceTeam)
                {
                    break;
                }

                newX--;
            }

            newX = currentPosition[0];
            newY = currentPosition[1];

            tempState = (Piece[, ])currentState.Clone();
            newY--;

            while (TestValidMove(new[] { newX, newY }, currentPosition, PieceTeam, currentState))
            {
                tempState = (Piece[, ])currentState.Clone();

                tempState[newX, newY] = tempState[currentPosition[0], currentPosition[1]];
                tempState[currentPosition[0], currentPosition[1]] = null;
                possibleMoves.Add(new PotentialMove(currentPosition, new[] { newX, newY }, tempState));
                if (currentState[newX, newY] != null && currentState[newX, newY].PieceTeam != currentState[currentPosition[0], currentPosition[1]].pieceTeam)
                {
                    break;
                }

                newY--;
            }

            return(possibleMoves);
        }
Ejemplo n.º 16
0
        /// <summary>
        /// Gets all moves a given knight could make
        /// </summary>
        /// <param name="currentState">TYhe game's current state</param>
        /// <param name="currentPosition">The knight's current position</param>
        /// <returns>The list of valid moves</returns>
        public override List <PotentialMove> GetAllValidMoves(Piece[,] currentState, int[] currentPosition)
        {
            List <PotentialMove> possibleMoves = new List <PotentialMove>();

            Piece[,] tempState = new Piece[8, 8];
            int newX, newY;

            newX      = currentPosition[0] - 1;
            newY      = currentPosition[1] - 2;
            tempState = (Piece[, ])currentState.Clone();

            if (TestValidMove(new[] { newX, newY }, currentPosition, currentState[currentPosition[0], currentPosition[1]].PieceTeam, currentState))
            {
                tempState[newX, newY] = tempState[currentPosition[0], currentPosition[1]];
                tempState[currentPosition[0], currentPosition[1]] = null;
                possibleMoves.Add(new PotentialMove(currentPosition, new[] { newX, newY }, tempState));
            }

            newX      = currentPosition[0] + 1;
            newY      = currentPosition[1] - 2;
            tempState = (Piece[, ])currentState.Clone();

            if (TestValidMove(new[] { newX, newY }, currentPosition, currentState[currentPosition[0], currentPosition[1]].PieceTeam, currentState))
            {
                tempState[newX, newY] = tempState[currentPosition[0], currentPosition[1]];
                tempState[currentPosition[0], currentPosition[1]] = null;
                possibleMoves.Add(new PotentialMove(currentPosition, new[] { newX, newY }, tempState));
            }

            newX      = currentPosition[0] - 1;
            newY      = currentPosition[1] + 2;
            tempState = (Piece[, ])currentState.Clone();

            if (TestValidMove(new[] { newX, newY }, currentPosition, currentState[currentPosition[0], currentPosition[1]].PieceTeam, currentState))
            {
                tempState[newX, newY] = tempState[currentPosition[0], currentPosition[1]];
                tempState[currentPosition[0], currentPosition[1]] = null;
                possibleMoves.Add(new PotentialMove(currentPosition, new[] { newX, newY }, tempState));
            }

            newX      = currentPosition[0] + 1;
            newY      = currentPosition[1] + 2;
            tempState = (Piece[, ])currentState.Clone();

            if (TestValidMove(new[] { newX, newY }, currentPosition, currentState[currentPosition[0], currentPosition[1]].PieceTeam, currentState))
            {
                tempState[newX, newY] = tempState[currentPosition[0], currentPosition[1]];
                tempState[currentPosition[0], currentPosition[1]] = null;
                possibleMoves.Add(new PotentialMove(currentPosition, new[] { newX, newY }, tempState));
            }

            newX      = currentPosition[0] + 2;
            newY      = currentPosition[1] - 1;
            tempState = (Piece[, ])currentState.Clone();

            if (TestValidMove(new[] { newX, newY }, currentPosition, currentState[currentPosition[0], currentPosition[1]].PieceTeam, currentState))
            {
                tempState[newX, newY] = tempState[currentPosition[0], currentPosition[1]];
                tempState[currentPosition[0], currentPosition[1]] = null;
                possibleMoves.Add(new PotentialMove(currentPosition, new[] { newX, newY }, tempState));
            }

            newX      = currentPosition[0] + 2;
            newY      = currentPosition[1] + 1;
            tempState = (Piece[, ])currentState.Clone();

            if (TestValidMove(new[] { newX, newY }, currentPosition, currentState[currentPosition[0], currentPosition[1]].PieceTeam, currentState))
            {
                tempState[newX, newY] = tempState[currentPosition[0], currentPosition[1]];
                tempState[currentPosition[0], currentPosition[1]] = null;
                possibleMoves.Add(new PotentialMove(currentPosition, new[] { newX, newY }, tempState));
            }

            newX      = currentPosition[0] - 2;
            newY      = currentPosition[1] - 1;
            tempState = (Piece[, ])currentState.Clone();

            if (TestValidMove(new[] { newX, newY }, currentPosition, currentState[currentPosition[0], currentPosition[1]].PieceTeam, currentState))
            {
                tempState[newX, newY] = tempState[currentPosition[0], currentPosition[1]];
                tempState[currentPosition[0], currentPosition[1]] = null;
                possibleMoves.Add(new PotentialMove(currentPosition, new[] { newX, newY }, tempState));
            }

            newX      = currentPosition[0] - 2;
            newY      = currentPosition[1] + 1;
            tempState = (Piece[, ])currentState.Clone();

            if (TestValidMove(new[] { newX, newY }, currentPosition, currentState[currentPosition[0], currentPosition[1]].PieceTeam, currentState))
            {
                tempState[newX, newY] = tempState[currentPosition[0], currentPosition[1]];
                tempState[currentPosition[0], currentPosition[1]] = null;
                possibleMoves.Add(new PotentialMove(currentPosition, new[] { newX, newY }, tempState));
            }

            return(possibleMoves);
        }