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

📄 childview.cpp

📁 援引别人的经典游戏
💻 CPP
📖 第 1 页 / 共 2 页
字号:

	DrawButton( &dc, SURPRISE );
	int row = (point.y - 55) / 16;
	int col = (point.x - 12) / 16;
	if( point.x < 12 || point.y < 55 ||
		!InBound(row, col) )
		return;

	if( nFlags & MK_RBUTTON )
	{
		OnBothButtonDown( row, col );
		return;
	}

	if( m_board[row][col] == NONE )
	{
		m_lastrow = row;
		m_lastcol = col;
		DrawCell( &dc, row, col, 0 );
	}

	CWnd ::OnLButtonDown(nFlags, point);
}

void CChildView::OnRButtonDown(UINT nFlags, CPoint point) 
{
	if( m_gameState != GS_ACTIVE )
		return;

	int row = (point.y - 55) / 16;
	int col = (point.x - 12) / 16;
	if( point.x < 12 || point.y < 55 ||
		!InBound(row, col) )
		return;

	if( nFlags & MK_LBUTTON )
	{
		OnBothButtonDown( row, col );
		return;
	}

	CClientDC dc(this);	
	if( m_board[row][col] == NONE )
	{
		m_board[row][col] = FLAG;
		DrawLCD( &dc, 17, 16, --m_nMinesLeft );
	}
	else if( m_board[row][col] == FLAG )
	{
		if( m_bMark )
			m_board[row][col] = MARK;
		else
			m_board[row][col] = NONE;
		DrawLCD( &dc, 17, 16, ++m_nMinesLeft );
	}
	else if( m_board[row][col] == MARK )
		m_board[row][col] = NONE;

	DrawCell( &dc, row, col );
	
	CWnd ::OnRButtonDown(nFlags, point);
}


void CChildView::OnBothButtonUp( int row, int col )
{
	CClientDC dc( this );
	if( m_board[row][col] > 8 )
	{
		if( m_lastrow >= 0 )
			DrawAdjacentCells( &dc, m_lastrow, m_lastcol, NONE );
		m_lastrow = -1;
		return;
	}
		
	int num = 0;
	for( int i = 0; i < 8; i ++ )
	{
		int nextrow = row + ymove[i];
		int nextcol = col + xmove[i];
		if( InBound(nextrow, nextcol) &&
			m_board[nextrow][nextcol] == FLAG )
			num ++;
	}
	
	if( m_board[row][col] == num )
	{
		for( int i = 0; i < 8; i ++ )
		{
			int nextrow = row + ymove[i];
			int nextcol = col + xmove[i];
			if( InBound(nextrow, nextcol) &&
				m_mines[nextrow][nextcol] == 9 &&
				m_board[nextrow][nextcol] != FLAG )
			{
				GameOver( nextrow, nextcol );
				break;
			}
		}
		if( m_gameState == GS_ACTIVE )
		{
			ZeroMemory( &m_graph, sizeof(m_graph) );
			Expand( &dc, row, col );
			if( CheckForWin() )
			{
				Win();
				return;
			}
			Invalidate();
		}
	}
	else if( m_lastrow >= 0 )
		DrawAdjacentCells( &dc, m_lastrow, m_lastcol, NONE );
	m_lastrow = -1;
}


void CChildView::OnBothButtonDown( int row, int col )
{
	m_lastrow = row;
	m_lastcol = col;
	CClientDC dc(this);
	DrawAdjacentCells( &dc, row, col, 0 );
}

void CChildView::Expand( CDC *pDC, int row, int col )
{
	m_graph[row][col] = TRUE;

	for( int i = 0; i < 8; i ++ )
	{
		int nextrow = row + ymove[i];
		int nextcol = col + xmove[i];
		if( InBound(nextrow, nextcol) &&
			!m_graph[nextrow][nextcol] )
		{
			if( m_mines[nextrow][nextcol] != 9 &&
				(m_board[nextrow][nextcol] == NONE ||
				m_board[nextrow][nextcol] == MARK) )
			{
				m_board[nextrow][nextcol] = m_mines[nextrow][nextcol];
				m_nDug ++;
			}
			if( m_mines[nextrow][nextcol] == 0 )
				Expand( pDC, nextrow, nextcol );
		}
	}
}

void CChildView::OnLButtonUp(UINT nFlags, CPoint point) 
{
	ReleaseCapture();

	CClientDC dc(this);
	if( m_rcButton.PtInRect( point ) )
	{
		OnGameNew();
		return;
	}
	if( m_gameState != GS_ACTIVE )
		return;

	DrawButton( &dc, SMILE );

	int row = (point.y - 55) / 16;
	int col = (point.x - 12) / 16;
	if( point.x < 12 || point.y < 55 ||
		!InBound(row, col) )
	{
		m_lastrow = -1;
		return;
	}
	
	if( nFlags & MK_RBUTTON )
	{
		OnBothButtonUp( row, col );
		return;
	}

	m_lastrow = -1;
	if( m_board[row][col] != NONE )
		return;

	if( m_bFirst )
	{
		m_bFirst = FALSE;
		SetTimer( 1, 1000, NULL );
	}

	if( m_mines[row][col] != 9 )
	{
		m_board[row][col] = m_mines[row][col];
		m_nDug ++;
		if( CheckForWin() )
		{
			Win();
			return;
		}
		if( m_board[row][col] == 0 )
		{
			ZeroMemory( &m_graph, sizeof(m_graph) );
			Expand( &dc, row, col );
			if( CheckForWin() )
			{
				Win();
				return;
			}
			Invalidate();
		}
		else
			DrawCell( &dc, row, col );
	}
	else
		GameOver( row, col );

	CWnd ::OnLButtonUp(nFlags, point);
}

void CChildView::OnMouseMove(UINT nFlags, CPoint point) 
{
	CClientDC dc(this);
	if( !m_rcButton.PtInRect( point ) )
	{
		if( m_bButtonPressed )
		{
			m_bButtonPressed = FALSE;
			if( m_gameState == GS_GAMEOVER )
				DrawButton( &dc, ANGRY);
			else if( m_gameState == GS_ACTIVE )
				DrawButton( &dc, SMILE );
			else
				DrawButton( &dc, WIN );
		}
	}
	else if( !m_bButtonPressed &&
			 (nFlags & MK_LBUTTON) )
	{
		m_bButtonPressed = TRUE;
		DrawButton( &dc, PRESSED );
	}

	if( !(nFlags & MK_LBUTTON) || m_gameState != GS_ACTIVE )
		return;

	int row = (point.y - 55) / 16;
	int col = (point.x - 12) / 16;
	if( nFlags & MK_RBUTTON )
	{
		if( point.x < 12 || point.y < 55 ||
			!InBound(row, col) )
		{
			if( m_lastrow >= 0 )
				DrawAdjacentCells( &dc, m_lastrow, m_lastcol, NONE );
			m_lastrow = -1;
			return;
		}
		if( row != m_lastrow || col != m_lastcol )
		{
			if( m_lastrow >= 0 )
				DrawAdjacentCells( &dc, m_lastrow, m_lastcol, NONE );
			DrawAdjacentCells( &dc, row, col, 0 );
			m_lastrow = row;
			m_lastcol = col;
		}
		return;
	}


	if( point.x < 12 || point.y < 55 ||
		!InBound(row, col) || m_board[row][col] != NONE )
	{
		if( m_lastrow >= 0 )
			DrawCell( &dc, m_lastrow, m_lastcol, NONE );
		m_lastrow = -1;
		return;
	}
	if( row != m_lastrow || col != m_lastcol )
	{
		if( m_lastrow >= 0 )
			DrawCell( &dc, m_lastrow, m_lastcol, NONE );
		DrawCell( &dc, row, col, 0 );
		m_lastrow = row;
		m_lastcol = col;
	}
	
	CWnd ::OnMouseMove(nFlags, point);
}

void CChildView::OnRButtonUp(UINT nFlags, CPoint point) 
{
	if( nFlags & MK_LBUTTON )
	{
		int row = (point.y - 55) / 16;
		int col = (point.x - 12) / 16;
		OnBothButtonUp( row, col );
	}
	
	CWnd ::OnRButtonUp(nFlags, point);
}


BOOL CChildView::CheckForWin()
{
	return (m_nDug >= m_width*m_height-m_nMines);
}

void CChildView::Win()
{
	for( int i = 0; i < m_height; i ++ )
		for( int j = 0; j < m_width; j ++ )
		{
			if( m_mines[i][j] == 9 )
				m_board[i][j] = FLAG;
		}
	m_nMinesLeft = 0;
	m_gameState = GS_WIN;
	KillTimer( 1 );
	Invalidate();
	
	if( m_level <= 2 &&
		m_time < m_recordTime[m_level] )
	{
		CWinDlg dlg;
		dlg.m_level = m_level;
		dlg.m_strName = m_recordName[m_level];
		if( dlg.DoModal() == IDOK )
		{
			m_recordName[m_level] = dlg.m_strName;
			m_recordTime[m_level] = m_time;
		}
	}
}


void CChildView::OnGameLevel( UINT nID )
{
	m_level = nID - ID_GAME_BASIC;
	OnGameNew();
}

void CChildView::OnUpdateGameLevel( CCmdUI *pCmdUI )
{
	int level = pCmdUI->m_nID - ID_GAME_BASIC;
	pCmdUI->SetCheck( m_level == level );
}

void CChildView::OnTimer(UINT nIDEvent) 
{
	if( !AfxGetMainWnd()->IsIconic() )
	{
		if( ++m_time > 999 )
			m_time = 999;
		CRect rc;
		GetClientRect( &rc );
		CClientDC dc(this);
		DrawLCD( &dc, rc.Width()-57, 16, m_time );
	}
	
	CWnd ::OnTimer(nIDEvent);
}

void CChildView::OnGameCustomize() 
{
	CCustomDlg dlg;
	dlg.m_width = m_width;
	dlg.m_height = m_height;
	dlg.m_mines = m_nMines;
	if( dlg.DoModal() == IDOK )
	{
		m_level = CUSTOM;
		m_width = dlg.m_width;
		m_height = dlg.m_height;
		m_nMines = dlg.m_mines;
		OnGameNew();
	}
}

void CChildView::OnGameMark() 
{
	m_bMark = !m_bMark;
}

void CChildView::OnUpdateGameMark(CCmdUI* pCmdUI) 
{
	pCmdUI->SetCheck( m_bMark );
}

void CChildView::OnGameRecord() 
{
	CRecordDlg dlg;
	for( int i = 0; i < 3; i ++ )
	{
		dlg.m_strName[i] = m_recordName[i];
		dlg.m_strTime[i].Format( "%d", m_recordTime[i] );
	}
	dlg.DoModal();
	for( i = 0; i < 3; i ++ )
	{
		m_recordName[i] = dlg.m_strName[i];
		sscanf( dlg.m_strTime[i], "%d", &m_recordTime[i] );
	}
}

void CChildView::OnHelpTopics() 
{
	HtmlHelp( NULL, "Winmine.chm::/Default.htm", HH_DISPLAY_TOPIC, 0 );
}

⌨️ 快捷键说明

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