View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# For loop doesn't work as expected

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

10 replies to this topic

### #1Jaap85  Members

Posted 25 May 2012 - 01:27 AM

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 first game: Tycoon!

### #2StratfordCanning  Members

Posted 25 May 2012 - 03:17 AM

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

### #3StratfordCanning  Members

Posted 25 May 2012 - 03:22 AM

Just looked at your link to the complete code - I think I was talking nonsense. Apologies!

### #4ColinDuquesnoy  Members

Posted 25 May 2012 - 06:59 AM

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 !!!
}


### #5BCullis  Members

Posted 25 May 2012 - 12:34 PM

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, 25 May 2012 - 12:36 PM.

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

### #6jwezorek  Members

Posted 25 May 2012 - 01:09 PM

You need to show more code. How is recursion terminated? Where is MoveList updated? etc.

Edited by jwezorek, 25 May 2012 - 01:10 PM.

### #7BeerNutts  Members

Posted 25 May 2012 - 02:57 PM

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, 25 May 2012 - 02:58 PM.

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)

### #8BCullis  Members

Posted 25 May 2012 - 09:13 PM

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)

### #9Jaap85  Members

Posted 26 May 2012 - 10:12 AM

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 first game: Tycoon!

### #10jwezorek  Members

Posted 26 May 2012 - 11:51 AM

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

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?

### #11Jaap85  Members

Posted 27 May 2012 - 08:02 AM

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

My first game: Tycoon!