CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Oct 2006
    Posts
    23

    using recursion to find possible moves in a checkers game

    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.

    Code:
    //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.

  2. #2
    Join Date
    Jan 2004
    Location
    Düsseldorf, Germany
    Posts
    2,401

    Re: using recursion to find possible moves in a checkers game

    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.
    More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity. --W.A.Wulf

    Premature optimization is the root of all evil --Donald E. Knuth


    Please read Information on posting before posting, especially the info on using [code] tags.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured