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

📄 honnoiview.cpp

📁 汉罗塔
💻 CPP
字号:
// HonnoiView.cpp : implementation of the CHonnoiView class
//

#include "stdafx.h"
#include "Honnoi.h"

#include "HonnoiDoc.h"
#include "HonnoiView.h"

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

/////////////////////////////////////////////////////////////////////////////
// CHonnoiView

IMPLEMENT_DYNCREATE(CHonnoiView, CView)

BEGIN_MESSAGE_MAP(CHonnoiView, CView)
	//{{AFX_MSG_MAP(CHonnoiView)
	ON_COMMAND(IDC_START, OnStart)
	ON_COMMAND(IDC_SHEZHI, OnShezhi)
	//}}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()

/////////////////////////////////////////////////////////////////////////////
// CHonnoiView construction/destruction

CHonnoiView::CHonnoiView()
{
	// TODO: add construction code here
    Total = 3;
	Time = 50;
	int i;
	HonnoiSeat = new CHonnoiSeat[3];
	HonnoiTray = new CHonnoiTray[Total];
	for(i=1;i<=Total;i++)
	{
		CRect Rect1((100-i*5),(100+i*10),(100+5+i*5),(100+5+(i*10)));
		HonnoiTray[i-1] = Rect1;
	}
	for(i=0;i<3;i++)
	{
		CRect Rect2((100+300*i),100,(100+300*i+5),(100+5+Total*10));
		HonnoiSeat[i] = Rect2;
	}
	Needle[0] = 0;
	Needle[1] = Total;
	Needle[2] = 0;
	Needle[3] = 0;
	Start = true;
	shezhi = false;
}

CHonnoiView::~CHonnoiView()
{
	delete []HonnoiSeat;
	if( HonnoiTray != NULL )
		delete []HonnoiTray;
}

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

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CHonnoiView drawing

void CHonnoiView::OnDraw(CDC* pDC)
{
	CHonnoiDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here
	for(int i=0;i<3;i++)
	{
		HonnoiSeat[i].Draw(pDC);
	}
	for(i=0;i<Total;i++)
	{
		HonnoiTray[i].Draw(pDC);
	}
	pDC->TextOut(99+300*0,50,"A");
	pDC->TextOut(97+300*1,50,"B");
	pDC->TextOut(97+300*2,50,"C");
}

/////////////////////////////////////////////////////////////////////////////
// CHonnoiView printing

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CHonnoiView diagnostics

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CHonnoiView message handlers
void CHonnoiView::honnoi(int N, int s, int e, int t)
{
	CClientDC dc(this);			
	OnPrepareDC(&dc);
	if(N > 0)
	{
		honnoi(N-1,s,t,e);
		Needle[s]--;
		move_a_disk(N,s,e);
		for(int i=0;i<3;i++)
		{
			HonnoiSeat[i].Draw(&dc);
		}
		for(i=0;i<Total;i++)
		{
			HonnoiTray[i].Draw(&dc);
		}
		Sleep(Time);
		Needle[e]++;
		honnoi(N-1,t,e,s);
	}
}

void CHonnoiView::move_a_disk(int N, int s, int e)
{
	CClientDC dc(this);			
	OnPrepareDC(&dc);
	dc.SetROP2(R2_NOTXORPEN);
	int x1,y1,x2,y2;
	x1 = 100+300*(s-1)+N*5+5;
	y1 = (100+5+10*Total)-(10*Needle[s]);
	x2 = 100+300*(e-1)+N*5+5;
	y2 = (100+5+10*Total)-(10*Needle[e]);
	CRect rect1((x1-5-N*5*2),y1-5,x1,y1);
	CRect rect2((x2-5-N*5*2),y2-5,x2,y2);
	int i,j,m;
	bool k = true;
	CRect rect3 = rect1;
	while(k)
	{
		for(i = 1;i<=(y1-5) - 93;i++)
		{
			HonnoiTray[N-1] = rect3;
			HonnoiTray[N-1].Draw(&dc);
			CRect rect((x1-5-N*5*2),y1-5-i,x1,y1-i);
			HonnoiTray[N-1] = rect;
			HonnoiTray[N-1].Draw(&dc);
			Draw();
			rect3 = rect;
		}
		i--;
		if(x2>x1)
		{
			for(j = 1;j <= x2-x1;j++)
			{
				HonnoiTray[N-1] = rect3;
				HonnoiTray[N-1].Draw(&dc);
				CRect rect((x1-5-N*5*2)+j,y1-5-i,x1+j,y1-i);
				HonnoiTray[N-1] = rect;
				HonnoiTray[N-1].Draw(&dc);
				Draw();
				rect3 = rect;
			}
			j--;
			for( m = 1;m <= (y2-5)-93 ; m++)
			{
				HonnoiTray[N-1] = rect3;
				HonnoiTray[N-1].Draw(&dc);
				CRect rect((x1-5-N*5*2)+j,y1-5-i+m,x1+j,y1-i+m);
				HonnoiTray[N-1] = rect;
				HonnoiTray[N-1].Draw(&dc);
				Draw();
				rect3 = rect;
			}
		}
		if(x2<x1)
		{
			for(j = 1;j <= x1-x2;j++)
			{
				HonnoiTray[N-1] = rect3;
				HonnoiTray[N-1].Draw(&dc);
				CRect rect((x1-5-N*5*2)-j,y1-5-i,x1-j,y1-i);
				HonnoiTray[N-1] = rect;
				HonnoiTray[N-1].Draw(&dc);
				Draw();
				rect3 = rect;
			}
			j--;
			for( m = 1;m <= (y2-5)-93 ; m++)
			{
				HonnoiTray[N-1] = rect3;
				HonnoiTray[N-1].Draw(&dc);
				CRect rect((x1-5-N*5*2)-j,y1-5-i+m,x1-j,y1-i+m);
				HonnoiTray[N-1] = rect;
				HonnoiTray[N-1].Draw(&dc);
				Draw();
				rect3 = rect;
			}
		}

		k = false;
	}
}

void CHonnoiView::OnStart() 
{
	// TODO: Add your command handler code here
	honnoi(Total,1,3,2);
	Sleep(1000);
	for(int i=1;i<=Total;i++)
	{
		CRect Rect1((100-i*5),(100+i*10),(100+5+i*5),(100+5+(i*10)));
		HonnoiTray[i-1] = Rect1;
	}
	for(i=0;i<3;i++)
	{
		CRect Rect2((100+300*i),100,(100+300*i+5),(100+5+Total*10));
		HonnoiSeat[i] = Rect2;
	}
	Needle[0] = 0;
	Needle[1] = Total;
	Needle[2] = 0;
	Needle[3] = 0;
	Invalidate();
}

void CHonnoiView::OnShezhi() 
{
	// TODO: Add your command handler code here
	CSHEZHI dlg;
	int i;
	if( dlg.DoModal() == IDOK)
	{
		if( Total == dlg.panshu && Time == dlg.m_shiyan )
			return;
		Total = dlg.panshu;
		Time = dlg.m_shiyan;
	}
	else
		return;
	
	if( HonnoiTray != NULL )
	{
		delete []HonnoiTray;
		HonnoiTray = new CHonnoiTray[Total];
	}
	else
		HonnoiTray = new CHonnoiTray[Total];
	for(i=1;i<=Total;i++)
	{
		CRect Rect1((100-i*5),(100+i*10),(100+5+i*5),(100+5+(i*10)));
		HonnoiTray[i-1] = Rect1;
	}
	for(i=0;i<3;i++)
	{
		CRect Rect2((100+300*i),100,(100+300*i+5),(100+5+Total*10));
		HonnoiSeat[i] = Rect2;
	}
	Needle[0] = 0;
	Needle[1] = Total;
	Needle[2] = 0;
	Needle[3] = 0;
	Invalidate();
}

void CHonnoiView::Draw()
{
	CClientDC dc(this);			
	OnPrepareDC(&dc);
	for(int i=0;i<3;i++)
	{
		HonnoiSeat[i].Draw(&dc);
	}
	for(i=0;i<Total;i++)
	{
		HonnoiTray[i].Draw(&dc);
	}
	Sleep(1);
}

⌨️ 快捷键说明

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