📄 chessdlg.cpp
字号:
#include "stdafx.h"
#include "Chat.h"
#include "ChessDlg.h"
#include "SetNetDlg.h"
#include "SocketThread.h"
#define DEEP 3
// CChessDlg 对话框
/*
CChessDlg * ppdlg;
UINT ListenThread(LPVOID lpParam)
{
ppdlg->SetWindowText("正在监听网络连接");
ppdlg->m_socket.Listen(1) ;
}
*/
IMPLEMENT_DYNAMIC(CChessDlg, CDialog)
CChessDlg::CChessDlg(CWnd* pParent /*=NULL*/)
: CDialog(CChessDlg::IDD, pParent)
{
for (int i = 0 ;i < 20 ; i++ )
for (int j=0;j<20;j++ )
Fivearray[i][j] = 0;
is_myturn = false;
set_netflag = false;
is_connect = false;
m_and_p_flag = false;
}
CChessDlg::~CChessDlg()
{
m_socket.Close();
m_socket.ShutDown(2);
m_con_socket.Close();
m_con_socket.ShutDown(2);
StepList.RemoveAll();
for (int i = 0 ;i < 20 ; i++ )
for (int j=0;j<20;j++ )
Fivearray[i][j] = 0;
}
void CChessDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Control(pDX, IDC_STATIC2, m_smain);
DDX_Control(pDX, IDC_EDIT1, m_edit);
DDX_Control(pDX, IDC_LIST1, m_listbox);
DDX_Control(pDX, IDC_BUTTON4, bt1);
DDX_Control(pDX, IDC_BUTTON3, bt2);
DDX_Control(pDX, IDC_BUTTON5, bt3);
DDX_Control(pDX, IDC_BUTTON2, bt4);
DDX_Control(pDX, IDC_BUTTON6, bt5);
DDX_Control(pDX, IDC_BUTTON7, bt7);
DDX_Control(pDX, IDC_COMBO1, m_combox);
}
BEGIN_MESSAGE_MAP(CChessDlg, CDialog)
ON_WM_CTLCOLOR()
ON_WM_PAINT()
ON_WM_NCLBUTTONDBLCLK()
ON_WM_LBUTTONDOWN()
ON_BN_CLICKED(IDC_BUTTON2, OnRegret)
ON_BN_CLICKED(IDC_BUTTON3, OnNet)
ON_MESSAGE(WAS_ACCEPT,OnMyMessage)
ON_BN_CLICKED(IDC_BUTTON5, OnSendMessage)
ON_WM_CLOSE()
ON_BN_CLICKED(IDC_BUTTON4, OnMachineAndPeson)
ON_WM_TIMER()
END_MESSAGE_MAP()
// CChessDlg 消息处理程序
BOOL CChessDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// TODO: 在此添加额外的初始化
//ShowWindow(SW_MAXIMIZE);
CRect rc ;
m_smain.GetClientRect(&rc);
int x = rc.right ;
int y = rc.bottom ;
step_x = (x)/20 ;
step_y = (y)/20;
if ( m_and_p_flag == true )
{
bt2.EnableWindow(false);
m_combox.SetCurSel(0);
is_myturn = true;
SetWindowText("五子棋(人机对战)");
//Fivearray[10][10] = 3 ;
SetTimer(1,500,0);
return true;
}
bt1.EnableWindow(false);
m_con_socket.SetHwnd((HWND)this);
m_socket.SetHwnd((HWND)this);
return TRUE; // return TRUE unless you set the focus to a control
// 异常:OCX 属性页应返回 FALSE
}
HBRUSH CChessDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
// TODO: 在此更改 DC 的任何属性
// TODO: 如果默认的不是所需画笔,则返回另一个画笔
pDC->SetBkMode(TRANSPARENT);
if (nCtlColor != CTLCOLOR_EDIT && nCtlColor != CTLCOLOR_LISTBOX)
hbr = ::CreateSolidBrush(RGB(128,128,200));
return hbr;
}
void CChessDlg::OnPaint()
{
CPaintDC dc(this); // device context for painting
// TODO: 在此添加消息处理程序代码
// 不为绘图消息调用 CDialog::OnPaint()
DrawGrid();
DrawAllChess();
}
void CChessDlg::DrawGrid()
{
CDC *pdc = m_smain.GetDC();
CRect rc ;
m_smain.GetClientRect(&rc);
int x = rc.right ;
int y = rc.bottom ;
int xx = rc.left ;
int yy = rc.top+6 ;
//画竖线
for (int i = 0 ; i < 21; i++ )
{
pdc->MoveTo(xx,yy);
pdc->LineTo(xx,y);
xx += step_x ;
}
xx = rc.left ;
yy = rc.top +6;
//画横线
for ( i = 0 ; i < 20 ; i++ )
{
pdc->MoveTo(xx,yy);
pdc->LineTo(x,yy);
yy += step_y ;
}
}
//鼠标左键事件
void CChessDlg::OnNcLButtonDblClk(UINT nHitTest, CPoint point)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
CDialog::OnNcLButtonDblClk(nHitTest, point);
}
void CChessDlg::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
if ( m_and_p_flag == true )
{
if ( is_myturn == true)
{
is_myturn = false;
DrawChess(point);
if ( IsSuccess()|| is_myturn == true)
return ;
//初级
if ( m_combox.GetCurSel() == 0 )
{
Step st;
if (SearchValue(Fivearray,st,true) != 100000)
SearchMaxValue(st,Fivearray);
st.side = 3;
Fivearray[st.x][st.y] = 3;
StepList.AddTail(st);
DrawChess(st.x,st.y,false);
m_currentstep.x = st.x;
m_currentstep.y = st.y;
//Invalidate();
}
//高级
else
{
Step st;
int score = SearchMaxValue(st,Fivearray);
if ( score >= 1000 || score <= 20 )
{
st.side = 3;
Fivearray[st.x][st.y] = 3;
StepList.AddTail(st);
DrawChess(st.x,st.y,false);
m_currentstep.x = st.x;
m_currentstep.y = st.y;
//Invalidate();
}
else
{
GameStatus temp;
for (int i = 0 ; i < 20 ; i++)
for (int jj = 0 ; jj< 20 ;jj++)
temp.fivearray[i][jj] = Fivearray[i][jj];
temp.deep = 0;
temp.is_machine = true ;
temp.score = 0;
Step st1;
Step st2;
//机器方下得分
int score1 = DeepSearch(temp,st1,true,0);
//换人方下得分
int score2 = DeepSearch(temp,st2,false,0);
if ( score1 > score2 )
{
st1.side = 3;
Fivearray[st1.x][st1.y] = 3;
StepList.AddTail(st1);
DrawChess(st1.x,st1.y ,false);
m_currentstep.x = st1.x;
m_currentstep.y = st1.y;
//Invalidate();
}
if ( score1 < score2 )
{
st2.side = 3;
Fivearray[st2.x][st2.y] = 3;
StepList.AddTail(st2);
DrawChess(st2.x,st2.y,false );
m_currentstep.x = st2.x;
m_currentstep.y = st2.y;
//Invalidate();
}
if ( score1 == score2 )
{
if ( st1.deep <= st2.deep )
{
st1.side = 3;
Fivearray[st1.x][st1.y] = 3;
StepList.AddTail(st1);
DrawChess(st1.x,st1.y,false);
m_currentstep.x = st1.x;
m_currentstep.y = st1.y;
//Invalidate();
}
else
{
st2.side = 3;
Fivearray[st2.x][st2.y] = 3;
StepList.AddTail(st2);
DrawChess(st2.x,st2.y,false );
m_currentstep.x = st2.x;
m_currentstep.y = st2.y;
//Invalidate();
}
}
}
}
if (! IsSuccess())
{
is_myturn = true;
}
}
return;
}
if (set_netflag == false)
{
MessageBox("你还未设置网络,如要网络对战,请点网络对战按钮","友情提示",MB_OK);
return ;
}
if ( is_connect == false)
{
MessageBox("没有网络连接","友情提示",MB_OK);
return;
}
if ( is_myturn == false)
{
MessageBox("别急别急,对方还没下呢","友情提示",MB_OK);
return;
}
DrawChess(point);
CDialog::OnLButtonDown(nFlags, point);
}
//画棋子
void CChessDlg::DrawChess (CPoint point)
{
int x = point.x -12;
int y = point.y-18 ;
int linex = y / step_y;
int liney = x / step_x ;
if ( (y - linex * step_y) > (step_y/2) )
{
linex++;
}
if ( (x - liney * step_x) > (step_x/2) )
{
liney++;
}
CDC * pdc = m_smain.GetDC();
if ( linex <= 0 || linex >= 20 || liney <= 0 || liney >= 20||Fivearray[linex][liney] != 0 )
{
is_myturn = true;
return ;
}
is_myturn = false;
Fivearray[linex][liney] = 1;
Step tstep;
tstep.side = 1;
tstep.x = linex;
tstep.y = liney;
StepList.AddTail(tstep);
pdc->SelectStockObject(BLACK_BRUSH);
//pdc->SelectStockObject(WHITE_PEN);
pdc->Ellipse(liney*step_x-10,linex*step_y-10+6,liney * step_x +10,linex * step_y +10+6);
//pdc->Rectangle(liney*step_x-10,linex*step_y-10+6,liney * step_x +10,linex * step_y +10+6);
if ( m_and_p_flag != true )
{
CString ms;
ms.Format("*****%d*****%d*****",linex,liney);
m_con_socket.Send(ms,ms.GetLength(),0);
IsSuccess();
}
}
void CChessDlg::DrawChess (int linex ,int liney,bool is_current= false)
{
CDC * pdc = m_smain.GetDC();
if (Fivearray[linex][liney] == 0)
return;
if ( Fivearray[linex][liney] == 1 )
{
pdc->SelectStockObject(BLACK_BRUSH);
}
else
{
pdc->SelectStockObject(WHITE_BRUSH);
}
if ( is_current == true)
{
CBrush br(RGB(211,198,244));
pdc->SelectObject(br);
pdc->Ellipse(liney*step_x-10,linex*step_y-10+6,liney * step_x +10,linex * step_y +10+6);
}
else
pdc->Ellipse(liney*step_x-10,linex*step_y-10+6,liney * step_x +10,linex * step_y +10+6);
if ( m_and_p_flag != true )
{
IsSuccess();
}
}
void CChessDlg::DrawAllChess()
{
for (int i = 0 ; i <20 ; i++ )
for (int j= 0; j<20 ; j++ )
{
if (Fivearray[i][j] != 0)
{
if ( m_currentstep.x == i && m_currentstep.y == j)
{
DrawChess(i,j,true);
}
else
DrawChess(i,j);
}
}
}
//悔棋
void CChessDlg::OnRegret()
{
// TODO: 在此添加控件通知处理程序代码
if (StepList.GetCount() == 0 )
{
MessageBox("你一个子都没下,悔什么悔啊,别乱搞","提示",MB_OK);
return;
}
if ( m_and_p_flag == true )
{
if ( StepList.GetCount() > 0)
{
Step tem = StepList.GetTail();
StepList.RemoveTail();
Fivearray[tem.x][tem.y] = 0 ;
if ( StepList.GetCount() > 0)
{
tem = StepList.GetTail();
if ( tem.side == 1 )
{
StepList.RemoveTail();
Fivearray[tem.x][tem.y] = 0;
}
}
}
Invalidate();
if ( StepList.GetCount() > 0 )
{
Step tem = StepList.GetTail();
m_currentstep = tem;
}
is_myturn = true ;
return ;
}
Step temp = StepList.GetTail();
CString str = "regretregret";
m_con_socket.Send(str,str.GetLength(),0);
}
//网络对战
void CChessDlg::OnNet()
{
// TODO: 在此添加控件通知处理程序代码
CSetNetDlg dlg;
if ( dlg.DoModal() ==IDOK)
{
if ( set_netflag == true )
{
CString str = "------bybyebyebye";
m_con_socket.Send(str,str.GetLength(),0);
}
m_socket.Close();
m_socket.ShutDown(2);
m_con_socket.Close();
m_con_socket.ShutDown(2);
StepList.RemoveAll();
for (int i = 0 ;i < 20 ; i++ )
for (int j=0;j<20;j++ )
Fivearray[i][j] = 0;
Invalidate();
is_connect = false;
set_netflag = true ;
int port = dlg.m_port;
//服务器
if ( dlg.checked_server == true )
{
is_server = true;
is_myturn = true;
if (m_socket.Create(port,SOCK_STREAM,NULL) == SOCKET_ERROR )
{
AfxMessageBox("创建端口失败");
return ;
}
SetWindowText("网络对战(正在等待连接)");
m_socket.Listen(1) ;
WSAAsyncSelect(m_socket,this->m_hWnd,WAS_ACCEPT,FD_ACCEPT|FD_READ);
}
else
{
is_server = false;
is_myturn = false;
m_con_socket.Create(5001,SOCK_STREAM);
CString ip;
ip = dlg.ip ;
if ( m_con_socket.Connect(ip,port) == TRUE )
{
this->SetWindowText("连接"+ip+"成功");
is_connect = true;
}
else
{
this->SetWindowText("连接"+ip+"失败");
MessageBox("连接远端服务器失败,请检查网络","友情提示",MB_OK);
}
}
}
}
void CChessDlg::OnReceive()
{
char buf[1024];
memset(buf,'\0',1024);
m_con_socket.Receive(buf,sizeof(buf),0);
if ( strstr(buf,"*****") )
{
is_myturn = true;
char *position1 = buf + 5 ;
char *position2 = strstr(position1,"*****");
char *position3 = position2 + 5 ;
char *position4 = strstr(position3,"*****");
*position4 = '\0';
*position2 = '\0';
int i = atoi(position1);
int j = atoi(position3);
Fivearray[i][j] = 2 ;
Step tstep;
tstep.side = 2;
tstep.x = i;
tstep.y = j;
StepList.AddTail(tstep);
DrawChess(i,j);
return;
}
CString str = buf;
if ( str == "overoveryouoverover")
{
MessageBox("你输了,结束战斗!","提示",MB_OK);
StepList.RemoveAll();
is_myturn = false;
for (int i = 0 ;i < 20 ; i++ )
for (int j=0;j<20;j++ )
Fivearray[i][j] = 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 ;
}
if ( str =="regretregret" )
{
if ( MessageBox(" 对方要求悔棋,你同意否?","悔棋",MB_YESNO) == IDYES )
{
is_myturn = false ;
CString str_agree;
str_agree = "agreeregrete";
m_con_socket.Send(str_agree,str_agree.GetLength(),0);
if ( StepList.GetCount() == 0)
{
return ;
}
Step st = StepList.GetTail();
Fivearray[st.x][st.y] = 0;
StepList.RemoveTail();
if (StepList.GetCount() != 0)
{
st = StepList.GetTail();
if ( st.side == 2 )
{
Fivearray[st.x][st.y] = 0;
StepList.RemoveTail();
}
}
}
else
{
CString str_no = "++++++nonononon++++++";
m_con_socket.Send(str_no,str_no.GetLength(),0);
}
Invalidate();
return ;
}
if ( str == "agreeregrete")
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -