For a C # AI program, I use a recursive call to find the best next move (using a 30x30 array to store the current state of the board). For each step that I take, I want to see which of the possible steps that I can take from the new state of the board will be better ... and so on, until I reach the end of the game position (no further steps in this state) or a timer stops the process, and further recursive calls are not made (and the "best" known position is returned). This just explains why I should use recursion (this is not tail recursion), and I cannot use one (global) state of the board, but I must look for all possible states of the board from the current state.
(Sometimes) I get a System.StackOverflowException. Is there a way to check the available stack space before the next recursive call? Then I could simply return the current state as βthe best position so far foundβ and not make the next recursive call. That is, when the available stack gets too small, it should also be considered basic.
Another option, of course, could be to simply put each recursive call into a try..catch block and throw a System.StackOverflowException, using it as a base case?
stack c # stack-overflow artificial-intelligence recursion
Chavoux luyt
source share