Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: Beginning Chess Program. Need simple example of search Algorithm

Author: Nicolas Carrasco

Date: 17:12:36 12/25/99

Go up one level in this thread


I LOOKED THE SAME THAN YOU WHEN I WAS A BEGINNER!

Try this:


int minmax(int depth)
{
int x;
int score,best;

if(depth == 0)
  return (static_eval());

best = -INFINITE;

/* this function should generate all legal moves and sometimes some
   ilegal moves because it doesn't check if your king is in CHECK after
   doing it*/
generate_phseudo_legal_moves();

  for(x=0;x<number_of_phseudo_legal_moves;x++)
  {
    /* make_move() checks if you are in check after doing the move.
       if after doing the move you are in check it unmakes the move automaticaly
       and returns 0. If not returns 1.

       So if the "if" condition it haven't been done the computer realized that
 it is     an ILEGAL MOVE*/
    if(make_move(move[x]))
    {
    score = minmax(depth-1);
    unmake_move();

    if(score>best)
      best = score;
    }
  }

return (best);
}

With the last function you won't know what is the best move, so I add this:

int search(int depth)
{
int x;
int score,best;
int best_move_number;

if(depth == 0)
  return (static_eval());

best = -INFINITE;

generate_phseudo_legal_moves();

  for(x=0;x<number_of_phseudo_legal_moves;x++)
  {
    if(make_move(move[x]))
    {
    score = minmax(depth-1);
    unmake_move();

    if(score>best)
      {
      best_move_number = x;
      best = score;
      }
    }
  }
return (best_move_number);
}

void main(void)
{
  int best_move_number;
  best_move_number = search(4);
  generate_phseudo_legal_moves();
  make_move(moves[best_move_number]);
}

Hope that this helps. :)



This page took 0 seconds to execute

Last modified: Thu, 15 Apr 21 08:11:13 -0700

Current Computer Chess Club Forums at Talkchess. This site by Sean Mintz.