Ejemplo n.º 1
0
void
NewGame(void)
{
    short l, c, p, max_opening_sequence;
#ifdef HAVE_GETTIMEOFDAY
    struct timeval tv;
#endif
    compptr = oppptr = 0;
    stage = 0;
    stage2 = -1;    /* the game is not yet started */
    flag.illegal = flag.mate = flag.post = flag.quit
        = flag.reverse = flag.bothsides = flag.onemove = flag.force
        = false;
    flag.material = flag.coords = flag.hash = flag.easy
        = flag.beep = flag.rcptr
        = true;
    flag.stars  = flag.shade = flag.back = flag.musttimeout = false;
    flag.gamein = false;
    flag.rv     = true;

    mycnt1 = mycnt2 = 0;
    GenCnt = NodeCnt = et0 = dither =  XCmore = 0;
    znodes = ZNODES;
    WAwindow = WAWNDW;
    WBwindow = WBWNDW;
    BAwindow = BAWNDW;
    BBwindow = BBWNDW;
    xwndw = BXWNDW;

    if (!MaxSearchDepth)
        MaxSearchDepth = MAXDEPTH - 1;

    contempt = 0;
    GameCnt = 0;
    Game50 = 1;
    CptrFlag[0] = TesujiFlag[0] = false;
    hint = OPENING_HINT;
    ZeroRPT();
    GameType[0] = GameType[1] = UNKNOWN;
    Pscore[0] = Tscore[0] = (SCORE_LIMIT + 3000);
    opponent = player = black;
    computer = white;

    for (l = 0; l < TREE; l++)
        Tree[l].f = Tree[l].t = 0;

    gsrand((unsigned int) 1);

    if (!InitFlag)
    {
        for (c = black; c <= white; c++)
        {
            for (p = pawn; p <= king; p++)
            {
                for (l = 0; l < NO_SQUARES; l++)
                {
                    (*hashcode)[c][p][l].key
                         = (((unsigned long) urand()));
                    (*hashcode)[c][p][l].key
                        += (((unsigned long) urand()) << 16);
                    (*hashcode)[c][p][l].bd
                         = (((unsigned long) urand()));
                    (*hashcode)[c][p][l].bd
                        += (((unsigned long) urand()) << 16);
#if SIZEOF_LONG == 8  /* 64-bit long i.e. 8 bytes */
                    (*hashcode)[c][p][l].key
                        += (((unsigned long) urand()) << 32);
                    (*hashcode)[c][p][l].key
                        += (((unsigned long) urand()) << 48);
                    (*hashcode)[c][p][l].bd
                        += (((unsigned long) urand()) << 32);
                    (*hashcode)[c][p][l].bd
                        += (((unsigned long) urand()) << 48);
#endif
                }
            }
        }

        for (c = black; c <= white; c++)
        {
            for (p = pawn; p <= king; p++)
            {
                for (l = 0; l < MAX_CAPTURED; l++)
                {
                    (*drop_hashcode)[c][p][l].key
                         = (((unsigned long) urand()));
                    (*drop_hashcode)[c][p][l].key
                        += (((unsigned long) urand()) << 16);
                    (*drop_hashcode)[c][p][l].bd
                         = (((unsigned long) urand()));
                    (*drop_hashcode)[c][p][l].bd
                        += (((unsigned long) urand()) << 16);
#if SIZEOF_LONG == 8  /* 64-bit long i.e. 8 bytes */
                    (*drop_hashcode)[c][p][l].key
                        += (((unsigned long) urand()) << 32);
                    (*drop_hashcode)[c][p][l].key
                        += (((unsigned long) urand()) << 48);
                    (*drop_hashcode)[c][p][l].bd
                        += (((unsigned long) urand()) << 32);
                    (*drop_hashcode)[c][p][l].bd
                        += (((unsigned long) urand()) << 48);
#endif
                }
            }
        }
    }

    for (l = 0; l < NO_SQUARES; l++)
    {
        board[l] = Stboard[l];
        color[l] = Stcolor[l];
        Mvboard[l] = 0;
    }

    ClearCaptured();
    ClearScreen();
    InitializeStats();

#ifdef HAVE_GETTIMEOFDAY
    gettimeofday(&tv, NULL);
    time0 = tv.tv_sec*100 + tv.tv_usec/10000;
#else
    time0 = time((long *) 0);
#endif

    /* resetting reference time */
    ElapsedTime(COMPUTE_AND_INIT_MODE);
    flag.regularstart = true;
    Book = BOOKFAIL;

    if (!InitFlag)
    {
        char sx[256];
        strcpy(sx, CP[169]);

        if (TCflag)
            SetTimeControl();
        else if (MaxResponseTime == 0)
            SelectLevel(sx);

        UpdateDisplay(0, 0, 1, 0);
        GetOpenings();
        GetOpeningPatterns(&max_opening_sequence);

        InitFlag = true;
    }

#if ttblsz
    if (TTadd)
    {
        ZeroTTable();
        TTadd = 0;
    }
#endif /* ttblsz */

    hashbd = hashkey = 0;
    return;
}
Ejemplo n.º 2
0
/* computer move */
static PyObject *
engine_cmove(PyObject *self, PyObject *args)
{
    if (verbose)
    {
        printf("gshogi builtin engine settings\n");
        printf("  - computer player (side to move) is: %d", computer);
        if (computer == white)
        {
            printf(" (white)\n");
        }
        else
        {
            printf(" (black)\n");
        }
        printf("  - depth=%i\n",MaxSearchDepth);
        printf("  - nodes=%ld",NodeCntLimit);
        if (NodeCntLimit > 0)
        {
            printf("\n");
        }
        else
        {
            printf(" (no limit)\n");
        }
        printf("  - tournament (classical) TC (true/false): %d\n", TCflag);
        printf("  - tournament TC minutes: %d\n", TCminutes); 
        printf("  - tournament TC seconds: %d\n", TCseconds); 
        printf("  - max response time if not using tournament TC (centiseconds): %ld\n", MaxResponseTime);
    }    
    winner = 0;
    compptr = (compptr + 1) % MINGAMEIN;

    if (!(flag.quit || flag.mate || flag.force))
    {
#ifdef INTERRUPT_TEST
        printf("starting search...\n");
#endif
        Py_BEGIN_ALLOW_THREADS 
        SelectMove(computer, FOREGROUND_MODE);
        Py_END_ALLOW_THREADS

        if (computer == white)
        {
            if (flag.gamein)
            {
                TimeCalc();
            }
            else if (TimeControl.moves[computer] == 0)
            {
                if (XC)
                {
                    if (XCmore < XC)
                    {
                        TCmoves = XCmoves[XCmore];
                        TCminutes = XCminutes[XCmore];
                        TCseconds = XCseconds[XCmore];
                        XCmore++;
                    }
                }
                 SetTimeControl();
            }
        }
    }
Ejemplo n.º 3
0
int
main (int argc, char **argv)
{
    /*
     * Process command-line arguments.
     */

    /* Get rid of the program name. */

    argc--;
    argv++;

    /* CHECKME: get rid of the '+' syntax? */

    while ((argc > 0) && ((argv[0][0] == '-') || (argv[0][0] == '+')))
    {
        switch (argv[0][1])
        {
        case 'a':
            /* Need the "+" syntax here... */
            ahead = ((argv[0][0] == '-') ? false : true);
            break;


        case 'b':
            argc--;
            argv++;

            if (argc > 0)
            {
                bookfile = argv[0];
#ifdef BINBOOK
                binbookfile = NULL;
#endif
            }

            break;

#ifdef BINBOOK
        case 'B':
            argc--;
            argv++;

            if (argc > 0)
                binbookfile = argv[0];

            break;
#endif

        case 'C':
            /* Curses interface. */
            display_type = DISPLAY_CURSES;

            break;


        case 'h':
            /* Need the "+" syntax here... */
            hash = ((argv[0][0] == '-') ? false : true);
            break;


        case 'l':
            argc--;
            argv++;

            if (argc > 0)
                Lang = argv[0];

            break;


        case 'L':
            argc--;
            argv++;

            if (argc > 0)
                strcpy(listfile, argv[0]);
            break;


        case 's':
            argc--;
            argv++;

            if (argc > 0)
                strcpy(savefile, argv[0]);

            break;


        case 'P':
            argc--;
            argv++;

            if (argc > 0)
                bookmaxply = atoi(argv[0]);

            break;


        case 'R':
            /* Raw text interface. */
            display_type = DISPLAY_RAW;

            break;


        case 'S':
            argc--;
            argv++;

            if (argc > 0)
                booksize = atoi(argv[0]);
            break;

#if ttblsz
        case 'r':
            argc--;
            argv++;

            if (argc > 0)
                rehash = atoi(argv[0]);

            if (rehash > MAXrehash)
                rehash = MAXrehash;

            break;


        case 'T':
            argc--;
            argv++;

            if (argc > 0)
                ttblsize = atoi(argv[0]);

            if ((ttblsize <= MINTTABLE))
                ttblsize = (MINTTABLE) + 1;

            break;

#ifdef HASHFILE
        case 'c':   /* Create or test persistent transposition table. */
            argc--;
            argv++;

            if (argc > 0)
                filesz = atoi(argv[0]);
            else
                filesz = vfilesz;

            if ((filesz > 0) && (filesz < 24))
                filesz = (1 << filesz) - 1 + MAXrehash;
            else
                filesz = filesz + MAXrehash;

            if ((hashfile = fopen(HASHFILE, RWA_ACC)) == NULL)
                hashfile = fopen(HASHFILE, WA_ACC);

            if (hashfile != NULL)
            {
                long j;
                struct fileentry n;

                printf(CP[66]);
                n.f = n.t = 0;
                n.flags = 0;
                n.depth = 0;
                n.sh = n.sl = 0;

                for (j = 0; j < filesz + 1; j++)
                    fwrite(&n, sizeof(struct fileentry), 1, hashfile);

                fclose(hashfile);
            }
            else
            {
                printf(CP[50], HASHFILE);
            }

            return 0;


        case 't':   /* Create or test persistent transposition table. */
            hashfile = fopen(HASHFILE, RWA_ACC);

            if (hashfile)
            {
                fseek(hashfile, 0L, SEEK_END);
                filesz = (ftell(hashfile) / (sizeof(struct fileentry))) - 1;
            }

            if (hashfile != NULL)
            {
                long i, j;
                int nr[MAXDEPTH];
                struct fileentry n;

                printf(CP[49]);

                for (i = 0; i < MAXDEPTH; i++)
                    nr[i] = 0;

                fseek(hashfile, 0L, SEEK_END);
                i = ftell(hashfile) / (sizeof(struct fileentry));
                fseek(hashfile, 0L, SEEK_SET);

                for (j = 0; j < i + 1; j++)
                {
                    fread(&n, sizeof(struct fileentry), 1, hashfile);

                    if (n.depth > MAXDEPTH)
                    {
                        printf("ERROR\n");
                        exit(1);
                    }

                    if (n.depth)
                    {
                        nr[n.depth]++;
                        nr[0]++;
                    }
                }

                printf(CP[109], nr[0], i);

                for (j = 1; j < MAXDEPTH; j++)
                    printf("%d ", nr[j]);

                printf("\n");
            }

            return 0;


#endif /* HASHFILE */
#endif /* ttblsz */

        case 'v':
            fprintf(stderr, CP[102], version, patchlevel);
            exit(1);


        case 'X':
            /* X interface. */
            display_type = DISPLAY_X;

            break;


        case 'x':
            argc--;
            argv++;

            if (argc > 0)
                xwin = argv[0];

            break;


        default:
            fprintf(stderr, CP[113]);
            exit(1);
        }

        argc--;
        argv++;
    }

    if (argc == 2)
    {
        char *p;

        MaxResponseTime = 100L * strtol(argv[1], &p, 10);

        if (*p == ':')
        {
            MaxResponseTime = 60L * MaxResponseTime +
                100L * strtol(++p, (char **) NULL, 10);
        }

        TCflag    = false;
        TCmoves   = 0;
        TCminutes = 0;
        TCseconds = 0;
    }

    if (argc >= 3)
    {
        char *p;

        if (argc > 9)
        {
            printf("%s\n", CP[220]);
            exit(1);
        }

        TCmoves   = atoi(argv[1]);
        TCminutes = (short)strtol(argv[2], &p, 10);

        if (*p == ':')
            TCseconds = (short)strtol(p + 1, (char **) NULL, 10);
        else
            TCseconds = 0;

        TCflag = true;
        argc -= 3;
        argv += 3;

        while (argc > 1)
        {
            XCmoves[XC]   = atoi(argv[0]);
            XCminutes[XC] = (short)strtol(argv[1], &p, 10);

            if (*p == ':')
                XCseconds[XC] = (short)strtol(p + 1, (char **) NULL, 10);
            else
                XCseconds[XC] = 0;

            if (XCmoves[XC] && (XCminutes[XC] || XCseconds[XC]))
                XC++;
            else
            {
                printf("%s\n", CP[220]);
                exit(1);
            }

            argc -= 2;
            argv += 2;
        }

        if (argc)
        {
            /*
             * If we got here, there are unknown arguments, so issue
             * an error message and quit.
             */

            printf("%s\n", CP[233]);
            print_arglist(argc, argv);
            exit(1);
        }
    }

    if (InitMain() != 0)
        exit(1);

    while (!flag.quit)
    {
        oppptr = (oppptr + 1) % MINGAMEIN;

        if (flag.bothsides && !flag.mate)
            SelectMove(opponent, FOREGROUND_MODE);
        else
            InputCommand(NULL);

        if (opponent == white)
        {
            if (flag.gamein || TCadd)
            {
                TimeCalc();
            }
            else if (TimeControl.moves[opponent] == 0)
            {
                if (XC)
                {
                    if (XCmore < XC)
                    {
                        TCmoves   = XCmoves[XCmore];
                        TCminutes = XCminutes[XCmore];
                        TCseconds = XCseconds[XCmore];
                        XCmore++;
                    }
                }

                SetTimeControl();
            }
        }

        compptr = (compptr + 1) % MINGAMEIN;

        if (!(flag.quit || flag.mate || flag.force))
        {
#ifdef INTERRUPT_TEST
            printf("starting search...\n");
#endif
            SelectMove(computer, FOREGROUND_MODE);

            if (computer == white)
            {
                if (flag.gamein)
                {
                    TimeCalc();
                }
                else if (TimeControl.moves[computer] == 0)
                {
                    if (XC)
                    {
                        if (XCmore < XC)
                        {
                            TCmoves = XCmoves[XCmore];
                            TCminutes = XCminutes[XCmore];
                            TCseconds = XCseconds[XCmore];
                            XCmore++;
                        }
                    }

                    SetTimeControl();
                }
            }
        }
    }

    ExitMain();

    return 0;
}