?? mychessview.cpp
字號:
// 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 + -