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

📄 mychessview.cpp

📁 一个用MFC做的象棋游戏,可以联网双人对下,
💻 CPP
📖 第 1 页 / 共 3 页
字号:
	CIPDlg *pIPDlg=new CIPDlg(this);
	pIPDlg->Create(IDD_IP);
	pIPDlg->ShowWindow(SW_SHOW);
}

void CMyChessView::OnOperateTalk()
{
	// TODO: Add your command handler code here
	//MessageBox(L"ddd");
	if (!m_TalkDlg)
	{
		m_pTalkDLG=new TalkDialog(this);
		m_pTalkDLG->Create(IDD_TALK);
		m_pTalkDLG->ShowWindow(SW_SHOW);
		m_TalkDlg=true;
	}
	
}

void CMyChessView::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
	// TODO: Add your message handler code here and/or call default
   //MessageBox("sds");
	CView::OnChar(nChar, nRepCnt, nFlags);
}

void CMyChessView::OnOperateRegret()
{
	// TODO: Add your command handler code here
	if (CChessMan::m_stcChosenID!=-1)
	{
		AfxMessageBox("you can't regret now");
		return;
	}
	/*
	if (GetDocument()->m_nWhoPlayNow==2)
		{
			return;
		}*/
	
	userMSG lButtonDownMsg;
	lButtonDownMsg.msgType=3;//表示回棋
	//m_pClientSocket->Send(&point, sizeof(CPoint));
	m_pClientSocket->Send((char *)&lButtonDownMsg,sizeof(lButtonDownMsg));

}

void CMyChessView::Regret()
{
	if (!m_bIfStart||!m_bHeStart)
	{
		return;
	}
	if (GetDocument()->GoBack())
	{
		if (GetDocument()->m_nWhoPlayNow==1)
		{
			GetDocument()->m_chessMap.m_nSteps--;
			GetDocument()->m_nWhoPlayNow=2;
			m_bIfBCanGo=true;
			::AfxGetMainWnd()->KillTimer(1);
			::AfxGetMainWnd()->SetTimer(2,1000,NULL);
		}
		else if (GetDocument()->m_nWhoPlayNow==2)
		{
			GetDocument()->m_chessMap.m_nSteps--;
			GetDocument()->m_nWhoPlayNow=1;
			::AfxGetMainWnd()->SetTimer(1,1000,NULL);
			::AfxGetMainWnd()->KillTimer(2);
			
		}
		/*
		m_bRegretFrom=false;
		m_bAgreeRegret=false;*/

		CRect rect(0,0,500,600);
		InvalidateRect(&rect,TRUE);
	}
	

}

bool CMyChessView::SaveAs(HWND hWnd)
{
	bool bResult;
	OPENFILENAME ofn_SaveFile;
	ZeroMemory(FilePath,256);          //清空内存
	ZeroMemory(&ofn_SaveFile,sizeof(OPENFILENAME));
	ofn_SaveFile.lStructSize=sizeof(OPENFILENAME);
	ofn_SaveFile.hwndOwner=hWnd;
	ofn_SaveFile.hInstance=NULL;
	ofn_SaveFile.nMaxFile=256;     //指定lpstrFile 缓冲的大小
	ofn_SaveFile.nMaxFileTitle=256;
	ofn_SaveFile.lpstrFilter="Picture Files(*.CHE)\0*.CHE\0All Files\0*.*\0\0";      //文件选择的过滤条件
	ofn_SaveFile.lpstrDefExt="che";       //默认扩展名
	ofn_SaveFile.lpstrFile=FilePath;   //指向包含初始化文件名编辑控件使用的文件名的缓冲
	ofn_SaveFile.Flags=OFN_OVERWRITEPROMPT;


	ofstream ofs_OutFile;
	bResult=GetSaveFileName(&ofn_SaveFile);
	ofs_OutFile.open(FilePath);
	int steps;
	steps=GetDocument()->m_chessMap.m_nSteps;
	ofs_OutFile<<steps<<endl;
	for (int i=0;i<steps;i++)
	{
		ofs_OutFile<<GetDocument()->m_CSave.VToFile[i];
	}

	ofs_OutFile.close();
	return bResult;
}

bool CMyChessView::Open(HWND hWnd)
{
	bool bResult;
	OPENFILENAME ofn_OpenFile;	
	ZeroMemory(FilePath,256);          //清空内存
	ZeroMemory(&ofn_OpenFile,sizeof(OPENFILENAME));
	ofn_OpenFile.lStructSize=sizeof(OPENFILENAME);
	ofn_OpenFile.hwndOwner=hWnd;
	ofn_OpenFile.hInstance=NULL;
	ofn_OpenFile.nMaxFile=256;     //指定lpstrFile 缓冲的大小
	ofn_OpenFile.nMaxFileTitle=256;
	ofn_OpenFile.lpstrFilter="Picture Files(*.CHE)\0*.CHE\0All Files\0*.*\0\0";      //文件选择的过滤条件
	ofn_OpenFile.lpstrDefExt="che";       //默认扩展名
	ofn_OpenFile.lpstrFile=FilePath;   //指向包含初始化文件名编辑控件使用的文件名的缓冲
	ofn_OpenFile.Flags=OFN_FILEMUSTEXIST|OFN_PATHMUSTEXIST;

	bResult=GetOpenFileName(&ofn_OpenFile);



	if (bResult)
	{
		ReviewSteps=0;
		ifstream InFile;
		InFile.open(FilePath);
		int steps;
		InFile>>steps;
		ReviewSteps=steps;
		for (int i=0;i<GetDocument()->m_CSave.size();i++)
		{
			GetDocument()->m_CSave.pop();
		}
		StSaveInfo HelpOpen;
		for (int i=0;i<steps;i++)
		{
			InFile>>HelpOpen.uRoleId>>HelpOpen.uTargetId>>HelpOpen.uOldRow>>HelpOpen.uOldCol>>HelpOpen.uNewRow>>HelpOpen.uNewCol;
			GetDocument()->m_CSave.push(HelpOpen);
		}
		IsOpen=true;
		m_BtNext.EnableWindow(TRUE);
		InvalidateRect(NULL,TRUE);
	}

	return bResult;
}

void CMyChessView::OnFileNew()
{
	// TODO: Add your command handler code here
	
	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;
	GetDocument()->m_chessMap.Clear();
	GetDocument()->OnNewDocument();
	InvalidateRect(NULL,true);

}

void CMyChessView::OnFileSaveas()
{
	// TODO: Add your command handler code here
	SaveAs((HWND)((CFrameWnd*)AfxGetApp()->m_pMainWnd->m_hWnd));

	
}

void CMyChessView::OnFileOpen32776()
{
	// TODO: Add your command handler code here
	//打开复盘
	GetDocument()->m_chessMap.m_nSteps=0;
	Open(AfxGetMainWnd()->GetSafeHwnd());
	Invalidate(TRUE);
	GetDocument()->OnNewDocument();
}

//void CMyChessView::OnOperateNext()
//{
//	// TODO: Add your command handler code here
//}

//void CMyChessView::OnOperatePrev()
//{
//	// TODO: Add your command handler code here
//}

int CMyChessView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CView::OnCreate(lpCreateStruct) == -1)
		return -1;

	// TODO:  Add your specialized creation code here

	CRect rect1(530,230,610,260);
	CRect rect2(530,270,610,300);
	CRect rect3(530,310,610,340);
	// TODO:  Add your specialized creation code here
	m_BtReview.Create("调入复盘",WS_CHILD|WS_VISIBLE|BS_DEFPUSHBUTTON,rect1,this,ID_BtReview);
	m_BtNext.Create("下一步",WS_CHILD|WS_VISIBLE|BS_DEFPUSHBUTTON,rect2,this,ID_BtNext);
	m_BtPrev.Create("上一步",WS_CHILD|WS_VISIBLE|BS_DEFPUSHBUTTON,rect3,this,ID_BtPrev);
	m_BtPrev.EnableWindow(FALSE);
	m_BtNext.EnableWindow(FALSE);
	return 0;
}

void CMyChessView::OnBtReview()
{
	//打开复盘
	GetDocument()->m_chessMap.m_nSteps=0;
	Open((HWND)((CFrameWnd*)AfxGetApp()->m_pMainWnd->m_hWnd));
	GetDocument()->OnNewDocument();
	CClientDC dc=CClientDC(this);

	GetDocument()->m_cpPaper.Draw(&dc);
	CChessMap *d=&GetDocument()->m_chessMap;
	GetDocument()->m_chessMap.Draw(&dc);
	m_pClientSocket=NULL;
	m_bIfLButtonDownMe=true;//默认表示是自己的单击
}

void CMyChessView::OnBtPrev()
{
	StSaveInfo HelpBack;
	CChessMan *pOriginalChessMan;
	CChessMan* pTargetChessMan;
	//GetDocument()->m_chessMap.m_nSteps--;
	int StepNumPrev=GetDocument()->m_chessMap.m_nSteps-1;

	if (!IsEnd&&StepNumPrev>=0)
	{
		m_BtPrev.EnableWindow(TRUE);
		HelpBack=GetDocument()->m_CSave.VToFile[StepNumPrev];
		GetDocument()->m_chessMap.m_nSteps--;

		UINT uOldChessX,uOldChessY,uTargetChessX,uTargetChessY;

		if (GetDocument()->FindIndex(HelpBack.uRoleId,uOldChessX,uOldChessY))
		{
			pOriginalChessMan=GetDocument()->myChessMap[uOldChessX][uOldChessY];//找到动的棋子的指针
		}
		if (GetDocument()->FindIndex(HelpBack.uTargetId,uTargetChessX,uTargetChessY))
		{
			pTargetChessMan=GetDocument()->myChessMap[uTargetChessX][uTargetChessY];
		}
		else
		{
			pTargetChessMan=NULL;
		}
		pOriginalChessMan->ResetPos(HelpBack.uOldCol,HelpBack.uOldRow);

		if (pTargetChessMan!=NULL)
		{
			pTargetChessMan->ResetPos(HelpBack.uNewCol,HelpBack.uNewRow);
		}

		GetDocument()->m_chessMap.TheStateOfAllChess[HelpBack.uOldRow][HelpBack.uOldCol]=pOriginalChessMan;
		GetDocument()->m_chessMap.TheStateOfAllChess[HelpBack.uNewRow][HelpBack.uNewCol]=pTargetChessMan;

		CClientDC dc(this);

		CString str;
		str.Format("已走步数: %d",GetDocument()->m_chessMap.m_nSteps);
		dc.TextOut(0,0,str);
		(*pOriginalChessMan).InvalidateMyRect(&dc,HelpBack.uNewRow,HelpBack.uNewCol);
		GetDocument()->m_cpPaper.Draw(&dc);
		GetDocument()->m_chessMap.Draw(&dc);

		if(StepNumPrev==-1)
		{
			IsEnd=true;
			m_BtPrev.EnableWindow(FALSE);
		}
	} 
	else
	{
		IsEnd=true;
		InvalidateRect(NULL,TRUE);
		m_BtPrev.EnableWindow(FALSE);
	}	
}

void CMyChessView::OnBtNext()
{
	StSaveInfo HelpMove;
	int StepNum=GetDocument()->m_chessMap.m_nSteps;
	if (IsOpen==true&&StepNum<ReviewSteps)
	{
		HelpMove=GetDocument()->m_CSave.VToFile[StepNum];

		IsEnd=false;//上一步
		GetDocument()->m_chessMap.m_nSteps++;

		CChessMan *pNextChessMan= GetDocument()->m_chessMap.FindStatePos(HelpMove.uOldRow,HelpMove.uOldCol);
		pNextChessMan->ResetPos(HelpMove.uNewCol,HelpMove.uNewRow);
		GetDocument()->m_chessMap.ResetStatePos(HelpMove.uOldRow,HelpMove.uOldCol,NULL);
		CChessMan *pTargetChessMan= GetDocument()->m_chessMap.FindStatePos(HelpMove.uNewRow,HelpMove.uNewCol);
		GetDocument()->m_chessMap.ResetStatePos(HelpMove.uNewRow,HelpMove.uNewCol,pNextChessMan);
		if (!pTargetChessMan)
		{
			::PlaySound("res\\go.wav",NULL,SND_FILENAME|SND_ASYNC);
		}
		else
		{
			::PlaySound("res\\eat.wav",NULL,SND_FILENAME|SND_ASYNC);
		}

		CClientDC dc=CClientDC(this); 

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

		pNextChessMan->InvalidateMyRect(&dc,HelpMove.uOldRow,HelpMove.uOldCol);
		GetDocument()->m_cpPaper.Draw(&dc);
		CChessMap *d=&GetDocument()->m_chessMap;
		GetDocument()->m_chessMap.Draw(&dc);

		LastId=HelpMove.uTargetId;
		m_BtPrev.EnableWindow(TRUE);
	}
	else
	{
		::PlaySound("res\\gamewin.wav",NULL,SND_FILENAME|SND_ASYNC);
		if (LastId==16)
		{
			::AfxMessageBox("A wins!");
		} 
		else if (LastId==32)
		{
			::AfxMessageBox("B wins");
		}
		else
		{
			::AfxMessageBox("Tie");
		}
		m_BtNext.EnableWindow(FALSE);
		m_BtPrev.EnableWindow(TRUE);
	}
}

//BOOL CMyChessView::OnEraseBkgnd(CDC* pDC)
//{
//	// TODO: Add your message handler code here and/or call default
//	return CView::OnEraseBkgnd(pDC);
//}

⌨️ 快捷键说明

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