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

📄 global.cpp

📁 一个人工智能的国际象棋游戏
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/****************************************************
Author: S. Senthil kumar
File: Global.cpp
Purpose:    For storing global information

******************************************************/

#include "OneMove.h"
#include "LastMove.h"
#include "Global.h"
#include "MusicPlayer.h"
#include "SearchAgent.h"
#include "myframe.h"
#include "TransTableEntry.h"
#include "TranspositionTable.h"
#include "myapp.h"


int end_of_game=0;		//Boolean variable to indicate playing of game
int drawthinking=0;		//Boolean variable to indicate whether thinking for a draw or just a normal move
int autoplay=0;			//Boolean variable to indicate bg music autoplay turned on/off
int sndenable=0;		//Boolean variable to indicate whether sounds for events enabled
CString defdir;			//Default directory for music files. Current Directory+"sounds"
CString deflevel;		//Default level

int computer_color=BLACK;	//Default Computer playing color
int search_depth=5;			//Default search depth (level: Intermediate)
int WHITEBASE=0;			//White pieces at top of board
int BLACKBASE=7;			//Black pieces at bottom
CString GetMusicDirectory();	//Retrieves Current Directory+"sounds"
int CheckCheck(int board[8][8],int target,int whilethinking=0);
UINT StartSearch(LPVOID param);
UINT Ponder(LPVOID param);
UINT RandomMusicPlay(LPVOID PARAM);
void AssignDefaults(int sndenable,int autoplay,CString deflevel,CString defdir,int ws,int blacks);
CStatusBarCtrl statusbar;		//Statusbar of window

volatile int timeover=0;	//Not used
volatile int searching=0;	//To indicate whether searching for move. Used to disable certain menu items
int debug=0;				//Flag variable
CWinThread * ponder_thread=NULL;	//Pondering thread handle
int pondering=0;					//Flag variable whether pondering
int threadrunning=0;				//Not used
int firsttime=1;					//First move. To play hardcoded pawn move
int movecount;						//Total moves played.

int whiteking_stalemate=0;			//Whether white king is under check currently. To check occurrence of stalemate
int blackking_stalemate=0;			//Whether black king is under check currently. To check occurrence of stalemate
int music_play=1;					//To control display of menuitem Play/Stop music

int BOARDSIZE=sizeof(int)*64;		//Boardsize. Used in memcpy()

extern COLORREF WHITESQUARE;		//Whitesquare color
extern COLORREF BLACKSQUARE;		//Blacksquare color

TranspositionTable table;			//Global Transpositiontable storing best moves for boards


char *filearray[50];				//Filearray used for Playing bg music
int count=0;						//Just a loop variable
		
volatile int keep_music_playing=1;	//Controlling bg music thread. Thread stops if set to 0
myapp a;							//CWinApp object.



CString GetMusicDirectory()
{
	char *buf=new char[defdir.GetLength()];
	::GetShortPathName(defdir,buf,defdir.GetLength());
	
	return (CString)buf;
}
	
/*Thread plays random files from GetMusicDirectory+"sounds"*/
UINT RandomMusicPlay(LPVOID PARAM)
{
		
	CString type;
	char lpszReturnString[10];
	while (keep_music_playing)
	{
		//Getting a random filename from filearray
		srand((unsigned int)time(NULL));
		int val=rand()%count;
		CString selectedfile=filearray[val];
		selectedfile.MakeUpper();
	
		//Getting file extension
		int dotposn=selectedfile.ReverseFind('.');
		if (dotposn!=-1)
		{
			CString ext=selectedfile.Mid((dotposn+1),3);
			//Setting type according to extension
			if (ext=="WAV")
			{
				type="waveaudio";
			}
			else if(ext=="RMI" || ext=="MID")
			{
				type="sequencer";
			}
			else if(ext=="CDA")
			{
				type="cdaudio";
			}
			statusbar.SetText((CString)"Playing "+selectedfile,2,0);	
			
			//Generating commandstring
			CString commandstring="open "+GetMusicDirectory()+"\\"+selectedfile+" type "+type+" alias aria";
			//Opening and playing using mciSendString()
			int retval=mciSendString(commandstring,lpszReturnString, lstrlen(lpszReturnString), NULL);
			mciSendString("play aria",lpszReturnString, lstrlen(lpszReturnString), NULL);
			strcpy(lpszReturnString,"playing");
			
			//Polling to see whether system is playing the file. Breaks from the loop on end of file.
			while(1)
			{
				mciSendString("status aria mode",lpszReturnString,10,NULL);
				if (strcmp(lpszReturnString,"playing")!=0)
				{
					break;
				}
			}
			//Closing the resource.
			mciSendString("close aria",lpszReturnString,lstrlen(lpszReturnString),NULL);
			statusbar.SetText((CString)"Finished Playing "+selectedfile,2,0);
			//Go back to top to select another file.
		}
		
		
		
	}
	
	statusbar.SetText((CString)"Stopped Playing Music",2,SBT_NOBORDERS);	


	return 0;
}



/*
int CheckMate(int board[8][8])
{
	debug=1;
	SearchAgent s(0,board,2,13850,-13850);
	char buf[255];
	debug=0;
	MessageBox(NULL,itoa(s.RetVal,buf,10),"",0);
	if (s.RetVal>=5000)
	{
		MessageBox(NULL,"White Won","End of Game",0);
		end_of_game=1;
		return 1;
	}
	return 0;	
}
*/

//Function to check whether target is in range of attack from another piece. Currently used by CheckForCheckMate for checking for a check against the king.
int InPieceRange(int board[8][8],int target)
{
	int i,j,k;
	int currentx,currenty;
	int found=0;
	int check=0;int checkmate=1;
	
	//Get the board position of the target.
	for (i=0;i<8 && !found;i++)
	{
		for (j=0;j<8;j++)
		{
			if (board[i][j]==target)
			{
				found=1;
				break;
			}
		}
	}
	i--;
	currenty=i;
	currentx=j;
	
	if (target==WHITE_KING)
	{
		//Checking along column, straight top
		for (k=i-1;k>=0;k--)
			{
				if (board[k][j]<10 && board[k][j]!=0)
				{
					
					break;
				}
				else
				{
					if (  board[k][j]>10)
					{
						if ((board[k][j]==BLACK_KING) && k==i-1) return 1;
						else if (board[k][j]==BLACK_ROOK || board[k][j]==BLACK_QUEEN)
						{
						
							return 1;
						}
						else
						{
							break;
						}
						
					}
					
				}

				
			}
		//Checking along column, straight down
		for (k=i+1;k<8;k++)
			{
				if (board[k][j]<10 && board[k][j]!=0)
				{
					
					break;
				}
				else
				{
					if (  board[k][j]>10)
					{
						if ((board[k][j]==BLACK_KING) && k==i+1) return 1;
						else if (board[k][j]==BLACK_ROOK || board[k][j]==BLACK_QUEEN)
						{
							
							return 1;
						}
						else
						{
							break;
						}
						
					}
					
				}
			}
			//Checking along row, along the right
			for (k=j+1;k<8;k++)
			{
				if (board[i][k]<10 && board[i][k]!=0)
				{
					
					break;
				}
				else
				{
					if (  board[i][k]>10)
					{
						if ((board[i][k]==BLACK_KING) && k==j+1) return 1;
						else if (board[i][k]==BLACK_ROOK || board[i][k]==BLACK_QUEEN)
						{
							
							return 1;
						}
						else
						{
							break;
						}
						
					}
					
				}
			}
			//Checking along row, along the left.
			for (k=j-1;k>=0;k--)
			{
				if (board[i][k]<10 && board[i][k]!=0)
				{
					break;
				}
				else
				{
					if (  board[i][k]>10)
					{
						if ((board[i][k]==BLACK_KING) && k==j-1) return 1;
						else if (board[i][k]==BLACK_ROOK || board[i][k]==BLACK_QUEEN)
						{
							//MessageBox("First"+(CString)itoa(k,buf,10));	
							return 1;
						}
						else
						{
							break;
						}
						
					}
					
					
				}
				
			}
			
			int m;
			//Now search diagnols.
			for (k=i-1,m=j-1;k>=0 && m>=0;k--,m--)
			{
				if (board[k][m]<10 && board[k][m]!=0)
				{
					break;
				}
				else
				{
					if (board[k][m]>10)
					{
						
						if ((board[k][m]==BLACK_KING) && k==i-1 && m==j-1) return 1;
						else if (board[k][m]==BLACK_QUEEN || board[k][m]==BLACK_BISHOP)
						{
							return 1;
						}
						else
						{
							break;
						}
						
					}
					
				}
			}
			for (k=i-1,m=j+1;k>=0 && m<8 ;k--,m++)
			{
				if (board[k][m]<10 && board[k][m]!=0)
				{
					break;
				}
				else
				{
					if (board[k][m]>10)
					{
						if ((board[k][m]==BLACK_KING) && k==i-1 && m==j+1) return 1;
						else if (board[k][m]==BLACK_QUEEN || board[k][m]==BLACK_BISHOP)
						{
							return 1;
						}
						else
						{
							break;
						}
						
					}
						
				}
			}
			
			for (k=i+1,m=j+1;k<8 && m<8;k++,m++)
			{
				
				if (board[k][m]<10 && board[k][m]!=0)
				{
					break;
				}
				else
				{
					if (board[k][m]>10)
					{
						if ((board[k][m]==BLACK_KING) && k==i+1 && m==j+1) return 1;
						else if (board[k][m]==BLACK_PAWN)
						{
							if ((k==i+1) && (m==j+1))
							{
								return 1;
							}
						}
						else if (board[k][m]==BLACK_QUEEN || board[k][m]==BLACK_BISHOP)
						{
							return 1;
						}
						else
						{
							break;
						}
			
					}
					
				}

			}
			for (k=i+1,m=j-1;k<8 && m>=0;k++,m--)
			{
				if (board[k][m]<10 && board[k][m]!=0)
				{
					break;
				}
				else
				{
					if (board[k][m]>10)
					{
						if ((board[k][m]==BLACK_KING) && k==i+1 && m==j-1) return 1;
						else if (board[k][m]==BLACK_PAWN)
						{
							if (k==i+1 && m==j-1)
							{
								return 1;
							}
						}
						else if (board[k][m]==BLACK_QUEEN || board[k][m]==BLACK_BISHOP)
						{
							return 1;
						}
						else
						{
							break;
						}
			
					}
						
				}
			}
			if ( (i<7 && j<6 && board[i+1][j+2]==BLACK_KNIGHT) || ((i>0 && j<6) && board[i-1][j+2]==BLACK_KNIGHT) || ((i<6 && j<7) &&board[i+2][j+1]==BLACK_KNIGHT) || ((i<6 && j>0) && board[i+2][j-1]==BLACK_KNIGHT))
			{
				return 1;
			}
			if (((i<7 && j>1) && board[i+1][j-2]==BLACK_KNIGHT) || ((i>0 && j>1) && board[i-1][j-2]==BLACK_KNIGHT) || ((i>1 && j>0) && board[i-2][j-1]==BLACK_KNIGHT) || ((i>1 && j<7) && board[i-2][j+1]==BLACK_KNIGHT))
			{
				return 1;
			}
			
			return 0;
				
		}
		
		//Same for Black King. Opponent piece only differs.
		else
		{
			for (k=i-1;k>=0;k--)
			{
				if (board[k][j]>10)
				{
					break;
				}
				else
				{
					if (  board[k][j]<10)
					{
						if ((board[k][j]==WHITE_KING) && k==i-1 ) return 1;
						if (board[k][j]==WHITE_ROOK || board[k][j]==WHITE_QUEEN)
						{
						
							return 1;
						}
						if (board[k][j]!=0)	
						break;
					}
					
				
				}

				
			}
		for (k=i+1;k<8;k++)
			{
				if (board[k][j]>10)
				{
					
					break;
				}
				else
				{
					if (  board[k][j]<10)
					{
						if ((board[k][j]==WHITE_KING) && k==i+1 ) return 1;
						if (board[k][j]==WHITE_ROOK || board[k][j]==WHITE_QUEEN)
						{
							
							return 1;
						}
					if (board[k][j]!=0)	
						break;		
					
					}
					
				}
			}
			for (k=j+1;k<8;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;
					}
					
				}

			}

⌨️ 快捷键说明

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