📄 wuziqiview.cpp
字号:
// wuziqiView.cpp : implementation of the CWuziqiView class
//
#include "stdafx.h"
#include "wuziqi.h"
#include "MainFrm.h"
#include "wuziqiDoc.h"
#include "wuziqiView.h"
#include "chess.h"
#include "blocksock.h"
#include "connectto.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CWuziqiView
IMPLEMENT_DYNCREATE(CWuziqiView, CView)
BEGIN_MESSAGE_MAP(CWuziqiView, CView)
//{{AFX_MSG_MAP(CWuziqiView)
ON_WM_LBUTTONDOWN()
ON_COMMAND(ID_BACK_BLACK, OnBackBlack)
ON_UPDATE_COMMAND_UI(ID_BACK_BLACK, OnUpdateBackBlack)
ON_COMMAND(ID_BACK_WHITE, OnBackWhite)
ON_UPDATE_COMMAND_UI(ID_BACK_WHITE, OnUpdateBackWhite)
ON_COMMAND(ID_RULE_CHANGE, OnRuleChange)
ON_UPDATE_COMMAND_UI(ID_RULE_CHANGE, OnUpdateRuleChange)
ON_WM_RBUTTONDOWN()
ON_WM_RBUTTONDBLCLK()
ON_COMMAND(ID_RULE_3344, OnRule3344)
ON_COMMAND(ID_RULE_CLEAN, OnRuleClean)
ON_COMMAND(ID_CONNECT, OnConnect)
ON_COMMAND(ID_STARTSERVER, OnStartserver)
ON_UPDATE_COMMAND_UI(ID_STARTSERVER, OnUpdateStartserver)
ON_COMMAND(ID_DISCONNECT, OnDisconnect)
ON_UPDATE_COMMAND_UI(ID_DISCONNECT, OnUpdateDisconnect)
ON_UPDATE_COMMAND_UI(ID_RULE_RESTART, OnUpdateRuleRestart)
ON_COMMAND(ID_FILE_OPEN, OnFileOpen)
ON_COMMAND(ID_FILE_SAVE, OnFileSave)
ON_COMMAND(ID_RULE_RESTART, OnRuleRestart)
ON_WM_DESTROY()
//}}AFX_MSG_MAP
ON_MESSAGE(WM_CONNECT,OnConnect)//得到对方网络数据发送的消息
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CWuziqiView construction/destruction
CBlockingSocket g_sListen;//链接对象实例
UINT ServerThreadProc(LPVOID pParam);//服务器端进程
UINT ClientSocketThreadProc(LPVOID pParam);//发送数据进程
char ACCEPT[64],SEND[64];//接受,发送,数据类型
CString CONADDR,SERVER;//ip,计算机名
CSockAddr saClient;//ip地址
CWuziqiView::CWuziqiView()
{
who=whotemp=whohasjinshou=1;//初始化先首是黑棋
connect=youcansendmessage=connect=startserver=0;//初始化没有连接
WSADATA wsd;//初始化连接数据
VERIFY(WSAStartup(0x0101, &wsd) == 0);
TRACE("WSAStartup -- min version = %x\n", wsd.wVersion);
}
CWuziqiView::~CWuziqiView()
{
}
/////////////////////////////////////////////////////////////////////////////
// CWuziqiView drawing
void CWuziqiView::OnDraw(CDC* pDC)
{
int nline,drawx,drawy,nCount;
CString str,out;
char number[3];
number[2]=number[1]='\0';number[0]='0';
CRect rectClient;
nCount=0;
GetClientRect(rectClient);
pDC->SetMapMode(MM_ANISOTROPIC);
pDC->SetWindowExt(64,64);
pDC->SetViewportExt(rectClient.right,rectClient.bottom);
//把试图分为64*64,没4格划一条线
for(nline=0;nline<=64;nline+=4)
{//划线和打印数字
pDC->MoveTo(nline,0);
pDC->LineTo(nline,64);
pDC->SetTextColor(RGB(255,0,0));
pDC->TextOut(nline,0,number);
pDC->SetTextColor(RGB(0,0,255));
pDC->TextOut(0,nline,number);
if(number[1]!='\0')number[1]++;
else
{
number[0]++;
if(number[0]>='9'+1)
{
number[0]='1';
number[1]='0';
}
}
}
for(nline=0;nline<=64;nline+=4)
{
pDC->MoveTo(0,nline);
pDC->LineTo(64,nline);
}
//棋盘绘制完毕,开始收缩棋盘数据画棋子,0为没有,1为黑棋,-1为白旗
for(drawx=0;drawx<=17;drawx++)
for(drawy=0;drawy<=17;drawy++)
{
if(all.allqipan[drawx][drawy]==1)
{
nCount++;
out.Format("%d",nCount);
CRect drawrect(drawx*4-1,drawy*4-1,drawx*4+1,drawy*4+1);
CBrush brushHatch(RGB(34,108,243));
pDC->SelectObject(&brushHatch);
pDC->Ellipse(drawrect);
}
if(all.qipantemp[drawx][drawy]==1)
{
CRect drawrect(drawx*4-1,drawy*4-1,drawx*4+1,drawy*4+1);
CBrush brushHatch(RGB(180,231,180));
pDC->SelectObject(&brushHatch);
pDC->Ellipse(drawrect);
}
if(all.allqipan[drawx][drawy]==-1)
{
CRect drawrect(drawx*4-1,drawy*4-1,drawx*4+1,drawy*4+1);
CBrush brushHatch(RGB(78,229,78));
pDC->SelectObject(&brushHatch);
pDC->Ellipse(drawrect);
}
if(all.qipantemp[drawx][drawy]==-1)
{
CRect drawrect(drawx*4-1,drawy*4-1,drawx*4+1,drawy*4+1);
CBrush brushHatch(RGB(165,191,239));
pDC->SelectObject(&brushHatch);
pDC->Ellipse(drawrect);
}
}
//设置状态栏
CMainFrame* pFrame=(CMainFrame*)AfxGetApp()->m_pMainWnd;
CStatusBar* pStatus=&pFrame->m_wndStatusBar;
if(pStatus)
{
if(connect==0)
{
if(who==1)
{
str.Format("black down %d,white down %d,black's turn!",black.nCountkey,white.nCountkey);
}
else
{
str.Format("black down %d,white down %d,white's turn!",black.nCountkey,white.nCountkey);
}
}
if(connect==1)
{
if(youcansendmessage==1)
{
int out1,out2;
if(black.nCountkey>=white.nCountkey)
{
out1=black.nCountkey;
out2=white.nCountkey;
}
else
{
out1=white.nCountkey;
out2=black.nCountkey;
}
str.Format("you down %d,her down %d,your turn!",out2,out1);
}
else
{
int out1,out2;
if(black.nCountkey>=white.nCountkey)
{
out1=black.nCountkey;
out2=white.nCountkey;
}
else
{
out1=white.nCountkey;
out2=black.nCountkey;
}
str.Format("you down %d,her down %d,her turn!",out1,out2);
}
}
pStatus->SetPaneText(0,str);
}
//自动考试服务器监听
// OnStartserver();
// TODO: add draw code for native data here
}
/////////////////////////////////////////////////////////////////////////////
// CWuziqiView diagnostics
#ifdef _DEBUG
void CWuziqiView::AssertValid() const
{
CView::AssertValid();
}
void CWuziqiView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CWuziqiDoc* CWuziqiView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CWuziqiDoc)));
return (CWuziqiDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CWuziqiView message handlers
void CWuziqiView::OnLButtonDown(UINT nFlags, CPoint point)
{
float temp;int com;
int x,y;
CRect rectClient;
GetClientRect(rectClient);
temp=point.x*16;
temp=temp/rectClient.right;
com=temp;
if(temp-com>0.5)x=temp+1;
else x=temp;
temp=point.y*16;
temp=temp/rectClient.bottom;
com=temp;
if(temp-com>0.5)y=temp+1;
else y=temp;
//精确定位棋子位置
//判断可不可以下棋
if(connect==0)
{//没有连接,直接更新数据
if(all.allqipan[x][y]==0)
{ MessageBeep(MB_OK);
if(who==1)
{
black.keydown(x,y,who);
all.allqipan[x][y]=1;
if(all.shuangsi(x,y)==1&&whohasjinshou==1)
{
Invalidate();
resultwin lost;
lost.m_Who.Format("black lost!double 4!");
lost.DoModal();
OnRuleRestart();
return;
}
int win=all.winchanglian(x,y);
if(win==1)
{
Invalidate();
resultwin win;
win.m_Who.Format("black win!");
win.DoModal();
OnRuleRestart();
return;
}
if(win==0&&whohasjinshou==1)
{
Invalidate();
resultwin lost;
lost.m_Who.Format("black lost!long!");
lost.DoModal();
OnRuleRestart();
return;
}
if(all.shuangsan(x,y)==1&&whohasjinshou==1)
{
Invalidate();
resultwin fail;
fail.m_Who.Format("Black lost!double 3!");
fail.DoModal();
OnRuleRestart();
return;
}
}
else
{
white.keydown(x,y,who);
all.allqipan[x][y]=-1;
if(all.shuangsi(x,y)==1&&whohasjinshou==-1)
{
Invalidate();
resultwin lost;
lost.m_Who.Format("white lost!double 4!");
lost.DoModal();
OnRuleRestart();
return;
}
int win=all.winchanglian(x,y);
if(win==1)
{
Invalidate();
resultwin win;
win.m_Who.Format("White win!");
win.DoModal();
OnRuleRestart();
return;
}
if(win==0&&whohasjinshou==-1)
{
Invalidate();
resultwin lost;
lost.m_Who.Format("white lost!long!");
lost.DoModal();
OnRuleRestart();
return;
}
if(all.shuangsan(x,y)==1&&whohasjinshou==-1)
{
Invalidate();
resultwin fail;
fail.m_Who.Format("white lost!double 3!");
fail.DoModal();
OnRuleRestart();
return;
}
/* CString str;
CMainFrame* pFrame=(CMainFrame*)AfxGetApp()->m_pMainWnd;
CStatusBar* pStatus=&pFrame->m_wndStatusBar;
if(pStatus)
{
str.Format("white downed %d",white.nCountkey);
pStatus->SetPaneText(1,str);
}*/
}
who=-who;
}
}
//////////////////////////////////////////////////////////////////
if(connect==1&&youcansendmessage==1)
{//如果有连接还要发送数据到对方电脑
if(whoiam==1)
{
who=1;
}
else
{
who=-1;
}
if(all.allqipan[x][y]==0)
{ MessageBeep(MB_OK);//发声程序
if(whoiam==1)
{
SEND[0]='3';//下黑棋
}
else
{
SEND[0]='4';//下白旗
}
SEND[2]=x;
SEND[3]=y;
AfxBeginThread(ClientSocketThreadProc, GetSafeHwnd());
youcansendmessage=0;
if(who==1)
{//如果我是黑棋
whohasjinshou=1;
black.keydown(x,y,who);
all.allqipan[x][y]=1;
if(all.shuangsi(x,y)==1&&whohasjinshou==1)
{
Invalidate();
resultwin lost;
lost.m_Who.Format("black lost!double 4!");
lost.DoModal();
youcansendmessage=1;whoiam=1;
OnRuleRestart();
// return;
}
int win=all.winchanglian(x,y);
if(win==1)
{
Invalidate();
resultwin win;
win.m_Who.Format("black win!");
win.DoModal();
youcansendmessage=0;whoiam=-1;
OnRuleRestart();
// return;
}
if(win==0&&whohasjinshou==1)
{
Invalidate();
resultwin lost;
lost.m_Who.Format("black lost!long!");
lost.DoModal();
youcansendmessage=1;whoiam=1;
OnRuleRestart();
//return;
}
if(all.shuangsan(x,y)==1&&whohasjinshou==1)
{
Invalidate();
resultwin fail;
fail.m_Who.Format("Black lost!double 3!");
fail.DoModal();
youcansendmessage=1;whoiam=1;
OnRuleRestart();
// return;
}
}
else
{//我是白棋
//sendmessage
whohasjinshou=-1;
white.keydown(x,y,who);
all.allqipan[x][y]=-1;
if(all.shuangsi(x,y)==1&&whohasjinshou==1)
{
Invalidate();
resultwin lost;
lost.m_Who.Format("white lost!double 4!");
youcansendmessage=1;whoiam=1;
lost.DoModal();
OnRuleRestart();
// return;
}
int win=all.winchanglian(x,y);
if(win==1)
{
Invalidate();
resultwin win;
win.m_Who.Format("White win!");
youcansendmessage=0;whoiam=-1;
win.DoModal();
OnRuleRestart();
//return;
}
if(win==0&&whohasjinshou==1)
{
Invalidate();
resultwin lost;
lost.m_Who.Format("white lost!long!");
lost.DoModal();
youcansendmessage=1;whoiam=1;
OnRuleRestart();
//return;
}
if(all.shuangsan(x,y)==1&&whohasjinshou==1)
{
Invalidate();
resultwin fail;
fail.m_Who.Format("white lost!double 3!");
youcansendmessage=1;whoiam=1;
fail.DoModal();
OnRuleRestart();
// return;
}
}
/*if(youcansendmessage==1)
{//我发送了数据,告诉人家该他发送数据到我电脑
SEND[0]='6';
AfxBeginThread(ClientSocketThreadProc, GetSafeHwnd());
}*/
}
}
all.cleartemp();//清除所有试走棋盘
Invalidate();
CView::OnLButtonDown(nFlags, point);
}
void CWuziqiView::OnBackBlack()
{//黑色悔棋
int tempx,tempy;
// CString str;
tempx=black.nKeydown[black.nCountkey]/100;
tempy=black.nKeydown[black.nCountkey]-100*tempx;
black.bqipan[tempx][tempy]=0;
all.allqipan[tempx][tempy]=0;
black.nCountkey--;
who=-who;
/* CMainFrame* pFrame=(CMainFrame*)AfxGetApp()->m_pMainWnd;
CStatusBar* pStatus=&pFrame->m_wndStatusBar;
if(pStatus)
{
str.Format("black downed %d",black.nCountkey);
pStatus->SetPaneText(0,str);
}*/
Invalidate();
}
void CWuziqiView::OnUpdateBackBlack(CCmdUI* pCmdUI)
{
if(who!=-1||black.nCountkey==0)
pCmdUI->Enable(0);
if(connect==1)
pCmdUI->Enable(0);
}
void CWuziqiView::OnBackWhite()
{//白色悔棋
int tempx,tempy;
// CString str;
tempx=white.nKeydown[white.nCountkey]/100;
tempy=white.nKeydown[white.nCountkey]-100*tempx;
white.bqipan[tempx][tempy]=0;
all.allqipan[tempx][tempy]=0;
white.nCountkey--;
who=-who;
/* CMainFrame* pFrame=(CMainFrame*)AfxGetApp()->m_pMainWnd;
CStatusBar* pStatus=&pFrame->m_wndStatusBar;
if(pStatus)
{
str.Format("white downed %d",white.nCountkey);
pStatus->SetPaneText(1,str);
}
*/
Invalidate();
}
void CWuziqiView::OnUpdateBackWhite(CCmdUI* pCmdUI)
{
if(who!=1||white.nCountkey==0)
pCmdUI->Enable(0);
if(connect==1)
pCmdUI->Enable(0);
}
void CWuziqiView::OnRuleChange()
{//五手两打(交换)
int count1,count2,temp,tempcountkey;
// CString str;
whohasjinshou=-whohasjinshou;
for(count1=0;count1<=17;count1++)
for(count2=0;count2<=17;count2++)
{
temp=black.bqipan[count1][count2];
black.bqipan[count1][count2]=white.bqipan[count1][count2];
white.bqipan[count1][count2]=temp;
if(all.allqipan[count1][count2]!=0)
{
if(all.allqipan[count1][count2]==1)
{
all.allqipan[count1][count2]=-1;
}
else
{
all.allqipan[count1][count2]=1;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -