Ejemplo n.º 1
0
        public char[] MinMax(char[] val, int depth, bool isOpening)
        {
            MiniMaxOpening m   = new MiniMaxOpening();
            MiniMaxGame    mid = new MiniMaxGame();

            if (depth > 0)
            {
                depth--;
                List <char[]> blackChild = new List <char[]>();
                char[]        minValue   = new char[43];
                char[]        maxValue;
                blackChild = GenerateBlackMove(val, isOpening);

                double v = Double.PositiveInfinity;

                for (int ind = 0; ind < blackChild.Count; ind++)
                {
                    maxValue = MaxMin(blackChild[ind], depth, isOpening);

                    double staticEstimate = isOpening ? m.StaticEstimationOpening(minValue) : mid.StaticEstimationMidEndGame(minValue, isOpening);
                    if (v > staticEstimate)
                    {
                        v = staticEstimate;
                        _MiniMaxEstimate = v;
                        minValue         = blackChild[ind];
                    }
                }
                return(minValue);
            }
            else if (depth == 0)
            {
                _Positions_Evaluated++;
            }
            return(val);
        }
Ejemplo n.º 2
0
        public char[] MaxMin(char[] val, int depth, bool isOpening)
        {
            MiniMaxOpening m   = new MiniMaxOpening();
            MiniMaxGame    mid = new MiniMaxGame();

            if (depth > 0)
            {
                depth--;
                List <char[]> child = new List <char[]>();
                char[]        minValue;
                char[]        maxValue = new char[43];
                child = isOpening ? m.GenerateAdd(val) : mid.GenerateMovesMidgameEndgame(val);  //so that midgame move and generate add are called by passing parameter from calling function
                double v = Double.NegativeInfinity;

                for (int ind = 0; ind < child.Count; ind++)
                {
                    minValue = MinMax(child[ind], depth, isOpening);

                    double staticEstimate = isOpening ? m.StaticEstimationOpening(minValue) : mid.StaticEstimationMidEndGame(minValue, isOpening);
                    if (v < staticEstimate)
                    {
                        v = staticEstimate;
                        _MiniMaxEstimate = v;
                        maxValue         = child[ind];
                    }
                }
                return(maxValue);
            }
            else if (depth == 0)
            {
                _Positions_Evaluated++;
            }
            return(val);
        }
Ejemplo n.º 3
0
        public List <char[]> GenerateBlackMove(char[] val, bool isOpening)
        {
            MiniMaxOpening m   = new MiniMaxOpening();
            MiniMaxGame    mid = new MiniMaxGame();

            char[] copyOfBoard = (char[])val.Clone();

            for (int ind = 0; ind < copyOfBoard.Length; ind++)
            {
                if (copyOfBoard[ind] == 'W')
                {
                    copyOfBoard[ind] = 'B';
                    continue;
                }
                if (copyOfBoard[ind] == 'B')
                {
                    copyOfBoard[ind] = 'W';
                }
            }

            List <char[]> blackMove        = new List <char[]>();
            List <char[]> blackMoveReplace = new List <char[]>();

            blackMove = isOpening ?  m.GenerateAdd(copyOfBoard) : mid.GenerateMovesMidgameEndgame(copyOfBoard);
            foreach (char[] b in blackMove)
            {
                char[] board = b;
                for (int i = 0; i < board.Length; i++)
                {
                    if (board[i] == 'W')
                    {
                        board[i] = 'B';
                        continue;
                    }
                    if (board[i] == 'B')
                    {
                        board[i] = 'W';
                    }
                }
                blackMoveReplace.Add(b);
            }
            return(blackMoveReplace);
        }
Ejemplo n.º 4
0
        static void Main(string[] args)
        {
            while (true)
            {
                Console.WriteLine("Do you prefer Improved - I / Unimproved - U static estimation : (I/U)");
                string isImproved = Console.ReadLine();
                if (isImproved.ToUpper() == "U")
                {
                    Console.WriteLine("Enter MiniMaxGame/MiniMaxGameBlack: (W/B)");
                    string gameType = Console.ReadLine();
                    if (gameType == "W")
                    {
                        Console.WriteLine("Enter the input file location : ");
                        string inputFile = Console.ReadLine();
                        Console.WriteLine("Enter the output file location : ");
                        string outputFile = Console.ReadLine();
                        Console.WriteLine("Enter depth : ");
                        int depth = Convert.ToInt32(Console.ReadLine());

                        Console.WriteLine("Is your game Opening or MidGame : (O/M)  ");
                        string isOpeningInput = Console.ReadLine();

                        Console.WriteLine("MiniMax / AlphaBeta : (M/A)  ");
                        string miniMaxOrAlphaBeta = Console.ReadLine();

                        try
                        {
                            using (StreamReader sr = new StreamReader(inputFile))
                            {
                                char[] inputPiecePosition = new char[50];
                                string wholeInput;
                                // read lines from the file
                                while ((wholeInput = sr.ReadLine()) != null)
                                {
                                    inputPiecePosition = wholeInput.ToCharArray();
                                }

                                BoardCommon    b         = new BoardCommon();
                                MiniMaxOpening mOpen     = new MiniMaxOpening();
                                ABOpening      aBOpening = new ABOpening();

                                Console.WriteLine("Input Board Position " + new String(inputPiecePosition));

                                bool isOpening = isOpeningInput.ToUpper() == "O" ? true : false;
                                bool isMiniMax = miniMaxOrAlphaBeta.ToUpper() == "M" ? true : false;

                                char[] afterMiniMax = isMiniMax ? b.MaxMin(inputPiecePosition, depth, isOpening) : aBOpening.MaxMin(inputPiecePosition, depth, isOpening, Double.NegativeInfinity, Double.PositiveInfinity);
                                Console.WriteLine("New Board Position " + new String(afterMiniMax));


                                if (isMiniMax)
                                {
                                    Console.WriteLine("Position evaluated " + b._Positions_Evaluated);
                                    Console.WriteLine("Minimax Estimate " + b._MiniMaxEstimate);

                                    using (StreamWriter file =
                                               new StreamWriter(outputFile))
                                    {
                                        file.WriteLine("New Board Position " + new String(afterMiniMax));
                                        file.WriteLine("Position evaluated " + b._Positions_Evaluated);
                                        file.WriteLine("Minimax Estimate " + b._MiniMaxEstimate);
                                    }
                                }
                                else
                                {
                                    Console.WriteLine("Position evaluated " + aBOpening._ABPositions_Evaluated);
                                    Console.WriteLine("Minimax Estimate " + aBOpening._ABMiniMaxEstimate);

                                    using (StreamWriter file =
                                               new StreamWriter(outputFile))
                                    {
                                        file.WriteLine("New Board Position " + new String(afterMiniMax));
                                        file.WriteLine("Position evaluated " + aBOpening._ABPositions_Evaluated);
                                        file.WriteLine("Minimax Estimate " + aBOpening._ABMiniMaxEstimate);
                                    }
                                }
                            }
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine("Exception Occured in Part 1");
                        }
                        Console.ReadLine();
                    }
                    else
                    {
                        try
                        {
                            Console.WriteLine("Is your game Opening or MidGame : (O/M)  ");
                            string isOpeningInput = Console.ReadLine();
                            bool   isOpening      = isOpeningInput.ToUpper() == "O" ? true : false;

                            MiniMaxGameBlack    miniMaxGameBlack    = new MiniMaxGameBlack();
                            MiniMaxOpeningBlack miniMaxOpeningBlack = new MiniMaxOpeningBlack();
                            if (isOpening)
                            {
                                miniMaxOpeningBlack.MiniMaxOpeningForBlack();
                            }
                            else
                            {
                                miniMaxGameBlack.MiniMaxGameForBlack();
                            }
                        }
                        catch (Exception ee)
                        {
                            Console.WriteLine("Exception Occured in MiniMax Black");
                        }
                    }
                }
                else
                {
                    try
                    {
                        Console.WriteLine("Is your game Opening or MidGame : (O/M)");
                        string isOpeningInput = Console.ReadLine();

                        if (isOpeningInput.ToUpper() == "O")
                        {
                            MiniMaxOpeningImproved miniMaxOpeningImproved = new MiniMaxOpeningImproved();
                            miniMaxOpeningImproved.MiniMaxOpeningForImproved();
                        }
                        else
                        {
                            MiniMaxGameImproved miniMaxGameImproved = new MiniMaxGameImproved();
                            miniMaxGameImproved.MiniMaxGameForImproved();
                        }
                    }
                    catch (Exception ee)
                    {
                        Console.WriteLine("Exception Occured in MiniMax Improveds");
                    }
                }
            }
        }