Example #1
0
  /* Sort the list. */
void CListHead::Sort(int (*pCompare)(const void *p0, const void *p1), int iNodeOffset)
{
  // get number of elements
  INDEX ctCount = Count();
  // if none
  if (ctCount==0) {
    // do not sort
  }

  // create array of that much integers (the array will hold pointers to the list)
  ULONG *aulPointers = new ULONG[ctCount];
  // fill it
  INDEX i=0;
  for ( CListIter<int, 0> iter(*this); !iter.IsPastEnd(); iter.MoveToNext() ) {
    aulPointers[i] = ((ULONG)&*iter)-iNodeOffset;
    i++;
  }

  // sort it
  qsort(aulPointers, ctCount, sizeof(SLONG), pCompare);

  // make temporary list
  CListHead lhTmp;
  // for each pointer
  {for(INDEX i=0; i<ctCount; i++) {
    ULONG ul = aulPointers[i];
    // get the node
    CListNode *pln = (CListNode*)(ul+iNodeOffset);
    // remove it from original list
    pln->Remove();
    // add it to the end of new list
    lhTmp.AddTail(*pln);
  }}

  // free the pointer array
  delete[] aulPointers;

  // move the sorted list here
  MoveList(lhTmp);
}
Example #2
0
void
SelectMove(short side, SelectMove_mode iop)
{
    static short i, tempb, tempc, tempsf, tempst, xside, rpt;
    static short alpha, beta, score;
    static struct GameRec  *g;
    short sqking, in_check, blockable;

#ifdef BOOKTEST
    printf("hashbd = %ld (hashkey >> 16)|side = %d\n",
           hashbd, (hashkey >> 16)|side);
#endif

    flag.timeout = false;
    flag.back = false;
    flag.musttimeout = false;

    xside = side ^ 1;

#if ttblsz
    recycle = (GameCnt % rehash) - rehash;
#endif

    ExaminePosition(side);

    /* if background mode set to infinite */
    if (iop == BACKGROUND_MODE)
    {
        background = true;
        /* if background mode set response time to infinite */
        ResponseTime = 9999999;
    }
    else
    {
        player = side;
        SetResponseTime(side);
    }

#ifdef QUIETBACKGROUND
    if (!background)
#endif /* QUIETBACKGROUND */
        ShowResponseTime();

    ExtraTime = 0;

    score = ScorePosition(side);

#ifdef QUIETBACKGROUND
    if (!background)
#endif /* QUIETBACKGROUND */
        ShowSidetoMove();

#ifdef QUIETBACKGROUND
    if (!background)
#endif /* QUIETBACKGROUND */
        SearchStartStuff(side);

#ifdef HISTORY
    array_zero(history, sizeof_history);
#endif

    FROMsquare = TOsquare = -1;
    PV = 0;

    if (iop == FOREGROUND_MODE)
        hint = 0;

    /*
     * If the last move was the hint, select the computed answer to the
     * hint as first move to examine.
     */

#if MAXDEPTH > 3
    if (GameCnt > 0)
    {
        SwagHt = (GameList[GameCnt].gmove == PrVar[2]) ? PrVar[3] : 0;
    }
    else
#endif
        SwagHt = 0;


    for (i = 0; i < MAXDEPTH; i++)
        PrVar[i] = killr0[i] = killr1[i] = killr2[i] = killr3[i] = 0;

    /* set initial window for search */

    if (flag.tsume)
    {
        alpha =  -(SCORE_LIMIT + 999);
        beta = SCORE_LIMIT + 999;
    }
    else
    {
        alpha = score - ((computer == white) ? BAwindow : WAwindow);
        beta  = score + ((computer == white) ? BBwindow : WBwindow);
    }

    rpt = 0;
    TrPnt[1] = 0;
    root = &Tree[0];

    sqking = PieceList[side][0];
    in_check = (board[sqking] == king)
        ? SqAttacked(sqking, side^1, &blockable)
        : false;

    MoveList(side, 1, in_check, blockable);

    for (i = TrPnt[1]; i < TrPnt[2]; i++)
    {
        if (!pick(i, TrPnt[2] - 1))
            break;
    }

    /* Can I get a book move? */

    if (flag.regularstart && Book)
    {
        flag.timeout = bookflag = OpeningBook(&hint, side);

        if (TCflag)
            ResponseTime += ResponseTime;
    }

    /* Zero stats for hash table. */

    reminus = replus = 0;
    GenCnt = NodeCnt = ETnodes = EvalNodes = HashCnt
        = FHashAdd = HashAdd = FHashCnt = THashCol = HashCol = 0;

    globalscore = plyscore = score;
    Jscore = 0;
    zwndw = 20;


    /********************* main loop ********************************/

    Sdepth = (MaxSearchDepth < (MINDEPTH - 1))
        ? MaxSearchDepth
        : (MINDEPTH - 1);

    while (!flag.timeout)
    {
        /* go down a level at a time */
        Sdepth++;

#ifdef NULLMOVE
        null = 0;
        PVari = 1;
#endif

        /* terminate search at DepthBeyond ply past goal depth */
        if (flag.tsume)
            DepthBeyond = Sdepth;
        else
#if defined SLOW_CPU
            DepthBeyond = Sdepth + ((Sdepth == 1) ? 3 : 5);
#else
        DepthBeyond = Sdepth + ((Sdepth == 1) ? 7 : 11);
#endif

# ifdef QUIETBACKGROUND
        if (!background)
#endif /* QUIETBACKGROUND */
            ShowDepth(' ');

        /* search at this level returns score of PV */
        score = search(side, 1, Sdepth, alpha, beta, PrVar, &rpt);

        /* save PV as killer */
        for (i = 1; i <= Sdepth; i++)
            killr0[i] = PrVar[i];

        /* low search failure re-search with (-inf, score) limits  */
        if (score < alpha)
        {
            reminus++;
#ifdef QUIETBACKGROUND
            if (!background)
#endif /* QUIETBACKGROUND */
                ShowDepth('-');

            if (TCflag && TCcount < MAXTCCOUNTR)
            {
                if (hard_time_limit)
                    ExtraTime += (MAXTCCOUNTR - TCcount) * TCleft;
                else
                    ExtraTime += (8 * TCleft);

                TCcount = MAXTCCOUNTR - 1;
            }

            score = search(side, 1, Sdepth, -(SCORE_LIMIT + 999),
                           (SCORE_LIMIT + 999), PrVar, &rpt);
        }
        /* high search failure re-search with (score, +inf) limits */
        else if (score > beta && !(root->flags & exact))
        {
            replus++;
#ifdef QUIETBACKGROUND
            if (!background)
#endif /* QUIETBACKGROUND */
                ShowDepth('+');

            score = search(side, 1, Sdepth, -(SCORE_LIMIT + 999),
                           (SCORE_LIMIT + 999), PrVar, &rpt);
        }

        /**************** out of search ***********************************/
        CheckForTimeout(score, globalscore, Jscore, zwndw);

        /************************ time control ****************************/

        /* save PV as killer */
        for (i = 1; i <= Sdepth + 1; i++)
            killr0[i] = PrVar[i];

        if (!flag.timeout)
            Tscore[0] = score;

        /* if (!flag.timeout) */
        /*
          for (i = TrPnt[1] + 1; i < TrPnt[2]; i++)
          if (!pick (i, TrPnt[2] - 1))
          break;
        */

        /* if done or nothing good to look at quit */
        if ((root->flags & exact) || (score < -SCORE_LIMIT))
            flag.timeout = true;

        /* find the next best move put below root */

        if (!flag.timeout)
        {
#if !defined NODYNALPHA
            Jscore = (plyscore + score) >> 1;
#endif
            zwndw = 20 + abs(Jscore / 12);
            plyscore = score;

            /* recompute search window */
            beta = score + ((computer == white) ? BBwindow : WBwindow);
#if !defined NODYNALPHA
            alpha = ((Jscore < score) ? Jscore : score)
                - ((computer == white) ? BAwindow : WAwindow)
                - zwndw;
#else
            alpha = score - ((computer == white) ? BAwindow : WAwindow);
#endif
        }

#ifdef QUIETBACKGROUND
        if (!background)
#endif /* QUIETBACKGROUND */
            ShowResults(score, PrVar, '.');
    }