www.pudn.com > TetrisWang.zip > Body.cpp


// Body.cpp: implementation of the CBody class. 
// 
////////////////////////////////////////////////////////////////////// 
 
#include "stdafx.h" 
#include "Tetris.h" 
#include "Body.h" 
 
#ifdef _DEBUG 
#undef THIS_FILE 
static char THIS_FILE[]=__FILE__; 
#define new DEBUG_NEW 
#endif 
 
////////////////////////////////////////////////////////////////////// 
// Construction/Destruction 
////////////////////////////////////////////////////////////////////// 
 
CBody::CBody() 
{ 
	m_nWidth = 10; 
	m_nHeight = 20; 
} 
 
CBody::CBody(CBody &b) 
{ 
	m_nWidth = b.m_nWidth; 
	m_nHeight = b.m_nHeight; 
	m_Tetris = b.m_Tetris; 
	m_ptStart = b.m_ptStart; 
	m_ptTetris = b.m_ptTetris; 
} 
 
CBody::~CBody() 
{ 
	ReSetBlocks(); 
} 
 
void CBody::Draw(CDC &dc) 
{ 
	int nSaveDC = dc.SaveDC(); 
	dc.SelectStockObject(WHITE_PEN); 
	dc.SelectStockObject(WHITE_BRUSH); 
	dc.Rectangle(CRect(m_ptStart, CSize(m_nWidth*CBlock::GetCell(), m_nHeight*CBlock::GetCell()))); 
 
	for (int i=0; i= m_nWidth || ptT.y >= m_nHeight) return false; 
		for (int j=0; j=0; nIndex--) 
			{ 
				int nYPos = m_Blocks[nIndex].GetYPos(); 
				if (nYPos < i) 
					m_Blocks[nIndex].SetYPos(nYPos+1); 
				if (nYPos == i) 
					m_Blocks.RemoveAt(nIndex); 
			} 
			nLines++; 
		} 
	} 
	return nLines; 
} 
 
void CBody::AddLines(int nLines, int nColor) 
{ 
	int i; 
	for (i=0; im_nHeight-nLines-1; i--) 
	{ 
		int nNull1 = rand() % m_nWidth; 
		int nNull2 = -1; 
		if (rand()%10 < 3) nNull2 = rand() % m_nWidth; 
		for (int n=0; n nScore) 
			{ 
				nScore = nS; 
				nRoate = nR; 
				nMove = nM; 
			} 
		} 
		m_Tetris.Roate(); 
	} 
	m_ptTetris = pt; 
} 
 
int CBody::GetScore(int nNearNull[9], int nRoate, int nMove, int nComputerIntelligence) 
{ 
	if (!IsTetrisFit(m_ptTetris, m_Tetris)) return 0; 
	int nScore = 0; 
	int nClearLines[4];		//	记录所消行号 
	int i, j; 
	for (i=0; i<4; i++) 
		nClearLines[i] = 0; 
 
	//	方块落下 
	for (i=1; ; i++) 
	{ 
		if (!IsTetrisFit(m_ptTetris + CSize(0, i), m_Tetris))	break; 
	} 
	m_ptTetris += CSize(0, i-1); 
	 
	//	计算消行加分 
	int nLines = 0; 
	for (i=m_ptTetris.y; i= m_nHeight-1) continue; 
		for (int j=0; j<4; j++) 
		{ 
			if (nClearLines[j] == nY) continue; 
		} 
		if (!HasBlock(nX, nY+1) && !HasTetrisBlock(nX, nY+1)) 
		{ 
			nNull++; 
		} 
	} 
	nScore -= 2000 * nNull; 
//	if (nNull) nScore -= 2000; 
 
	if (nComputerIntelligence == 0) return nScore; 
	//	高度差大扣分, 边上加分 
	int nMinX = 3; 
	int nMaxX = 0; 
	int nSub; 
	for (i=0; i nMaxX) nMaxX = nX; 
		if (nX < nMinX) nMinX = nX; 
	} 
	if (m_ptTetris.x + nMinX > 0) 
	{ 
		nSub = abs(GetCellMaxTier(m_ptTetris.x+nMinX) - GetCellMaxTier(m_ptTetris.x+nMinX-1)); 
		if (nSub > 2) nScore -= (nSub-2) * 800; 
		if (nSub = 2) nScore -= 500; 
	} 
	else 
	{ 
		nScore += 100; 
	} 
	if (m_ptTetris.x + nMaxX < m_nWidth-1) 
	{ 
		nSub = abs(GetCellMaxTier(m_ptTetris.x+nMaxX) - GetCellMaxTier(m_ptTetris.x+nMaxX+1)); 
		if (nSub > 2) nScore -= (nSub-2) * 800; 
		if (nSub = 2) nScore -= 500; 
	} 
	else 
	{ 
		nScore += 100; 
	} 
 
	if (nComputerIntelligence == 1) return nScore; 
	//	最近空行上不放方块 
	i = 0; 
	int nBlockOnNull; 
	while (nNearNull[i] >= 0) 
	{ 
		nBlockOnNull = 0; 
		for (j=0; j= m_nWidth) return 0; 
	int i; 
	int nY; 
	int nTier = m_nHeight-1; 
	for (i=0; i