Iterative Implementation of Negamax Alpha-Beta Please help me check the correctness of this c# iterative implementation of negamax! Thank you! Your help highly appreciated! Also, please help me out how to add alpha beta pruning to this(it was supposed to be alpha beta with negamax, but i forgot the alpha beta part... :&lt;)! Thanks in advance!

EDIT: I DO think that the implementaqtion works. I only want it to be checked. Also, i simply want SUGGESTIONS and GUIDANCE on how to add alpha beta to the algorithm, not someone write the code for me. Please do not be mistaken. Thank you.

Random r = new Random();

Stack&lt;int&gt; maxs = new Stack&lt;int&gt;();

Stack&lt;int&gt; levels = new Stack&lt;int&gt;();
Stack&lt;string&gt; todo = new Stack&lt;string&gt;();

Stack&lt;string&gt; undo = new Stack&lt;string&gt;();

maxs.Push(int.MinValue);

var le = legals(bd);
while (le.Any())
{
    var mv = le[r.Next(le.Count)];
    le.Remove(mv);
    todo.Push(mv);
    levels.Push(1);
}

while (todo.Any())
{
    //init
    string mv = todo.Pop();
    int level = levels.Pop();
    undo.Push(mv);
    if (level != depthleft) maxs.Push(int.MinValue);
    move(mv, level % 2 == 0 ? "b" : "w");

    //expand children/do operations,cleanup
    if (level == depthleft)
    {
         int score = -eval(bd, level % 2 == 0);
         if (score &gt; maxs.Last())
         {
              maxs.Pop();
              maxs.Push(score);
         }
         unmove(undo.Pop());

         int diff = level - levels.Last();
         if (diff != 0)
         {
              for (; diff &gt; 0; diff--)
              {
                   score = -maxs.Pop();
                   if (score &gt; maxs.Last())
                   {
                        maxs.Pop();
                        maxs.Push(score);
                   }
                   unmove(undo.Pop());
              }
         }
     }
     else
     {
          var ls = legals(bd);
          while (ls.Any())
          {
              var child = ls[r.Next(ls.Count)];
              ls.Remove(child);
              todo.Push(child);
              levels.Push(level + 1);
          }
     }
}
Some quick suggestions:


Split the code into smaller methods, with understandable names - it will become simpler!
Use clear variable names (even if they are longer) and try to avoid "shortenings";
Never use less than 3 characters for variable names... le? r? mv?!? (int i in for cycles is an exception because it is idiomatic);
Use PascalCase (not camelCase) for methods and properties;
Use camelCase (not lowercase) for local variables;
Don't repeat code (var le = legals(bd); while (le.Any()) { ... }), extract it to a method.