#include "Global.h"
#include "EvaluateSystem.h"
#include "SearchEngine.h"
using namespace std;
//辅助函数
void ShowTypeCount(int typeCount[2][13]);
void Print(int chessBoard[GRID_NUM][GRID_NUM]);
void ImportChessBoard(char *fileName,int chessBoard[][GRID_NUM]);
void Save(char *fileName,int chessBoard[][GRID_NUM]);
int main()
{
//int chessBoard[GRID_NUM][GRID_NUM]={
// {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255},
// {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255},
// {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255},
// {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255},
// {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255},
// {0, 255, 255, 255, 255, 255, 1, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255},
// {255, 255, 255, 255, 255, 255, 1, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255},
// {255, 255, 0, 255, 255, 255, 1, 255, 255, 1, 255, 255, 255, 255, 255, 255, 255, 255, 255},
// {255, 255, 255, 0, 255, 255, 1, 255, 255, 255, 0, 255, 255, 255, 255, 255, 255, 255, 255},
// {255, 255, 255, 255, 0, 1, 1, 1, 1, 1, 1, 255, 255, 255, 255, 255, 255, 255, 255},
// {255, 255, 255, 255, 255, 0, 1, 255, 255, 1, 255, 255, 255, 255, 255, 255, 255, 255, 255},
// {255, 255, 255, 1, 1, 1, 0, 255, 255, 1, 255, 255, 255, 255, 255, 255, 255, 255, 255},
// {255, 255, 255, 255, 1, 0, 255, 1, 255, 1, 255, 255, 255, 255, 255, 255, 255, 255, 255},
// {255, 255, 255, 255, 0, 1, 1, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255},
// {255, 255, 255, 0, 255, 255, 1, 0, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255},
// {255, 255, 0, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255},
// {255, 0, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255},
// {0, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255},
// {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}
//};
int chessBoard[GRID_NUM][GRID_NUM]={
{255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255},
{255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255},
{255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255},
{255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255},
{255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255},
{255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255},
{255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255},
{255, 255, 255, 255, 255, 255, 255, 255, 255, 0, 255, 255, 255, 255, 255, 255, 255, 255, 255},
{255, 255, 255, 255, 255, 255, 255, 255, 255, 0, 1, 255, 255, 255, 255, 255, 255, 255, 255},
{255, 255, 255, 255, 255, 255, 255, 255, 255, 0, 255, 1, 255, 255, 255, 255, 255, 255, 255},
{255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 1, 1, 255, 255, 255, 255, 255, 255, 255},
{255, 255, 255, 255, 255, 255, 255, 255, 255, 1, 255, 1, 255, 255, 255, 255, 255, 255, 255},
{255, 255, 255, 255, 255, 255, 255, 0, 0, 0, 0, 255, 255, 255, 255, 255, 255, 255, 255},
{255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255},
{255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255},
{255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255},
{255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255},
{255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255},
{255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}
};
char command[100],fileName[100];
int i,j;
while(true)
{
cin.clear();
cout<<"$";
cin>>command;
//显示棋型扫描结果
if(!stricmp(command,"show"))
{
scan(chessBoard);
ShowTypeCount(TypeCount);
}
//打印棋盘
else if(!stricmp(command,"print"))
{
Print(chessBoard);
}
//显示评估值
else if(!stricmp(command,"eval"))
{
scan(chessBoard);
cout<<"Black:"<<GetEvaluatedValue(chessBoard,Black);
cout<<"\tWhite:"<<GetEvaluatedValue(chessBoard,White)<<endl;
}
//b i j,黑方走棋
else if(!stricmp(command,"b"))
{
cin>>i;
cin>>j;
if(!IsLegal(i,j)||chessBoard[i][j]!=Empty)
{
cout<<"Illegal Move!"<<endl;
}
else
{
chessBoard[i][j]=Black;
Print(chessBoard);
WhoWin(chessBoard);
}
}
//w i j,白方走棋
else if(!stricmp(command,"w"))
{
cin>>i;
cin>>j;
if(!IsLegal(i,j)||chessBoard[i][j]!=Empty)
{
cout<<"Illegal Move!"<<endl;
}
else
{
chessBoard[i][j]=White;
Print(chessBoard);
WhoWin(chessBoard);
}
}
//mb,黑方走棋
else if(!stricmp(command,"mb"))
{
Move bestMoves[2];
int start=clock();
i=GetBestMove(chessBoard,Black,bestMoves);
for(j=0;j<i;j++)
{
cout<<"("<<bestMoves[j].x<<","<<bestMoves[j].y<<")"<<'\t';
MakeMove(chessBoard,bestMoves[j],Black);
}
cout<<endl;
cout<<(double)(clock()-start)/CLOCKS_PER_SEC<<"s"<<endl;
Print(chessBoard);
WhoWin(chessBoard);
}
//mw,白方走棋
else if(!stricmp(command,"mw"))
{
Move bestMoves[2];
int start=clock();
i=GetBestMove(chessBoard,White,bestMoves);
for(j=0;j<i;j++)
{
cout<<"("<<bestMoves[j].x<<","<<bestMoves[j].y<<")"<<'\t';
MakeMove(chessBoard,bestMoves[j],White);
}
cout<<endl;
cout<<(double)(clock()-start)/CLOCKS_PER_SEC<<"s"<<endl;
Print(chessBoard);
WhoWin(chessBoard);
}
//u i j,退棋
else if(!stricmp(command,"u"))
{
cin>>i;
cin>>j;
if(!IsLegal(i,j)||chessBoard[i][j]==Empty)
{
cout<<"Illegal UnMove!"<<endl;
}
else
{
chessBoard[i][j]=Empty;
Print(chessBoard);
}
}
//导入棋盘 import fileName
else if(!stricmp(command,"import"))
{
//cin>>fileName;
char buffer[100],*p;
cin.getline(buffer,100);
p=buffer;
//掠过空白符
while(*p==' '||*p=='\t')
{
p++;
}
//去除双引号
if(*p=='"')
{
p++;
*(p+strlen(p)-1)='\0';
}
strcpy(fileName,p);
ImportChessBoard(fileName,chessBoard);
}
//保存棋盘 save fileName
else if(!stricmp(command,"save"))
{
char buffer[100],*p;
cin.getline(buffer,100);
p=buffer;
//掠过空白符
while(*p==' '||*p=='\t')
{
p++;
}
//去除双引号
if(*p=='"')
{
p++;
*(p+strlen(p)-1)='\0';
}
strcpy(fileName,p);
Save(fileName,chessBoard);
}
//新建棋盘
else if(!stricmp(command,"new"))
{
for(i=0;i<GRID_NUM;i++)
{
for(j=0;j<GRID_NUM;j++)
{
chessBoard[i][j]=Empty;
}
}
}
//退出
else if(!stricmp(command,"quit"))
{
break;
}
}
return 0;
}
///<summary>
/// 输出棋型扫描的结果
///<summary/>
void ShowTypeCount(int typeCount[2][13] )
{
cout<<"黑方:\t\t\t\t白方:"
<<"\n眠二:"<<typeCount[Black][STWO]<<"\t活二:"<<typeCount[Black][TWO]
<<"\t\t\t"
<<"眠二:"<<typeCount[White][STWO]<<"\t活二:"<<typeCount[White][TWO]
<<"\n眠三:"<<typeCount[Black][STHREE]<<"\t活三:"<<typeCount[Black][THREE]<<"\t朦胧三:"<<typeCount[Black][MTHREE]
<<"\t"
<<"眠三:"<<typeCount[White][STHREE]<<"\t活三:"<<typeCount[White][THREE]<<"\t朦胧三:"<<typeCount[White][MTHREE]
<<"\n眠四:"<<typeCount[Black][SFOUR]<<"\t