📄 main.cpp
字号:
if((ch5[x-1][y-1]==empty)&&(ch5[x+3][y+3]==empty)&&(ch5[x+1][y+1]&ch5[x+2][y+2]&color))
ret++;
if((ch5[x-1][y+1]==empty)&&(ch5[x+3][y-3]==empty)&&(ch5[x+1][y-1]&ch5[x+2][y-2]&color))
ret++;
if((ch5[x][y-1]==empty)&&(ch5[x][y+3]==empty)&&(ch5[x][y+1]&ch5[x][y+2]&color))
ret++;
if((ch5[x][y+1]==empty)&&(ch5[x][y-3]==empty)&&(ch5[x][y-1]&ch5[x][y-2]&color))
ret++;
if((ch5[x+1][y]==empty)&&(ch5[x-3][y]==empty)&&(ch5[x-1][y]&ch5[x-2][y]&color))
ret++;
if((ch5[x+1][y-1]==empty)&&(ch5[x-3][y+3]==empty)&&(ch5[x-1][y+1]&ch5[x-2][y+2]&color))
ret++;
if((ch5[x+1][y+1]==empty)&&(ch5[x-3][y-3]==empty)&&(ch5[x-1][y-1]&ch5[x-2][y-2]&color))
ret++;
//ebEbe
if((ch5[x-2][y-2]==empty)&&(ch5[x+2][y+2]==empty)&&(ch5[x-1][y-1]&ch5[x+1][y+1]&color))
ret++;
if((ch5[x-2][y]==empty)&&(ch5[x+2][y]==empty)&&(ch5[x-1][y]&ch5[x+1][y]&color))
ret++;
if((ch5[x-2][y+2]==empty)&&(ch5[x+2][y-2]==empty)&&(ch5[x-1][y+1]&ch5[x+1][y-1]&color))
ret++;
if((ch5[x][y-2]==empty)&&(ch5[x][y+2]==empty)&&(ch5[x][y-1]&ch5[x][y+1]&color))
ret++;
//eEbebe
if((ch5[x-1][y]==empty)&&(ch5[x+2][y]==empty)&&(ch5[x+4][y]==empty)&&(ch5[x+1][y]&ch5[x+3][y]&color))
ret++;
if((ch5[x-1][y-1]==empty)&&(ch5[x+2][y+2]==empty)&&(ch5[x+4][y+4]==empty)&&(ch5[x+1][y+1]&ch5[x+3][y+3]&color))
ret++;
if((ch5[x-1][y+1]==empty)&&(ch5[x+2][y-2]==empty)&&(ch5[x+4][y-4]==empty)&&(ch5[x+1][y-1]&ch5[x+3][y-3]&color))
ret++;
if((ch5[x][y-1]==empty)&&(ch5[x][y+2]==empty)&&(ch5[x][y+4]==empty)&&(ch5[x][y+1]&ch5[x][y+3]&color))
ret++;
if((ch5[x][y+1]==empty)&&(ch5[x][y-2]==empty)&&(ch5[x][y-4]==empty)&&(ch5[x][y-1]&ch5[x][y-3]&color))
ret++;
if((ch5[x+1][y]==empty)&&(ch5[x-2][y]==empty)&&(ch5[x-4][y]==empty)&&(ch5[x-1][y]&ch5[x-3][y]&color))
ret++;
if((ch5[x+1][y+1]==empty)&&(ch5[x-2][y-2]==empty)&&(ch5[x-4][y-4]==empty)&&(ch5[x-1][y-1]&ch5[x-3][y-3]&color))
ret++;
if((ch5[x+1][y-1]==empty)&&(ch5[x-2][y+2]==empty)&&(ch5[x-4][y+4]==empty)&&(ch5[x-1][y+1]&ch5[x-3][y+3]&color))
ret++;
//eEebbe
if((ch5[x-1][y]==empty)&&(ch5[x+1][y]==empty)&&(ch5[x+4][y]==empty)&&(ch5[x+2][y]&ch5[x+3][y]&color))
ret++;
if((ch5[x-1][y+1]==empty)&&(ch5[x+1][y-1]==empty)&&(ch5[x+4][y-4]==empty)&&(ch5[x+2][y-2]&ch5[x+3][y-3]&color))
ret++;
if((ch5[x-1][y-1]==empty)&&(ch5[x+1][y+1]==empty)&&(ch5[x+4][y+4]==empty)&&(ch5[x+2][y+2]&ch5[x+3][y+3]&color))
ret++;
if((ch5[x][y+1]==empty)&&(ch5[x][y-1]==empty)&&(ch5[x][y-4]==empty)&&(ch5[x][y-2]&ch5[x][y-3]&color))
ret++;
if((ch5[x][y-1]==empty)&&(ch5[x][y+1]==empty)&&(ch5[x][y+4]==empty)&&(ch5[x][y+2]&ch5[x][y+3]&color))
ret++;
if((ch5[x+1][y]==empty)&&(ch5[x-1][y]==empty)&&(ch5[x-4][y]==empty)&&(ch5[x-2][y]&ch5[x-3][y]&color))
ret++;
if((ch5[x+1][y+1]==empty)&&(ch5[x-1][y-1]==empty)&&(ch5[x-4][y-4]==empty)&&(ch5[x-2][y-2]&ch5[x-3][y-3]&color))
ret++;
if((ch5[x+1][y-1]==empty)&&(ch5[x-1][y+1]==empty)&&(ch5[x-4][y+4]==empty)&&(ch5[x-2][y+2]&ch5[x-3][y+3]&color))
ret++;
//ebEebe
if((ch5[x-2][y]==empty)&&(ch5[x+1][y]==empty)&&(ch5[x+3][y]==empty)&&(ch5[x-1][y]&ch5[x+2][y]&color))
ret++;
if((ch5[x-2][y+2]==empty)&&(ch5[x+1][y-1]==empty)&&(ch5[x+3][y-3]==empty)&&(ch5[x-1][y+1]&ch5[x+2][y-2]&color))
ret++;
if((ch5[x-2][y-2]==empty)&&(ch5[x+1][y+1]==empty)&&(ch5[x+3][y+3]==empty)&&(ch5[x-1][y-1]&ch5[x+2][y+2]&color))
ret++;
if((ch5[x][y+2]==empty)&&(ch5[x][y-1]==empty)&&(ch5[x][y-3]==empty)&&(ch5[x][y+1]&ch5[x][y-2]&color))
ret++;
if((ch5[x][y-2]==empty)&&(ch5[x][y+1]==empty)&&(ch5[x][y+3]==empty)&&(ch5[x][y-1]&ch5[x][y+2]&color))
ret++;
if((ch5[x+2][y]==empty)&&(ch5[x-1][y]==empty)&&(ch5[x-3][y]==empty)&&(ch5[x+1][y]&ch5[x-2][y]&color))
ret++;
if((ch5[x+2][y+2]==empty)&&(ch5[x-1][y-1]==empty)&&(ch5[x-3][y-3]==empty)&&(ch5[x+1][y+1]&ch5[x-2][y-2]&color))
ret++;
if((ch5[x+2][y-2]==empty)&&(ch5[x-1][y+1]==empty)&&(ch5[x-3][y+3]==empty)&&(ch5[x+1][y-1]&ch5[x-2][y+2]&color))
ret++;
}
return(ret);
}
//--------------------------------------------------------------------------------
int __fastcall TMainForm::To2(int x,int y,int color)
{
int ret=0;
if(ch5[x][y]==empty)
{
//ebEe
if((ch5[x-2][y]==empty)&&(ch5[x+1][y]==empty)&&(ch5[x-1][y]&color))
ret++;
if((ch5[x-2][y-2]==empty)&&(ch5[x+1][y+1]==empty)&&(ch5[x-1][y-1]&color))
ret++;
if((ch5[x-2][y+2]==empty)&&(ch5[x+1][y-1]==empty)&&(ch5[x-1][y+1]&color))
ret++;
if((ch5[x][y+2]==empty)&&(ch5[x][y-1]==empty)&&(ch5[x][y+1]&color))
ret++;
if((ch5[x][y-2]==empty)&&(ch5[x][y+1]==empty)&&(ch5[x][y-1]&color))
ret++;
if((ch5[x+2][y]==empty)&&(ch5[x-1][y]==empty)&&(ch5[x+1][y]&color))
ret++;
if((ch5[x+2][y+2]==empty)&&(ch5[x-1][y-1]==empty)&&(ch5[x+1][y+1]&color))
ret++;
if((ch5[x+2][y-2]==empty)&&(ch5[x-1][y+1]==empty)&&(ch5[x+1][y-1]&color))
ret++;
//ebeEe
if((ch5[x-3][y]==empty)&&(ch5[x-1][y]==empty)&&(ch5[x+1][y]==empty)&&(ch5[x-2][y]&color))
ret++;
if((ch5[x-3][y+3]==empty)&&(ch5[x-1][y+1]==empty)&&(ch5[x+1][y-1]==empty)&&(ch5[x-2][y+2]&color))
ret++;
if((ch5[x-3][y-3]==empty)&&(ch5[x-1][y-1]==empty)&&(ch5[x+1][y+1]==empty)&&(ch5[x-2][y-2]&color))
ret++;
if((ch5[x][y+3]==empty)&&(ch5[x][y+1]==empty)&&(ch5[x][y-1]==empty)&&(ch5[x][y+2]&color))
ret++;
if((ch5[x][y-3]==empty)&&(ch5[x][y-1]==empty)&&(ch5[x][y+1]==empty)&&(ch5[x][y-2]&color))
ret++;
if((ch5[x+3][y]==empty)&&(ch5[x+1][y]==empty)&&(ch5[x-1][y]==empty)&&(ch5[x+2][y]&color))
ret++;
if((ch5[x+3][y+3]==empty)&&(ch5[x+1][y+1]==empty)&&(ch5[x-1][y-1]==empty)&&(ch5[x+2][y+2]&color))
ret++;
if((ch5[x+3][y-3]==empty)&&(ch5[x+1][y-1]==empty)&&(ch5[x-1][y+1]==empty)&&(ch5[x+2][y-2]&color))
ret++;
}
return(ret);
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::ServerSocket1Listen(TObject *Sender,
TCustomWinSocket *Socket)
{
Label4->Caption="Port "+IntToStr(ServerSocket1->Port)+" ... Listen";
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::FormPaint(TObject *Sender)
{
int width=ClientWidth,h=ClientHeight;
for(int i=h;i>=0;i--)
{
Canvas->Pen->Color=RGB(i*255/h/10,i*255/h/20,i*255/h);
Canvas->MoveTo(0,i);
Canvas->LineTo(width,i);
}
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::ServerSocket1ClientConnect(TObject *Sender,
TCustomWinSocket *Socket)
{
Label4->Caption="Connected.";
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::ServerSocket1ClientDisconnect(TObject *Sender,
TCustomWinSocket *Socket)
{
Label4->Caption="Disconnected.";
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::ClientSocket1Lookup(TObject *Sender,
TCustomWinSocket *Socket)
{
Label5->Caption="Searching...";
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::ClientSocket1Connecting(TObject *Sender,
TCustomWinSocket *Socket)
{
Label5->Caption="Connecting...";
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::ClientSocket1Connect(TObject *Sender,
TCustomWinSocket *Socket)
{
Label5->Caption="Connected";
SockForm->Hide();
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::ClientSocket1Disconnect(TObject *Sender,
TCustomWinSocket *Socket)
{
Label5->Caption="Disconnected";
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::ClientSocket1Error(TObject *Sender,
TCustomWinSocket *Socket, TErrorEvent ErrorEvent, int &ErrorCode)
{
ErrorCode=0;
Label5->Caption="";
Application->MessageBox("遇到错误!请检查服务器地址和端口!",
"An Error Occurs!",0);
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::ServerSocket1ClientError(TObject *Sender,
TCustomWinSocket *Socket, TErrorEvent ErrorEvent, int &ErrorCode)
{
ErrorCode=0;
Label4->Caption="ERROR!!!";
}
void __fastcall TMainForm::Debug()
{
for(int i=4;i<=22;i++)
for(int j=4;j<=22;j++)
DebugForm->SG->Cells[i-4][j-4]=ch5[i][j];
DebugForm->Update();
}
//---------------------------------------------------------------------------
int __fastcall TMainForm::Value(int x,int y,int color)
{
int ret=0;
//if(To5(x,y,color))ret+=10000000;
//if(To4(x,y,color)!=0&&To3(x,y,color)!=0)ret+=1000000;
//if(To4(x,y,color)>=2)ret+=1000000;
if(To3(x,y,color)>=2)ret+=200000+rand()%10000;
if(To4(x,y,color)==1)ret+=100000+rand()%10000;
if(To3(x,y,color)==1)ret+=90000+rand()%1000;
ret+=500*To2(x,y,color)+rand()%100;
return(ret);
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::Computer()
{
dots=0;
best bestpos;
man=false;
bestpos=Search(white,Level);
DrawChess(bestpos.x,bestpos.y,7,white);
ch5[bestpos.x][bestpos.y]=white;
Shape1->Left=Image2->Left+(bestpos.x-3)*20-4;
Shape1->Top=Image2->Top+(bestpos.y-3)*20-4;
man=true;
if(Check5(white))
Label1->Caption="输";
Debug();
}
//--------------------------------------------------------
TMainForm::best __fastcall TMainForm::Search(int color,int Depth)
{
int v=0;
best ret;
ret.x=0;
ret.y=0;
ret.value=0;
dots++;
if(Depth>0)
{
for(int i=4;i<=22;i++)
for(int j=4;j<=22;j++)
if(ch5[i][j]==empty&&To5(i,j,color))
{
ret.x=i;
ret.y=j;
ret.value=99999999;
return(ret);
}
for(int i=4;i<=22;i++)
for(int j=4;j<=22;j++)
if(ch5[i][j]==empty&&To5(i,j,3-color))
{
ret.x=i;
ret.y=j;
ret.value=99999999;
return(ret);
}
for(int i=4;i<=22;i++)
for(int j=4;j<=22;j++)
if(ch5[i][j]==empty&&To4(i,j,color)>1)
{
ret.x=i;
ret.y=j;
ret.value=99999999;
return(ret);
}
for(int i=4;i<=22;i++)
for(int j=4;j<=22;j++)
if(ch5[i][j]==empty&&To4(i,j,3-color)>1)
{
ret.x=i;
ret.y=j;
ret.value=99999999;
return(ret);
}
for(int i=4;i<=22;i++)
for(int j=4;j<=22;j++)
if(ch5[i][j]==empty&&To4(i,j,color)!=0&&To3(i,j,color)!=0)
{
ret.x=i;
ret.y=j;
ret.value=99999999;
return(ret);
}
for(int i=4;i<=22;i++)
for(int j=4;j<=22;j++)
if(ch5[i][j]==empty&&To4(i,j,3-color)!=0&&To3(i,j,3-color)!=0)
{
ret.x=i;
ret.y=j;
ret.value=99999999;
return(ret);
}
for(int i=4;i<=22;i++)
for(int j=4;j<=22;j++)
{
if(Depth==Level)
{
StatusLine->Panels->Items[3]->Text=((i-4)*19+j-4)*100/361;
StatusLine->Panels->Items[4]->Text=dots;
Application->ProcessMessages();
}
if(ch5[i][j]==empty)
{
// SaveBoard();
v=Value(i,j,color)+200;
if(v>ret.value)
{
ret.x=i;
ret.y=j;
ret.value=v;
}
v=Value(i,j,3-color);
if(v>ret.value)
{
ret.x=i;
ret.y=j;
ret.value=v;
}
ch5[i][j]=color;
// Debug();
if(v>400)
{
v+=Search(3-color,Depth-1).value;
if(v>ret.value)
{
ret.x=i;
ret.y=j;
ret.value=v;
}
}
ch5[i][j]=empty;
// RestoreBoard();
}
}
}
return(ret);
}
void __fastcall TMainForm::Level21Click(TObject *Sender)
{
Level21->Checked=true;
Level=3;
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::Level11Click(TObject *Sender)
{
Level11->Checked=true;
Level=2;
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::Level31Click(TObject *Sender)
{
Level31->Checked=true;
Level=4;
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::Demo1Click(TObject *Sender)
{
int finish=0;
dots=0;
best bestpos;
DrawChess(14,14,7,black);
ch5[14][14]=black;
while(!Check5(white)&&!Check5(black)&&finish<361)
{
bestpos=Search(white,Level);
finish++;
DrawChess(bestpos.x,bestpos.y,7,white);
ch5[bestpos.x][bestpos.y]=white;
Shape1->Left=Image2->Left+(bestpos.x-3)*20-4;
Shape1->Top=Image2->Top+(bestpos.y-3)*20-4;
if(Check5(white))
Label1->Caption="白棋赢";
Update();
Debug();
bestpos=Search(black,Level);
finish++;
DrawChess(bestpos.x,bestpos.y,7,black);
ch5[bestpos.x][bestpos.y]=black;
Shape1->Left=Image2->Left+(bestpos.x-3)*20-4;
Shape1->Top=Image2->Top+(bestpos.y-3)*20-4;
if(Check5(black))
Label1->Caption="黑棋赢";
Update();
Debug();
Application->ProcessMessages();
}
if(finish>=361)
Label1->Caption="和";
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::DebugForm1Click(TObject *Sender)
{
DebugForm1->Checked=!DebugForm1->Checked;
if(DebugForm1->Checked)
DebugForm->Show();
else
DebugForm->Hide();
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::NewNetworkGame1Click(TObject *Sender)
{
SockForm->Show();
InitBoard();
}
//---------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -