Пример #1
0
        public static int minimax()
        {
            StateNode sn = new StateNode();

            return(TupleToMove(sn.miniMax(Board.board, false)));
        }
Пример #2
0
        private int GenerateStates(StateNode root, int depth, bool isMaximizingPlayer, int alpha, int beta)
        {
            root.currentDepth = depth;
            if (Logic.WinCheck(root.gs) || depth >= maxDepth)
            {
                return(Logic.EvaluateBoard(root.gs, depth));
            }

            if (isMaximizingPlayer)
            {
                root.minimaxValue = int.MinValue;

                for (int i = 0; i < 3; i++)
                {
                    for (int j = 0; j < 3; j++)
                    {
                        if (root.gs.state[i, j] != 'X' && root.gs.state[i, j] != 'O')
                        {
                            var copiedGameState = root.gs.Copy();
                            var position        = copiedGameState.state[i, j];
                            copiedGameState.state[i, j] = player;

                            var newNode = new StateNode();
                            newNode.gs     = copiedGameState;
                            newNode.parent = root;
                            root.children.Add(newNode);

                            newNode.action       = Tuple.Create(i, j);
                            newNode.minimaxValue = GenerateStates(newNode, depth + 1, !isMaximizingPlayer, alpha, beta);
                            root.minimaxValue    = Math.Max(root.minimaxValue, newNode.minimaxValue);
                            alpha = Math.Max(alpha, root.minimaxValue);

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

                return(root.minimaxValue);
            }
            else
            {
                root.minimaxValue = int.MaxValue;

                for (int i = 0; i < 3; i++)
                {
                    for (int j = 0; j < 3; j++)
                    {
                        if (root.gs.state[i, j] != 'X' && root.gs.state[i, j] != 'O')
                        {
                            var copiedGameState = root.gs.Copy();
                            var position        = copiedGameState.state[i, j];
                            copiedGameState.state[i, j] = computer;

                            var newNode = new StateNode();
                            newNode.gs     = copiedGameState;
                            newNode.parent = root;
                            root.children.Add(newNode);

                            newNode.action       = Tuple.Create(i, j);
                            newNode.minimaxValue = GenerateStates(newNode, depth + 1, !isMaximizingPlayer, alpha, beta);
                            root.minimaxValue    = Math.Min(root.minimaxValue, newNode.minimaxValue);
                            beta = Math.Min(beta, root.minimaxValue);

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

                return(root.minimaxValue);
            }
        }