⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gnuchess.c

📁 早期freebsd实现
💻 C
📖 第 1 页 / 共 5 页
字号:
/*  C source for CHESS    Revision: 4-25-88  Copyright (C) 1986, 1987, 1988 Free Software Foundation, Inc.  Copyright (c) 1988   John Stanback  This file is part of CHESS.  CHESS is distributed in the hope that it will be useful,  but WITHOUT ANY WARRANTY.  No author or distributor  accepts responsibility to anyone for the consequences of using it  or for whether it serves any particular purpose or works at all,  unless he says so in writing.  Refer to the CHESS General Public  License for full details.  Everyone is granted permission to copy, modify and redistribute  CHESS, but only under the conditions described in the  CHESS General Public License.   A copy of this license is  supposed to have been given to you along with CHESS so you  can know your rights and responsibilities.  It should be in a  file named COPYING.  Among other things, the copyright notice  and this notice must be preserved on all copies.*/#include <stdio.h>#include <ctype.h>#ifdef MSDOS#include <stdlib.h>#include <time.h>#include <alloc.h>#define ttblsz 4096#else#include <sys/param.h>#include <sys/times.h>#define ttblsz 16384#define huge#endif MSDOS#include "move.h"#define neutral 2#define white 0#define black 1 #define no_piece 0#define pawn 1#define knight 2#define bishop 3#define rook 4#define queen 5#define king 6#define valueP 100#define valueN 350#define valueB 355#define valueR 550#define valueQ 1100#define valueK 1200#define ctlP 0x4000#define ctlN 0x2800#define ctlB 0x1800#define ctlR 0x0400#define ctlQ 0x0200#define ctlK 0x0100#define ctlBQ 0x1200#define ctlRQ 0x0600#define ctlNN 0x2000#define pxx " PNBRQK"#define qxx " pnbrqk"#define rxx "12345678"#define cxx "abcdefgh"#define check 0x0001#define capture 0x0002#define draw 0x0004#define promote 0x0008#define cstlmask 0x0010#define epmask 0x0020#define exact 0x0040#define pwnthrt 0x0080#define truescore 0x0001#define lowerbound 0x0002#define upperbound 0x0004#define maxdepth 30#define true 1#define false 0#define absv(x) ((x) < 0 ? -(x) : (x))#if (NEWMOVE < 1)#define taxicab(a,b) (abs(column[a]-column[b]) + abs(row[a]-row[b]))#endifstruct leaf  {    short f,t,score,reply;    unsigned short flags;  };struct GameRec  {    unsigned short gmove;    short score,depth,time,piece,color;    long nodes;  };struct TimeControlRec  {    short moves[2];    long clock[2];  };struct BookEntry  {    struct BookEntry *next;    unsigned short *mv;  };struct hashval  {    unsigned long bd;    unsigned short key;  };struct hashentry  {    unsigned long hashbd;    unsigned short mv,flags;    short score,depth;  };char mvstr1[5],mvstr2[5];struct leaf Tree[2000],*root;short TrPnt[maxdepth],board[64],color[64];short row[64],column[64],locn[8][8],Pindex[64],svalue[64];short PieceList[2][16],PieceCnt[2],atak[2][64],PawnCnt[2][8];short castld[2],kingmoved[2],mtl[2],pmtl[2],emtl[2],hung[2];short c1,c2,*atk1,*atk2,*PC1,*PC2,EnemyKing;short mate,post,opponent,computer,Sdepth,Awindow,Bwindow,dither;long ResponseTime,ExtraTime,Level,et,et0,time0,cputimer,ft;long NodeCnt,evrate,ETnodes,EvalNodes,HashCnt;short quit,reverse,bothsides,hashflag,InChk,player,force,easy,beep;short wking,bking,FROMsquare,TOsquare,timeout,Zscore,zwndw,xwndw,slk;short INCscore;short HasPawn[2],HasKnight[2],HasBishop[2],HasRook[2],HasQueen[2];short ChkFlag[maxdepth],CptrFlag[maxdepth],PawnThreat[maxdepth];short Pscore[maxdepth],Tscore[maxdepth],Threat[maxdepth];struct GameRec GameList[240];short GameCnt,Game50,epsquare,lpost,rcptr,contempt;short MaxSearchDepth;struct BookEntry *Book;struct TimeControlRec TimeControl;short TCflag,TCmoves,TCminutes,OperatorTime;short otherside[3]={1,0,2};short rank7[3]={6,1,0};short map[64]=   {0,1,2,3,4,5,6,7,    0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,    0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,    0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,    0x40,0x41,0x42,0x43,0x44,0x45,0x46,0x47,    0x50,0x51,0x52,0x53,0x54,0x55,0x56,0x57,    0x60,0x61,0x62,0x63,0x64,0x65,0x66,0x67,    0x70,0x71,0x72,0x73,0x74,0x75,0x76,0x77};short unmap[120]=   {0,1,2,3,4,5,6,7,-1,-1,-1,-1,-1,-1,-1,-1,    8,9,10,11,12,13,14,15,-1,-1,-1,-1,-1,-1,-1,-1,    16,17,18,19,20,21,22,23,-1,-1,-1,-1,-1,-1,-1,-1,    24,25,26,27,28,29,30,31,-1,-1,-1,-1,-1,-1,-1,-1,    32,33,34,35,36,37,38,39,-1,-1,-1,-1,-1,-1,-1,-1,    40,41,42,43,44,45,46,47,-1,-1,-1,-1,-1,-1,-1,-1,    48,49,50,51,52,53,54,55,-1,-1,-1,-1,-1,-1,-1,-1,    56,57,58,59,60,61,62,63};short Dcode[120]=    {0,1,1,1,1,1,1,1,0,0,0,0,0,0,0x0E,0x0F,    0x10,0x11,0x12,0,0,0,0,0,0,0,0,0,0,0,0x0F,0x1F,    0x10,0x21,0x11,0,0,0,0,0,0,0,0,0,0,0x0F,0,0,    0x10,0,0,0x11,0,0,0,0,0,0,0,0,0x0F,0,0,0,    0x10,0,0,0,0x11,0,0,0,0,0,0,0x0F,0,0,0,0,    0x10,0,0,0,0,0x11,0,0,0,0,0x0F,0,0,0,0,0,    0x10,0,0,0,0,0,0x11,0,0,0x0F,0,0,0,0,0,0,    0x10,0,0,0,0,0,0,0x11};short Stboard[64]=   {rook,knight,bishop,queen,king,bishop,knight,rook,    pawn,pawn,pawn,pawn,pawn,pawn,pawn,pawn,    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,    pawn,pawn,pawn,pawn,pawn,pawn,pawn,pawn,    rook,knight,bishop,queen,king,bishop,knight,rook};short Stcolor[64]=   {white,white,white,white,white,white,white,white,    white,white,white,white,white,white,white,white,    2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,    black,black,black,black,black,black,black,black,    black,black,black,black,black,black,black,black};short sweep[7]= {false,false,false,true,true,true,false};short Dpwn[3]={4,6,0};short Dstart[7]={6,4,8,4,0,0,0};short Dstop[7]={7,5,15,7,3,7,7};short Dir[16]={1,0x10,-1,-0x10,0x0F,0x11,-0x0F,-0x11,               0x0E,-0x0E,0x12,-0x12,0x1F,-0x1F,0x21,-0x21};short Pdir[34]={0,0x38,0,0,0,0,0,0,0,0,0,0,0,0,0x02,0x35,                0x38,0x35,0x02,0,0,0,0,0,0,0,0,0,0,0,0,0x02,                0,0x02};short pbit[7]={0,0x01,0x02,0x04,0x08,0x10,0x20};unsigned short killr0[maxdepth],killr1[maxdepth],killr2[maxdepth];unsigned short killr3[maxdepth],PrVar[maxdepth];unsigned short PV,hint,Swag0,Swag1,Swag2,Swag3,Swag4;unsigned short hashkey;unsigned long hashbd;struct hashval hashcode[2][7][64];struct hashentry huge *ttable,*ptbl;unsigned char history[8192];short Mwpawn[64],Mbpawn[64],Mknight[2][64],Mbishop[2][64];short Mking[2][64],Kfield[2][64];short value[7]={0,valueP,valueN,valueB,valueR,valueQ,valueK};short control[7]={0,ctlP,ctlN,ctlB,ctlR,ctlQ,ctlK};short PassedPawn0[8]={0,60,80,120,200,360,600,800};short PassedPawn1[8]={0,30,40,60,100,180,300,800};short PassedPawn2[8]={0,15,25,35,50,90,140,800};short PassedPawn3[8]={0,5,10,15,20,30,140,800};short ISOLANI[8] = {-12,-16,-20,-24,-24,-20,-16,-12};short BACKWARD[8] = {-6,-10,-15,-21,-28,-28,-28,-28};short BMBLTY[14] = {-2,0,2,4,6,8,10,12,13,14,15,16,16,16};short RMBLTY[14] = {0,2,4,6,8,10,11,12,13,14,14,14,14,14};short Kthreat[16] = {0,-8,-20,-36,-52,-68,-80,-80,-80,-80,-80,-80,                     -80,-80,-80,-80};short KNIGHTPOST,KNIGHTSTRONG,BISHOPSTRONG,KATAK,KBNKsq;short PEDRNK2B,PWEAKH,PADVNCM,PADVNCI,PAWNSHIELD,PDOUBLED,PBLOK;short RHOPN,RHOPNX,KHOPN,KHOPNX,KSFTY;short ATAKD,HUNGP,HUNGX,KCASTLD,KMOVD,XRAY,PINVAL;short stage,stage2,Zwmtl,Zbmtl,Developed[2],PawnStorm;short PawnBonus,BishopBonus,RookBonus;short KingOpening[64]=   {  0,  0, -4,-10,-10, -4,  0,  0,     -4, -4, -8,-12,-12, -8, -4, -4,    -12,-16,-20,-20,-20,-20,-16,-12,    -16,-20,-24,-24,-24,-24,-20,-16,    -16,-20,-24,-24,-24,-24,-20,-16,    -12,-16,-20,-20,-20,-20,-16,-12,     -4, -4, -8,-12,-12, -8, -4, -4,      0,  0, -4,-10,-10, -4,  0,  0};short KingEnding[64]=   { 0, 6,12,18,18,12, 6, 0,     6,12,18,24,24,18,12, 6,    12,18,24,30,30,24,18,12,    18,24,30,36,36,30,24,18,    18,24,30,36,36,30,24,18,    12,18,24,30,30,24,18,12,     6,12,18,24,24,18,12, 6,     0, 6,12,18,18,12, 6, 0};short DyingKing[64]=   { 0, 8,16,24,24,16, 8, 0,     8,32,40,48,48,40,32, 8,    16,40,56,64,64,56,40,16,    24,48,64,72,72,64,48,24,    24,48,64,72,72,64,48,24,    16,40,56,64,64,56,40,16,     8,32,40,48,48,40,32, 8,     0, 8,16,24,24,16, 8, 0};short KBNK[64]=   {99,90,80,70,60,50,40,40,    90,80,60,50,40,30,20,40,    80,60,40,30,20,10,30,50,    70,50,30,10, 0,20,40,60,    60,40,20, 0,10,30,50,70,    50,30,10,20,30,40,60,80,    40,20,30,40,50,60,80,90,    40,40,50,60,70,80,90,99};short pknight[64]=   { 0, 4, 8,10,10, 8, 4, 0,     4, 8,16,20,20,16, 8, 4,     8,16,24,28,28,24,16, 8,    10,20,28,32,32,28,20,10,    10,20,28,32,32,28,20,10,     8,16,24,28,28,24,16, 8,     4, 8,16,20,20,16, 8, 4,     0, 4, 8,10,10, 8, 4, 0};short pbishop[64]=   {14,14,14,14,14,14,14,14,    14,22,18,18,18,18,22,14,    14,18,22,22,22,22,18,14,    14,18,22,22,22,22,18,14,    14,18,22,22,22,22,18,14,    14,18,22,22,22,22,18,14,    14,22,18,18,18,18,22,14,    14,14,14,14,14,14,14,14};short PawnAdvance[64]=   { 0, 0, 0, 0, 0, 0, 0, 0,     4, 4, 4, 0, 0, 4, 4, 4,     6, 8, 2,10,10, 2, 8, 6,     6, 8,12,16,16,12, 8, 6,     8,12,16,24,24,16,12, 8,    12,16,24,32,32,24,16,12,    12,16,24,32,32,24,16,12,     0, 0, 0, 0, 0, 0, 0, 0};     main(argc,argv)int argc; char *argv[];{#ifdef MSDOS  ttable = (struct hashentry huge *)farmalloc(ttblsz *           (unsigned long)sizeof(struct hashentry));#else  ttable = (struct hashentry *)malloc(ttblsz *           (unsigned long)sizeof(struct hashentry));#endif  Level = 0; TCflag = false; OperatorTime = 0;  if (argc == 2) Level = atoi(argv[1]);  if (argc == 3)    {      TCmoves = atoi(argv[1]); TCminutes = atoi(argv[2]); TCflag = true;    }  Initialize();  NewGame();#if (NEWMOVE > 0)  Initialize_dist();#if (NEWMOVE > 1)  Initialize_moves();#endif#endif  while (!(quit))    {      if (bothsides && !mate) SelectMove(opponent,1); else InputCommand();      if (!(quit || mate || force)) SelectMove(computer,1);    }  ExitChess();}/* ............    INTERFACE ROUTINES    ........................... */int VerifyMove(s,iop,mv)char s[];short iop;unsigned short *mv;/*   Compare the string 's' to the list of legal moves available for the    opponent. If a match is found, make the move on the board. */{static short pnt,tempb,tempc,tempsf,tempst,cnt;static struct leaf xnode;struct leaf *node;  *mv = 0;  if (iop == 2)    {      UnmakeMove(opponent,&xnode,&tempb,&tempc,&tempsf,&tempst);      return(false);    }  cnt = 0;  MoveList(opponent,2);  pnt = TrPnt[2];  while (pnt < TrPnt[3])    {      node = &Tree[pnt++];      algbr(node->f,node->t,(short) node->flags & cstlmask);      if (strcmp(s,mvstr1) == 0 || strcmp(s,mvstr2) == 0)        {          cnt++; xnode = *node;        }    }  if (cnt == 1)    {      MakeMove(opponent,&xnode,&tempb,&tempc,&tempsf,&tempst);      if (SqAtakd(PieceList[opponent][0],computer))        {          UnmakeMove(opponent,&xnode,&tempb,&tempc,&tempsf,&tempst);          ShowMessage("Illegal Move!!");          return(false);        }      else        {          if (iop == 1) return(true);          if (xnode.flags & epmask) UpdateDisplay(0,0,1,0);          else UpdateDisplay(xnode.f,xnode.t,0,xnode.flags & cstlmask);          if (xnode.flags & cstlmask) Game50 = GameCnt;          else if (board[xnode.t] == pawn || (xnode.flags & capture))             Game50 = GameCnt;          GameList[GameCnt].depth = GameList[GameCnt].score = 0;          GameList[GameCnt].nodes = 0;          ElapsedTime(1);          GameList[GameCnt].time = (short)et;          TimeControl.clock[opponent] -= et;          --TimeControl.moves[opponent];          *mv = (xnode.f << 8) + xnode.t;          algbr(xnode.f,xnode.t,false);          return(true);        }     }  if (cnt > 1) ShowMessage("Ambiguous Move!");  return(false);}NewGame()/*   Reset the board and other variables to start a new game.*/{short l,r,c,p;  mate = quit = reverse = bothsides = post = false;  hashflag = force = PawnStorm = false;  beep = rcptr = easy = true;  lpost =  NodeCnt = epsquare = et0 = 0;  dither = 0;  Awindow = 90;  Bwindow = 90;  xwndw = 90;  MaxSearchDepth = 29;  contempt = 0;  GameCnt = -1; Game50 = 0;  Zwmtl = Zbmtl = 0;  Developed[white] = Developed[black] = false;  castld[white] = castld[black] = false;  kingmoved[white] = kingmoved[black] = 0;  PawnThreat[0] = CptrFlag[0] = Threat[0] = false;  Pscore[0] = 12000; Tscore[0] = 12000;  opponent = white; computer = black;  for (r = 0; r < 8; r++)    for (c = 0; c < 8; c++)      {        l = 8*r+c; locn[r][c] = l;        row[l] = r; column[l] = c;        board[l] = Stboard[l]; color[l] = Stcolor[l];      }  for (c = white; c <= black; c++)    for (p = pawn; p <= king; p++)      for (l = 0; l < 64; l++)        {          hashcode[c][p][l].key = (unsigned short)rand();          hashcode[c][p][l].bd = ((unsigned long)rand() << 16) +                                 (unsigned long)rand();        }  ClrScreen();  if (TCflag) SetTimeControl();  else if (Level == 0) SelectLevel();  UpdateDisplay(0,0,1,0);  InitializeStats();  time0 = time((long *)0);  ElapsedTime(1);  GetOpenings();}algbr(f,t,iscastle)short f,t,iscastle;{  mvstr1[0] = cxx[column[f]]; mvstr1[1] = rxx[row[f]];  mvstr1[2] = cxx[column[t]]; mvstr1[3] = rxx[row[t]];  mvstr2[0] = qxx[board[f]];  mvstr2[1] = mvstr1[2]; mvstr2[2] = mvstr1[3];  mvstr1[4] = '\0'; mvstr2[3] = '\0';  if (iscastle)    if (t > f) strcpy(mvstr2,"o-o");    else strcpy(mvstr2,"o-o-o");}/* ............    MOVE GENERATION & SEARCH ROUTINES    .............. */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -