Ejemplo n.º 1
0
        static GameBoard CalculateMaxMove(GameBoard parent)
        {
            /* Alpha values are stored in the max nodes
             * Alpha value = max(children's Beta Values of min nodes)
             * Because we want to minimize opponent moves             *
             */
            if (parent.GetChildren().Count <= 0)
            {
                CalculateMoves(parent);
            }

            GameBoard best_child = null;
            if (parent.GetChildren().Count > 0)
            {
                 best_child = parent.GetChildren().ElementAt(0); //best is first child

                foreach (GameBoard child in parent.GetChildren())
                {
                    if (CalculateMoves(child) >= parent.GetAlphaBetaValue()) //get max
                    {
                        best_child = child; //new best child
                        parent.SetAlphaBetaValue(best_child.GetAlphaBetaValue());
                    }
                }

                return best_child;
            }
            return null;
        }
Ejemplo n.º 2
0
        static GameBoard CalculateMinMove(GameBoard parent)
        {
            /* Beta values are stored in the min nodes
             * Beta value = min(alpha Values of max nodes)
             * Because we want to maximize our moves
             *
             */
            if (parent.GetChildren().Count <= 0)
            {
                CalculateMoves(parent);
            }

            GameBoard worst_child = null;

            if (parent.GetChildren().Count > 0)
            {
               worst_child = parent.GetChildren().ElementAt(0);

                foreach (GameBoard child in parent.GetChildren())
                {
                    if (CalculateMoves(child) <= parent.GetAlphaBetaValue())
                    {
                        worst_child = child;
                        parent.SetAlphaBetaValue(worst_child.GetAlphaBetaValue());
                    }
                }
                return worst_child;
            }
            return null;
        }
Ejemplo n.º 3
0
        static int CalculateAlphaBeta(GameBoard parent, int depth, int alpha, int beta, GameBoard.MinMax player)
        {
            int moves = 0;
            if (parent.GetChildren().Count == 0) //if there are no children for this node
            {
                moves = CalculateMoves(parent);
            }
            else
            {
                moves = parent.GetChildren().Count;
            }

            if (moves == 0 || depth == 0) //terminal node
            {
                return parent.GetAlphaBetaValue(); //return non null value
            }

            if (player == GameBoard.MinMax.Max) //max player
            {
                foreach (GameBoard child in parent.GetChildren())
                {
                    //children are min nodes
                    alpha = Math.Max(alpha, CalculateAlphaBeta(child, depth-1, alpha, beta, child.GetNodeType()));
                    child.SetAlphaBetaValue(alpha);
                    if(beta <= alpha)
                    {
                        parent.GetChildren().Remove(child);
                        break;
                    }
                }
                return alpha;
            }
            else //min player
            {
                foreach (GameBoard child in parent.GetChildren())
                {
                    //children are max nodes
                    beta = Math.Min(beta, CalculateAlphaBeta(child, depth-1, alpha, beta, child.GetNodeType()));
                    child.SetAlphaBetaValue(beta);
                    if (beta <= alpha)
                    {
                        parent.GetChildren().Remove(child);
                        break;
                    }
                }
                return beta;
            }
        }