radomgenerator.cpp

来自「该程序功能:用JAVa实现的彩色五珠棋小游戏」· C++ 代码 · 共 89 行

CPP
89
字号
// RadomGenerator.cpp: implementation of the CRadomGenerator class.
//
//////////////////////////////////////////////////////////////////////

#include <stdlib.h>
#include "stdafx.h"
#include "FivePlus.h"
#include "RadomGenerator.h"

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

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
//IMPLEMENT_SERIAL( CRadomGenerator, CObject, 1 )

CRadomGenerator::CRadomGenerator()
{	
	srand((unsigned)time( NULL ));
	m_pnMinLimit = NULL;
}

CRadomGenerator::~CRadomGenerator()
{
	delete m_pnMinLimit;
}

BOOL CRadomGenerator::Initialize()
{
	NextRandomInt();
	return TRUE;
}

int CRadomGenerator::NextRandomInt()
{	
	int randomInt = rand();
	for ( int i = 0; i < m_nRange; i++ ){
		if ( randomInt < m_pnMinLimit[i] )
			return i - 1;
	}
	return i - 1;
}

//DEL void CRadomGenerator::Serialize(CArchive &ar)
//DEL {
//DEL 	if (ar.IsStoring())	
//DEL 	{
//DEL 		ar << m_nRange;
//DEL 		for ( int i = 0; i < m_nRange; i++)
//DEL 		{
//DEL 			ar << m_pnMinLimit[i];
//DEL 		}
//DEL 	}else
//DEL 	{
//DEL 		ar >> m_nRange;
//DEL 		if ( m_pnMinLimit )
//DEL 			delete m_pnMinLimit;
//DEL 		m_pnMinLimit = new int[m_nRange];
//DEL 		for ( int i = 0; i < m_nRange; i++)
//DEL 		{
//DEL 			ar >> m_pnMinLimit[i];
//DEL 		}
//DEL 	}
//DEL }
void CRadomGenerator::SetProperty(int nRange, int pnProportion[])
{
	m_nRange = nRange;

	if ( m_pnMinLimit )
		delete m_pnMinLimit;
	m_pnMinLimit = new int[m_nRange];
	m_pnMinLimit[0] = 0;
	int total = pnProportion[0];

	for ( int i = 1; i < m_nRange; i++ ){
		m_pnMinLimit[i] = total;
		total += pnProportion[i];		
	}
	
	for ( i = 0; i < m_nRange; i++ ){
		m_pnMinLimit[i] = (m_pnMinLimit[i]*RAND_MAX)/total;
	}
	
}

⌨️ 快捷键说明

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