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

📄 mychessview.cpp

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

#include "stdafx.h"
#include "MyChess.h"
#include "Array.h"

//加载DLL
#pragma comment(lib,"ArrayDll")
#include "MyChessDoc.h"
#include "MyChessView.h"
#include "CClientSOcket.h"
#include "CServerSocket.h"
#include "afxsock.h"
#include "talkDiag.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")
// 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_CONECT, &CMyChessView::OnHelpConect)
	ON_COMMAND(ID_OPERATING_TALK, &CMyChessView::OnOperatingTalk)
	ON_COMMAND(ID_FILE_NEW, &CMyChessView::OnFileNew)
	ON_COMMAND(ID_OPERATING_REGRET, &CMyChessView::OnOperatingRegret)

 	ON_BN_CLICKED(ID_BtReview,OnBtReview)
    ON_BN_CLICKED(ID_BtNext,OnBtNext)
    ON_BN_CLICKED(ID_BtPrev,OnBtPrev)

//	ON_COMMAND(ID_FILE_SAVE_AS, &CMyChessView::OnFileSaveAs)
ON_COMMAND(ID_FILE_SAVEAS, &CMyChessView::OnFileSaveas)
ON_COMMAND(ID_FILE_OPEN32776, &CMyChessView::OnFileOpen32776)
//ON_COMMAND(ID_OPERATING_NEXT, &CMyChessView::OnOperatingNext)
//ON_COMMAND(ID_OPERATING_PREV, &CMyChessView::OnOperatingPrev)
ON_WM_CREATE()
END_MESSAGE_MAP()

// CMyChessView construction/destruction

CMyChessView::CMyChessView()
{
	// TODO: add construction code here
	m_pServerSocket=NULL;
	m_pClientSocket=NULL;
   m_bIfLButtonDownMe=true;//默认表示是自己的单击
 //  m_bIfAFinished=false;
 //  m_bIfBFinished=false;
   m_bIfACanGo=true;
   m_TalkDlg=false;
   m_bRegretFrom=false;
   m_bIfStart=false;
   m_bHeStart=false;
   m_bAgreeRegret=false;//初始化为不允许回棋 
   m_bMessFrom=false;//表示一开始信息不是从外面发过来的
   setJISHIToZero=true;
}

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)
{
	return CView::PreCreateWindow(cs);
}

void CMyChessView::AcceptConnect()
{
	CClientSocket* socket = new CClientSocket(this);
	//接受客户端的连接
	if (m_pServerSocket->Accept(*socket))
	{
		m_pClientSocket=socket;
		AfxMessageBox("connect");
	}
		//////////////////////////////////////////////////////////////////////////

		///以后准备在这里加东西
}
void CMyChessView::ReceiveData(CClientSocket* socket)
{

    userMSG recvMSG;
	  CPoint *point=new CPoint;
	  int result=socket->Receive((char *)&recvMSG,sizeof(recvMSG));
	
	 UINT uRegret;
	 UINT uStart;
	 jishi=false;
	switch(recvMSG.msgType)
		{
		case 1://单击消息
			m_bIfLButtonDownMe=false;
			m_bIfACanGo=true;
			m_bMessFrom=true;//表示这个消息是对方发过来的
			memcpy(point,recvMSG.msgContent,sizeof(CPoint));
			OnLButtonDown(MK_SHIFT,*point);
			break;
		case 2://聊天消息
			if (!m_TalkDlg)
			{
				m_myTalk=new talkDiag(this);
				m_myTalk->Create(IDD_TALK);
				m_myTalk->ShowWindow(SW_SHOW);
				m_TalkDlg=true;
				CString talkContent;
				//recvMSG.msgContent[result-4]='\0';
				talkContent.Format("%s",recvMSG.msgContent);
				talkContent="对方说:"+talkContent;
				int i=m_myTalk->m_content.GetCount();
			    m_myTalk->m_content.InsertString(i,talkContent);

			}
			else
			{
				CString talkContent;
				//recvMSG.msgContent[result-4]='\0';
				talkContent.Format("%s",recvMSG.msgContent);
				talkContent="对方说:"+talkContent;
				int i=m_myTalk->m_content.GetCount();
				m_myTalk->m_content.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("好了游戏开始啦");
				SetTimer(1,300,NULL);
				SetTimer(2,1000,NULL);
                if (!jishi)
                {
					::AfxGetMainWnd()->SetTimer(1,1000,NULL);
					::AfxGetMainWnd()->KillTimer(2);
					jishi=true;
					setJISHIToZero=false;
                }
				m_pClientSocket->Send((char *)&agreeRegret,sizeof(agreeRegret));
				m_bHeStart=true;
				m_bIfStart=true;
			}
			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("好了游戏开始啦,你是A方,你先下");
			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;

		}


// CMyChessView drawing
}
void CMyChessView::OnDraw(CDC* pDC)
{

	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)
{
	
	/*
	CString he;
		he.Format("x: %d, y: %d",point.x,point.y);
		MessageBox(he);*/
	
	// TODO: Add your message handler code here and/or call default
  /*
    CString str;
  	str.Format(_T("x:%d,Y:%d"),point.x,point.y);
  	AfxMessageBox(str);*/
	CClientDC *pDC=new CClientDC(this);
	if (!m_pClientSocket)
	{
		AfxMessageBox("请先连接");
		return;
	}
	if (!m_bIfStart||!m_bHeStart)
	{
		AfxMessageBox("请先选择开始");	
		return;
	}
  if (true==m_bIfACanGo)
  {

	if (m_pClientSocket)
	{
		
		if (true==m_bIfLButtonDownMe&&GetDocument()->m_nWhoPlayNow==1)
		{
			//char *buffer[sizeof(CPoint)];
			//memset(buffer,0,sizeof(CPoint));
			//memcpy(buffer,&point,sizeof(point));
			//m_pClientSocket->Send(&point,sizeof(CPoint));
			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(&point, sizeof(CPoint));
			m_pClientSocket->Send((char *)&lButtonDownMsg,sizeof(lButtonDownMsg));
		}
	}
	CClientDC dc(this);
	int nPosX=0,nPosY=0;
	//CChessMan *pBuffer;
	//CRect rect;
   CChessMan *pCurrentChess= GetDocument()->m_chessMap.FindStatePos(point,nPosX,nPosY);
  /*
   CString str;
    
     str.Format(_T("nPosX: %d,nPosY %d"),GetDocument()->m_cpPaper.bm.bmWidth,GetDocument()->m_cpPaper.bm.bmHeight);
       AfxMessageBox(str);
    */
  
  // CSoldier tempChess;
   /*if(pCurrentChess)//鼠标当前点击的位置有子存在
   {*/
	   if (1==GetDocument()->m_nWhoPlayNow)//表示是A方下了
	   {
		   
		   if (m_bMessFrom==true)

⌨️ 快捷键说明

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