I am developping the AI for Tic Tac Toe. At easy dificulty i got something like(when AI must place 0) :
CheckForWinPosibility()->CheckIfPlayerCanWin->ChooseARandomButton().
The problem is that sometimes(not often , something like 1 in 30, more or less) i get the "Program has stopped running" and i debugged it with VS and it appears that i get a Stack Overflow.
I belive that i get that Stack Overflow because of the random function. I got 2 variables : x=randmom.next(1,4); y=random.next(1,4) that give me the index for my random button (a[x,y]).
if(a[x,y]==0)//is free
instructions
else
ChooseARandomButton()// try again untill i get a free random button.
And i thought that i get stackoverflow because let's say if i got only 2 free buttons of nine it will take a while until it will return a free button and could lead to a stackoverflow. That's why I changed the code a little and now if i got only 2 free buttons it will mark the first one else if i got more than 2 free buttons it will mark one using the random function. But i just got that error again with6 free buttons!
I still think it's because of the random function but i'm not sure. But i don't have any other loops so ..
How can I change the ChooseARandomButton() function from { x=randmom.next(1,4); y=random.next(1,4); if(a[x,y]==0) instructions else ChooseARandomButton()} to something like
get all the free buttons and put them in an array//this would be no problem
Choose a random number from the numbers i got in the array
something like v={1,4,7,8}; x=Random(v)//return a random number that can be 1,4,7 or 8.