Exemple #1
0
        internal List <Mutare> EvaluareMutare(int adancime, MainGame m)
        {
            List <Mutare> ret       = new List <Mutare>();
            List <Mutare> generator = new List <Mutare>();

            if (m.DatePartidaCastig.Culoare == CuloarePiesa.Negru)
            {
                generator = m.GetToateMutarilePosibileWhite(m);
            }
            else
            {
                generator = m.GetToateMutarilePosibileBlack(m);
            }
            List <Mutare> newGameMoves    = generator.OrderByDescending(o => o.Value).ToList();
            int           alpha           = Int32.MinValue;
            int           beta            = Int32.MaxValue;
            int           bestMove        = alpha;
            Mutare        bestMoveFound   = new Mutare();
            bool          immediateAttack = false;

            for (int j = 0; j < newGameMoves.Count; j++)
            {
                Mutare myMove        = newGameMoves[j];
                string tempToName    = m.pieceIdBoard[myMove.To].PieceName;
                int    tempToValue   = m.pieceIdBoard[myMove.To].PieceValue;
                string tempFromName  = m.pieceIdBoard[myMove.From].PieceName;
                int    tempFromValue = m.pieceIdBoard[myMove.From].PieceValue;
                AttackValue = 0;

                //Make the move
                ExecutaMutarea(m, myMove);

                //check for immediate attack
                immediateAttack = CheckForImmediateAttack(m, immediateAttack, myMove, tempToValue);

                // if none, get the best move
                if (!immediateAttack)
                {
                    int value = MinMax(adancime - 1, false, alpha, beta, m);

                    if (value >= bestMove)
                    {
                        if (bestMoveFound.To == 0 && bestMoveFound.From == 0)
                        {
                            bestMoveFound = myMove;
                        }
                        bestMoveFound = myMove;
                        ret.Add(bestMoveFound);
                        bestMove = value;
                    }
                }

                UndoMove(m, myMove, tempToName, tempToValue, tempFromName, tempFromValue);

                immediateAttack = false;
            }
            List <Mutare> retturnList = ret.OrderBy(o => o.Value).ToList();

            return(retturnList);
        }
Exemple #2
0
        public bool IsCheckMove(List <Mutare> validMoves, int pos, int dest, MainGame m, CuloarePiesa playerculoare)
        {
            MakeMoveLight(pos, dest, m);

            List <Mutare> list2 = new List <Mutare>();


            if (CuloarePiesa.Alb == playerculoare)
            {
                list2 = m.GetToateMutarilePosibileBlack(m);
                string sfpartida = m.NumePiesaafisaj(m.DatePartidaCastig.NumePiesa, CuloarePiesa.Alb);
                // if white king position is same as black's destination position,
                // this would result in check, return true
                for (int i = 0; i < m.DatePartidaCastig.NumarColoane * m.DatePartidaCastig.NumarRanduri; i++)
                {
                    if (String.Compare(m.pieceIdBoard[i].PieceName, sfpartida) == 0)
                    {
                        foreach (Mutare move2 in list2)
                        {
                            if (m.pieceIdBoard[i].PiecePosition == move2.To)
                            {
                                return(true);
                            }
                        }
                    }
                }

                return(false);
            }
            else
            {
                list2 = m.GetToateMutarilePosibileWhite(m);
                string sfpartida = m.NumePiesaafisaj(m.DatePartidaCastig.NumePiesa, CuloarePiesa.Negru);
                // if white king position is same as black's destination position,
                // this would result in check, return true
                for (int i = 0; i < m.DatePartidaCastig.NumarColoane * m.DatePartidaCastig.NumarRanduri; i++)
                {
                    if (String.Compare(m.pieceIdBoard[i].PieceName, sfpartida) == 0)
                    {
                        foreach (Mutare move2 in list2)
                        {
                            if (m.pieceIdBoard[i].PiecePosition == move2.To)
                            {
                                return(true);
                            }
                        }
                    }
                }

                return(false);
            }
            return(false);
        }
Exemple #3
0
        private static bool CheckForImmediateAttack(MainGame m, bool skip, Mutare myMove, int tempToValue)
        {
            List <Mutare> pnewGameMoves;

            if (m.DatePartidaCastig.Culoare == CuloarePiesa.Alb)
            {
                pnewGameMoves = m.GetToateMutarilePosibileWhite(m);
            }
            else
            {
                pnewGameMoves = m.GetToateMutarilePosibileBlack(m);
            }
            foreach (Mutare mov in pnewGameMoves)
            {
                if (mov.To == myMove.To)
                {
                    if (tempToValue <= Math.Abs(m.pieceIdBoard[myMove.To].PieceValue))
                    {
                        skip = true;
                    }
                }
            }
            return(skip);
        }
Exemple #4
0
        public int MinMax(int depth, bool isMaximiser, int alpha, int beta, MainGame m)
        {
            //end of specified traversal length, evaluate board
            if (depth == 0)
            {
                int k = -EvaluateBoard(m);
                return(k);
            }

            List <Mutare> newGameMoves  = new List <Mutare>();
            List <Mutare> pnewGameMoves = new List <Mutare>();

            if (isMaximiser)
            {
                if (m.DatePartidaCastig.Culoare == CuloarePiesa.Negru)
                {
                    pnewGameMoves = m.GetToateMutarilePosibileWhite(m);
                }
                else
                {
                    pnewGameMoves = m.GetToateMutarilePosibileBlack(m);
                }
                newGameMoves = pnewGameMoves.OrderByDescending(o => o.Value).ToList();
                int bestMove = Int32.MinValue;
                for (int i = 0; i < newGameMoves.Count; i++)
                {
                    Mutare myMove        = newGameMoves[i];
                    string tempToName    = m.pieceIdBoard[myMove.To].PieceName;
                    int    tempToValue   = m.pieceIdBoard[myMove.To].PieceValue;
                    string tempFromName  = m.pieceIdBoard[myMove.From].PieceName;
                    int    tempFromValue = m.pieceIdBoard[myMove.From].PieceValue;
                    int    minValue      = m.pieceIdBoard[myMove.To].PieceValue;

                    ExecutaMutarea(m, myMove);

                    bestMove = Math.Max(bestMove, MinMax(depth - 1, false, alpha, beta, m));

                    UndoMove(m, myMove, tempToName, tempToValue, tempFromName, tempFromValue);

                    alpha = Math.Max(alpha, bestMove);
                    if (alpha >= beta)
                    {
                        break;
                    }
                }
                return(bestMove);
            }

            else
            {
                if (m.DatePartidaCastig.Culoare != CuloarePiesa.Negru)
                {
                    pnewGameMoves = m.GetToateMutarilePosibileWhite(m);
                }
                else
                {
                    pnewGameMoves = m.GetToateMutarilePosibileBlack(m);
                }
                newGameMoves = pnewGameMoves.OrderByDescending(o => o.Value).ToList();
                int bestMove = Int32.MaxValue;

                for (int i = 0; i < newGameMoves.Count; i++)
                {
                    Mutare myMove        = newGameMoves[i];
                    int    maxValue      = m.pieceIdBoard[myMove.To].PieceValue;
                    string tempToName    = m.pieceIdBoard[myMove.To].PieceName;
                    int    tempToValue   = m.pieceIdBoard[myMove.To].PieceValue;
                    string tempFromName  = m.pieceIdBoard[myMove.From].PieceName;
                    int    tempFromValue = m.pieceIdBoard[myMove.From].PieceValue;
                    int    minValue      = m.pieceIdBoard[myMove.To].PieceValue;

                    ExecutaMutarea(m, myMove);
                    bestMove = Math.Min(bestMove, MinMax(depth - 1, true, alpha, beta, m));
                    UndoMove(m, myMove, tempToName, tempToValue, tempFromName, tempFromValue);

                    beta = Math.Min(beta, bestMove);

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