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

📄 fivechessview.cpp

📁 文件包含了很多VC实例
💻 CPP
字号:
// FivechessView.cpp : implementation of the CFivechessView class
//

#include "stdafx.h"
#include "Fivechess.h"

#include "FivechessDoc.h"
#include "FivechessView.h"

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

/////////////////////////////////////////////////////////////////////////////
// CFivechessView

IMPLEMENT_DYNCREATE(CFivechessView, CView)

BEGIN_MESSAGE_MAP(CFivechessView, CView)
	//{{AFX_MSG_MAP(CFivechessView)
	ON_WM_LBUTTONDOWN()
	ON_WM_RBUTTONDOWN()
	//}}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()

/////////////////////////////////////////////////////////////////////////////
// CFivechessView construction/destruction

CFivechessView::CFivechessView()
{
	RightORLeft = 0 ;
	for(int i = 0 ; i < 20 ; i++ )
	{
		for(int j = 0 ; j < 20 ; j++ )
		{
			chessArr[i][j] == 0 ;
		}
	}
}

CFivechessView::~CFivechessView()
{
}

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

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CFivechessView drawing

void CFivechessView::OnDraw(CDC* pDC)
{
	CFivechessDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	Checkerboard(pDC);
	// TODO: add draw code for native data here
}

void CFivechessView::Checkerboard(CDC * pDC)
{
	for(int i = 0 ; i < 20 ; i++ )
	{
		pDC->MoveTo(i*50,0);
		pDC->LineTo(i*50,1000);
	}
	for(i = 0 ; i < 20 ; i++ )
	{
		pDC->MoveTo(0,i*50);
		pDC->LineTo(1000,i*50);
	}
}

/////////////////////////////////////////////////////////////////////////////
// CFivechessView printing

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CFivechessView diagnostics

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CFivechessView message handlers

void CFivechessView::OnLButtonDown(UINT nFlags, CPoint point) 
{
	if(RightORLeft == 0 )
	{
		CBrush* brush = new CBrush(RGB(0,0,0)) ;
		CDC* pDC = this->GetDC();
		pDC->SelectObject(brush);
		for(int i = 0 ; i < 1000 ; i+=50)
		{
			for(int j = 0 ; j < 1000 ; j+=50)
			{
				CRect rect(i,j,i+50,j+50);
				CPoint tempPoint ;
				tempPoint = rect.CenterPoint();
				if(rect.PtInRect(point)&& isLocation(tempPoint) )
				{
					pDC->Ellipse(tempPoint.x-20,tempPoint.y-20,tempPoint.x+20,tempPoint.y+20);
					Location(tempPoint);
					chessArr[tempPoint.x/50][tempPoint.y/50] = 1 ;
					if( isWin(point) )
					{
						MessageBox("黑色棋子获胜!");
					}
					RightORLeft++ ;
				}
			}
		}
	}
	CView::OnLButtonDown(nFlags, point);
}

void CFivechessView::OnRButtonDown(UINT nFlags, CPoint point) 
{
	if(RightORLeft == 1  )
	{
		CBrush* brush = new CBrush(RGB(255,255,255)) ;
		CDC* pDC = this->GetDC();
		pDC->SelectObject(brush);
		for(int i = 0 ; i < 1000 ; i+=50)
		{
			for(int j = 0 ; j < 1000 ; j+=50)
			{
				CRect rect(i,j,i+50,j+50);
				CPoint tempPoint ;
				tempPoint = rect.CenterPoint();
				if(rect.PtInRect(point)&& isLocation(tempPoint))
				{
					pDC->Ellipse(tempPoint.x-20,tempPoint.y-20,tempPoint.x+20,tempPoint.y+20);
					Location(tempPoint);
					chessArr[tempPoint.x/50][tempPoint.y/50] = 2 ;
					if( isWin(point) )
					{
						MessageBox("白色棋子获胜!");
					}
					RightORLeft-- ;
				}
			}
		}
	}
	CView::OnRButtonDown(nFlags, point);
}

bool CFivechessView::isLocation(CPoint cp)
{
	bool isLoc = true ;
	for(int i = 0 ; i < chessVec.size() ; i++)
	{
		if(cp.x == chessVec[i].x && cp.y == chessVec[i].y)
		{
			isLoc = false ;
		}
	}
	return isLoc ;
}

void CFivechessView::Location(CPoint cp)
{
	chessVec.push_back(cp);
}

bool CFivechessView::isWin(CPoint cp)
{
	int cp_x = cp.x / 50 ;
	int cp_y = cp.y / 50 ;
	int i = cp_x ;
	int j = cp_y ;
	int count = 1 ;
	int nowChessColor = chessArr[cp_x][cp_y] ;
	while(1)
	{
		if(i > 0 && i < 20)
		{
			i-- ;
			if( nowChessColor == chessArr[i][j] )
			{
				count++ ;
				if( count == 5 )
				{
					return true ;
				}
			}
			else
			{
				break ;
			}
		}
		else
		{
			break;
		}
	}
	i = cp_x ;

	while(1)
	{
		if(i > 0 && i < 20)
		{
			i++ ;
			if( nowChessColor == chessArr[i][j] )
			{
				count++ ;
				if( count == 5 )
				{
					return true ;
				}
			}
			else
			{
				break ;
			}
		}
		else
		{
			break;
		}
	}
	i = cp_x ;
	count = 1 ;

	while(1)
	{
		if(j > 0 && j < 20)
		{
			j-- ;
			if( nowChessColor == chessArr[i][j] )
			{
				count++ ;
				if( count == 5 )
				{
					return true ;
				}
			}
			else
			{
				break ;
			}
		}
		else
		{
			break;
		}
	}
	j = cp_y ;

	while(1)
	{
		if(j > 0 && j < 20)
		{
			j++ ;
			if( nowChessColor == chessArr[i][j] )
			{
				count++ ;
				if( count == 5 )
				{
					return true ;
				}
			}
			else
			{
				break ;
			}
		}
		else
		{
			break;
		}
	}
	j = cp_y ;
	count = 1 ;

	while(1)
	{
		if(j > 0 && j < 20 && i > 0 && i < 20)
		{
			j++ ;
			i++ ;
			if( nowChessColor == chessArr[i][j] )
			{
				count++ ;
				if( count == 5 )
				{
					return true ;
				}
			}
			else
			{
				break ;
			}
		}
		else
		{
			break;
		}
	}
	j = cp_y ;
	i = cp_x ;

	while(1)
	{
		if(j > 0 && j < 20 && i > 0 && i < 20)
		{
			j-- ;
			i-- ;
			if( nowChessColor == chessArr[i][j] )
			{
				count++ ;
				if( count == 5 )
				{
					return true ;
				}
			}
			else
			{
				break ;
			}
		}
		else
		{
			break;
		}
	}
	j = cp_y ;
	i = cp_x ;
	count = 1 ;

	while(1)
	{
		if(j > 0 && j < 20 && i > 0 && i < 20)
		{
			j++ ;
			i-- ;
			if( nowChessColor == chessArr[i][j] )
			{
				count++ ;
				if( count == 5 )
				{
					return true ;
				}
			}
			else
			{
				break ;
			}
		}
		else
		{
			break;
		}
	}
	j = cp_y ;
	i = cp_x ;

	while(1)
	{
		if(j > 0 && j < 20 && i > 0 && i < 20)
		{
			j-- ;
			i++ ;
			if( nowChessColor == chessArr[i][j] )
			{
				count++ ;
				if( count == 5 )
				{
					return true ;
				}
			}
			else
			{
				break ;
			}
		}
		else
		{
			break;
		}
	}
	j = cp_y ;
	i = cp_x ;
	count = 1 ;

	return false ;
}

⌨️ 快捷键说明

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