예제 #1
0
        public float Minimax(Grid g, out GridCells best, int depth = 1)
        {
            miniMaxCount++;
            best = GridCells.None;
            var       bestResult = -10f;
            GridCells garbage;

            if (g.IsDraw)
            {
                return(0f);
            }

            if (g.CurrentIsWinner)
            {
                return(1f / depth);
            }

            if (g.CurrentIsLoser)
            {
                return(-1f / depth);
            }

            foreach (var move in g.GetMoves())
            {
                var other  = g.MakeMove(move);
                var result = -Minimax(other, out garbage, depth + 1);

                if (result > bestResult)
                {
                    best       = move;
                    bestResult = result;
                }
            }
            return(bestResult);
        }
예제 #2
0
        public int Minimax(Grid g, out GridCells best)
        {
            best = GridCells.None;
            var       bestResult = -10;
            GridCells garbage;

            if (g.IsDraw)
            {
                return(0);
            }

            if (g.CurrentIsWinner)
            {
                return(1);
            }

            if (g.CurrentIsLoser)
            {
                return(-1);
            }

            foreach (var move in g.GetMoves())
            {
                var other  = g.MakeMove(move);
                var result = -Minimax(other, out garbage);

                if (result > bestResult)
                {
                    best       = move;
                    bestResult = result;
                }
            }
            return(bestResult);
        }
예제 #3
0
        public float Minimax(Grid g, out GridCells best, float alpha, float beta, int depth = 1)
        {
            miniMaxCount++;
            best = GridCells.None;
            var bestResult = -10f;
            GridCells garbage;
            if (g.IsDraw)
                return 0f;

            if (g.CurrentIsWinner)
                return 1f / depth;

            if (g.CurrentIsLoser)
                return -1f / depth;

            foreach (var move in g.GetMoves())
            {
                var other = g.MakeMove(move);
                alpha = -Minimax(other, out garbage, -beta, -alpha, depth + 1);

                if (beta <= alpha)
                    return alpha;

                if (alpha > bestResult)
                {
                    best = move;
                    bestResult = alpha;
                }
            }
            return bestResult;
        }
예제 #4
0
        public int Minimax(Grid g, out GridCells best)
        {
            best = GridCells.None;
            var bestResult = -10;
            GridCells garbage;
            if (g.IsDraw)
                return 0;

            if (g.CurrentIsWinner)
                return 1;

            if (g.CurrentIsLoser)
                return -1;

            foreach (var move in g.GetMoves())
            {
                var other = g.MakeMove(move);
                var result = -Minimax(other, out garbage);

                if (result > bestResult)
                {
                    best = move;
                    bestResult = result;
                }
            }
            return bestResult;
        }