Practices
  Home arrow Practices arrow Page 8 - Solving Problems with Recursion
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Actuate Whitepapers 
VeriSign Whitepapers 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PRACTICES

Solving Problems with Recursion
By: Mohamed Saad
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 32
    2004-11-17

    Table of Contents:
  • Solving Problems with Recursion
  • The Plot Thickens
  • First Programming Example
  • The Flood Fill Algorithm
  • How it Works
  • Connect 4 AI program
  • The Minimax Method
  • Placing Pieces
  • Trying it Out
  • Two More Tips

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    Solving Problems with Recursion - Placing Pieces


    (Page 8 of 10 )

    Now what? We make a loop over all 7 columns and try to put a piece in each place. We try to put a piece in column #i if it has an empty slot.

    After trying to put a piece we call a function called value() to tell us about the value of this position. The function value(), which we are going to cover in a moment, is given the current situation, and gives us a score that tells us how well this position is for the computer--higher is better, of course.

    We call this function first to check for a game over. If it returns +100 the computer has won, or if it returns -100 the human player has won (this is how we are going to program it). In both cases, we don't need to continue trying further moves. The game is over.

    So, we check if the return from this function was +100 or -100 or if we have reached the end of our analysis (depth is equal to maxDepth) and we just take the value returned from the value() function.

    If not, we recursively call the bestMove() function to continue digging deeper into the tree of possibilities.

    In both cases, we check the return from the function against the best score found so far. If the new score is better than the best score found so far (i.e. it is larger than the best score if it is the computer's turn, or smaller than the best score if it is the human's turn), we replace the best score with the new score.

    Finally, we return the board to what it was by removing the piece we just put. We repeat again for the next column and so on…

    The Value Function

    Now, what about that value function? This function should tell us how good a position is from the point of view of the computer player. For the sake of simplicity, we will make a simple value function that just checks who is winning. Here it is…

    int value()
    {//get the value of a position

     //check for 4 in a row
     for(int i=0;i<5;i++)
      for(int j=0;j<7;j++)
       if(board[i][j]!=0&&board[i][j]==board[i+1][j]&&
       board[i][j]==board[i+2][j]&&board[i][j]==board[i+3][j])
        if(board[i][j]==1)
         return 100;
        else return -100;
        
     //check for 4 in a column
     for(int i=0;i<8;i++)
      for(int j=0;j<4;j++)
       if(board[i][j]!=0&&board[i][j]==board[i][j+1]&&
       board[i][j]==board[i][j+2]&&board[i][j]==board[i][j+3])
        if(board[i][j]==1)
         return 100;
        else return -100;
        
     //check for 4 in a diagonal
     for(int i=0;i<5;i++)
      for(int j=0;j<4;j++)
       if(board[i][j]!=0&&board[i][j]==board[i+1][j+1]&&
       board[i][j]==board[i+2][j+2]&&board[i][j]==board[i+3][j+3])
        if(board[i][j]==1)
         return 100;
        else return -100;
       
     //check for 4 in the reverse diagonal
     for(int i=3;i<8;i++)
      for(int j=0;j<4;j++)
       if(board[i][j]!=0&&board[i][j]==board[i-1][j+1]&&
       board[i][j]==board[i-2][j+2]&&board[i][j]==board[i-3][j+3])
        if(board[i][j]==1)
         return 100;
        else return -100;
     return 0;
    }

    As you see, the function just checks if there are 4 pieces in a row, column or diagonal, and if it is the computer's pieces, it returns +100, and if it’s the human's pieces it returns -100.

    More Practices Articles
    More By Mohamed Saad


       · Great article, looking forward to seeing more from you.
       · Must have been a loot of work.:))
       · I think the article is great and recursion is a very powerful technique to use when...
       · Excellent Article, looking for articles like these in the future.
       · have had a problem with my new ipod it keep running but does not play song that i...
     

       

    PRACTICES ARTICLES

    - Finishing the System`s Outlines
    - The System in So Many Words
    - Basic Data Types and Calculations
    - What`s the Address? Pointers
    - Design with ArgoUML
    - Pragmatic Guidelines: Diagrams That Work
    - Five-Step UML: OOAD for Short Attention Span...
    - Five-Step UML: OOAD for Short Attention Span...
    - Introducing UML: Object-Oriented Analysis an...
    - Class and Object Diagrams
    - Class Relationships
    - Classes
    - Basic Ideas
    - Choosing the Right Team
    - Trees





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway