# 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... :<)! 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);
}
}
}

-
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
... 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

• Never use less than 3 characters for variable names... le? r? mv?!? (int i in for cycles is an exception because it is idiomatic);
• Don't repeat code (var le = legals(bd); while (le.Any()) { ... }), extract it to a method.