Click to See Complete Forum and Search --> : using recursion to find possible moves in a checkers game


Illbred
April 8th, 2008, 03:37 PM
Hi all, I am attempting to write a neural-network backed program that will learn and play checkers. Everything has been going peachy so far, but when I decided to implement an Alpha-Beta search algorithm, i realized that I needed a function to look at all possible moves at a certain point in the game. I thought that using recursion would be the best way to do this, since you need to go through every possible move on the board. I also wanted to return the board state (represented as a 1d array), but that is where i ran into a problem.


//returnAllPosMoves fnc
//****UNDER CONSTRUCTION****\\
//returns every possible board position at point in game.
//@ turn: bool to determin turn; true = red, false = black.
int *returnAllPosMoves(bool turn, int placeInLookUp)
{
int V = 0;
int i = placeInLookUp;
bool off;

//BASE CASE: i==40
if(i==40)
return boardRep;

//CASE: red turn
else if(turn == true)
{
//is offset
if(rowOffset(i))
{
//check move one
if(V=validMove(i,i+4,true) == 1)
{
move(i,i+4);
return boardRep;
}
//check move two
if(V=validMove(i,i+5,true) == 1)
{
move(i,i+5);
return boardRep;
}

}
//not offset
else
{
//check move one
if(V=validMove(i,i+5,true) == 1)
{
move(i,i+5);
return boardRep;
}
//check move two
if(V=validMove(i,i+6,true) == 1)
{
move(i,i+6);
return boardRep;
}
}
}

//CASE: black turn
else
{
//is offset
if(rowOffset(i))
{
//check move one
if(V=validMove(i,i-6,false) == 1)
{
move(i,i-6);
return boardRep;
}
//check second move
if(V=validMove(i,i-5,false) == 1)
{
move(i, i-5);
return boardRep;
}
}
//not offset
else
{
//check move one
if(V=validMove(i,i-5,false) == 1)
{
move(i,i-5);
return boardRep;
}
//check second move
if(V=validMove(i,i-4,false) == 1)
{
move(i, i-4);
return boardRep;
}
}
}
}


The problem is that I am not sure where to put the recursion. Since hitting a return statement exits a function, I figure it should be beforehand, but then it would never finish. Anybody have any ideas to help me out?

PS. move() and validMove() are functions i built in; the move() fnc moves a piece, and validMove() checks to make sure the move is not out of bounds/moving on another peice.

treuss
April 9th, 2008, 06:14 AM
I'm having problems understanding what you are doing in the function. You are returning "boardRep" which is not declared. Should I assume that it is declared globally? Why to return a global variable - the calling function should have access to it anyways.

To help you with your question: To use recursion, you will usually pass the current board status to the recursive function by value, meaning it should be copied for each call so that the previous state is retained in the calling function.