📄 chessdlg.cpp
字号:
{
is_myturn = true;
Step st = StepList.GetTail();
Fivearray[st.x][st.y] = 0;
StepList.RemoveTail();
if ( StepList.GetCount() != 0 )
{
st = StepList.GetTail();
if ( st.side == 1 )
{
Fivearray[st.x][st.y] = 0;
StepList.RemoveTail();
}
}
Invalidate();
MessageBox("对方已同意悔棋","悔棋提示",MB_OK);
return ;
}
if ( str == "++++++nonononon++++++")
{
MessageBox("对方未同意你悔棋情求","悔棋提示",MB_OK);
return;
}
if ( str == "------bybyebyebye")
{
MessageBox("对方已经逃跑了","提示",MB_OK);
is_myturn = false;
set_netflag = false;
is_connect = false;
SetWindowText("五子棋");
return;
}
m_listbox.AddString (str);
}
LRESULT CChessDlg::OnMyMessage(WPARAM wParam,LPARAM lParam)
{
switch( lParam)
{
case FD_ACCEPT:
if (m_socket.Accept(m_con_socket) )
{
CString ip;
UINT port;
m_con_socket.GetPeerName(ip,port);
SetWindowText("网络对战(远端主机"+ip+"已连接)");
is_connect = true;
}
break;
case FD_READ:
OnReceive();
}
return 0;
}
void CChessDlg::OnSendMessage()
{
// TODO: 在此添加控件通知处理程序代码
CString mssage ;
m_edit.GetWindowText(mssage);
m_con_socket.Send(mssage,mssage.GetLength(),0);
m_edit.SetWindowText("");
}
bool CChessDlg::IsSuccess()
{
if ( m_and_p_flag == true )
{
if ( StepList.GetCount() == 0 )
return false;
Step temp = StepList.GetTail();
int i = temp.x ;
int j = temp.y ;
int side = temp.side ;
if ( LeftRight(i,j,side) == 5 || UpDown(i,j,side) == 5
||LupToRdown(i,j,side) == 5 || RuptoLdown(i,j,side)==5)
{
if ( side == 3 )
{
MessageBox("电脑胜利,结束战斗!","提示",MB_OK);
}
else
{
MessageBox("你胜利了,结束战斗!","提示",MB_OK);
}
/*
StepList.RemoveAll();
for (int i = 0 ;i < 20 ; i++ )
for (int j=0;j<20;j++ )
Fivearray[i][j] = 0;
*/
is_myturn = false;
Invalidate();
return true;
}
return false ;
}
if ( StepList.GetCount() == 0 )
return false;
Step temp = StepList.GetTail();
int i = temp.x ;
int j = temp.y ;
int side = temp.side ;
if ( side == 2 )
return false;
if ( LeftRight(i,j,side) == 5 || UpDown(i,j,side) == 5
||LupToRdown(i,j,side) == 5 || RuptoLdown(i,j,side)==5)
{
MessageBox("胜利,结束战斗!","提示",MB_OK);
StepList.RemoveAll();
for (int i = 0 ;i < 20 ; i++ )
for (int j=0;j<20;j++ )
Fivearray[i][j] = 0;
is_myturn = false;
CString sendstr;
sendstr = "overoveryouoverover";
m_con_socket.Send(sendstr,sendstr.GetLength(),0);
Invalidate();
if ( MessageBox("继续玩吗?","友情提示",MB_YESNO) ==IDNO)
{
SetWindowText("五子棋");
set_netflag = false;
is_connect = false;
CString str = "------bybyebyebye";
m_con_socket.Send(str,str.GetLength(),0);
}
else
{
if (is_server == true)
{
is_myturn = true;
}
}
return true;
}
return false;
}
int CChessDlg::LeftRight(int i, int j,int side)
{
int tempi,count;
tempi = i;
count = 1;
//toleft
while ( --tempi > 0 && Fivearray[tempi][j] == side)
{
count ++;
}
tempi = i;
while ( ++ tempi <20 && Fivearray[tempi][j] == side )
{
count ++;
}
return count;
}
int CChessDlg::UpDown(int i, int j,int side)
{
int tempj,count;
tempj = j;
count = 1;
while ( --tempj > 0 && Fivearray[i][tempj] == side)
{
count ++;
}
tempj = j;
while ( ++ tempj <20 &&Fivearray[i][tempj] == side )
{
count ++;
}
return count;
}
int CChessDlg::LupToRdown(int i, int j,int side)
{
int tempi,tempj,count;
tempi = i,tempj = j;
count = 1;
while ( --tempi > 0 && -- tempj >0 && Fivearray[tempi][tempj] == side)
{
count ++;
}
tempi = i,tempj = j;
while ( ++ tempi<20 && ++ tempj < 20 &&Fivearray[tempi][tempj] == side )
{
count ++;
}
return count;
}
int CChessDlg::RuptoLdown(int i, int j,int side)
{
int tempi,tempj,count;
tempi = i,tempj = j;
count = 1;
while ( --tempi > 0 && ++tempj <20 && Fivearray[tempi][tempj] == side)
{
count ++;
}
tempi = i,tempj = j;
while ( ++ tempi<20 && -- tempj > 0 && Fivearray[tempi][tempj] == side )
{
count ++;
}
return count;
}
void CChessDlg::OnCancel()
{
// TODO: 在此添加专用代码和/或调用基类
//CDialog::OnCancel();
}
void CChessDlg::OnOK()
{
// TODO: 在此添加专用代码和/或调用基类
//CDialog::OnOK();
}
void CChessDlg::OnClose()
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
//CDialog::OnClose();
m_socket.Close();
m_socket.ShutDown(2);
CString str = "------bybyebyebye";
m_con_socket.Send(str,str.GetLength(),0);
StepList.RemoveAll();
for (int i = 0 ;i < 20 ; i++ )
for (int j=0;j<20;j++ )
Fivearray[i][j] = 0;
m_con_socket.Close();
m_con_socket.ShutDown(2);
CDialog::OnCancel();
}
void CChessDlg::SocketClose()
{
MessageBox("远端主机已关闭连接","提示",MB_OK);
SetWindowText("五子棋");
}
void CChessDlg::GetCurrentScore( GameStatus &board_situation)
{
int i = board_situation.st.x ;
int j = board_situation.st.y;
board_situation.score = 0;
//状态
//返回8 成5:five
//返回7 活4:alivefour
//返回6 活3:alivethree
//返回5 活2:alivetwo
//返回4 死4:deadfour
//返回3 死3:deadthree
//返回2 死2:deadtwo
//返回0,
//搜索出左到右的状态
int lr =LeftToRight_Status(i , j ,board_situation.fivearray);
if ( lr == 8 )
{
if ( board_situation.is_machine )
board_situation.score = 100000;
else
board_situation.score = -100000;
return ;
}
//从上到下
int ud = UpToDown_Status(i,j ,board_situation.fivearray );
if ( ud == 8 )
{
if ( board_situation.is_machine )
board_situation.score = 100000;
else
board_situation.score = -100000;
return ;
}
//从左上到右下
int lutrd = LeftUpToRightDown_Status(i,j ,board_situation.fivearray );
if ( lutrd == 8 )
{
if ( board_situation.is_machine )
board_situation.score = 100000;
else
board_situation.score = -100000;
return ;
}
//从左下到右上
int ldtru =LeftDownToRightUp_Status(i,j ,board_situation.fivearray );
if ( ldtru == 8 )
{
if ( board_situation.is_machine )
board_situation.score = 100000;
else
board_situation.score = -100000;
return ;
}
//是否活4
if ( lr == 7 || ud == 7 || lutrd == 7 || ldtru == 7 )
{
if ( board_situation.is_machine )
board_situation.score = 10000;
else
board_situation.score = -10000;
return ;
}
//双死4
if (
( lr == 4 && (ud == 4 || lutrd == 4 || ldtru == 4))
|| (ud == 4 && (lutrd == 4|| ldtru == 4))
|| (lutrd == 4 && ldtru == 4)
)
{
if ( board_situation.is_machine )
board_situation.score = 10000;
else
board_situation.score = -10000;
return ;
}
//死4活3
if (
( lr == 4 && (ud == 6 || lutrd == 6 || ldtru == 6))
|| ( ud == 4 && (lr == 6 || lutrd == 6 || ldtru == 6))
|| ( lutrd == 4 && (lr == 6 || ud== 6 || ldtru == 6))
|| ( ldtru== 4 && (lr == 6 || ud== 6 || lutrd == 6))
)
{
if ( board_situation.is_machine )
board_situation.score = 10000;
else
board_situation.score = -10000;
return ;
}
//双活3
if (
( lr == 6 && (ud == 6 || lutrd == 6 || ldtru == 6))
|| (ud == 6 && (lutrd == 6|| ldtru == 6))
|| (lutrd == 6 && ldtru == 6)
)
{
if ( board_situation.is_machine )
board_situation.score = 5000;
else
board_situation.score = -5000;
return ;
}
//活3双活2
if ( ( lr == 6 || ud == 6 || lutrd == 6 || ldtru == 6 ) &&
( ( lr == 5 && (ud == 5 || lutrd == 5 || ldtru == 5))
|| (ud == 5 && (lutrd == 5|| ldtru == 5))
|| (lutrd == 5 && ldtru == 5) )
)
{
if ( board_situation.is_machine )
board_situation.score = 5000;
else
board_situation.score = -5000;
return ;
}
//活3死3
if (
( lr == 3 && (ud == 6 || lutrd == 6 || ldtru == 6))
|| ( ud == 3 && (lr == 6 || lutrd == 6 || ldtru == 6))
|| ( lutrd == 3 && (lr == 6 || ud== 6 || ldtru == 6))
|| ( ldtru== 3 && (lr == 6 || ud== 6 || lutrd == 6))
)
{
if ( board_situation.is_machine )
board_situation.score = 1000;
else
board_situation.score = -1000;
return ;
}
//死4
if ( lr == 4 || ud == 4 || lutrd == 4 || ldtru == 4 )
{
if ( board_situation.is_machine )
board_situation.score = 500;
else
board_situation.score = -500;
return ;
}
//单活3
if ( lr == 6 || ud == 6 || lutrd == 6 || ldtru == 6 )
{
if ( board_situation.is_machine )
board_situation.score = 200;
else
board_situation.score = -200;
return ;
}
//双活2
if (
( lr == 5 && (ud == 5 || lutrd == 5 || ldtru == 5))
|| (ud == 5 && (lutrd == 5|| ldtru == 5))
|| (lutrd == 5 && ldtru == 5)
)
{
if ( board_situation.is_machine )
board_situation.score = 100;
else
board_situation.score = -100;
return ;
}
//双死3
if (
( lr == 3 && (ud == 3 || lutrd == 3 || ldtru == 3))
|| (ud == 3 && (lutrd == 3|| ldtru == 3))
|| (lutrd == 3 && ldtru == 3)
)
{
if ( board_situation.is_machine )
board_situation.score = 50;
else
board_situation.score = -50;
return ;
}
//单活2
if ( lr == 5 || ud == 5 || lutrd == 5 || ldtru == 5 )
{
if ( board_situation.is_machine )
board_situation.score = 10;
else
board_situation.score = -10;
return ;
}
//单死3
if ( lr == 3 || ud == 3 || lutrd == 3 || ldtru == 3 )
{
if ( board_situation.is_machine )
board_situation.score = 5;
else
board_situation.score = -5;
return ;
}
//单死2
if ( lr == 2 || ud == 2 || lutrd == 2 || ldtru == 2 )
{
if ( board_situation.is_machine )
board_situation.score = 2;
else
board_situation.score = -2;
return ;
}
//死1
if ( lr == 1 || ud == 1 || lutrd == 1 || ldtru == 1 )
{
if ( board_situation.is_machine )
board_situation.score = 1;
else
board_situation.score = -1;
return ;
}
return ;
}
int CChessDlg::LeftToRight_Status(int i, int j ,int array[][20])
{
int tempj,count;
tempj = j;
count = 1;
bool aliveone = false;
bool alivetwo = false;
bool beyond = false;
int side = array[i][j];
//toleft
while ( --tempj > 0 && array[i][tempj] == side)
{
count ++;
}
if ( tempj > 0 && array[i][tempj] == 0)
{
aliveone = true;
}
else if ( tempj > 0 && array[i][tempj] == 1 )
{
beyond = true;
}
tempj = j;
while ( ++ tempj <20 && array[i][tempj] == side )
{
count ++;
}
if ( tempj < 20 && array[i][tempj] == 0)
{
alivetwo = true;
}
else if ( tempj > 0 && array[i][tempj] == 1 )
{
beyond = true;
}
if ( count >= 5)
{
return 8 ;
}
if (count == 1 && aliveone == true && alivetwo == true )
{
return 0;
}
if ( count == 1 && beyond == true )
{
return 1 ;
}
if ( count > 1 && aliveone == true && alivetwo == true )
{
return count + 3;
}
if ( count > 1 && (aliveone == true || alivetwo == true ))
{
return count ;
}
return 0;
}
int CChessDlg::UpToDown_Status(int i, int j ,int array[][20])
{
int tempi,count;
tempi = i;
count = 1;
bool aliveone = false;
bool alivetwo = false;
bool beyond = false;
int side = array[i][j];
while ( --tempi > 0 && array[tempi][j] == side)
{
count ++;
}
if ( tempi > 0 && array[tempi][j] == 0 )
{
aliveone = true;
}
else if (tempi > 0 && array[tempi][j] == 1 )
{
beyond = true;
}
tempi = i;
while ( ++ tempi < 20 && array[tempi][j] == side )
{
count ++;
}
if ( tempi < 20 && array[tempi][j] == 0 )
{
alivetwo = true;
}
else if ( tempi < 20 && array[tempi][j] == 1)
{
beyond = true ;
}
if ( count >= 5)
{
return 8 ;
}
if (count == 1 && aliveone == true && alivetwo == true )
{
return 0;
}
if ( count == 1 && beyond == true )
{
return 1 ;
}
if ( count > 1 && aliveone == true && alivetwo == true )
{
return count + 3;
}
if ( count > 1 && (aliveone == true || alivetwo == true ))
{
return count ;
}
return 0;
}
int CChessDlg::LeftUpToRightDown_Status(int i, int j ,int array[][20])
{
int tempi,tempj,count;
tempi = i,tempj = j;
count = 1;
bool aliveone = false;
bool alivetwo = false;
bool beyond = false;
int side = array[i][j];
while ( --tempi > 0 && -- tempj >0 && array[tempi][tempj] == side)
{
count ++;
}
if ( tempi > 0 && tempj >0 && array[tempi][tempj] == 0 )
{
aliveone = true;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -