• Advertisement
Sign in to follow this  

For loop doesn't work as expected

This topic is 2068 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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/

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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 !!!
}

Share this post


Link to post
Share on other sites

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. Edited by BCullis

Share this post


Link to post
Share on other sites
You need to show more code. How is recursion terminated? Where is MoveList updated? etc. Edited by jwezorek

Share this post


Link to post
Share on other sites
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:

Edited by BeerNutts

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 :(

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
Share on other sites
All right, this is the point where i feel relieved but stupid :)

I finally got the code to work and also realised which mistakes i made:

1. When using the "foreach int k in MoveList" statement, i failed to adapt MoveList[k] to k, which makes sure the program goes in an endless loop.

2. I somehow overlooked the fact that the for loop was indeed connecting when debugging, I really don't understand it since i checked it a few dozen times before posting here, but in the end it appeared to work and since i didnt change anything in my code, i really must have overlooked it.

Anyhow, i got the code working and also fixed another mistake which caused the bad results to appear. Thank you very much again for helping me learn!


Now on to the next problem, which should be easier to solve. My girlfriend hates the fact that the stones in my Connect Four game just appear instead of falling down nicely, so i will try to fix that now :)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement