

/**** Don't need move_lis, it's not used ****/

Best_Move  minmax_AB (char b_state[5][5],move_t move_lis,
			int cur_depth,  int player,
              		int alpha_max,int beta_min) 

{
int best_score = 0;	/*** Make sure this is local, NOT global **/
  int i = 0, count;	// best_score; 
  Best_Move result ;
  move_t M,best_move;
  move_t move_list[45]; 
  int	O = PLAYER_2;    
/*** Need a temporary board , uncommented this one ***/
  char temp_state[5][5];

  if (cur_depth == DESIRED_DEPTH){  // return heuristic(b_state)NULL;
	result.thresh = heuristic (b_state, player);
       
  	result.move.srcX = NULL;
  	result.move.srcY = NULL;
 	 result.move.destX = NULL;
 	 result.move.destY = NULL;
	return result;    // Return best move
	} 

 count = Possible_MoveGen(b_state,move_list,player);// Count & gather possible moves
	fprintf(stderr, "count: %d, depth: %d\n", count, cur_depth);
//	fprintf(stderr, "move_list[%d].srcX: %d\n", 0, move_list[0].srcX);  
//	fprintf(stderr, "move_list[%d].srcY: %d\n", 0, move_list[0].srcY);  
  best_move.srcX = NULL; 
  best_move.srcY = NULL; 
  best_move.destX = NULL; 
  best_move.destY = NULL; 

/* Make sure you return the right result ? Need to return - or + 
   depending on who is making the move? */
  if ((LIsWinner (b_state, player)) == TRUE)
		return result;

  i = count ;

while(i > 0 ) { // Does for loop job  
    fprintf(stderr,"enter while,  i=%d  player is %d\n",i, player);
/***** Try using this modified memcpy here to copy the board
       to a temporary board ****/
	memcpy ((char *) temp_state[0],(char *) b_state[0],
                         5*5*sizeof(char));
  	ApplyMove (temp_state, move_list[i],player);
	result = minmax_AB(temp_state, move_list[i],cur_depth+1 ,
	    opponent(player),  alpha_max, beta_min);  

	M = move_list[i];		// Store move information
        fprintf(stderr, "m.destX: %d,m.destY  %d\n", move_list[i].destX, move_list[i].destY);

	i--;
	fprintf (stderr, "result.thresh: %d, alpha_max %d, beta_min %d\n",
		result.thresh,alpha_max,beta_min);

	 // If I am O, prune check for O.  

	if (player == MYTURN && player == O)  result.thresh *= -1;   


	if(player == MYTURN) { 
		fprintf(stderr,"MYTURN\n");  
    		if(result.thresh > alpha_max) {
      			alpha_max = result.thresh;
      			best_move = M;
      			best_score = result.thresh;
    		fprintf(stderr, "best_move.destX: %d\n",best_move.destX);	
    		fprintf(stderr, "best_move.destY: %d\n",best_move.destY);	
		}
    		if(alpha_max >= beta_min) {
      			result.thresh = beta_min;
        		result.move = best_move; 
		fprintf(stderr,"result.move.destX: %d\n",result.move.destX);
		fprintf(stderr,"result.move.destY: %d\n",result.move.destY);
   			return  result;
     	}}
	else 
	if(player == HISTURN) { 
		fprintf(stderr, "HISTURN\n");   
   		if(result.thresh < beta_min) {
      			beta_min = result.thresh;
      			best_move = M;
      			best_score = result.thresh;
    		}
    		if(alpha_max <= beta_min) {
        		result.thresh = alpha_max;
        		result.move = best_move;    
		fprintf(stderr,"result.move.destX: %d\n",result.move.destX);
		fprintf(stderr,"result.move.destY: %d\n",result.move.destY);
    			return  result;
    	}}
/*	 result.thresh = best_score;
	result.move = best_move;    
	return  result;  
 */      
       
	fprintf(stderr, "at end while loop bracket\n"); 
  } // end while loop
	result.thresh = best_score;
	result.move = best_move;    
	return  result;


} // end minmax
