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

📄 3_1view.cpp

📁 一本游戏的入门书籍并附有源代码
💻 CPP
字号:
// 3_1View.cpp : implementation of the CMy3_1View class
//

#include "stdafx.h"
#include "3_1.h"

#include "3_1Doc.h"
#include "3_1View.h"
#include "MainFrm.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CMy3_1View

IMPLEMENT_DYNCREATE(CMy3_1View, CView)

BEGIN_MESSAGE_MAP(CMy3_1View, CView)
	//{{AFX_MSG_MAP(CMy3_1View)
	ON_WM_LBUTTONUP()
	ON_WM_SETCURSOR()
	ON_COMMAND(ID_START, OnStart)
	ON_COMMAND(ID_SAVE, OnSave)
	ON_COMMAND(ID_OPEN, OnOpen)
	//}}AFX_MSG_MAP
	// 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)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMy3_1View construction/destruction

CMy3_1View::CMy3_1View()
{
	// TODO: add construction code here
	//Load鼠标图像和棋子位图
	hcursorblack=AfxGetApp()->LoadCursor(IDC_CURSOR1);	
	hcursorwhite=AfxGetApp()->LoadCursor(IDC_CURSOR2);
	m_bmwhite.LoadBitmap(IDB_WHITE);
	m_bmblack.LoadBitmap(IDB_BLACK);
	//清理棋盘
	for(int i=0;i<19;i++)
		for(int j=0;j<19;j++)
			wzq[i][j]=0;
	//白棋先下
    colorwhite=true;
}

CMy3_1View::~CMy3_1View()
{
}

BOOL CMy3_1View::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CMy3_1View drawing

void CMy3_1View::OnDraw(CDC* pDC)
{
	CMy3_1Doc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here
	//画背景
    CBrush mybrush1;
 	mybrush1.CreateSolidBrush(RGB(192,192,192));	
 	CRect myrect1(0,0,1200,800);
 	pDC->FillRect(myrect1,&mybrush1);
	//画黑框线
	CPen mypen;
	CPen*myoldPen;
	mypen.CreatePen(PS_SOLID,1,RGB(0,0,0));
    myoldPen=pDC->SelectObject(&mypen);
    for(int i=0;i<19;i++)
	{		
		pDC->MoveTo(40,40+i*20);
		pDC->LineTo(400,40+i*20);
		pDC->MoveTo(40+i*20,40);
		pDC->LineTo(40+i*20,400);
	}
    //重画时显示存在的棋子
	CDC Dc;
	if(Dc.CreateCompatibleDC(pDC)==FALSE)
 	    AfxMessageBox("Can't create DC");
	for(int n=0;n<19;n++)
		for(int m=0;m<19;m++)
			if(wzq[n][m]==1)
			{
				//显示白棋
				Dc.SelectObject(m_bmwhite);
				pDC->BitBlt(n*20+32,m*20+32,160,160,&Dc,0,0,SRCCOPY);	
			}
			else if(wzq[n][m]==-1)
				{
				//显示黑棋
					Dc.SelectObject(m_bmblack);
					pDC->BitBlt(n*20+32,m*20+32,160,160,&Dc,0,0,SRCCOPY);	
				}
}

/////////////////////////////////////////////////////////////////////////////
// CMy3_1View printing

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CMy3_1View diagnostics

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CMy3_1View message handlers

 

void CMy3_1View::OnLButtonUp(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	CDC *pDC=GetDC();
	CDC Dc;
	if(Dc.CreateCompatibleDC(pDC)==FALSE)
 	    AfxMessageBox("Can't create DC");	
	 //是否在棋盘内 	 
	if(point.x>30&&point.x<410&&point.y>30&&point.y<410)
	{
		int px=(point.x-30)/20;
		int py=(point.y-30)/20;
		//是否已经有棋子
		if(colorwhite&&wzq[px][py]==0)
		{
			Dc.SelectObject(m_bmwhite);
			pDC->BitBlt(px*20+32,py*20+32,160,160,&Dc,0,0,SRCCOPY);	
			//表示存在白棋
			wzq[px][py]=1;
			//检查是否结束
			over(point);
			//换黑棋下
			colorwhite=false;
		}
		else if(wzq[px][py]==0)
			{
			Dc.SelectObject(m_bmblack);
		 	pDC->BitBlt(px*20+32,py*20+32,160,160,&Dc,0,0,SRCCOPY);	
			wzq[px][py]=-1;
			over(point);
			colorwhite=true;
			}
	}
	
	CView::OnLButtonUp(nFlags, point);
}

BOOL CMy3_1View::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message) 
{
	// TODO: Add your message handler code here and/or call default
	if(nHitTest==HTCLIENT)
	{
		//白棋下,显示白棋鼠标
		if(colorwhite)
		{
			CMainFrame*pFrm=(CMainFrame*)AfxGetApp()->m_pMainWnd;
			CStatusBar*pStatus=&pFrm->m_wndStatusBar;		
			if(pStatus)
			{
				pStatus->GetStatusBarCtrl().SetIcon(0,AfxGetApp()->LoadIcon(IDI_WHITE));
				pStatus->SetPaneText(0,"白棋下");
			}
			SetCursor(hcursorwhite);
		}
		//显示黑棋鼠标
		else
		{
			SetCursor(hcursorblack);
			CMainFrame*pFrm=(CMainFrame*)AfxGetApp()->m_pMainWnd;
			CStatusBar*pStatus=&pFrm->m_wndStatusBar;		
			if(pStatus)
			{
				//显示图像
				pStatus->GetStatusBarCtrl().SetIcon(0,AfxGetApp()->LoadIcon(IDI_BLACK));
				//显示文字
				pStatus->SetPaneText(0,"黑棋下");
			}
		}
		return 1;
	}
	return CView::OnSetCursor(pWnd, nHitTest, message);
}

void CMy3_1View::over(CPoint point)
{
	//获取鼠标指向数组位置
	int x=(point.x-30)/20;
	int y=(point.y-30)/20;
	//计算开始判断的坐标 xx,yy
	int xx,yy;
    if(x<4)
		xx=0;
	else
		xx=x-4;
	if(y<4)
		yy=0;
	else
		yy=y-4;
	
	int i,j,a;
	//横向判断
	for(i=xx;i<15;i++)
	{ 
		a=0;
		for(j=i;j<i+5;j++)
		{
			a=a+wzq[j][y];		
			//五个都是白棋
 			if(a==5)
			{
				AfxMessageBox("白棋胜!");
				//重新开始
				OnStart();return;
			}
			//五个都是黑棋
			if(a==-5)
			{
				AfxMessageBox("黑棋胜!");
				OnStart();return;
			}
		}
	}
	//竖向判断
	for(i=yy;i<15;i++)
	{ 
		a=0;
		for(j=i;j<i+5;j++)
		{
			a=a+wzq[x][j];		
 			if(a==5)
			{
				AfxMessageBox("白棋胜!");
				OnStart();return;
			}
			if(a==-5)
			{
				AfxMessageBox("黑棋胜!");
				OnStart();return;
			}
		}
	}
	//向右下角
	//判断起点位置
	if(x<y)
	{
	if(xx==0)
		yy=y-x;
	}
	else
	{
	if(yy==0)
		xx=x-y;
	}
	//参数over=1时退出循环
	int over=0;
	do
	{
		a=0;
		for(i=0;i<5;i++)
		{
			if( ((xx+i)<19)||((yy+i)<19) )
			{ 
				a=a+wzq[xx+i][yy+i];
				if(a==5)
				{
				AfxMessageBox("白棋胜!");
				OnStart();return;
				}
				if(a==-5)
				{
				AfxMessageBox("黑棋胜!");
				OnStart();return;
				}
			}
			//到了边界
			else
				over=11;		
		}
	    xx++; 
		yy++;				
	}while(over==0);
 	//向左下角
    if(y>(18-x))
	{
		if(x>13)
		{
			yy=y-(18-x);
			xx=18;
		}
		else
		{
			yy=y-4;
			xx=x+4;
		}
	}
	else
	{
		if(y<5)
		{
			xx=x+y;
			yy=0;
		}
		else
		{
			yy=y-4;
			xx=x+4;
		}
	}
    over=0;
	do
	{
		a=0;
		for(i=0;i<5;i++)
		{
			if((xx-i)>=0||(yy+i)<19)
			{	
				a=a+wzq[xx-i][yy+i];
				if(a==5)
			{
				AfxMessageBox("白棋胜!");
				OnStart();return;
			}
			if(a==-5)
			{
				AfxMessageBox("黑棋胜!");
				OnStart();return;
			}
			}
			//到了边界
			else
				over=1;
		}
	    xx-=1;
		yy+=1;				
	}while(over==0);
	 			
}

void CMy3_1View::OnStart() 
{
	// TODO: Add your command handler code here
	for(int i=0;i<19;i++)
		for(int j=0;j<19;j++)
			wzq[i][j]=0;
    colorwhite=true;
	Invalidate();
}


//保存文件
void CMy3_1View::OnSave() 
{
	// TODO: Add your command handler code here
	//设置保存的文件,后缀名wzq
	CFileDialog dlg(FALSE,"wzq",NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,"(*.WZQ)|*.wzq|All Files|*.*||",this);
	//如果公共类对话框为确定
	if(dlg.DoModal()==IDOK) 
		//获取文件名
		dlg.GetFileName();
	//否则,退出
	else
		return;
	//字符串变量
	CString str;
	int i,j;

 	CStdioFile file;
	//如果有问题,退出
	if(file.Open(dlg.GetFileName(),CFile::modeCreate|CFile::modeWrite|CFile::typeText)==0)
	{
		AfxMessageBox("save error!");
		return;
	}
	//循环把棋盘数组的值写进文件
	for(i=0;i<19;i++)
		for(j=0;j<19;j++)
		{
			if(wzq[i][j]==-1)
				file.WriteString("-1\n");
			if(wzq[i][j]==0)
				file.WriteString("0\n");
			if(wzq[i][j]==1)
				file.WriteString("1\n");
		}
	//保存当前下棋颜色
	if(colorwhite==true)
		file.WriteString("1\n");
	else
		file.WriteString("0\n");
	//关闭文件
 	file.Close();
	
}
//与保存文件相反
void CMy3_1View::OnOpen() 
{
	// TODO: Add your command handler code here
	CFileDialog dlg(TRUE,"wzq",NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,"(*.WZQ)|*.wzq|All Files|*.*||",this);
	if(dlg.DoModal()==IDOK) 
		dlg.GetFileName();
	else
		return;


	CString str;
	int i,j,m;

	CStdioFile file;
	if(file.Open(dlg.GetFileName(),CFile::modeRead)==0)
	{
		AfxMessageBox("save error!");
		return;
	}
	
	CArchive ar(&file,CArchive::load);

	for(i=0;i<19;i++)
		for(j=0;j<19;j++)
		{
			ar.ReadString(str);
			sscanf(str,"%d",&m);
			if(m==-1)
				wzq[i][j]=-1;
			if(m==0)
				wzq[i][j]=0;
			if(m==1)
				wzq[i][j]=1;
		}
	ar.ReadString(str);
	sscanf(str,"%d",&m);
	if(m==1)
		colorwhite=true;
	else
		colorwhite=false;
	file.Close();
	ar.Close();

	Invalidate(false);
	
}

⌨️ 快捷键说明

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