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

📄 cgpornode.cpp

📁 游戏编程AI源码
💻 CPP
字号:
//GPExample
//Copyright John Manslow
//29/09/2001

////////////////////////////////////////////////////////////
//Only when compiling under Windows
#include "stdafx.h"
#define new DEBUG_NEW
////////////////////////////////////////////////////////////

#include "CGPORNode.h"
#include "CGPNode.h"
#include "CGP.h"
#include "assert.h"

extern CGPNode *pGPCreateRandomSubtree(void);

extern CGPNode **pPrototypeList;
extern unsigned long ulNumberOfPrototypes;

CGPORNode ORPrototype;

CGPORNode::CGPORNode()
{
	unsigned long i;
	nType=105;

	pChildren=NULL;
	ulNumberOfChildren=2;

	sprintf(psName,"ORNode");

	nIsPrototype=1;
	if(AddToPrototypeList())
	{
		nIsPrototype=0;
		pChildren=new CGPNode*[ulNumberOfChildren];
		for(i=0;i<ulNumberOfChildren;i++)
		{
			pChildren[i]=NULL;
		}
	}
	dPrior=0.1;
}

CGPORNode::~CGPORNode()
{
}

CGPNode *CGPORNode::pGetCopy(CGP* pGP)
{
	unsigned long i;
	CGPORNode *pNewNode=new CGPORNode;
	assert(!(pNewNode==NULL));
	for(i=0;i<ulNumberOfChildren;i++)
	{
		if(pNewNode->pChildren[i]!=NULL)
		{
			delete pNewNode->pChildren[i];
		}
		if(!nIsPrototype)
		{
			pNewNode->pChildren[i]=pChildren[i]->pGetCopy(pGP);
		}
		else
		{
			pNewNode->pChildren[i]=pGP->pGetRandomSubtree();
		}
	}
	return (CGPNode*)pNewNode;
}

double CGPORNode::dEvaluate(void)
{
	//This function returns the arithmetical-logical OR of the return values of the node's children
	double dReturn1=pChildren[0]->dEvaluate();
	double dReturn2=pChildren[1]->dEvaluate();
	return dReturn1+dReturn2-dReturn1*dReturn2;
}

char* CGPORNode::psGetString(char*pString)
{
	char *pSubString=new char[50000];
	if(pString!=NULL && strlen(pString)<10000)
	{
		sprintf(pSubString,"");
		pSubString=pChildren[0]->psGetString(pSubString);
		sprintf(pString,"%s OR((%s),",pString,pSubString);
		sprintf(pSubString,"");
		pSubString=pChildren[1]->psGetString(pSubString);
		sprintf(pString,"%s(%s))",pString,pSubString);
	}
	delete []pSubString;
	return pString;
}

⌨️ 快捷键说明

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