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

📄 doc.old

📁 讲mfc的书
💻 OLD
字号:
// Doc.cpp : implementation of the CBfieldDoc class
//

#include "stdafx.h"
#include "bfield.h"
#include "insert.h"
#include "Doc.h"
#include "ConnDlg.h"
#include "HostDlg.h"
#include "packet.h"
#include "mainfrm.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CBfieldDoc

IMPLEMENT_DYNCREATE(CBfieldDoc, CDocument)

BEGIN_MESSAGE_MAP(CBfieldDoc, CDocument)
	//{{AFX_MSG_MAP(CBfieldDoc)
	ON_UPDATE_COMMAND_UI(IDM_SETUP, OnUpdateSetup)
	ON_COMMAND(IDM_CONNECT, OnConnect)
	ON_COMMAND(IDM_HOST, OnHost)
	ON_UPDATE_COMMAND_UI(IDM_CONNECT, OnUpdateConnect)
	ON_UPDATE_COMMAND_UI(IDM_HOST, OnUpdateHost)
	ON_COMMAND(IDM_RESIGN, OnResign)
	ON_UPDATE_COMMAND_UI(IDM_RESIGN, OnUpdateResign)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CBfieldDoc construction/destruction

CBfieldDoc::CBfieldDoc()
{
	// TODO: add one-time construction code here
	sockfile=NULL;
	xmit=NULL;
	rcv=NULL;
	MyTurn=FALSE;
}

CBfieldDoc::~CBfieldDoc()
{
NetClose();
}

BOOL CBfieldDoc::OnNewDocument()
{
	if (!CDocument::OnNewDocument())
		return FALSE;
	NetClose();

	for (int i=0;i<2;i++)
		for (int x=0;x<10;x++)
			for (int y=0;y<10;y++)
				grid[i][x][y]='.';
	mode=0;
	placed=0;
	MyTurn=FALSE;
	return TRUE;
}

/////////////////////////////////////////////////////////////////////////////
// CBfieldDoc serialization

void CBfieldDoc::Serialize(CArchive& ar)
{
	if (ar.IsStoring())
	{
		// TODO: add storing code here
	}
	else
	{
		// TODO: add loading code here
	}
}

/////////////////////////////////////////////////////////////////////////////
// CBfieldDoc diagnostics

#ifdef _DEBUG
void CBfieldDoc::AssertValid() const
{
	CDocument::AssertValid();
}

void CBfieldDoc::Dump(CDumpContext& dc) const
{
	CDocument::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CBfieldDoc commands

char CBfieldDoc::GetState(int x,int y)
{
   return grid[mode][x][y];
}

void CBfieldDoc::OnClick(int x,int y)
{
if (mode==0)
	{
	int q,step=1;
	CInsert dlg;
	if (placed==0x1F)	
		{
		AfxMessageBox("You have placed all your pieces");
		return;
		}
	if (grid[0][x][y]!='.')
		{
		AfxMessageBox("Please click a blank square");
		return;
		}
	dlg.m_placed=placed;
	dlg.m_x=x;
	dlg.m_y=y;
	int res=dlg.DoModal();
	if (res==-1) return;
	if (res=='.') return; // shouldn't happen
	if ((dlg.m_Dir==DIR_WEST && x-dlg.m_Len<-1)||
	    (dlg.m_Dir==DIR_EAST && x+dlg.m_Len>10)||
		(dlg.m_Dir==DIR_NORTH && y-dlg.m_Len<-1)||
		(dlg.m_Dir==DIR_SOUTH && y+dlg.m_Len>10))
		{
		AfxMessageBox("The piece must completely fit on the board");
		return;
		}
	if (dlg.m_Dir==DIR_WEST||dlg.m_Dir==DIR_NORTH) step=-1;
	if (dlg.m_Dir==DIR_EAST||dlg.m_Dir==DIR_WEST)
		{
		for (q=0;q<dlg.m_Len;q++)
			if (grid[0][x+step*q][y]!='.')
			  {
			  AfxMessageBox("The piece overlaps another piece!");
			  return;
			  }
// set it up
		for (q=0;q<dlg.m_Len;q++)
		   grid[0][x+step*q][y]=res;
		}
	else
		{
		for (q=0;q<dlg.m_Len;q++)
			if (grid[0][x][y+step*q]!='.')
			  {
			  AfxMessageBox("The piece overlaps another piece!");
			  return;
			  }
		for (q=0;q<dlg.m_Len;q++)
		    grid[0][x][y+step*q]=res;
		}
	placed|=dlg.m_Piece;
	UpdateAllViews(NULL);
	}
else
	{
	CPacket pkt;
	if (!MyTurn) return;
	pkt.cmd=CMD_CLICK;
	pkt.x=x;
	pkt.y=y;
	pkt.Serialize(*xmit);
	xmit->Flush();
	pkt.Serialize(*rcv);
	if (pkt.cmd!=CMD_REPLY)
		{
		AfxMessageBox("Bad reply");
		return;
		}
	grid[1][pkt.x][pkt.y]=pkt.data;
	UpdateAllViews(NULL);
	MyTurn=FALSE;
	GoAhead();
	}
}

int CBfieldDoc::GetMode(void)
{
   return mode;
}

void CBfieldDoc::OnUpdateSetup(CCmdUI* pCmdUI) 
{
	pCmdUI->SetCheck(mode==0&&placed!=0x1F);
}

void CBfieldDoc::OnConnect() 
{
    CConnDlg dlg;
	if (dlg.DoModal()==IDCANCEL) return;
	mode=-1;
	UpdateStatus();
	if (!socket.Create())
		{
		err(socket.GetLastError());
		return;
		}
	
	while (!socket.Connect(dlg.m_Host,dlg.m_Port))
		if (socket.GetLastError()!=WSAECONNREFUSED)
		  {
		  err(socket.GetLastError());
		  return;
		  }
	sockfile=new CSocketFile(&socket);
	xmit=new CArchive(sockfile,CArchive::store);
	rcv=new CArchive(sockfile,CArchive::load);
	mode=1;
	UpdateStatus();
	UpdateAllViews(NULL);
	GoAhead();
}

void CBfieldDoc::OnHost() 
{
	CHostDlg dlg;
	if (dlg.DoModal()==IDCANCEL) return;
	mode=-1;
	UpdateStatus();
	if (!hostsocket.Create(dlg.m_Port))
		{
		err(GetLastError());
		return;
		}
	if (!hostsocket.Listen())
		{
		err(GetLastError());
		return;
		}
	while (!hostsocket.Accept(socket) && !hostsocket.GetLastError());
	if (hostsocket.GetLastError())
		{
		err(hostsocket.GetLastError());
		return;
		}
    sockfile=new CSocketFile(&socket);
	xmit=new CArchive(sockfile,CArchive::store);
	rcv=new CArchive(sockfile,CArchive::load);
	mode=1;
	UpdateStatus();
	UpdateAllViews(NULL);
	MyTurn=TRUE;
	AfxMessageBox("Connected: It is your turn");
	}

void CBfieldDoc::OnUpdateConnect(CCmdUI* pCmdUI) 
  {
  pCmdUI->Enable(mode==0&&placed==0x1F);
  }

void CBfieldDoc::OnUpdateHost(CCmdUI* pCmdUI) 
{
pCmdUI->Enable(mode==0&&placed==0x1F);
}

void CBfieldDoc::OnResign() 
{
CPacket pkt;
if (AfxMessageBox("Do you really want to quit?",MB_YESNO)==IDNO) return;
pkt.cmd=CMD_RESIGN;
pkt.Serialize(*xmit);
NetClose();	
OnNewDocument();
UpdateAllViews(NULL);
}

void CBfieldDoc::OnUpdateResign(CCmdUI* pCmdUI) 
{
pCmdUI->Enable(mode==1);
}

void CBfieldDoc::err(int ern)
{
CString s;
s.Format("%x-%x",ern,GetLastError());
AfxMessageBox(s);
}

void CBfieldDoc::NetClose()
{
if (xmit) delete xmit;
if (rcv) delete rcv;
if (sockfile) delete sockfile;
sockfile=NULL;
xmit=NULL;
rcv=NULL;
mode=0;
placed=0;
}

void CBfieldDoc::OnCloseDocument() 
{
	NetClose();
	CDocument::OnCloseDocument();
}

void CBfieldDoc::GoAhead(void)
{
CPacket pkt;
CPacket outgoing;
UpdateStatus();
pkt.Serialize(*rcv);
if (pkt.cmd==CMD_RESIGN)
	{
	AfxMessageBox("Your opponent resigned");
	NetClose();
	OnNewDocument();
	UpdateAllViews(NULL);
	return;
	}
if (pkt.cmd!=CMD_CLICK)
	{
	AfxMessageBox("Unexpected error");
	return;
	}
outgoing.data=grid[0][pkt.x][pkt.y];
outgoing.cmd=CMD_REPLY;
outgoing.x=pkt.x;
outgoing.y=pkt.y;
if (outgoing.data=='.') outgoing.data='X';
outgoing.Serialize(*xmit);
xmit->Flush();
MyTurn=TRUE;
AfxMessageBox("It is your turn");
}

BOOL CBfieldDoc::GetTurn()
{
return MyTurn;
}

void CBfieldDoc::UpdateStatus(void)
{
  CMainFrame *fw=(CMainFrame *)AfxGetMainWnd();
  fw->UpdateStatus();
}

BOOL CBfieldDoc::GetReady(void)
{
  return placed==0x1F;
}

⌨️ 快捷键说明

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