-+-+-+-+-+-+-+-+ START OF PART 2 -+-+-+-+-+-+-+-+ X while (pnt < TrPnt`5B3`5D) X `7B X node = &Tree`5Bpnt`5D; pnt++; X if ( aflag == 0) `7B node->f = 255; aflag = 2; castld`5Bplayer`5D = tr Vue;`7D X else if ( aflag == 1) `7B node->t = 255; aflag = 2; castld`5Bplayer`5D V = true;`7D X algbrnot(node->f,node->t); X if (strcmp(s,mvstr1) == 0 `7C`7C strcmp(s,mvstr2) == 0) X `7B X xnode = node; cnt++; X `7D X `7D X if (cnt == 1) X `7B X MakeMove(opponent,xnode,&tempb,&tempc); X CaptureSearch(computer,opponent,3,1,-9999,9999,0,&x,nxtline); X if (x == 10000) UnmakeMove(opponent,xnode,&tempb,&tempc); X else X `7B X *ok = true; PrintBoard(opponent,xnode->f,xnode->t,0); X if (xnode->f == 255 `7C`7C xnode->t == 255) Game50 = GameCnt; X else if (board`5Bxnode->t`5D == pawn `7C`7C (xnode->flags & captur Ve))`20 X Game50 = GameCnt; X `7D`20 X `7D X if ( *ok == false) `7B X/*`09DrawPiece(h+(8*v)); */ X`09gotoXY(1,1); printw("Invalid - press '?' for help."); ClrEoln(); X`09printf("\007"); X lib$wait(&0.8); X gotoXY(1,1); ClrEoln(); `20 X `7D X`7D X X`20 Xinput_command() X`7B Xshort ok,i,f,t; Xchar s`5B20`5D,fname`5B20`5D; XFILE *fd; X X ok=quit=false; X while (!(ok `7C`7C quit)) X `7B X allow = true; X strcpy( start,"\0\0\0"); X strcpy( end,"\0\0\0"); X readline( 1,24," ",s); X player = opponent; X VerifyMove(s,&ok,aflag); X standout(); X DrawPiece(h+(8*v)); X standend(); X if (strcmp(s,"prt") == 0) X `7B X ClrScreen(); PrintBoard(white,0,0,1); X `7D X if (strcmp(s,"quit") == 0) quit = true; X if (strcmp(s,"post") == 0) post = !post; X if (strcmp(s,"set") == 0) SetBoard(); X if (strcmp(s,"go") == 0) ok = true; X if (strcmp(s,"help") == 0) help(); X if (strcmp(s, "redraw") == 0) X`09`7B X`09 ClrScreen(); X`09 PrintBoard(white,0,0,1); X`09`7D X if (strcmp(s,"hint") == 0) X `7B X algbrnot(prvar`5B2`5D>>8,prvar`5B2`5D & 0xFF); X gotoXY(50,13); printw("try %5s",mvstr1); ClrEoln(); X `7D X if (strcmp(s,"both") == 0) X `7B X bothsides = !bothsides; X select_move(opponent); X ok = true; X `7D X if (strcmp(s,"reverse") == 0) X `7B X reverse = !reverse; X ClrScreen(); X PrintBoard(white,0,0,1); X `7D X if (strcmp(s,"switch") == 0) X `7B X computer = otherside`5Bcomputer`5D; X opponent = otherside`5Bopponent`5D; X ok = true; X `7D X if (strcmp(s,"save") == 0) X `7B X`09 readline(50, 21, "file name? ", fname); X SaveGame(fname); X `7D X if (strcmp(s,"get") == 0) X `7B X`09 readline(50, 21, "file name? ", fname); X GetGame(fname); X InitializeStats(); X PrintBoard(white,0,0,1); X `7D X if (strcmp(s,"time") == 0) X `7B X`09 readline(50, 21, "enter time: ", fname); X`09 response_time = atoi(fname); X `7D X if (strcmp(s,"undo") == 0 && GameCnt > 0) X `7B X f = GameList`5BGameCnt`5D>>8; t = GameList`5BGameCnt`5D & 0xFF; X board`5Bf`5D = board`5Bt`5D; color`5Bf`5D = color`5Bt`5D; X board`5Bt`5D = GamePc`5BGameCnt`5D; color`5Bt`5D = GameClr`5BGameC Vnt`5D; X GameCnt--; X PrintBoard(white,0,0,1); X InitializeStats(); X `7D X if (strcmp(s,"list") == 0) X `7B X fd = fopen("chess.lst","w"); X for (i = 0; i <= GameCnt; i++) X `7B X f = GameList`5Bi`5D>>8; t = (GameList`5Bi`5D & 0xFF); X algbrnot(f,t); X if ((i % 2) == 0) fprintf(fd,"\n"); X fprintf(fd," %5s %6d ",mvstr1,GameScore`5Bi`5D); X `7D X fprintf(fd,"\n"); X fclose(fd); X `7D X `7D X ElapsedTime(1); X`7D X X XgotoXY(x,y) Xshort x,y; X`7B X move(y-1,x-1); X`7D X X XClrScreen() X`7B X clear(); refresh(); X`7D X X XClrEoln() X`7B X clrtoeol(); refresh(); X`7D X X Xalgbrnot(f,t) Xshort f,t; X`7B X if (f == 255) X `7B strcpy(mvstr1,"o-o"); strcpy(mvstr2,"o-o"); `7D X else if (t == 255) X `7B strcpy(mvstr1,"o-o-o"); strcpy(mvstr2,"o-o"); `7D X else X `7B X mvstr1`5B0`5D = cx`5Bcol`5Bf`5D`5D; mvstr1`5B1`5D = rx`5Brow`5Bf`5D`5D V; X mvstr1`5B2`5D = cx`5Bcol`5Bt`5D`5D; mvstr1`5B3`5D = rx`5Brow`5Bt`5D`5D V; X mvstr2`5B0`5D = qx`5Bboard`5Bf`5D`5D; X mvstr2`5B1`5D = mvstr1`5B2`5D; mvstr2`5B2`5D = mvstr1`5B3`5D; X mvstr1`5B4`5D = '\0'; mvstr2`5B3`5D = '\0'; X `7D X`7D X X Xparse(s,m) Xunsigned short *m; char s`5B`5D; X`7B Xshort r1,r2,c1,c2; X if (s`5B4`5D == 'o') *m = 0x00FF; X else if (s`5B0`5D == 'o') *m = 0xFF00; X else X `7B X c1 = s`5B0`5D - 'a'; r1 = s`5B1`5D - '1'; X c2 = s`5B2`5D - 'a'; r2 = s`5B3`5D - '1'; X *m = (locn`5Br1`5D`5Bc1`5D<<8) + locn`5Br2`5D`5Bc2`5D; X `7D X`7D X X XGetOpenings() X`7B XFILE *fd; Xint c,j; Xchar s`5B80`5D,*p; X fd = fopen("chess.opn","r"); X BookSize = 0; BookDepth = 24; j = -1; c = '?'; X while (c != EOF) X `7B X p = s; X while ((c=getc(fd)) != EOF) X if (c == '\n') break; else *(p++) = c; X *p = '\0'; X if (c != EOF) X if (s`5B0`5D == '!') X `7B X while (j < BookDepth) Book`5BBookSize`5D`5Bj++`5D = 0;`20 X BookSize++; j = -1; X `7D X else if (j < 0) j++; X else X `7B X parse(&s`5B0`5D,&Book`5BBookSize`5D`5Bj`5D); j++; X parse(&s`5B6`5D,&Book`5BBookSize`5D`5Bj`5D); j++; X `7D`20 X `7D X fclose(fd); X`7D X X XGetGame(fname) Xchar fname`5B20`5D; X`7B XFILE *fd; Xint c; Xshort loc; Xunsigned short m; X X if (fname`5B0`5D == '\0') strcpy(fname,"chess.000"); X if ((fd = fopen(fname,"r")) != NULL) X `7B X fscanf(fd,"%hd%hd",&castld`5Bwhite`5D,&castld`5Bblack`5D); X fscanf(fd,"%hd%hd",&kingmoved`5Bwhite`5D,&kingmoved`5Bblack`5D); X for (loc = 0; loc < 64; loc++) X `7B X fscanf(fd,"%hd",&m); board`5Bloc`5D = (m >> 8); color`5Bloc`5D = ( Vm & 0xFF); X `7D X GameCnt = -1; c = '?'; X while (c != EOF) X c = fscanf(fd,"%hd%hd%hd%hd",&GameList`5B++GameCnt`5D,&GameScore`5BG VameCnt`5D, X &GamePc`5BGameCnt`5D,&GameClr`5BGameCnt`5D); X fclose(fd); X `7D X`7D X X XSaveGame(fname) Xchar fname`5B20`5D; X`7B XFILE *fd; Xshort loc,i; X X if (fname`5B0`5D == '\0') strcpy(fname,"chess.000"); X fd = fopen(fname,"w"); X fprintf(fd,"%d %d\n",castld`5Bwhite`5D,castld`5Bblack`5D); X fprintf(fd,"%d %d\n",kingmoved`5Bwhite`5D,kingmoved`5Bblack`5D); X for (loc = 0; loc < 64; loc++) X fprintf(fd,"%d\n",256*board`5Bloc`5D + color`5Bloc`5D); X for (i = 0; i <= GameCnt; i++) X fprintf(fd,"%d %d %d %d\n",GameList`5Bi`5D,GameScore`5Bi`5D,GamePc`5Bi`5 VD,GameClr`5Bi`5D); X fclose(fd); X`7D X X XElapsedTime(iop) Xshort iop; X`7B Xint minute,second; X et = time(0) - time0; X if (et < et0) et0 = 0; X if (et > et0 `7C`7C iop == 1) X `7B X if (et > response_time+extra_time) timeout = true; X et0 = et; X if (iop == 1) X `7B X et0 = 0; time0 = time(0); X `7D X minute = et/60; second = (et - 60*minute); X if (player == computer) gotoXY(50,18); else gotoXY(50,23); X printw("%d:%d",minute,second); ClrEoln(); X if (et > 0) srate = NodeCnt/et; else srate = 0; X/* if (post) X `7B X gotoXY(18,24); printw("Nodes/Sec= %d",srate); ClrEoln(); X `7D*/ X `7D X`7D X X Xpost_move(node) Xstruct leaf *node; X`7B Xshort d,e,ply; X d = 4; ply = 1; X gotoXY(60,d); printw("%6d ",node->score); X while (prvar`5Bply`5D > 0) X `7B X algbrnot(prvar`5Bply`5D>>8,prvar`5Bply`5D & 0x00FF); X gotoXY(50,d); printw("%5s",mvstr1); X ply++; d++; X `7D X e = d; X while (d < lpost) X `7B X gotoXY(50,d++); ClrEoln(); X `7D X lpost = e; X refresh(); X`7D X X XDrawPiece(loc) Xshort loc; X`7B Xshort r,c; char x; X if (reverse) r = 7-row`5Bloc`5D; else r = row`5Bloc`5D; X if (reverse) c = 7-col`5Bloc`5D; else c = col`5Bloc`5D; X if (color`5Bloc`5D == black) x = '*'; else x = ' '; X gotoXY(5+5*c,4+2*(7-r)); printw("%c%c",x,px`5Bboard`5Bloc`5D`5D," "); X`7D X X XPrintBoard(side,f,t,flag) Xshort side,f,t,flag; X`7B Xshort i,l,c,z;`20 X X if (side == white) c = 0; else c = 56; X if (flag) X `7B X i = 2; X gotoXY(3,++i); X printw("`7C----`7C----`7C----`7C----`7C----`7C----`7C----`7C----`7C"); X while (i<19) X `7B X gotoXY(1,++i); X if (reverse) z = (i/2)-1; else z = 10-(i/2); X printw("%d `7C `7C `7C `7C `7C `7C `7C `7C V `7C",z); X gotoXY(3,++i); X printw("`7C----`7C----`7C----`7C----`7C----`7C----`7C----`7C----`7 VC"); X `7D X gotoXY(3,20); X if (reverse) printw(" h g f e d c b a"); X else printw(" a b c d e f g h"); X for (l = 0; l < 64; l++) DrawPiece(l); X `7D X else if (f == 255) X `7B X DrawPiece(c+4); DrawPiece(c+6); X DrawPiece(c+7); DrawPiece(c+5); X `7D X else if (t == 255) X `7B X DrawPiece(c+4); DrawPiece(c+2); X DrawPiece(c); DrawPiece(c+3); X `7D X else X `7B X DrawPiece(f); DrawPiece(t); X `7D X refresh(); X`7D X X XSetBoard() X`7B Xshort a,r,c,loc; Xchar s`5B20`5D; X X ClrScreen(); PrintBoard(white,0,0,1); X a = white; X do X `7B X gotoXY(50,2); printw(". Exit to Main"); X gotoXY(50,3); printw("# Clear Board"); X readline(49,5, "Enter piece & location: ", s); X if (s`5B0`5D == '#') X `7B X for (loc = 0; loc < 64; loc++) X `7B board`5Bloc`5D = no_piece; color`5Bloc`5D = neutral; `7D X PrintBoard(white,0,0,1); X `7D X if (s`5B0`5D == 'c' `7C`7C s`5B0`5D == 'C') a = otherside`5Ba`5D; X c = s`5B1`5D-'a'; r = s`5B2`5D-'1'; X if ((c >= 0) && (c < 8) && (r >= 0) && (r < 8)) X `7B X loc = locn`5Br`5D`5Bc`5D; X color`5Bloc`5D = a; X if (s`5B0`5D == 'p') board`5Bloc`5D = pawn; X else if (s`5B0`5D == 'n') board`5Bloc`5D = knight; X else if (s`5B0`5D == 'b') board`5Bloc`5D = bishop; X else if (s`5B0`5D == 'r') board`5Bloc`5D = rook; X else if (s`5B0`5D == 'q') board`5Bloc`5D = queen; X else if (s`5B0`5D == 'k') board`5Bloc`5D = king; X else `7B board`5Bloc`5D = no_piece; color`5Bloc`5D = neutral; `7D X DrawPiece(loc); refresh(); X `7D X `7D X while (s`5B0`5D != '.'); X if (board`5B4`5D != king) kingmoved`5Bwhite`5D = 10; X if (board`5B61`5D != king) kingmoved`5Bblack`5D = 10; X GameCnt = -1; Game50 = -1; BookDepth = 0; X InitializeStats(); ClrScreen(); PrintBoard(white,0,0,1); X`7D X `20 X XNewGame() X`7B Xshort l,r,c; Xchar buff`5B20`5D; X X mate = quit = reverse = bothsides = post = false; X lpost = NodeCnt = epsquare = xkillr = 0; X GameCnt = Game50 = -1; X castld`5Bwhite`5D = castld`5Bblack`5D = false; X kingmoved`5Bwhite`5D = kingmoved`5Bblack`5D = 0; X opponent = white; computer = black; X for (r = 0; r < 8; r++) X for (c = 0; c < 8; c++) X `7B X l = 8*r+c; locn`5Br`5D`5Bc`5D = l; X row`5Bl`5D = r; col`5Bl`5D = c; X `7D X ClrScreen(); X readline(1, 20, "enter response time (or RETURN for default): ", buff); X response_time = buff`5B0`5D ? atoi(buff) : DEF_TIME; X ClrScreen(); PrintBoard(white,0,0,1); X InitializeStats(); X ElapsedTime(1); X GetOpenings(); X`7D X X Xhelp() X`7B Xchar c; Xchar buff`5B10`5D; X XClrScreen(); Xprintw("\n"); Xprintw("This program attempts to play CHESS\n\n"); Xprintw("To make a move, move the cursor onto the piece you wish to move usin Vg\n"); Xprintw("the keys i,j,k,l so that it is highlighted. Press return, then move V the cursor\n"); Xprintw("onto the space you wish to move to, then press return again.\n"); Xprintw("Press 'c' to enter one of these other commands:\n"); Xprintw("o-o castle king side\n"); Xprintw("o-o-o castle queen side\n"); Xprintw("set set up a board position\n"); Xprintw("switch switch sides with computer\n"); Xprintw("go skip your move\n"); Xprintw("reverse reverse board display\n"); Xprintw("redraw (`5ER) re-paint display\n"); Xprintw("undo (`5EB) undo last move\n"); Xprintw("both computer plays both sides\n"); Xprintw("time change response time\n"); Xprintw("post post best line of play\n"); Xprintw("hint computer suggests your move\n"); Xprintw("list list moves to file chess.lst\n"); Xprintw("save save game to disk\n"); Xprintw("get get game from disk\n"); Xprintw("quit exit CHESS\n"); Xrefresh(); Xprintw("Type return:"); Xsmg$read_keystroke(&kb,&buff_char); XClrScreen(); XPrintBoard(white,0,0,1); X`7D X X XUpdatePieceList(side,loc,iop) Xshort side,loc,iop; X X/* X Array PieceList`5Bside`5D`5Bindx`5D contains the location of all the pie Vces of X either side. Array Index`5Bloc`5D contains the indx into PieceList for a X given square. X*/ X X`7B Xregister short i; X if (iop == 1) X `7B X PieceCnt`5Bside`5D--; X for (i = Index`5Bloc`5D; i <= PieceCnt`5Bside`5D; i++) X `7B X PieceList`5Bside`5D`5Bi`5D = PieceList`5Bside`5D`5Bi+1`5D; X Index`5BPieceList`5Bside`5D`5Bi`5D`5D = i; X `7D X `7D X else X `7B X PieceCnt`5Bside`5D++; X PieceList`5Bside`5D`5BPieceCnt`5Bside`5D`5D = loc; X Index`5Bloc`5D = PieceCnt`5Bside`5D; X `7D X`7D X X XInitializeStats() X`7B Xregister short i,loc; X for (i = 0; i < 8; i++) X PawnCnt`5Bwhite`5D`5Bi`5D = PawnCnt`5Bblack`5D`5Bi`5D = 0; X mtl`5Bwhite`5D = mtl`5Bblack`5D = pmtl`5Bwhite`5D = pmtl`5Bblack`5D=0; X PieceCnt`5Bwhite`5D = PieceCnt`5Bblack`5D = 0; X for (loc = 0; loc < 64; loc++) X if (color`5Bloc`5D != neutral) X `7B X mtl`5Bcolor`5Bloc`5D`5D += value`5Bboard`5Bloc`5D`5D; +-+-+-+-+-+-+-+- END OF PART 2 +-+-+-+-+-+-+-+-