Original Post
I have a recursive function (source below) which is crashing when it returns sometimes. I'm completely stumped as to why it's crashing. The ouput it produces is listed below as well EDIT: Code has been revised, and output to match it. EDIT2: I've fixed the problem. Code: Output: Have I missed something? Can anyone identify where I've gone wrong and why it's crashing. [Edited by - Alexistran on March 12, 2007 11:36:33 AM]
int BoardImp::futureSight(BoardImp tempb, int colour, int pegN, int forwardCount)
{
int moveScoresT[16]; // stores the move scores for this level
for (int c = 0; c < 16; c++) // going to be using += so need to
moveScoresT[c] = 0; // make sure it's set to 0
cout << "FC = " << forwardCount << endl;
// ------------------- control colour -----------------------------//
// 3 moves will be simulated.
// colour passed (forwardCount = 0)
// opposite colour (forwardCount = 1)
// colour passed (forwardCount = 2)
if (forwardCount != 0)
colour = oppColour(colour);
// ------------------- end control colour -------------------------//
// ------------------- control depth ------------------------------//
forwardCount++;
// ------------------- end control depth ---------------------------//
// ------------------- simulate move -------------------------------//
// make the move
string location = peg[pegN].GetNext();
tempb.updateBoard(location,colour);
int a = 0;
for (int i = 0; i < 16; i++)
{
cout << "i="<<i<<endl;
// no need to check forwardCount == 1 as thats done before this has even been called once
int s = tempb.offDefMove(i,colour); // check if a win can be made
if ((s >= 1000) && (forwardCount == 2)) // opponent can win
return -1000000; // this path found a winning move for opponent (DO NOT TAKE THIS PATH)
else if ((s >= 1000) && (forwardCount == -1)) // we can win
moveScoresT += 1000; // this path found a winning move for us (offset against opponent blocking)
else if ((s > 0 ) && (s < 1000) && (forwardCount == 2)) // opponent can block
moveScoresT += -1000; // this path found a blocking move for opponent (offset against us finding a win)
else if ((s > 0 ) && (s < 1000) && (forwardCount == -1)) // we can block a win
moveScoresT += 100;
// ------------------- recursive step ------------------------------//
// call function again
cout << "Forward Count before = " << forwardCount << endl;
cout << "moveScoresT[" << i << "] = " << moveScoresT << endl;
if (forwardCount == 3)
a = 10;
else
{
a = futureSight(tempb, colour,i,forwardCount); // call recursively
//forwardCount--;
cout << "Forward Count after = " << forwardCount << endl;
}
cout << "A="<<a<<endl;
moveScoresT += a;
cout << "moveScoresT[" << i << "] = " << moveScoresT << endl;
// ------------------- end recursive step -------------------------//
}
// ------------------- end simulate move ---------------------------//
// ------------------- calculate return score ---------------------//
// After recursion (where its returning)
int highest = 0;
for (int k = 0; k < 16; k++)
{
// search through moveScoresT for the highest value
// then return that
if (moveScoresT[k] > moveScoresT[highest])
highest = k;
}
cout << "highest = " << highest << endl;
return moveScoresT[highest];
// ------------------- end calculate return score -----------------//
}
FC = 0
i=0
Forward Count before = 1
moveScoresT[0] = 0
FC = 1
i=0
Forward Count before = 2
moveScoresT[0] = 0
FC = 2
i=0
Forward Count before = 3
moveScoresT[0] = 0
A=10
moveScoresT[0] = 10
i=1
Forward Count before = 3
moveScoresT[1] = 0
A=10
moveScoresT[1] = 10
i=2
Forward Count before = 3
moveScoresT[2] = 0
A=10
moveScoresT[2] = 10
i=3
Forward Count before = 3
moveScoresT[3] = 0
A=10
moveScoresT[3] = 10
i=4
Forward Count before = 3
moveScoresT[4] = 0
A=10
moveScoresT[4] = 10
i=5
Forward Count before = 3
moveScoresT[5] = 0
A=10
moveScoresT[5] = 10
i=6
Forward Count before = 3
moveScoresT[6] = 0
A=10
moveScoresT[6] = 10
i=7
Forward Count before = 3
moveScoresT[7] = 0
A=10
moveScoresT[7] = 10
i=8
Forward Count before = 3
moveScoresT[8] = 0
A=10
moveScoresT[8] = 10
i=9
Forward Count before = 3
moveScoresT[9] = 0
A=10
moveScoresT[9] = 10
i=10
Forward Count before = 3
moveScoresT[10] = 0
A=10
moveScoresT[10] = 10
i=11
Forward Count before = 3
moveScoresT[11] = 0
A=10
moveScoresT[11] = 10
i=12
Forward Count before = 3
moveScoresT[12] = 0
A=10
moveScoresT[12] = 10
i=13
Forward Count before = 3
moveScoresT[13] = 0
A=10
moveScoresT[13] = 10
i=14
Forward Count before = 3
moveScoresT[14] = 0
A=10
moveScoresT[14] = 10
i=15
Forward Count before = 3
moveScoresT[15] = 0
A=10
moveScoresT[15] = 10
highest = 0
Forward Count after = 2
A=10
moveScoresT[0] = 10
i=1
Forward Count before = 2
moveScoresT[1] = 0
FC = 2
i=0
Forward Count before = 3
moveScoresT[0] = 0
A=10
moveScoresT[0] = 10
i=1
Forward Count before = 3
moveScoresT[1] = 0
A=10
moveScoresT[1] = 10
i=2
Forward Count before = 3
moveScoresT[2] = 0
A=10
moveScoresT[2] = 10
i=3
Forward Count before = 3
moveScoresT[3] = 0
A=10
moveScoresT[3] = 10
i=4
Forward Count before = 3
moveScoresT[4] = 0
A=10
moveScoresT[4] = 10
i=5
Forward Count before = 3
moveScoresT[5] = 0
A=10
moveScoresT[5] = 10
i=6
Forward Count before = 3
moveScoresT[6] = 0
A=10
moveScoresT[6] = 10
i=7
Forward Count before = 3
moveScoresT[7] = 0
A=10
moveScoresT[7] = 10
i=8
Forward Count before = 3
moveScoresT[8] = 0
A=10
moveScoresT[8] = 10
i=9
Forward Count before = 3
moveScoresT[9] = 0
A=10
moveScoresT[9] = 10
i=10
Forward Count before = 3
moveScoresT[10] = 0
A=10
moveScoresT[10] = 10
i=11
Forward Count before = 3
moveScoresT[11] = 0
A=10
moveScoresT[11] = 10
i=12
Forward Count before = 3
moveScoresT[12] = 0
A=10
moveScoresT[12] = 10
i=13
Forward Count before = 3
moveScoresT[13] = 0
A=10
moveScoresT[13] = 10
i=14
Forward Count before = 3
moveScoresT[14] = 0
A=10
moveScoresT[14] = 10
i=15
Forward Count before = 3
moveScoresT[15] = 0
A=10
moveScoresT[15] = 10
highest = 0
Press any key to continue . . . << Crashes here