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

📄 dlgagamescore.cpp

📁 ◆◆◆ 《投掷飞镖记分工具》◆◆◆ 操作系统 : Windows Mobile 5.0 说明 : 中秋节和家人赏月
💻 CPP
字号:
// DlgScore.cpp : 实现文件
//

#include "stdafx.h"
#include "DartScore.h"
#include "DlgAGameScore.h"
#include "DlgOnePlayerScore.h"
#include "APlayerData.h"
#include "DartScoreDlg.h"

// CDlgAGameScore 对话框

IMPLEMENT_DYNAMIC(CDlgAGameScore, CDialogWM)

CDlgAGameScore::CDlgAGameScore(CWnd* pParent /*=NULL*/)
	: CDialogWM(CDlgAGameScore::IDD, IDR_MENU_AGameScore, pParent)
	, m_pPtrAry_APlayerData ( NULL )
	, m_nCurPlayerIndex ( 0 )
{

}

CDlgAGameScore::~CDlgAGameScore()
{
}

void CDlgAGameScore::DoDataExchange(CDataExchange* pDX)
{
	CDialogWM::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_LIST1, m_ListCtrl);
}


BEGIN_MESSAGE_MAP(CDlgAGameScore, CDialogWM)
	ON_COMMAND(IDC_NEXT_PLAYER, &CDlgAGameScore::OnScoreNext)
	ON_COMMAND(ID_MENU_DELETE, &CDlgAGameScore::OnMenuDelete)
END_MESSAGE_MAP()


void CDlgAGameScore::CreateList()
{
	if ( !m_pPtrAry_APlayerData ) return;

	m_ListCtrl.SetRegSection ( _T("CDlgAGameScore::m_ListCtrl") );
	m_ListCtrl.SetListCtrlGridAttr( FALSE, LVS_REPORT );
	m_ListCtrl.SetExtendedStyle ( m_ListCtrl.GetExtendedStyle() | LVS_EX_ONECLICKACTIVATE );

	// 生成列表
	CString csHead;
	for ( int i=0; i<m_pPtrAry_APlayerData->GetSize(); i++ )
	{
		CAPlayerData *pAPlayerData = (CAPlayerData*)m_pPtrAry_APlayerData->GetAt(i);
		if ( !pAPlayerData ) continue;
		CString csOneHead;
		csOneHead.Format ( _T("%s,%d;"), pAPlayerData->m_csPlayerName, pAPlayerData->m_csPlayerName.GetLength()*20 );
		csHead += csOneHead;
	}

	m_ListCtrl.SetHeadings( csHead );
	m_ListCtrl.SubclassWindow();
	m_ListCtrl.LoadColumnInfo();

	CStringArray StrAry;
	for ( int i=0; i<m_ListCtrl.GetColumnCount (); i++ )
	{
		StrAry.Add ( _T("--") );
	}
	m_ListCtrl.InsertItem ( -1, StrAry, -1 );
	m_ListCtrl.InsertItem ( -1, StrAry, -1 );
}

void CDlgAGameScore::OnInit ()
{
	CreateList ();

	if ( !m_pPtrAry_APlayerData ) return;
	BOOL bExitLoop = FALSE;
	for ( int nCycle=0; ; nCycle++ )
	{
		for ( int nPlayer=0; nPlayer<m_pPtrAry_APlayerData->GetSize(); nPlayer++ )
		{
			CAPlayerData *pAPlayerData = (CAPlayerData*)m_pPtrAry_APlayerData->GetAt(nPlayer);
			if ( !pAPlayerData ) continue;
			CUIntArray UIntAry_Scores;
			for ( int nDart=0; nDart<g_AGameData.nDartNum; nDart++ )
			{
				if ( nCycle >= pAPlayerData->m_pUIntAry_Scores[nDart].GetSize() )
				{
					bExitLoop = TRUE;
					break;
				}
				UIntAry_Scores.Add ( pAPlayerData->m_pUIntAry_Scores[nDart].GetAt(nCycle) );
			}
			if ( bExitLoop )
			{
				m_nCurPlayerIndex = nPlayer;
				if ( m_nCurPlayerIndex >= m_pPtrAry_APlayerData->GetSize() )
				{
					m_nCurPlayerIndex = 0;
				}
				break;
			}
			AddScoreRecord ( nPlayer, UIntAry_Scores );
		}
		if ( bExitLoop ) break;
	}

	CalcTotal ();

	CDialogWM::OnInit ();
}

void CDlgAGameScore::AddScoreRecord ( int nSubItem, CUIntArray &UIntAry_Scores )
{
	if ( nSubItem < 0 || nSubItem >= m_ListCtrl.GetColumnCount () )
		return;
	CStringArray StrAry;
	CString csItemText;
	CString csText;
	for ( int i=0; i<UIntAry_Scores.GetSize(); i++ )
	{
		csText.Format ( _T("%u"), UIntAry_Scores.GetAt(i) );
		if ( !csItemText.IsEmpty() ) csItemText += _T(" ");
		csItemText += csText;
	}

	int nItem = m_ListCtrl.GetItemCount () - 2;
	if ( nItem < 0 ) return;
	if ( nSubItem == 0 )
	{
		StrAry.RemoveAll ();
		for ( int i=0; i<m_ListCtrl.GetColumnCount (); i++ )
		{
			StrAry.Add ( _T(" ") );
		}
		nItem = m_ListCtrl.InsertItem ( nItem, StrAry, -1 );
	}
	else
	{
		nItem = m_ListCtrl.GetItemCount () - 3;
	}
	if ( nItem < 0 ) return;
	m_ListCtrl.SetItemText ( nItem, nSubItem, csItemText );
}

//
// 计算总和
//
void CDlgAGameScore::CalcTotal ()
{
	int nItem = m_ListCtrl.GetItemCount () - 1;
	if ( nItem < 0 ) return;
	CUIntArray UIntAry;
	( (CDartScoreDlg*)AfxGetMainWnd() )->CalcTotalScore ( UIntAry );
	CStringArray StrAry;
	for ( int i=0; i<UIntAry.GetSize(); i++ )
	{
		CString csText;
		csText.Format ( _T("%u,%u"), 1+CalcOverCount(UIntAry,UIntAry.GetAt(i)), UIntAry.GetAt(i) );
		StrAry.Add ( csText );
	}
	m_ListCtrl.SetItemText ( nItem, StrAry );
	( (CDartScoreDlg*)AfxGetMainWnd() )->SaveCurGameData ();

	// 滚动在最底行
	m_ListCtrl.SendMessage ( WM_VSCROLL, (WPARAM)SB_BOTTOM, (LPARAM)NULL );

	AutoSizeColumns ( &m_ListCtrl );

	ShowStatus ();
}

//
// 计算 UIntAry 中 比 nData 大的数有多少个,用来排名
//
int CDlgAGameScore::CalcOverCount ( CUIntArray &UIntAry, UINT nData )
{
	int nCount = 0;
	for ( int i=0; i<UIntAry.GetSize(); i++ )
	{
		if ( UIntAry.GetAt(i) > nData )
			nCount ++;
	}
	return nCount;
}

void CDlgAGameScore::OnScoreNext()
{
	if ( !m_pPtrAry_APlayerData ) return;
	CAPlayerData *pAPlayerData = (CAPlayerData*)m_pPtrAry_APlayerData->GetAt(m_nCurPlayerIndex);
	if ( !pAPlayerData ) return;
	if ( g_AGameData.nDartNum < 1 ) return;
	// 本局游戏已经完成
	if ( pAPlayerData->m_pUIntAry_Scores[g_AGameData.nDartNum-1].GetSize() >= g_AGameData.nCycleTimes )
	{
		AfxMessageBox ( _T("Game Over"), MB_ICONINFORMATION );
		return;
	}

	CDlgOnePlayerScore dlg;
	dlg.m_csPlayerName = pAPlayerData->m_csPlayerName;
	if ( dlg.DoModal() != IDOK ) return;

	ASSERT ( dlg.m_UIntArray_Scores.GetSize() == g_AGameData.nDartNum );
	for ( int i=0; i<g_AGameData.nDartNum; i++ )
	{
		pAPlayerData->m_pUIntAry_Scores[i].Add ( dlg.m_UIntArray_Scores.GetAt(i) );
	}
	AddScoreRecord ( m_nCurPlayerIndex, dlg.m_UIntArray_Scores );
	CalcTotal ();

	m_nCurPlayerIndex ++;
	if ( m_nCurPlayerIndex >= m_pPtrAry_APlayerData->GetSize() )
	{
		m_nCurPlayerIndex = 0;
	}
}

void CDlgAGameScore::OnMenuDelete()
{
	int nCurSel = m_ListCtrl.GetSelectionMark ();
	if ( nCurSel < 0 )
		return;
	if ( nCurSel >= m_ListCtrl.GetItemCount()-2 )
	{
		AfxMessageBox ( _T("Can't delete statistic line") );
		return;
	}

	BOOL bIsLastScoreLine = (nCurSel == m_ListCtrl.GetItemCount()-3);

	for ( int nPlayer=0; nPlayer<m_pPtrAry_APlayerData->GetSize(); nPlayer++ )
	{
		CAPlayerData *pAPlayerData = (CAPlayerData*)m_pPtrAry_APlayerData->GetAt(nPlayer);
		if ( !pAPlayerData ) continue;
		for ( int nDart=0; nDart<g_AGameData.nDartNum; nDart++ )
		{
			if ( nCurSel < pAPlayerData->m_pUIntAry_Scores[nDart].GetSize() )
			{
				pAPlayerData->m_pUIntAry_Scores[nDart].RemoveAt ( nCurSel );
			}
		}
	}
	m_ListCtrl.DeleteItem ( nCurSel );

	CalcTotal ();

	if ( bIsLastScoreLine ) m_nCurPlayerIndex = 0;
}

void CDlgAGameScore::ShowStatus ()
{
	if ( !m_pPtrAry_APlayerData ) return;
	int nPlayerNum = m_pPtrAry_APlayerData->GetSize();
	if ( nPlayerNum < 1 ) return;
	CAPlayerData *pAPlayerData = (CAPlayerData*)m_pPtrAry_APlayerData->GetAt(nPlayerNum-1);
	if ( !pAPlayerData ) return;
	if ( !pAPlayerData->m_pUIntAry_Scores || pAPlayerData->m_pUIntAry_Scores->GetSize() < 1 )
		return;
	int nCycleAlready = pAPlayerData->m_pUIntAry_Scores->GetSize();
	int nRemainCycle = g_AGameData.nCycleTimes - nCycleAlready;
	CString csStatus;
	csStatus.Format ( _T("Total:%d, Remian:%d"), g_AGameData.nCycleTimes, nRemainCycle );
	SetDlgItemText ( IDC_STATIC_Status, csStatus );
}

⌨️ 快捷键说明

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