📄 global.cpp
字号:
for (k=j-1;k>=0;k--)
{
if (board[i][k]>10 )
{
break;
}
else
{
if ( board[i][k]<10)
{
if ((board[i][k]==WHITE_KING) && k==j-1 ) return 1;
if (board[i][k]==WHITE_ROOK || board[i][k]==WHITE_QUEEN)
{
return 1;
}
if (board[i][k]!=0)
break;
}
}
}
int m;
for (k=i-1,m=j-1;k>=0 && m>=0;k--,m--)
{
if (board[k][m]>10)
{
break;
}
else
{
if (board[k][m]<10)
{
if ((board[k][m]==WHITE_KING) && k==i-1 && m==j-1 ) return 1;
if (board[k][m]==WHITE_PAWN)
{
if (k==i-1 && m==j-1)
{
return 1;
}
}
if (board[k][m]==WHITE_QUEEN || board[k][m]==WHITE_BISHOP)
{
return 1;
}
if (board[k][m]!=0)
break;
}
}
}
for (k=i-1,m=j+1;k>=0 && m<8 ;k--,m++)
{
if (board[k][m]>10)
{
break;
}
else
{
if (board[k][m]<10)
{
if ((board[k][m]==WHITE_KING) && k==i-1 && m==j+1 ) return 1;
if (board[k][m]==WHITE_PAWN)
{
if ((k==i-1) && (m==j+1))
{
return 1;
}
}
if (board[k][m]==WHITE_QUEEN || board[k][m]==WHITE_BISHOP)
{
return 1;
}
if (board[k][m]!=0)
break;
}
}
}
for (k=i+1,m=j+1;k<8 && m<8;k++,m++)
{
if (board[k][m]>10)
{
break;
}
else
{
if (board[k][m]<10)
{
if ((board[k][m]==WHITE_KING) && k==i+1 && m==j+1 ) return 1;
if (board[k][m]==WHITE_QUEEN || board[k][m]==WHITE_BISHOP)
{
return 1;
}
if (board[k][m]!=0)
break;
}
}
}
for (k=i+1,m=j-1;k<8 && m>=0;k++,m--)
{
if (board[k][m]>10)
{
break;
}
else
{
if (board[k][m]<10)
{
if ((board[k][m]==WHITE_KING) && k==i+1 && m==j-1 ) return 1;
if (board[k][m]==WHITE_QUEEN || board[k][m]==WHITE_BISHOP)
{
return 1;
}
if (board[k][m]!=0)
break;
}
}
}
if ( (i<7 && j<6 && board[i+1][j+2]==WHITE_KNIGHT) || ((i>0 && j<6) && board[i-1][j+2]==WHITE_KNIGHT) || ((i<6 && j<7) &&board[i+2][j+1]==WHITE_KNIGHT) || ((i<6 && j>0) && board[i+2][j-1]==WHITE_KNIGHT))
{
return 1;
}
if (((i<7 && j>1) && board[i+1][j-2]==WHITE_KNIGHT) || ((i>0 && j>1) && board[i-1][j-2]==WHITE_KNIGHT) || ((i>1 && j>0) && board[i-2][j-1]==WHITE_KNIGHT) || ((i>1 && j<7) && board[i-2][j+1]==WHITE_KNIGHT))
{
return 1;
}
return 0;
}
}
//Function checks whether target is in check.Calls InPieceRange. Used only for displaying on status bar.
int CheckCheck(int currentboard[8][8],int target,int whilethinking)
{
CString targetname;
if (target==BLACK_KING)
{
targetname="Black King";
//MessageBox("Checking for Black King");
}
else
{
targetname="White King";
}
//}
if (InPieceRange(currentboard,target))
{
/*if (target==BLACK_KING)
CheckMate(currentboard);
*/
if (!whilethinking)
statusbar.SetWindowText("Check Alert!!! "+targetname+" is under check");
return 1;
}
return 0;
}
/*Checks for Checkmate. Calls searchagent with depth 2. Also detect stalemate by verifying wheter
king is in check before checking for checkmate.If king is not in check, then it is stalemate.*/
void CheckForCheckMate(int board[8][8])
{
int target=0;
if (!CheckCheck(board,WHITE_KING,1))
{
whiteking_stalemate=1;
}
else
{
whiteking_stalemate=0;
}
if (!CheckCheck(board,BLACK_KING,1))
{
blackking_stalemate=1;
}
else
{
blackking_stalemate=0;
}
//Call SearchAgent with opposite color.
if (computer_color==WHITE)
SearchAgent s(0,board,2,-13850,13850,1);
else
SearchAgent s(1,board,2,-13850,13850,1);
}
/*The Main Search Thread. Calls searchagent with specified parameters. Also makes the first move
and generates the algebraic notation for move currently played.
*/
UINT StartSearch(LPVOID param)
{
if (!drawthinking)
{
statusbar.SetWindowText("The Genius is thinking ..... Don't disturb");
}
else
{
statusbar.SetWindowText("The Genius is considering your proposal");
}
OneMove temp;
//Get myframe object.
myframe *obj=(myframe *)param;
char buf[10];
//If this is the first move.
if (firsttime==1)
{
//Reject draw offer made on or before first move.
if (drawthinking==1)
{
MessageBox(NULL,"Sorry, The Genius has rejected your proposal for a draw","Draw offer not accepted",0);
drawthinking=0;
return 0;
}
//Generating algebraic move notation
CString move;
move+=(CString)itoa(++(movecount),buf,10)+". ";
//If computer playing white, move specified pawn.
if (computer_color==WHITE)
{
firsttime=0;
if (WHITEBASE==0)
{
obj->startsquare[1]=1;
obj->endsquare[1]=3;
move+=(char)((3)+97);move+=itoa(8-(1),buf,10);move+=" - ";move+=(char)(3+97);move+=itoa(8-(3),buf,10);
}
else
{
obj->startsquare[1]=6;
obj->endsquare[1]=4;
move+=(char)((3)+97);move+=itoa((1),buf,10);move+=" - ";move+=(char)(3+97);move+=itoa((3),buf,10);
}
obj->startsquare[0]=3;
obj->endsquare[0]=3;
obj->movelist.AddString(move);
obj->movePieceInRealBoard();
obj->lastmoved=WHITE;
statusbar.SetWindowText("Your turn now");
}
else
{
//Computer is playing black. Make specified pawn move.
firsttime=0;
if (BLACKBASE==0)
{
obj->startsquare[1]=1;
obj->endsquare[1]=3;
move+=(char)((3)+97);move+=itoa(8-(1),buf,10);move+=" - ";move+=(char)(3+97);move+=itoa(8-(3),buf,10);
}
else
{
obj->startsquare[1]=6;
obj->endsquare[1]=4;
move+=(char)((3)+97);move+=itoa((1),buf,10);move+=" - ";move+=(char)(3+97);move+=itoa((3),buf,10);
}
obj->startsquare[0]=3;
obj->endsquare[0]=3;
obj->movelist.AddString(move);
obj->movePieceInRealBoard();
obj->lastmoved=BLACK;
statusbar.SetWindowText("Your turn now");
}
return 0;
}
//Clone current board. (Because array is passed by reference)
int newboard[8][8];
memmove(newboard,obj->board,sizeof(int)*64);
timeover=0;
int color=0;int alpha;int beta;
//Set parameters according to color
if (computer_color==WHITE)
{
color=1;
alpha=-13850;
beta=10000;
}
else
{
color=0;
alpha=-13850;
beta=13850;
}
//Set searching to one
searching=1;
//Disable certain menu items. (like Level)
obj->enable_menus=0;
//Actual AI Agent
SearchAgent s(color,newboard,search_depth,alpha,beta); //Invoke AI Agent
//Enable disabled menuitems.
obj->enable_menus=1;
searching=0;
//If thinking has been made for a draw then
if (drawthinking)
{
drawthinking=0;
//If return value indicates loss for computer color, then accept else reject.
if ((computer_color==WHITE && s.RetVal<-100) || (computer_color==BLACK && s.RetVal>100))
{
MessageBox(NULL,"The Genius accepts your proposal gracefully","Draw Offer Accepted",0);
end_of_game=1;
}
else
{
MessageBox(NULL,"Sorry, The Genius has rejected your proposal for a draw","Draw offer not accepted",0);
drawthinking=0;
}
return 0;
}
//If game has not been ended. SearchAgent modifies end_of_game on seeing checkmate or stalemate
if (!end_of_game)
{
//Get Best move
temp=s.getBestMove();
//Set variables to point to new move.
obj->piece=temp.piece;
obj->startsquare[0]=temp.sourcex;
obj->startsquare[1]=temp.sourcey;
obj->endsquare[0]=temp.destx;
obj->endsquare[1]=temp.desty;
//Generate algebraic notation for move.
CString move;
move+=(CString)itoa(++(movecount),buf,10)+". ";
if ((computer_color==WHITE && WHITEBASE==0) || (computer_color==BLACK && BLACKBASE==0))
{
move+=(char)((temp.sourcex)+97);move+=itoa(8-(temp.sourcey),buf,10);move+=" - ";move+=(char)(temp.destx+97);move+=itoa(8-(temp.desty),buf,10);
}
else
{
move+=(char)((temp.sourcex)+97);move+=itoa(8-(temp.sourcey),buf,10);move+=" - ";move+=(char)(temp.destx+97);move+=itoa(8-(temp.desty),buf,10);
}
obj->movelist.AddString(move);
//Add move to undo.
if (computer_color==WHITE)
{
obj->undo.whitemove=temp;
obj->undo.cut_blackpiece=0;
if (obj->board[temp.desty][temp.destx]>10 )
{
//Display piece in respective area.
obj->DisplayCutPiece(obj->board[temp.desty][temp.destx]);
obj->undo.cut_blackpiece=obj->board[temp.desty][temp.destx];
}
}
else
{
obj->undo.cut_whitepiece=0;
obj->undo.blackmove=temp;
if (obj->board[temp.desty][temp.destx]<10 && obj->board[temp.desty][temp.destx]>0 )
{
obj->DisplayCutPiece(obj->board[temp.desty][temp.destx]);
obj->undo.cut_whitepiece=obj->board[temp.desty][temp.destx];
}
}
//Move piece in board.
obj->movePieceInRealBoard();
//Set lastmoved.
obj->lastmoved=(color==1?WHITE:BLACK);
//Generate another clone for checking for checkmate.
memmove(newboard,obj->board,sizeof(int)*64);
CheckForCheckMate(newboard);
if (!end_of_game)
{
statusbar.SetWindowText("Your turn now");
}
}
return 0;
}
//Used to ponder (ie) Think on opponent time.
UINT Ponder(LPVOID param)
{
statusbar.SetText("The Genius is thinking on your time",1,0);
myframe *obj=(myframe *)param;
char buf[10];
int newboard[8][8];
memmove(newboard,obj->board,sizeof(int)*64);
int color;
if (computer_color==WHITE) color=0;
else
color=1;
pondering=1;
//Loop through search depths until user makes move.
for (int i=4;;i++)
{
statusbar.SetText("Thinking "+(CString)itoa(i,buf,10)+" ahead",1,0);
SearchAgent s(color,newboard,i,-13850,13850); //Invoke AI Agent
}
pondering=0;
threadrunning=0;
return 0;
}
//Assign values to global variables based on Registry values. Called from myapp
void AssignDefaults(int snden,int autoply,CString dflevel,CString dfdir,int whitesquare,int blacksquare)
{
//Get Default directory and autoplay settings.
defdir=dfdir;
autoplay=autoply;
if (sndenable || autoplay)
{
//Check to see if sound can be played.
CString command="open "+GetMusicDirectory()+"\\Wav\\Blip.wav type waveaudio alias test";
char ret[100];
char errormsg[100];
int err=mciSendString(command,ret,100,NULL);
if (err==0)
{
err=mciSendString("play test",ret,100,NULL);
if (err!=0)
{
mciGetErrorString(err,errormsg,100);
MessageBox(NULL,errormsg,"",0);
sndenable=0;
autoplay=0;
}
}
else
{
//Sound cannot be played. Report Error.
mciGetErrorString(err,errormsg,100);
MessageBox(NULL,errormsg,"",0);
sndenable=0;
autoplay=0;
}
mciSendString("close test",ret,100,NULL);
}
//Start Autoplay.
if (autoplay==1){ MusicPlayer p; }
sndenable=snden;
//Set search depth according to default level.
if (dflevel=="I Dunno Chess")
{
search_depth=2;
}
else if(dflevel=="Novice")
{
search_depth=4;
}
else if(dflevel=="Intermediate")
{
search_depth=5;
}
else if(dflevel=="Expert")
{
search_depth=7;
}
//Set default black and white squares.
WHITESQUARE=(COLORREF)whitesquare;
BLACKSQUARE=(COLORREF) blacksquare;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -