Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

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... :<)! 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<int> maxs = new Stack<int>();

Stack<int> levels = new Stack<int>();
Stack<string> todo = new Stack<string>();

Stack<string> undo = new Stack<string>();

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 > maxs.Last())
         {
              maxs.Pop();
              maxs.Push(score);
         }
         unmove(undo.Pop());

         int diff = level - levels.Last();
         if (diff != 0)
         {
              for (; diff > 0; diff--)
              {
                   score = -maxs.Pop();
                   if (score > 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);
          }
     }
}     
share|improve this question
2  
I think your question does not fit well on this site. This site is for requests to improve code that you think works, not to check whether it works at all and certainly not to write code for you. –  svick Nov 18 '12 at 15:29
    
yes, i think my code should work. i just want someone to check its correctness on behalf of puny me. also, i only wanted SUGGESTIONS on howto incooperate alpha beta into the algorithm, certainly not necessarily writing code for me either. maybe it was my unclear question. ill edit it –  namehere Nov 19 '12 at 0:56
    
@idiotretard (such an odd name): your question seems to fit on StackOverflow. HTH –  ANeves Nov 19 '12 at 8:54
1  
... i initially posted this on stack overflow, got told to move it here, and got closed there... wth should i do then!? –  namehere Nov 19 '12 at 10:28
    
"What kind of questions can I ask here? Code Review - Stack Exchange is for sharing code from projects you are working on for peer review. If you are looking for feedback on a specific working piece of code from your project in the following areas… Best practices and design pattern usage Security issues Performance Correctness in unanticipated cases then you are in the right place!" –  namehere Nov 19 '12 at 10:32
show 4 more comments

1 Answer

up vote 1 down vote accepted

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.
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.