⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mychessview.cpp

📁 一个用MFC做的象棋游戏,可以联网双人对下,
💻 CPP
📖 第 1 页 / 共 3 页
字号:
// MyChessView.cpp : implementation of the CMyChessView class
//

#include "stdafx.h"
#include "MyChess.h"
#include "Array.h"
#include "MyChessDoc.h"
#include "MyChessView.h"
#include "IPDlg.h"
#include "TalkDialog.h"
#include "MainFrm.h"
#include<windows.h>
#include<mmsystem.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
#define  BUFFERSIZE 1024
#pragma  comment(lib,"winmm.lib")
//加载DLL
#pragma  comment(lib,"ArrayDll")
// CMyChessView
extern CMyArr theArr;
TCHAR FilePath[256];
int  ReviewSteps;
int LastId;
bool IsOpen=false;
bool IsEnd=true;


IMPLEMENT_DYNCREATE(CMyChessView, CView)

BEGIN_MESSAGE_MAP(CMyChessView, CView)
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, &CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, &CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, &CView::OnFilePrintPreview)
	ON_BN_CLICKED(IDC_START_BUTTON,OnStartButtonClicked)
	ON_WM_LBUTTONDOWN()
	ON_COMMAND(ID_HELP_CONNECT, &CMyChessView::OnHelpConnect)
	ON_COMMAND(ID_OPERATE_TALK, &CMyChessView::OnOperateTalk)


	//按钮消息映射
	ON_BN_CLICKED(ID_BtReview,OnBtReview)
	ON_BN_CLICKED(ID_BtNext,OnBtNext)
	ON_BN_CLICKED(ID_BtPrev,OnBtPrev)

	ON_WM_CHAR()
	ON_COMMAND(ID_OPERATE_REGRET, &CMyChessView::OnOperateRegret)
	ON_COMMAND(ID_FILE_NEW, &CMyChessView::OnFileNew)
	ON_COMMAND(ID_FILE_SAVEAS, &CMyChessView::OnFileSaveas)
	ON_COMMAND(ID_FILE_OPEN32776, &CMyChessView::OnFileOpen32776)
//	ON_COMMAND(ID_OPERATE_NEXT, &CMyChessView::OnOperateNext)
//	ON_COMMAND(ID_OPERATE_PREV, &CMyChessView::OnOperatePrev)
	ON_WM_CREATE()
//	ON_WM_ERASEBKGND()
END_MESSAGE_MAP()

// CMyChessView construction/destruction

CMyChessView::CMyChessView()
{
	m_pClientSocket=NULL;
	m_bIfLButtonDownMe=true;
	m_bIfBCanGo=false;
	m_TalkDlg=false;
	m_bMessFrom=false;
	m_bRegretFrom=false;
	m_bIfStart=false;
	m_bHeStart=false;
    setJISHIToZero=true;
	


	 //m_bIfButtDownFromMe=true;//表示在未接受到对方发送的消息之前是可以接受到自己单击消息的,
	// TODO: add construction code here

}
void  CMyChessView::RecieveData(CClientSocket *socket)
{
	userMSG recvMSG;

	char buffer[sizeof(CPoint)];
	memset(buffer,0,sizeof(CPoint));
	CPoint *point=new CPoint;
	//int result=socket->Receive(point,sizeof(CPoint));
	int result=socket->Receive((char *)&recvMSG,sizeof(recvMSG));
	CString str;
	str.Format(_T("%d,%d"),point->x,point->y);
	//AfxMessageBox(str);
	//memcpy(&point,buffer,result);
	UINT uRegret;
	UINT uStart;
	jishi=false;
	switch(recvMSG.msgType)
		{
		case 1://单击消息
			m_bIfLButtonDownMe=false;
			m_bIfBCanGo=true;
			m_bMessFrom=true;
	        memcpy(point,recvMSG.msgContent,sizeof(CPoint));
			OnLButtonDown(MK_SHIFT,*point);
			break;
		case 2://聊天消息
			if (!m_TalkDlg)
			{
				m_pTalkDLG=new TalkDialog(this);
				m_pTalkDLG->Create(IDD_TALK);
				m_pTalkDLG->ShowWindow(SW_SHOW);
				m_TalkDlg=true;
				CString talkContent;
				//recvMSG.msgContent[result-4]='\0';
				talkContent.Format("%s",recvMSG.msgContent);
				talkContent="对方说:"+talkContent;
				int i=m_pTalkDLG->m_talkList.GetCount();
				m_pTalkDLG->m_talkList.InsertString(i,talkContent);

			}
			else
			{
				CString talkContent;
				//recvMSG.msgContent[result-4]='\0';
				talkContent.Format("%s",recvMSG.msgContent);
				talkContent="对方说:"+talkContent;
				int i=m_pTalkDLG->m_talkList.GetCount();
				m_pTalkDLG->m_talkList.InsertString(i,talkContent);
			}
			break;
		case 3://悔棋消息
			uRegret =MessageBox("Do you agree?","Regret",MB_YESNO);
			if (uRegret==IDYES)
			{
				/*
				m_bRegretFrom=true;
				m_bAgreeRegret=true;*/

				Regret();
				userMSG agreeRegret;
				agreeRegret.msgType=4;//表示答应悔棋
				m_pClientSocket->Send((char *)&agreeRegret,sizeof(agreeRegret));
			}
			else if (uRegret==IDNO)
			{
				/*m_bAgreeRegret=false;*/
				userMSG agreeRegret;
				agreeRegret.msgType=5;//表示不答应悔棋
				m_pClientSocket->Send((char *)&agreeRegret,sizeof(agreeRegret));
			}
			break;
		case 4://表示对方答应悔棋
			Regret();
			break;
		case 5:
			AfxMessageBox("对方不答应悔棋");
			//表示对方不答应悔棋
			break;
		case 6://表示收到对方开始游戏的请求
			uStart=MessageBox("Do you start?","Request",MB_YESNO);
			if (uStart==IDYES)
			{
				userMSG agreeRegret;
				agreeRegret.msgType=7;//表示答应开始游戏
				//AfxMessageBox("好了游戏开始啦,你是B方,A先下");
				m_pClientSocket->Send((char *)&agreeRegret,sizeof(agreeRegret));
				m_bHeStart=true;
				m_bIfStart=true;
				SetTimer(1,300,NULL);
				SetTimer(2,1000,NULL);
				if (!jishi)
				{
					::AfxGetMainWnd()->SetTimer(1,1000,NULL);
					::AfxGetMainWnd()->KillTimer(2);
					jishi=true;
					setJISHIToZero=false;
				}
			}
			else if (uStart==IDNO)
			{
				userMSG agreeRegret;
				agreeRegret.msgType=8;//表示不答应开始游戏
				m_pClientSocket->Send((char *)&agreeRegret,sizeof(agreeRegret));
				m_bHeStart=false;
			}
			break;
		case 7://表示对方答应开始游戏
			m_bHeStart=true;
			//AfxMessageBox("好了游戏开始啦");
			SetTimer(1,300,NULL);
			SetTimer(2,1000,NULL);
			if (!jishi)
			{
				::AfxGetMainWnd()->SetTimer(1,1000,NULL);
				::AfxGetMainWnd()->KillTimer(2);
				jishi=true;
				setJISHIToZero=false;
			}
			break;
		case 8:
			//对方不答应开始游戏
			m_bHeStart=false;
			AfxMessageBox("等会现在对方没开始");
			break;

		}
	
	
	/*
		m_bIfLButtonDownMe=false;
				m_bIfBCanGo=true;
				
				OnLButtonDown(MK_SHIFT,*point);*/
		
	
}
CMyChessView::~CMyChessView()
{
}
void CMyChessView::OnStartButtonClicked()
{
	if (!m_pClientSocket)
	{
		AfxMessageBox("请先连接");
		return;
	}
	if (m_bIfStart)
	{
	   return;
	}
	//AfxMessageBox("sds");
	m_bIfStart=true;
	userMSG lButtonDownMsg;
	lButtonDownMsg.msgType=6;//表示请求开始游戏
	//m_pClientSocket->Send(&point, sizeof(CPoint));
	m_pClientSocket->Send((char *)&lButtonDownMsg,sizeof(lButtonDownMsg));
}
BOOL CMyChessView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs
   ///初始化button控件

	//CRect rect(50,30,55,35);
	//this->GetClientRect(&rect);
	//m_wndStartButton.Create(_T("Start"),WS_CHILD||WS_VISIBLE||BS_PUSHBUTTON,rect,this,IDC_START_BUTTON);

	return CView::PreCreateWindow(cs);
}

// CMyChessView drawing

void CMyChessView::OnDraw(CDC* pDC)
{
	//pDC->SetBkMode()
	CMyChessDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	if (!pDoc)
		return;

	CString myStr;
	myStr.Format("已走步数: %d",GetDocument()->m_chessMap.m_nSteps);
	pDC->TextOut(0,0,myStr);

    pDoc->m_cpPaper.Draw(pDC);
    pDoc->m_chessMap.Draw(pDC);
	if (setJISHIToZero)
	{
		CMainFrame *pMainFrame=(CMainFrame*)AfxGetMainWnd();
		CClientDC dc=CClientDC(this);
		pMainFrame->Timer1->SetToZero(&dc,0);
		pMainFrame->Timer2->SetToZero(&dc,300);

	}
}


// CMyChessView printing

BOOL CMyChessView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CMyChessView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CMyChessView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}


// CMyChessView diagnostics

#ifdef _DEBUG
void CMyChessView::AssertValid() const
{
	CView::AssertValid();
}

void CMyChessView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CMyChessDoc* CMyChessView::GetDocument() const // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMyChessDoc)));
	return (CMyChessDoc*)m_pDocument;
}
#endif //_DEBUG


// CMyChessView message handlers

void CMyChessView::OnLButtonDown(UINT nFlags, CPoint point)
{
	if (!m_pClientSocket)
	{
		AfxMessageBox("请先连接");
		return;
	}
	if (!m_bIfStart||!m_bHeStart)
	{
		AfxMessageBox("请先选择开始");
		return;
	}
	CClientDC *pDC=new CClientDC(this);
	if (true==m_bIfBCanGo)
	{

	if (m_pClientSocket)
	{
		if (true==m_bIfLButtonDownMe&&GetDocument()->m_nWhoPlayNow==2)
		{
		   GetDocument()->m_nWhoPlayNow=2;
	
		   uOCol=(point.x-GetDocument()->m_cpPaper.m_btBeginX+(GetDocument()->m_cpPaper.bm.bmWidth/8)/2)/(GetDocument()->m_cpPaper.bm.bmWidth/8);
		   uORow=(point.y-GetDocument()->m_cpPaper.m_btBeginY+(GetDocument()->m_cpPaper.bm.bmHeight/9)/2)/(GetDocument()->m_cpPaper.bm.bmHeight/9);

		   Npoint.x=GetDocument()->m_cpPaper.m_btBeginX-(GetDocument()->m_cpPaper.bm.bmWidth/8)/2+(8-uOCol)*(GetDocument()->m_cpPaper.bm.bmWidth/8);
		   Npoint.y=GetDocument()->m_cpPaper.m_btBeginY-(GetDocument()->m_cpPaper.bm.bmHeight/9)/2+(9-uORow)*(GetDocument()->m_cpPaper.bm.bmHeight/9);
		   userMSG lButtonDownMsg;
		   lButtonDownMsg.msgType=1;
           memcpy(lButtonDownMsg.msgContent,&Npoint,sizeof(Npoint));


		   m_pClientSocket->Send((char *)&lButtonDownMsg,sizeof(lButtonDownMsg));
		}
		
	}
	
	// TODO: Add your message handler code here and/or call default

  
	
	CClientDC dc(this);
	int nPosX=0,nPosY=0;

   CChessMan *pCurrentChess= GetDocument()->m_chessMap.FindStatePos(point,nPosX,nPosY);
 
  // CSoldier tempChess;
   /*if(pCurrentChess)//鼠标当前点击的位置有子存在
   {*/
	   if (1==GetDocument()->m_nWhoPlayNow)//表示是A方下了
	   {
		   if (!m_bMessFrom)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -