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

📄 main.c

📁 GNU国际象棋C++源代码Windows版的
💻 C
📖 第 1 页 / 共 3 页
字号:
/*
 * main.c - C source for GNU CHESS for Windows
 *
 * Copyright (c) 1988,1989,1990 John Stanback
 * Copyright (c) 1992-1995 Free Software Foundation
 * Modified by Conor McCarthy for the Windows environment
 * Additions for the Windows version Copyright (c) 1996 Conor McCarthy
 *
 * This file is part of GNU CHESS.
 *
 * GNU Chess is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * GNU Chess is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with GNU Chess; see the file COPYING.  If not, write to
 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
 */
#ifdef WIN386
 #define INCLUDE_COMMDLG_H
#endif
#include "gnuchess.h"
#ifndef WIN386
 #include <commdlg.h>
#endif
#include "version.h"
#include "ttable.h"
#include <dos.h>
#include <signal.h>
#if defined Think_C && defined Window_Events
#include <console.h>
#endif
CHAR *ColorStr[2];
CHAR *CP[CPSIZE];

UTSHORT ETABLE = DEFETABLE;
CHAR savefile[128] = "";
CHAR listfile[128] = "";
#ifdef HISTORY
UTSHORT history[32768];
#endif
tshort svalue[64];
struct leaf Tree[TREE], *root;
SHORT TrPnt[MAXDEPTH+1];
SHORT PieceList[2][64], PawnCnt[2][8];
SHORT castld[2], Mvboard[64];
struct flags flag;
SHORT opponent, computer, dither;
tshort  WAwindow, WBwindow, WXwindow, BAwindow, BBwindow, BXwindow;
SHORT  INCscore;
long ResponseTime, ExtraTime, MaxResponseTime, et, et0, time0, ft;
unsigned long GenCnt, NodeCnt, ETnodes, EvalNodes;
long replus, reminus;
tshort HashDepth = HASHDEPTH, HashMoveLimit = HASHMOVELIMIT;
SHORT player;
struct GameRec GameList[MAXMOVES + MAXDEPTH];
SHORT Sdepth, Game50, MaxSearchDepth;
SHORT GameCnt = 0;
SHORT epsquare;
tshort  contempt, xwndw;
int Book;
struct TimeControlRec TimeControl;
int TCadd = 0;
SHORT TCflag, TCmoves, TCminutes, TCseconds, OperatorTime;
SHORT XCmoves[3], XCminutes[3], XCseconds[3],XCadd[3], XC, XCmore;
const SHORT otherside[3] =
{black, white, neutral};
SHORT hint;
SHORT TOflag;     /* force search re-init if we backup search */

SHORT mtl[2], pmtl[2], hung[2];
SHORT Pindex[64];
SHORT PieceCnt[2];
SHORT FROMsquare, TOsquare;
SHORT HasKnight[2], HasBishop[2], HasRook[2], HasQueen[2];
SHORT ChkFlag[MAXDEPTH], CptrFlag[MAXDEPTH], PawnThreat[MAXDEPTH];
SHORT QueenCheck[MAXDEPTH]; /* tom@izf.tno.nl */
SHORT NMoves[MAXDEPTH]; /* tom@izf.tno.nl */
SHORT Threat[MAXDEPTH]; /* tom@izf.tno.nl */
SHORT ThreatSave[MAXDEPTH]; /* tom@izf.tno.nl */
SHORT Pscore[MAXDEPTH], Tscore[MAXDEPTH];
const SHORT qrook[3] =
{0, 56, 0};
const SHORT krook[3] =
{7, 63, 0};
const SHORT kingP[3] =
{4, 60, 0};
const SHORT rank7[3] =
{6, 1, 0};
const SHORT sweep[8] =
{false, false, false, true, true, true, false, false};
UTSHORT killr0[MAXDEPTH+1], killr1[MAXDEPTH+1], killr2[MAXDEPTH+1];
UTSHORT PV, SwagHt, Swag0, Swag1, Swag2, Swag4, sidebit;
#ifdef KILLT
tshort killt[0x4000];
#endif
const SHORT value[7] =
{0, valueP, valueN, valueB, valueR, valueQ, valueK};
const SHORT control[7] =
{0, ctlP, ctlN, ctlB, ctlR, ctlQ, ctlK};
SHORT stage, stage2, Developed[2];
unsigned int starttime;
SHORT PCRASH, PCENTER;

char szAppName[]="GNU Chess";
char szChess[]="CHESS";
char szHelpFile[]="gnuchess.hlp";
char move[6];
SHORT command=0;
SHORT LineSpace,SLineSpace,CharWidth,CompColorExt,WhoseTurnExt,
     CompMoveExt,squarebd;
HWND hWnd;
HFONT hFont;
char msg[200];
SHORT selected;
SHORT selectX,selectY;
HANDLE hInst;
HBRUSH hBrushBackGround,hDlgBkgrnd;
HWND hComputerColor,hWhoseTurn,hInCheck,hComputerMove,hClockBlack,
     hClockWhite,hBlack,hWhite,hDepth,hStats;
HACCEL hAccel;
SHORT ok;
DWORD CustCols[16];
CHOOSECOLOR cc;
extern char *lpszTitle;
extern tshort DlgInt1,DlgInt2,DlgInt3,DlgInt4,DlgInt5,DlgInt6;
extern long size;
extern int Znodes;
char szBtnBar[]="btnbar";
extern SHORT button;
extern SHORT WinSizeX,WinSizeY,WinPosX,WinPosY;
extern HWND hBtnWnd;
extern SHORT origbrd[64],origcol[64];
SHORT OrigGameCnt;
extern RECT wr;
extern SHORT maximized,forcing;
BOOL CALLBACK (*lpfnDlgProc)(HWND, unsigned, WORD, LONG);


LRESULT WINAPI WndProc(HWND, UINT, WPARAM, LPARAM) ;
LRESULT WINAPI BtnWndProc(HWND, UINT, WPARAM, LPARAM) ;
void SizeWindow(int);
BOOL GetColor(DWORD);
void LoadDefaultColors(void);
BOOL LoadSettings(void);
void SaveSettings(void);
BOOL CALLBACK ChangeWinDlgProc (HWND, UINT, WPARAM, LPARAM);
BOOL CALLBACK HashDepthDlgProc (HWND, UINT, WPARAM, LPARAM);
BOOL CALLBACK GetNumDlgProc (HWND, UINT, WPARAM, LPARAM);
BOOL CALLBACK AboutDlgProc (HWND, UINT, WPARAM, LPARAM);
BOOL CALLBACK TimeDlgProc (HWND, UINT, WPARAM, LPARAM);
BOOL CALLBACK StatDlgProc (HWND, UINT, WPARAM, LPARAM);
BOOL CALLBACK TypeMoveDlgProc (HWND, UINT, WPARAM, LPARAM);
BOOL CALLBACK PromoteDlgProc (HWND, UINT, WPARAM, LPARAM);
BOOL CALLBACK CreateHashDlgProc (HWND, UINT, WPARAM, LPARAM);

int timeopp[MINGAMEIN], timecomp[MINGAMEIN];
int compptr, oppptr;
inline void
TimeCalc ()
{
/* adjust number of moves remaining in gamein games */
  int increment = 0;
  int topsum = 0;
  int tcompsum = 0;
  int me,him;
  int i;
/* dont do anything til you have enough numbers */
  if (GameCnt < (MINGAMEIN * 2)) return;
/* calculate average time in sec for last MINGAMEIN moves */
  for (i = 0; i < MINGAMEIN; i++)
    {
      tcompsum += timecomp[i];
      topsum += timeopp[i];
    }
  topsum /= (100 * MINGAMEIN);
  tcompsum /= (100 * MINGAMEIN);
/* if I have less time than opponent add another move */
   me = TimeControl.clock[computer]/100; 
   him = TimeControl.clock[opponent]/100;
   if(me < him) increment += 2;
/* if I am > 60 sec behind increment or if I am less and in last 2 min */
   if((him - me) > 60 || (me<him && me < 120))increment++;
/* if I am losing more time with each move add another */
  if ( ((me - him) > 60) && tcompsum < topsum) increment--;
  if ( tcompsum > topsum) increment +=2;
/* if I am doing really well use more time per move */
  else if (me > him && tcompsum < topsum) increment = -1;
  if (me > (him+60) ) increment = -1;
  TimeControl.moves[computer] += increment;
/* but dont let moves go below MINMOVES */
  if (TimeControl.moves[computer] < MINMOVES )TimeControl.moves[computer] = MINMOVES;
}

int PASCAL WinMain (HANDLE hInstance, HANDLE
hPrevInstance, LPSTR lpszCmdLine, int nCmdShow)
{
  WNDCLASS   wndClass;

  if (LoadSettings()) nCmdShow = SW_SHOWMAXIMIZED;
  hBrushBackGround=CreateSolidBrush(clrBackGround);
  hDlgBkgrnd=CreateSolidBrush(GetSysColor(COLOR_BTNFACE));

  wndClass.style        =0;
  wndClass.lpfnWndProc  =WndProc;
  wndClass.cbClsExtra   =0;
  wndClass.cbWndExtra   =0;
  wndClass.hInstance    =hInstance;
  wndClass.hIcon        =LoadIcon(hInstance,szChess);
  wndClass.hCursor      =LoadCursor(NULL,IDC_ARROW);
  wndClass.hbrBackground=0;
  wndClass.lpszMenuName =szChess;
  wndClass.lpszClassName=szAppName;
  if (!RegisterClass(&wndClass))return FALSE;
  wndClass.style=0;
  wndClass.lpfnWndProc=BtnWndProc;
  wndClass.hbrBackground=GetStockObject(LTGRAY_BRUSH);
  wndClass.lpszMenuName=0;
  wndClass.lpszClassName=szBtnBar;
  if (!RegisterClass(&wndClass))return false;

  hInst=hInstance;
  InitConst (0);
  GetMetrics();
  InitScreen();
  hAccel=LoadAccelerators(hInstance,szChess);
  hWnd = CreateWindow(szAppName,szAppName,WS_OVERLAPPEDWINDOW,
             WinPosX,WinPosY,WinSizeX,WinSizeY,NULL,NULL,hInstance,NULL);
  if (!WinSizeX || !WinSizeY) SizeWindow(1);
  ShowWindow(hWnd, nCmdShow);
  CheckMenuItem ( GetMenu(hWnd), boardsize/12+IDM_SMALL, MF_CHECKED);
  CreateStatics();
  {
    long t;        /*rough speed test to calculate Znodes*/
    SHORT i;
    t=GetTickCount();
    while(GetTickCount()-t<250) {Znodes++; for(i=0;i<5000;i++);}
  }
  gsrand (starttime = ((unsigned int) time ((long *) 0)));  /* init urand */
#ifdef ttblsz
  ttblsize = ttblsz;
  rehash = -1;
#endif /* ttblsz */
  ColorStr[0] = CP[40];
  ColorStr[1] = CP[41];
  TCflag = false;

 {
  SHORT i;
  for ( i=pawn; i<=king; i++ )
   {
     pieces[i].detail = LoadBitmap (hInst, MAKEINTRESOURCE(i+boardsize));
     pieces[i].mask = LoadBitmap (hInst, MAKEINTRESOURCE(i+boardsize+6));
   }
 }

  Initialize_dist ();
  Initialize_moves ();
  NewGame ();

  if (flag.post) /*if post is set in the ini file*/
   {
    flag.post=false;
    SendMessage(hWnd,WM_COMMAND,IDM_POST,0L);
   }

  OpenHashFile(); /* Open persistent transposition table */
  while (!(flag.quit))
    {
      oppptr = (oppptr + 1) % MINGAMEIN;
      if (flag.bothsides && !flag.mate && !flag.quit)
       {
        SaveBoard();
        SelectMove (opponent, 1);
        if (flag.abort || command) {command|=0x8000; InputCommand(); }
       }
      else
    InputCommand ();
      if (opponent == black)
   if (flag.gamein || TCadd)
     {
       TimeCalc ();
     }
   else if (TimeControl.moves[opponent] == 0 && TCflag)
     {
       SetTimeControl ();
     }

      compptr = (compptr + 1) % MINGAMEIN;
      if (!(flag.quit || flag.mate || flag.force))
   {
     SaveBoard();
     SelectMove (computer, 1);
     if (computer == black)
       if (flag.gamein)
         {
      TimeCalc ();
         }
       else if (TimeControl.moves[computer] == 0 && TCflag)
         {
      SetTimeControl ();
         }
   }
    }
        CloseHashFile();

  Exit(0);
  return 0;
}

LRESULT WINAPI WndProc(HWND hWnd, UINT Message,
                       WPARAM wParam, LPARAM lParam)
{
  HDC hDC;
  PAINTSTRUCT ps;

  switch(Message)
  { case WM_PAINT:
      hDC=BeginPaint(hWnd,&ps);
      UpdateDisplay(0,0,1,0,hDC);
      if (selected)
       {
         HRGN hRgn;

         hRgn=CreatePolygonRgn(&squares[selectX][selectY][0],
                 4, ALTERNATE);
         InvertRgn(hDC,hRgn);
         DeleteObject(hRgn);
       }
      EndPaint(hWnd,&ps);
      return 0;

⌨️ 快捷键说明

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