Example #1
0
void ParseGo(POS *p, char *ptr) {

  char token[180], bestmove_str[6], ponder_str[6];
  int pv[MAX_PLY];

  Timer.Clear();
  pondering = 0;

  for (;;) {
    ptr = ParseToken(ptr, token);
    if (*token == '\0')
      break;
    if (strcmp(token, "ponder") == 0) {
      pondering = 1;
    } else if (strcmp(token, "wtime") == 0) {
      ptr = ParseToken(ptr, token);
      Timer.SetData(W_TIME, atoi(token));
    } else if (strcmp(token, "btime") == 0) {
      ptr = ParseToken(ptr, token);
      Timer.SetData(B_TIME, atoi(token));
    } else if (strcmp(token, "winc") == 0) {
      ptr = ParseToken(ptr, token);
      Timer.SetData(W_INC, atoi(token));
    } else if (strcmp(token, "binc") == 0) {
      ptr = ParseToken(ptr, token);
      Timer.SetData(B_INC, atoi(token));
    } else if (strcmp(token, "movestogo") == 0) {
      ptr = ParseToken(ptr, token);
      Timer.SetData(MOVES_TO_GO, atoi(token));
    } else if (strcmp(token, "nodes") == 0) {
      ptr = ParseToken(ptr, token);
      Timer.SetData(FLAG_INFINITE, 1);
      Timer.SetData(MAX_NODES, atoi(token));
    } else if (strcmp(token, "movetime") == 0) {
      ptr = ParseToken(ptr, token);
      Timer.SetData(MOVE_TIME, atoi(token) );
    } else if (strcmp(token, "depth") == 0) {
      ptr = ParseToken(ptr, token);
      Timer.SetData(FLAG_INFINITE, 1);
      Timer.SetData(MAX_DEPTH, atoi(token));
    } else if (strcmp(token, "infinite") == 0) {
      Timer.SetData(FLAG_INFINITE, 1);
    }
  }

  Timer.SetSideData(p->side);
  Timer.SetMoveTiming();
  Think(p, pv);
  MoveToStr(pv[0], bestmove_str);
  if (pv[1]) {
    MoveToStr(pv[1], ponder_str);
    printf("bestmove %s ponder %s\n", bestmove_str, ponder_str);
  } else
    printf("bestmove %s\n", bestmove_str);
}
Example #2
0
int sBook::GetBookMove(sPosition *p, int canPrint, int *flagIsProblem) {
	int i;
	int floorVal = 0;
	int curVal   = 0;
	int bestVal  = 0;
	int choice   = 0;
	int maxFreq  = 0;
	int values[100];
	U64 localHash = GetBookHash(p);

	nOfChoices = 0;

	if (Data.isAnalyzing) return 0; // book moves aren't returned in analyse mode

	for (i = 0; i < nOfGuideRecords; i++ ) {
		if (guideBook[i].hash == localHash
		&& IsLegal(p, guideBook[i].move ) )
		{
			moves[nOfChoices]  = guideBook[i].move;
			if (guideBook[i].freq > 0 ) values[nOfChoices] = guideBook[i].freq + 20;
			else                        values[nOfChoices] = -1;
	       // we add 20, so that any move not marked as bad in the text-based
		   // opening book has a chance to be played
			nOfChoices++;
		}
	}

	if (nOfChoices) {

       srand(Timer.GetMS() );

	   if (canPrint) printf("info string ");
	   for (i = 0; i < nOfChoices; i++ ) {
		   
		   // display info about possible choices
		   if (canPrint) printf(" ");
		   MoveToStr(moves[i], testString);
		   printf( testString );
		   if (canPrint) { 
			   if (values[i] > 0 ) printf(" %d ", values[i] );
		       else                printf("? ");
		   }

           // pick move with the best random value based on frequency
		   if (values[i] > 0) curVal = 1 + rand() % values[i]; 
		   else curVal = -1;

		   if (curVal > bestVal) {
			   bestVal = curVal;
			   choice  = i;
		   }
	   }
	   if (canPrint) printf(" guide \n");

       return moves[choice];
	}

	return GetPolyglotMove(p, 1);
}
Example #3
0
void PvToStr(int *pv, char *pv_str) {

  int *movep;
  char move_str[6];

  pv_str[0] = '\0';
  for (movep = pv; *movep; movep++) {
    MoveToStr(*movep, move_str);
    strcat(pv_str, move_str);
    strcat(pv_str, " ");
  }
}
Example #4
0
void PrintMove(int move)
{
  char moveString[6];
  MoveToStr(move, moveString);
  printf("%s", moveString);
}
Example #5
0
Move StrToMove(const string& str, Position& pos)
{
  if (str.length() < 2) return 0;

  string s = str;
  size_t len = s.length();
  while ((len > 1) &&
         (s[len - 1] == '+' ||
         s[len - 1] == '#' ||
         s[len - 1] == '!' ||
         s[len - 1] == '?'))
  {
    s = s.substr(0, len - 1);
    --len;
  }
  if (len < 2) return 0;

  MoveList mvlist[256];
  GenAllMoves(pos, mvlist);

  // LONG NOTATION

  for (MoveList* ptr = mvlist; ptr->mv; ++ptr)
  {
    Move mv = ptr->mv;
    if (MoveToStr(mv) == s)
    {
      if (pos.MakeMove(mv))
      {
        pos.UnmakeMove();
        return mv;
      }
    }
  }

  // SHORT NOTATION
 
  COLOR side = pos.Side();
  PIECE piece;

  if (str.find("N") == 0)
    piece = NW | side;
  else if (str.find("B") == 0)
    piece = BW | side;
  else if (str.find("R") == 0)
    piece = RW | side;
  else if (str.find("Q") == 0)
    piece = QW | side;
  else if (str.find("K") == 0 || str == "O-O" || str == "O-O-O")
    piece = KW | side;
  else
    piece = PW | side;

  for (MoveList* ptr = mvlist; ptr->mv; ++ptr)
  {
    Move mv = ptr->mv;
    if (mv.Piece() != piece) continue;
    if (MoveToStrShort(mv, pos, mvlist) == s)
    {
      if (pos.MakeMove(mv))
      {
        pos.UnmakeMove();
        return mv;
      }
    }
  }

  return 0;
}
Example #6
0
int sBook::GetPolyglotMove(sPosition *p, int printOutput) {

   int bestMove  = 0;
   int bestScore = 0;
   int maxWeight = 0;
   int sumOfWeights = 0;
   int pos;
   polyglot_move entry[1];
   int move;
   int score;
   int values[100];
   U64 key = GetPolyglotKey(p);
   char moveString[6];

   nOfChoices = 0;

   if (bookFile != NULL && bookSize != 0) {
	  srand(Timer.GetMS() );

      for (pos = FindPos(key); pos < bookSize; pos++) {

         ReadEntry(entry,pos);
         if (entry->key != key) break;

         move = entry->move;
		 score = entry->weight;

		 // ugly hack to convert polyglot move to a real one
		 int fsq = Tsq(move);
		 int tsq = Fsq(move);

		 // correction for castling moves
		 if (fsq == E1 && tsq == H1 && p->kingSquare[WHITE] == E1) tsq = G1;
		 if (fsq == E8 && tsq == H8 && p->kingSquare[BLACK] == E8) tsq = G8;
		 if (fsq == E1 && tsq == A1 && p->kingSquare[WHITE] == E1) tsq = C1;
		 if (fsq == E8 && tsq == A8 && p->kingSquare[BLACK] == E8) tsq = C8;

		 // now we want to get a move with full data, not only from and to squares
		 int realMove = (tsq << 6) | fsq;
		 MoveToStr(realMove, moveString);
		 realMove = StrToMove(p, moveString);

		 if (maxWeight < score) maxWeight = score;
		 sumOfWeights += score;
		 moves[nOfChoices] = realMove;
		 values[nOfChoices] = score;
		 nOfChoices++;
      }

      // pick a move, filtering out those with significantly lower weight
	  for (int i = 0; i<nOfChoices; i++) {

		 // report about possible choices and rejected moves
		 if (values[i] > 1 || maxWeight == 1) {
            if (printOutput) {
		       printf("info string ");
		       PrintMove(moves[i]);
		       printf(" %d %%", (values[i] * 100) / sumOfWeights );
			   if (IsInfrequent(values[i], maxWeight)) printf(" infrequent ");
			}
		 }
		 
		 // shall we pick this move?
		 if (!IsInfrequent(values[i], maxWeight)) {
		    bestScore += values[i];
            if (my_random(bestScore) < values[i]) bestMove = moves[i];
		 }
		 
		 printf("\n");

	  }
   }
   //if (printOutput) PrintMissingMoves(p);
   return bestMove;
}