📄 core.cpp
字号:
#include "stdafx.h"
#include "Core.h"
#include <queue>
#include <fstream>
#include "UI.h"
//外界dll文件载入
extern UI * pBox;
typedef TPos (Import)(const int data[8][8],int mark);
typedef char * (Import1)();
Import *calc1;
Import *calc2;
Import1 *GetName1;
Import1 *GetName2;
CBad::CBad()
{
memset(iData,0,8*8*4);
iData[3][3]=1;
iData[3][4]=2;
iData[4][3]=2;
iData[4][4]=1;
iFirst=1;//谁先
iWinner=0;//谁赢
iMode=1;//模式,0 比赛;1 录像
iTotalStep=0;
iStep=0;
iTurn=1;
WalkFlag=false;
hThread=NULL;
hThreadUI=NULL;
hInst1=NULL;
hInst2=NULL;
iTimeLimit=1000;
bBlackLoad=false;
bWhiteLoad=false;
iPos.x=0;
iPos.y=0;
}
CBad:: ~CBad()
{
Exit();
}
bool CBad ::LoadSuccess()
{
return bBlackLoad && bWhiteLoad;
}
bool CBad ::SelecetDllName(string dll,int mark)
{
if(mark==1)
{
if(hInst1){FreeLibrary(hInst1);hInst1=NULL;bBlackLoad=false;}
hInst1=LoadLibrary(&dll[0]);
if(hInst1==NULL)
{
if(hInst1){FreeLibrary(hInst1);hInst1=NULL;}
return false;
}
calc1=(Import *)GetProcAddress(hInst1,"Calc");
GetName1=(Import1 *)GetProcAddress(hInst1,"GetName");
if(calc1==NULL||GetName1==NULL)
{
if(hInst1){FreeLibrary(hInst1);hInst1=NULL;}
return false;
}
iBlackName=GetName1();
pBox->ShowName(iBlackName,1);
bBlackLoad=true;
return true;
}
if(mark==2)
{
if(hInst2){FreeLibrary(hInst2);hInst2=NULL;bWhiteLoad=false;}
hInst2=LoadLibrary(&dll[0]);
if(hInst2==NULL)
{
if(hInst2){FreeLibrary(hInst2);hInst2=NULL;}
return false;
}
calc2=(Import *)GetProcAddress(hInst2,"Calc");
GetName2=(Import1 *)GetProcAddress(hInst2,"GetName");
if(calc2==NULL||GetName2==NULL)
{
if(hInst2){FreeLibrary(hInst2);hInst2=NULL;}
return false;
}
iWhiteName=GetName2();
pBox->ShowName(iWhiteName,2);
bWhiteLoad=true;
return true;
}
return false;
}
void CBad::Clear()
{
memset(iData,0,sizeof(iData));
iData[4][4]=1;
iData[3][3]=1;
iData[4][3]=2;
iData[3][4]=2;
iWinner=0;//谁赢
iMode=0;//模式,0 比赛;1 录像
iTotalStep=0;
iStep=0;
iTurn=iFirst;
WalkFlag=false;
hThread=NULL;
hThreadUI=NULL;
iPos.x=0;
iPos.y=0;
}
void CBad::Exit()
{
if(hThreadUI)TerminateThread(hThreadUI,0);
if(hThread)TerminateThread(hThread,0);
if(hInst1)FreeLibrary(hInst1);
if(hInst2)FreeLibrary(hInst2);
}
bool CBad::GameBegin()
{
while(1)
{
if(!JudgeEnd(iTurn))
{
if(!JudgeEnd(3-iTurn))
{
char t[30];
string msg;
int c1=Count(1),c2=Count(2);
if(c1>c2){iWinner=1;msg=iBlackName+"赢了"+iWhiteName;}
if(c1<c2){iWinner=2;msg=iBlackName+"输了"+iWhiteName;}
if(c1==c2){iWinner=0;msg=iBlackName+"平了"+iWhiteName;}
pBox->EndResult(msg);//向接口发结束信息
WriteFile();
Clear();//进行清理
return true;//正常的比赛结束
}else
{
pBox->Show("出现停手情况!");
iTurn=3-iTurn;
}
}
WalkFlag=false;//开始调用算法了
Wait();//启动队伍iturn处理算法
for(int i=0;i<=iTimeLimit;i+=10)
{
_sleep(9);
char t[20];
pBox->ShowTime(itoa(i,t,10));
}//等待时间
if(!WalkFlag)//这么长时间还没有下一步
{
//杀掉他
TerminateThread(hThread,0);
hThread=NULL;
string msg;
string win;
if(iTurn==1){msg=iBlackName;win=iWhiteName;iWinner=1;}
else {msg=iWhiteName;win=iBlackName;iWinner=2;}
msg+="的算法超时,程序自动判其输掉比赛\r\n"+win+"赢了";
pBox->EndResult(msg);
WriteFile();
Clear();
return false;
}
SetAMap();
if(!SetPos(iTurn))
{
string msg;
string win;
char t[30];
if(iTurn==1){msg=iBlackName;win=iWhiteName;iWinner=1;}
else {msg=iWhiteName;win=iBlackName;iWinner=2;}
msg+="放了错误的位置(";
msg+=itoa(iPos.x+1,t,10);
msg+=",";
msg+=itoa(iPos.y+1,t,10);
msg+="),程序自动判罚其失败\r\n";
msg+=win+" win~!";
pBox->EndResult(msg);
WriteFile();
Clear();
return false;//不规则的结束
}
char tt[30];
string score=itoa(Count(1),tt,10);
score+=" : ";
score+=itoa(Count(2),tt,10);
pBox->ShowScore(score);
pBox->NewChess();
iTurn=3-iTurn;//换下一个人
}
return true;
}
//1 判断能不能下,2 能则吃子
bool CBad ::SetPos(int mark)
{
if(!IsValidPos(iPos,mark))return false;
queue<TPos>q;
q.push(iPos);
iData[iPos.y][iPos.x]=mark;
while(!q.empty())
{
_sleep(50);
TPos head=q.front();
q.pop();
//left
int x=head.x-1;
int y=head.y;
bool flag=false;
while(x>0&&iData[y][x]==3-mark)
{
x--;
flag=true;
}
if(flag&&x>=0&&iData[y][x]==mark)//121
{
for(int i=head.x-1;i>x;i--)
{
iData[y][i]=mark;
TPos temp;
temp.x=i;
temp.y=y;
q.push(temp);
}
}
//right
x=head.x+1;
y=head.y;
flag=false;
while(x<8&&iData[y][x]==3-mark)
{
x++;
flag=true;
}
if(flag&&x<8&&iData[y][x]==mark)//121
{
for(int i=head.x+1;i<x;i++)
{
iData[y][i]=mark;
TPos temp;
temp.x=i;
temp.y=y;
q.push(temp);
}
}
//up
x=head.x;
y=head.y-1;
flag=false;
while(y>0&&iData[y][x]==3-mark)
{
y--;
flag=true;
}
if(flag&&y>=0&&iData[y][x]==mark)//121
{
for(int i=head.y-1;i>y;i--)
{
iData[i][x]=mark;
TPos temp;
temp.y=i;
temp.x=x;
q.push(temp);
}
}
//down
x=head.x;
y=head.y+1;
flag=false;
while(y<8&&iData[y][x]==3-mark)
{
y++;
flag=true;
}
if(flag&&y<8&&iData[y][x]==mark)//121
{
for(int i=head.y+1;i<y;i++)
{
iData[i][x]=mark;
TPos temp;
temp.y=i;
temp.x=x;
q.push(temp);
}
}
//left and up
x=head.x-1;
y=head.y-1;
flag=false;
while(x>0&&y>0&&iData[y][x]==3-mark)
{
x--;
y--;
flag=true;
}
if(flag&&x>=0&&y>=0&&iData[y][x]==mark)//121
{
for(int j=head.x-1,i=head.y-1;j>x&&i>y;i--,j--)
{
iData[i][j]=mark;
TPos temp;
temp.x=j;
temp.y=i;
q.push(temp);
}
}
//right up
x=head.x+1;
y=head.y-1;
flag=false;
while(x<8&&y>0&&iData[y][x]==3-mark)
{
x++;
y--;
flag=true;
}
if(flag&&x<8&&y>=0&&iData[y][x]==mark)//121
{
for(int j=head.x+1,i=head.y-1;j<x&&i>y;i--,j++)
{
iData[i][j]=mark;
TPos temp;
temp.x=j;
temp.y=i;
q.push(temp);
}
}
//left and down
x=head.x-1;
y=head.y+1;
flag=false;
while(y<8&&x>0&&iData[y][x]==3-mark)
{
y++;
x--;
flag=true;
}
if(flag&&y<8&&x>=0&&iData[y][x]==mark)//121
{
for(int i=head.y+1,j=head.x-1;i<y&&j>x;i++,j--)
{
iData[i][j]=mark;
TPos temp;
temp.y=i;
temp.x=j;
q.push(temp);
}
}
//right and down
x=head.x+1;
y=head.y+1;
flag=false;
while(y<8&&x<8&&iData[y][x]==3-mark)
{
y++;
x++;
flag=true;
}
if(flag&&y<8&&x<8&&iData[y][x]==mark)//121
{
for(int i=head.y+1,j=head.x+1;i<y&&j<x;i++,j++)
{
iData[i][j]=mark;
TPos temp;
temp.y=i;
temp.x=j;
q.push(temp);
}
}
}
return true;
}
//下面数颜色数,点点
int CBad ::Count(int n)
{
int num=0;
for(int i=0;i<8;i++)
for(int j=0;j<8;j++)
if(iData[i][j]==n)num++;
return num;
}
//下面判断是不是应该停手的 杨凯
bool CBad ::JudgeEnd(int mark) const
{
int i,j;
for(i=0;i<8;i++)
for(j=0;j<8;j++)
{
TPos a;
a.x=j;
a.y=i;
if(IsValidPos(a, mark))return true;
}
return false;
}
bool CBad::IsValidPos(TPos pos,int mark) const
{
if(pos.x<0||pos.y<0||pos.x>7||pos.y>7||iData[pos.y][pos.x]!=0)return false;//非法的放置
bool flag=false;
//up
int x=pos.x;
int y=pos.y-1;
while(y>=0&&iData[y][x]==3-mark)
{
y--;
flag=true;
}
if(flag&&y>=0&&iData[y][x]==mark)return true;
//down
flag=false;
x=pos.x;
y=pos.y+1;
while(y<8&&iData[y][x]==3-mark)
{
y++;
flag=true;
}
if(flag&&y<8&&iData[y][x]==mark)return true;
//right
flag=false;
x=pos.x+1;
y=pos.y;
while(x<8&&iData[y][x]==3-mark)
{
x++;
flag=true;
}
if(flag&&x<8&&iData[y][x]==mark)return true;
//left
flag=false;
x=pos.x-1;
y=pos.y;
while(x>=0&&iData[y][x]==3-mark)
{
x--;
flag=true;
}
if(flag&&x>=0&&iData[y][x]==mark)return true;
//left and up
flag=false;
x=pos.x-1;
y=pos.y-1;
while(x>=0&&y>=0&&iData[y][x]==3-mark)
{
x--;
y--;
flag=true;
}
if(flag&&x>=0&&y>=0&&iData[y][x]==mark)return true;
//left and down
flag=false;
x=pos.x-1;
y=pos.y+1;
while(x>=0&&y<8&&iData[y][x]==3-mark)
{
x--;
y++;
flag=true;
}
if(flag&&x>=0&&y<8&&iData[y][x]==mark)return true;
//right and up
flag=false;
x=pos.x+1;
y=pos.y-1;
while(x<8&&y>=0&&iData[y][x]==3-mark)
{
x++;
y--;
flag=true;
}
if(flag&&x<8&&y>=0&&iData[y][x]==mark)return true;
//right and down
flag=false;
x=pos.x+1;
y=pos.y+1;
while(x<8&&y<8&&iData[y][x]==3-mark)
{
x++;
y++;
flag=true;
}
if(flag&&x<8&&y<8&&iData[y][x]==mark)return true;
return false;
}
void Thread1(CBad * bad)//队伍1的思考
{
bad->iPos=calc1(bad->iData,bad->iTurn);
bad->WalkFlag=true;
}
void Thread2(CBad *bad)//队伍2的思考
{
bad->iPos=calc2(bad->iData,bad->iTurn);
bad->WalkFlag=true;
}
void CBad::Wait()
{
if(iTurn==1)
hThread=CreateThread(0,0,(LPTHREAD_START_ROUTINE)Thread1,this,0,0);
if(iTurn==2)
hThread=CreateThread(0,0,(LPTHREAD_START_ROUTINE)Thread2,this,0,0);
}
bool CBad::WriteFile()
{
SetAMap();
string name=iBlackName+"_"+iWhiteName+".xx";
ofstream file(&name[0]);
file<<"黑方 :"<<iBlackName <<" 白方:"<<iWhiteName<<endl;
if(iFirst==1)file<<"先手 :黑先 "<<endl;
if(iFirst==2)file<<"先手 :白先 "<<endl;
if(iWinner==1)file<<"结果:黑胜 "<<endl;
if(iWinner==2)file<<"结果:白胜 "<<endl;
if(iWinner==9)file<<"结果:平局 "<<endl;
file<<"比分 黑:白= "<<Count(1)<<" : "<<Count(2)<<endl;
file<<"总步数: "<<iTotalStep<<endl;
for(int i=0;i<iTotalStep;i++)
{
for(int j=0;j<8;j++)
{
for(int k=0;k<8;k++)
file <<iChessRecord[i][j][k]<<" ";
file<<endl;
}
file<<endl;
}
file.close();
return true;
}
bool CBad::SetAMap()
{
for(int i=0;i<8;i++)
for(int j=0;j<8;j++)
iChessRecord[iStep][i][j]=iData[i][j];
iStep++;
iTotalStep++;
return true;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -