www.pudn.com > black_and_white.rar > black_and_whiteView.cpp, change:2005-01-27,size:6136b


// black_and_whiteView.cpp : implementation of the CBlack_and_whiteView class 
// 
 
#include "stdafx.h" 
#include "black_and_white.h" 
 
#include "black_and_whiteDoc.h" 
#include "black_and_whiteView.h" 
#include <time.h> 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
///////////////////////////////////////////////////////////////////////////// 
// CBlack_and_whiteView 
 
IMPLEMENT_DYNCREATE(CBlack_and_whiteView, CView) 
 
BEGIN_MESSAGE_MAP(CBlack_and_whiteView, CView) 
	//{{AFX_MSG_MAP(CBlack_and_whiteView) 
	ON_WM_CREATE() 
	ON_WM_LBUTTONDOWN() 
	ON_COMMAND(ID_NEW, OnNew) 
	ON_WM_MOUSEMOVE() 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CBlack_and_whiteView construction/destruction 
 
CBlack_and_whiteView::CBlack_and_whiteView() 
{ 
	// TODO: add construction code here 
 
} 
 
CBlack_and_whiteView::~CBlack_and_whiteView() 
{ 
} 
 
BOOL CBlack_and_whiteView::PreCreateWindow(CREATESTRUCT& cs) 
{ 
	// TODO: Modify the Window class or styles here by modifying 
	//  the CREATESTRUCT cs 
	return CView::PreCreateWindow(cs); 
} 
 
///////////////////////////////////////////////////////////////////////////// 
// CBlack_and_whiteView drawing 
 
void CBlack_and_whiteView::OnDraw(CDC* pDC) 
{ 
	CBlack_and_whiteDoc* pDoc = GetDocument(); 
	ASSERT_VALID(pDoc); 
	// TODO: add draw code for native data here 
	CClientDC dc(this); 
	CBrush redb; 
	int j; 
	int i; 
	dc.Rectangle(199,49,501,351); 
	redb.CreateSolidBrush(RGB(255,100,100)); 
	dc.SelectObject(redb); 
	dc.Rectangle(100,0,600,400); 
	for(int k=0;k<9;k++) 
	{	if(pos[k]) 
		      dc.SelectObject(&blueb); 
	        else 
			  dc.SelectObject(&greenb); 
	        i=k/3; 
	        j=k%3;  
	dc.Rectangle(200+i*100,50+j*100,300+i*100,150+j*100); 
	}	 
	CString str="你还有的步数:"; 
	dc.TextOut(220,360,str); 
	str.Format("%d",9-count_step); 
    dc.TextOut(380,360,str); 
} 
 
///////////////////////////////////////////////////////////////////////////// 
// CBlack_and_whiteView diagnostics 
 
#ifdef _DEBUG 
void CBlack_and_whiteView::AssertValid() const 
{ 
	CView::AssertValid(); 
} 
 
void CBlack_and_whiteView::Dump(CDumpContext& dc) const 
{ 
	CView::Dump(dc); 
} 
 
CBlack_and_whiteDoc* CBlack_and_whiteView::GetDocument() // non-debug version is inline 
{ 
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CBlack_and_whiteDoc))); 
	return (CBlack_and_whiteDoc*)m_pDocument; 
} 
#endif //_DEBUG 
 
///////////////////////////////////////////////////////////////////////////// 
// CBlack_and_whiteView message handlers 
 
int CBlack_and_whiteView::OnCreate(LPCREATESTRUCT lpCreateStruct)  
{ 
	if (CView::OnCreate(lpCreateStruct) == -1) 
		return -1; 
	 
	// TODO: Add your specialized creation code here 
	start_flag=TRUE; 
	count_step=0; 
	first=FALSE; 
	x_3d=5; 
	y_3d=5; 
	for(int i=0;i<9;i++) 
	{ 
		{	pos[i]=TRUE;} 
	} 
	int aaa[9][3]={{1,3,4},{0,4,2},{1,4,5},{0,4,6},{0,0,0},{2,4,8},{3,4,7},{6,4,8},{7,4,5}}; 
	int k; 
 srand( (unsigned)time(NULL)); 
	for(int j=0;j<30;j++) 
	{  k=rand()%9; 
	   pos[k]=!pos[k]; 
	   if(k==4) 
		  { pos[1]=!pos[1]; 
		    pos[3]=!pos[3]; 
			pos[5]=!pos[5]; 
			pos[7]=!pos[7]; 
		  } 
	   else{ 
       for(i=0;i<3;i++); 
       pos[aaa[k][i]]=!pos[aaa[k][i]]; 
	   } 
	} 
    blueb.CreateSolidBrush(RGB(100,100,255)); 
    greenb.CreateSolidBrush(RGB(100,255,100)); 
	return 0; 
} 
 
void CBlack_and_whiteView::OnLButtonDown(UINT nFlags, CPoint point)  
{ 
	// TODO: Add your message handler code here and/or call default 
	int aaa[9][3]={{1,3,4},{0,4,2},{1,4,5},{0,4,6},{0,0,0},{2,4,8},{3,4,7},{6,4,8},{7,4,5}}; 
	if(start_flag) 
	{if((point.x>200)&&(point.x<500)) 
		if((point.y>50)&&(point.y<350)) 
		{int x=(point.x-200)/100; 
		 int y=(point.y-50)/100; 
//		 if(x=) 
		  pos[x*3+y]=!pos[x*3+y]; 
		  if(x==1&&y==1) 
		  { pos[1]=!pos[1]; 
		    pos[3]=!pos[3]; 
			pos[5]=!pos[5]; 
			pos[7]=!pos[7]; 
		  } 
		  else 
		  { for(int i=0;i<3;i++) 
		      pos[aaa[x*3+y][i]]=!pos[aaa[x*3+y][i]]; 
		  } 
         count_step++; 
		 x_3d=5; 
		 y_3d=5; 
		}		 
		InvalidateRect(NULL,TRUE); 
		UpdateWindow();		 
	    for(int i=0;i<9;i++) 
		{if(pos[i]!=TRUE)break; 
		 if(i==8) 
		 {  start_flag=0; 
			 if(MessageBox("you win!",NULL,MB_YESNO)==IDYES) 
		   OnNew(); 
		 }	 
		} 
		if(i!=8&&count_step>=9) 
		{  start_flag=0; 
		  if(MessageBox("you lose!",NULL,MB_YESNO)==IDYES) 
		     OnNew(); 
		} 
	} 
	CView::OnLButtonDown(nFlags, point); 
} 
 
 
void CBlack_and_whiteView::OnNew()  
{ 
	// TODO: Add your command handler code here 
	    start_flag=TRUE; 
		count_step=0; 
	for(int i=0;i<9;i++) 
	{ 
		{	pos[i]=TRUE;} 
	} 
	int aaa[9][3]={{1,3,4},{0,4,2},{1,4,5},{0,4,6},{0,0,0},{2,4,8},{3,4,7},{6,4,8},{7,4,5}}; 
	int k; 
 srand( (unsigned)time(NULL)); 
	for(int j=0;j<30;j++) 
	{  k=rand()%9; 
	   pos[k]=!pos[k]; 
	   if(k==4) 
		  { pos[1]=!pos[1]; 
		    pos[3]=!pos[3]; 
			pos[5]=!pos[5]; 
			pos[7]=!pos[7]; 
		  } 
	   else{ 
       for(i=0;i<3;i++); 
       pos[aaa[k][i]]=!pos[aaa[k][i]]; 
	   } 
	} 
       InvalidateRect(NULL,TRUE); 
		UpdateWindow();	 
} 
 
void CBlack_and_whiteView::OnMouseMove(UINT nFlags, CPoint point)  
{ 
	// TODO: Add your message handler code here and/or call default 
	CClientDC dc(this); 
		int y; 
		int x; 
	if(start_flag) 
	{if((point.x>200)&&(point.x<500)) 
	{	if((point.y>50)&&(point.y<350)) 
		{	 x=(point.x-200)/100; 
			 y=(point.y-50)/100; 
          if(x!=x_3d||y!=y_3d) 
		  {	dc.Draw3dRect(x*100+200,y*100+50,100,100,RGB(255,255,255),RGB(0,0,0));	 
		   if(first) 
		   {   if(pos[x_3d*3+y_3d]) 
		       dc.SelectObject(&blueb); 
	        else 
				dc.SelectObject(&greenb); 
			dc.Rectangle(x_3d*100+200,y_3d*100+50,x_3d*100+300,y_3d*100+150); 
		   } 
            x_3d=x; 
			y_3d=y; 
		    first=TRUE; 
		  } 
		} 
	} 
	else  
	{   if(first) 
		   {   if(pos[x_3d*3+y_3d]) 
		       dc.SelectObject(&blueb); 
	        else 
				dc.SelectObject(&greenb); 
			dc.Rectangle(x_3d*100+200,y_3d*100+50,x_3d*100+300,y_3d*100+150); 
		   } 
		first=FALSE; 
	 x_3d=5; 
	 y_3d=5; 
	} 
	} 
 
 
	CView::OnMouseMove(nFlags, point); 
}