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

📄 tttview.cpp

📁 API经典入门
💻 CPP
字号:
// tttview.cpp : implementation of the CTTTView class
//

#include "stdafx.h"
#include "ttt.h"

#include "tttdoc.h"
#include "tttview.h"

#ifdef _DEBUG
#undef THIS_FILE
static char BASED_CODE THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CTTTView

IMPLEMENT_DYNCREATE(CTTTView, CView)

BEGIN_MESSAGE_MAP(CTTTView, CView)
	//{{AFX_MSG_MAP(CTTTView)
	ON_WM_LBUTTONDOWN()
	ON_COMMAND(ID_OPTIONS_HUMANSTART, OnOptionsHumanstart)
	ON_UPDATE_COMMAND_UI(ID_OPTIONS_HUMANSTART, OnUpdateOptionsHumanstart)
	ON_WM_MOUSEMOVE()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTTTView construction/destruction

CTTTView::CTTTView():
	StartX(30),
	StartY(30),
	RegionWidth(100),
	RegionHeight(100)
{
}

CTTTView::~CTTTView()
{
}

/////////////////////////////////////////////////////////////////////////////
// CTTTView drawing

void CTTTView::OnDraw(CDC* pDC)
{
	CTTTDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);

	// TODO: add draw code for native data here
	
	DrawBoard(pDC);
	
	// Draw Moves
	for (int i = 0; i < BOARD_SIZE; i++)
		for (int j = 0; j < BOARD_SIZE; j++)
			if (pDoc->m_acBoard[i][j] == 'X')
				DrawX (pDC, i, j);
			else if (pDoc->m_acBoard[i][j] == 'O')
				DrawO (pDC, i, j);
				
	DisplayStatus (pDC);
	
}

void CTTTView::DrawBoard (CDC* pDC)
{
	int i;
	int nSize = BOARD_SIZE;
	
	// draw horizontal lines
	for (i = 1; i < nSize; i++)
	{
		pDC->MoveTo (StartX, StartY + i*RegionHeight);
		pDC->LineTo (StartX + nSize*RegionWidth, StartY + i*RegionHeight);
	}

	// draw vertical lines
	for (i = 1; i < nSize; i++)
	{
		pDC->MoveTo (StartX + i*RegionWidth, StartY);
		pDC->LineTo (StartX + i*RegionWidth, StartY + nSize*RegionHeight);
	}
	
}

void CTTTView::DrawX (CDC* pDC, int I, int J)
{
	int AtX = StartX + J*RegionWidth;
	int AtY = StartY + I*RegionHeight;

	pDC->MoveTo (AtX, AtY);
	pDC->LineTo (AtX + RegionWidth, AtY + RegionHeight);
	
	pDC->MoveTo (AtX, AtY + RegionHeight);
	pDC->LineTo (AtX + RegionWidth, AtY);
}

void CTTTView::DrawO (CDC* pDC, int I, int J)
{
	int AtX = StartX + J*RegionWidth;
	int AtY = StartY + I*RegionHeight;

	pDC->Ellipse (AtX, AtY, AtX + RegionWidth, AtY + RegionHeight);
}

void CTTTView::DisplayStatus (CDC* pDC)
{
	CTTTDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
    
    int X = StartX;
    int Y = StartY + BOARD_SIZE*RegionHeight;
    
    // Retrieve icon handle and draw icon.
    HICON hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
    pDC->DrawIcon(X, Y, hIcon);
    
    // Shift text over to accommodate icon.
    X += 40;
    
    CString S;
    
    switch (pDoc->m_cGameStatus)
    {
    	case 'X':
    	S.LoadString (IDS_HUMAN_WIN);
		//pDC->TextOut (X, Y, "You Win!  Congratulations.");
		break;
		
		case 'O':
		S.LoadString (IDS_COMPUTER_WIN);
		//pDC->TextOut (X, Y, "I Win!  Dumb Human.");
		break;
		
		case 'C':
		S.LoadString (IDS_DRAW);
		//pDC->TextOut (X, Y, "Meow! A draw -- Cat's game.");
		break;
		
		default:
		S.LoadString (IDS_IN_PROGRESS);
		//pDC->TextOut (X, Y, "Game in progress.");
	}
	
	pDC->TextOut (X, Y, S);
}

/////////////////////////////////////////////////////////////////////////////
// CTTTView diagnostics

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CTTTView message handlers

void CTTTView::OnLButtonDown(UINT nFlags, CPoint point)
{
	// TODO: Add your message handler code here and/or call default
	
	CTTTDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	
	// allow moves only if the game is in progress
	if (pDoc->m_cGameStatus != ' ')
		return;
		
	// Translate logical coordinate into TicTacToe array units
	
	int J = point.x - StartX;
	int I = point.y - StartY;
	
	// ignore mouse click if above or left of board
	if ( (I < 0) || (J < 0) )
		return;
		
	J = J/RegionWidth;
	I = I/RegionHeight;
	
	// ignore mouse click if below or right of board
	if ( (I >= BOARD_SIZE) || (J >= BOARD_SIZE) )
		return;
		
	// Accept mouse click only if clicked square was empty
	if (pDoc->m_acBoard[I][J] == ' ')
	{
		pDoc->m_acBoard[I][J] = 'X';
		pDoc->AnalyzeBoard();
		
		if (pDoc->m_cGameStatus == ' ')
		{
			pDoc->m_acBoard[pDoc->m_nBestI][pDoc->m_nBestJ] = 'O';
			pDoc->AnalyzeBoard();
		}
		
		pDoc->UpdateAllViews(NULL);
	}
	
	CView::OnLButtonDown(nFlags, point);
}

void CTTTView::OnOptionsHumanstart()
{
	// TODO: Add your command handler code here
	// Toggle the document's who starts variable
	
	CTTTDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	
	if (pDoc->m_cWhoGoesFirst == 'X')
		pDoc->m_cWhoGoesFirst = 'O';
	else
		pDoc->m_cWhoGoesFirst = 'X';	
	
}

void CTTTView::OnUpdateOptionsHumanstart(CCmdUI* pCmdUI)
{
	// TODO: Add your command update UI handler code here
	
	CTTTDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	
	pCmdUI->SetCheck (pDoc->m_cWhoGoesFirst == 'X');
}

void CTTTView::OnMouseMove(UINT nFlags, CPoint point)
{
	// TODO: Add your message handler code here and/or call default
	
	//CWinApp* pApp = AfxGetApp();
	//HCURSOR pC = pApp->LoadCursor(IDC_NewArrow);
	//::SetCursor(pC);
	//CView::OnMouseMove(nFlags, point);
}

⌨️ 快捷键说明

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