Thread: recursive function problems

  1. #1
    Registered User
    Join Date
    Dec 2003
    Posts
    6

    recursive function problems

    Hi there,
    I am writing a game called othello. This game is played by the computer and the computer user. The game is played on a board.
    The player can choose between white and black color. Where blacks starts first. It's an assignment for college.

    I have added the complete source code.
    I am having problems with this recursive function:
    Code:
    int O_bord::eval(int diepte, int & i, int & j, bool spelersbeurt){
      
      int stand = 0; // voor de verschilfunctie
      int c, d; // variabelen voor de coordinaten en de for-lussen
      int voorlopigestand = 0; //om de waarde van een stand te retourneren
      int spelerzwart = -100; //warde voor speler met kleur zwart 
      int spelerwit = 100; //waarde voor speler met kleur wit
      
      //basis geval als diepte de waarde 0 bereikt
      if (diepte == 0 || speelcontrolle()){
         
         stand = verschil(); //de stand wordt geretourneerd
         
         return stand;      
      
      }//if
      
      //creert een nieuwe object O_bord om mee te werken
      O_bord b(hoogte, breedte, kleurspeler);
      b = *this;
      
        
      //cout << "De kopie wordt afgedrukt" << endl;
      //b.drukaf();//drukt af het nieuwe bord
       
      //als de diepte nog niet nul is
      for (c = 0; c < hoogte; c++){
        for(d = 0; d < breedte; d++){
          if (toegestanezet(c,d,spelersbeurt)){
            
    	//cout << "Er werd een gezet op de nieuwe kopie" << endl;
    	b = *this; //reset van het nieuwe bord
    	
    	b.doezet(c,d,spelersbeurt); // een zet wordt uitgevoerd op de nieuwe object
    	
    	//b.drukaf(); ////drukt af het nieuwe bord
    	
    	//b.spelersbeurt = !spelersbeurt; // beurten worden geswicht
    	spelersbeurt = true; // beurten worden geswicht
    	
    	
    	voorlopigestand = b.eval((diepte -1), i, j, spelersbeurt); //hier wordt dieper gekeken
    	
    	//enkele statistieken
    	cout << "diepte: " << diepte << " c: " << c << " d: " << d << " i: " << i << 
    	     " j: " << j <<" voorlopigestand: " << voorlopigestand << endl;
    	
    	spelersbeurt = false; //beurten worden geswicht
    	
    	//================================//
    	//als de computer met zwart speelt//
    	//================================//
    	if (computersgetal == 1){ 
    	  
    	  //als de computer aan de beurt is
    	  if(!spelersbeurt){
    	  
    	    //de voorlopige beste zet wordt opgeslagen
    	    if (voorlopigestand >= spelerwit){
    	      i = c; 
    	      j = d;
    	      spelerwit = voorlopigestand;
    	    }//if
    	    
    	  }//if
    	  
    	  //als de computer niet aan de beurt is
              else {
    	  
    	    if (voorlopigestand <= spelerzwart) {
    	      i = c; 
    	      j = d;
    	      spelerzwart = voorlopigestand;
    	    }//if
    	    
    	  }//else  
    	
    	}//if
    	
    	//==============================//
    	//als de computer met wit speelt//
    	//==============================//
    	if (computersgetal == 2){
    	  
    	  //als de computer aan de beurt is
    	  if (!spelersbeurt){
    	    
    	    if (voorlopigestand <= spelerzwart){
    	      i = c; 
    	      j = d;
    	      spelerzwart = voorlopigestand;
    	    }//if
    	    
    	  }//if
    	  
    	  //als de computer niet aan de beurt is
    	  else {
    	    
    	    if (voorlopigestand >= spelerwit){
    	      i = c; 
    	      j = d;
    	      spelerwit = voorlopigestand;
    	    }//if
    	  
    	  }//else
    	}//if
             
          }//if
        }//for
      }//for
    	
      
      //geef aan welke waarden geretourneerd moeten worden
      if (computersgetal == 1){
        return spelerzwart;
      }
      else {
        return spelerwit;
      }
    
    }//othello::eval
    
    
    
    void O_bord::verstandigezet(bool spelersbeurt, int diepte){
      
      int x; // wordt toegekend aan de functieaanroep eval
      int i,j; //coordinaten voor de functie  
      // de recursieve functie moet dan de bestezet bepalen
      x = eval (diepte, i, j, spelersbeurt);
    	 
      cout << "i: " << i << "j: " << j << " binnen verstandigezet " << endl;
      // voert uit de verstandigezet voor de computer
      
      //checkt eerst of de coordinaten correct zijn zodat de variabelen van die zet true worden in toegestanezet
      if (toegestanezet(i,j,spelersbeurt)) {
        doezet(i, j, spelersbeurt); 
      }//if     
    }//O_bord::verstandigezet
    Some way I don't get the right coordinates passed to doezet(i,j, spelersbeurt)

    So if anyone can give me some comments. I'll appreciate it.
    If you don't understand, because of the comments
    written in dutch, let me now.

    Thanks

  2. #2
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    I think we should make English as the standard for variable/function naming. This is just like reading an assembly language
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  3. #3
    Registered User
    Join Date
    Jan 2004
    Posts
    4
    What doesn't work (versta je code wel), works all fine in here (didn't notice a thing).

    Maybe you swapped height and width

  4. #4
    Registered User
    Join Date
    Dec 2003
    Posts
    6

    killgore

    To Killgore.

    The problem we have now is that when we try the option verstandig zetten and diepte e.g 2, so the computer sees the status of the game two turns ahead and decides what the best i and j coordinates are to make a move. But the function eval() doens't pass the correct coordinates i and j to the doezet function in verstandigezet().

    when you try the option verstandig and diepte 2 you will see that the computer doesn't make a move at all.

    Can you please take a look a it.

    thanks

    Jonathan

  5. #5
    Registered User tu_user's Avatar
    Join Date
    Jan 2004
    Posts
    36

    Lightbulb

    I agree with alphaoide.

  6. #6
    Registered User
    Join Date
    Dec 2003
    Posts
    6

    english version of recursive function

    Hi there,
    this is the english version of my recursive function:
    Code:
    //=======================================================================================//
    //                      function which finds the best computer's move  		         //
    //=======================================================================================//
    int O_board::eval(int depth, int & i, int & j, bool playersturn){
      
      int score = 0; // for the difference() fucntion
      int c, d; // variables needed for the for-loops
      int tempscore = 0; //to store the value of the temporary score 
      int playerblack = -100; //value for the player with black color 
      int playerwhite = 100; //value for the player with white color
      
      //basic case if depth reaches value zero
      if (depth == 0 || gamecontrol()){
         
         score = difference(); //returns the score
         
         return score;      
      
      }//if
      
      //creates a new object O_board to work with
      O_board b(height, width, playerscolor);
      b = *this;
      
        
      //cout << "The copy is printed" << endl;
      //b.print();//prints the new board
       
      //if depth hasn't reached value zero
      for (c = 0; c < height; c++){
        for(d = 0; d < width; d++){
          if (move_permitted(c,d, playersturn)){
            
    	//cout << "A move was made on the board" << endl;
    	b = *this; //resets the new board 
    	
    	b.executemove(c,d,playersturn); // a move is executed on the new board
    	
    	//b.print(); ////prints the new board	
    	
    	playersturn = true; // turns are switched between computer and player
    	
    	
    	tempscore = b.eval((depth -1), i, j, playersturn); //The computer looks further more moves
    	
    	//some statistics
    	cout << "depth: " << depth << " c: " << c << " d: " << d << " i: " << i << 
    	     " j: " << j <<" tempscore: " << tempscore << endl;
    	
    	playersturn = false; // turns are switched between player and computer
    	
    	//================================//
    	//if the computer plays with black//
    	//================================//
    	if (computersnumber == 1){ 
    	  
    	  //if it's the computer's turn
    	  if(!playersturn){
    	  
    	    //stores the temporary best score
    	    if (tempscore >= playerwhite){
    	      i = c; 
    	      j = d;
    	      playerwhite = tempscore;
    	    }//if
    	    
    	  }//if
    	  
    	  //if it's not the computer's turn
              else {
    	  
    	    if (tempscore <= playerblack) {
    	      i = c; 
    	      j = d;
    	      playerblack = tempscore;
    	    }//if
    	    
    	  }//else  
    	
    	}//if
    	
    	//==============================//
    	//if the computer plays with white//
    	//==============================//
    	if (computersnumber == 2){
    	  
    	  //if it's computer's turn
    	  if (!playersturn){
    	    
    	    if (tempscore <= playerblack){
    	      i = c; 
    	      j = d;
    	      playerblack = tempscore;
    	    }//if
    	    
    	  }//if
    	  
    	  //if it's not the computer's turn
    	  else {
    	    
    	    if (tempscore >= playerwhite){
    	      i = c; 
    	      j = d;
    	      playerwhite = tempscore;
    	    }//if
    	  
    	  }//else
    	}//if
             
          }//if
        }//for
      }//for
    	
      
      //decides which value should be returned
      if (computersnumber == 1){
        return playerblack;
      }
      else {
        return playerwhite;
      }
    
    }//othello::eval
    
    
    //=======================================================================================//
    //                      function which executes a smart move on board    	         //
    //=======================================================================================//
    void O_board::smart_move(bool playersturn, int depth){
      
      int x; // stores the value returned by eval
      int i,j; //coordinates for the fucntion eval  
      
      // calling of the recursive fucntion which determined the best move for the computer
      x = eval (depth, i, j, playersturn);
    	 
      cout << "i: " << i << "j: " << j << " inside smart_move " << endl;
      
      //checks first if the coordinates a permitted move are
      // then exexutes the move on the real board
      if (move_permitted(i,j,playersturn)) {
        executemove(i, j, playersturn); 
      }//if     
    }//O_bord::smart_move
    The problem we have now is that when we try the option smart_move and depth e.g 2, so the computer sees the status of the game two turns ahead and decides what the best i and j coordinates are to make a move. But the function eval() doens't pass the correct coordinates i and j to the executemove() function in smart_move().

    when you try the option verstandig and diepte 2 you will see that the computer doesn't make a move at all.

    Can you please take a look a it.

    thanks

    Jonathan

  7. #7
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    I don't know, dude. A combination of recursive function and loops blows my head. Do you know how to debug? Trace the value of i and j and find where they return the incorrect values.
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  5. C++ compilation issues
    By Rupan in forum C++ Programming
    Replies: 1
    Last Post: 08-22-2005, 05:45 AM