For loop doesn't work as expected

Started by
9 comments, last by Jaap85 11 years, 10 months ago
Hello everybody,

I am currently trying to create a recursive function, but i found out that it isn't working and i dont really understand why this happens or how i could fix it.

I use the following function:



public AIMove RecursiveMiniMaxAI(int Depth, int MaxDepth, gamestate StateOfBoard, int Player)
{
//Some general code

if (Player == 2)
{
for (int i = 0; i < MoveList.Count; i++)
//foreach (int i in MoveList)
{
//Some code

//Recursive function
ReturnMove = RecursiveMiniMaxAI(Depth + 1, MaxDepth, AIStates[Depth + 1, j], 1);

//Some code

}
}
else
{
for (int k = 0; k < MoveList.Count; k++)
//foreach (int k in MoveList)
{
//Some code

//Recursive function call
ReturnMove = RecursiveMiniMaxAI(Depth + 1, MaxDepth, AIStates[Depth + 1, j], 2);

//Some code
}
}

//Some code

return ReturnMove;
}


The problem is that both for loops only loop once. Even if i or k are 3,4 or 5, the loop runs once for i =0 and then doesn't loop again. This happens with both the for statement and the foreach statement.

If i remove the recursive function call, the loop runs just fine, so it must have something to do with that.

I would be really happy if anyone could point me in the right direction.

PS. i already posted this topic in the Artificial Intelligence forum, but since this doesn't really concern AI anymore I als posted it here.

For reference, here is the link to my older post with my complete code in it.

http://www.gamedev.net/topic/622037-connect-four-ai/

My personal blog on game development!

Black Wolf Game Development

Advertisement
Do you need to set the count to something BEFORE recursing, maybe it is getting stamped on somewhere else?

eg

public AIMove RecursiveMiniMaxAI(int Depth, int MaxDepth, gamestate StateOfBoard, int Player)
{
//Some general code

int MoveList_Count = MoveList.Count;

if (Player == 2)
{
for (int i = 0; i < MoveList_Count; i++)
//foreach (int i in MoveList)
{
//Some code

//Recursive function
ReturnMove = RecursiveMiniMaxAI(Depth + 1, MaxDepth, AIStates[Depth + 1, j], 1);

//Some code
Just looked at your link to the complete code - I think I was talking nonsense. Apologies!
You have infinite recursivity.


void recursive(int player)
{
if( player == 2)
recursive(1); // will fall into the else case
else
recursive(2); // will fall into the if player == 2 case
// When are you stopping the recursivity ???
}

// should be something like this
void recursive(int player)
{
if(player == 1 )
recursive(2)
else
if( player == 2 )
recursive( -1); // next call will fall into the return case
else
return; // stop recursivity here !!!
}

You have infinite recursivity.


With the code in its current snipped state, yes. I'm guessing the "maxdepth" value is supposed to be the stopping point, as each recursion is one 'level' deeper.

edit: yes, it does check for maxdepth, so no worries about infinite recursion.

Hazard Pay :: FPS/RTS in SharpDX (gathering dust, retained for... historical purposes)
DeviantArt :: Because right-brain needs love too (also pretty neglected these days)

You need to show more code. How is recursion terminated? Where is MoveList updated? etc.
As other have said you have infinite recursion. You should check if Depth == MaxDpeth 1st, then return

public AIMove RecursiveMiniMaxAI(int Depth, int MaxDepth, gamestate StateOfBoard, int Player)
{
//Some general code
if (MaxDepth == Depth) {
return;
}
// Continue
...
}



Hello everybody,

I am currently trying to create a recursive function, but i found out that it isn't working and i dont really understand why this happens or how i could fix it.

I use the following function:

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

The full code is through the link: http://www.gamedev.net/topic/622037-connect-four-ai/

They do check for maxdepth first. It's not an infinite recursion issue.

Hazard Pay :: FPS/RTS in SharpDX (gathering dust, retained for... historical purposes)
DeviantArt :: Because right-brain needs love too (also pretty neglected these days)

Thanks everyone for the responses. In my opinion, infinite recursion is not the issue, especially because the loop does finish (so it is not infinite). The problem occurs with the for statement. When this statement has run once, it doesn't start over again, although it does run over again when i remove the recursive function call. Still don't know how to solve this though :(

My personal blog on game development!

Black Wolf Game Development


Thanks everyone for the responses. In my opinion, infinite recursion is not the issue, especially because the loop does finish (so it is not infinite). The problem occurs with the for statement. When this statement has run once, it doesn't start over again, although it does run over again when i remove the recursive function call. Still don't know how to solve this though sad.png

The recursive call must be clearing or removing items from MoveList. Can't tell from what you posted.Where is MoveList defined, anyway? -- Is it a global variable?

This topic is closed to new replies.

Advertisement