simplepredictor.cpp

来自「C人工智能游戏开发的一些实例源代码 C Game development in 」· C++ 代码 · 共 54 行

CPP
54
字号
//----------------------------------------------------------------------------------------------
// Sequential Prediction Demo: The positioning pattern
// 
// Author:  Fri Mommersteeg
// Date:    10-09-2001
// File:    SimplePredictor.h
//----------------------------------------------------------------------------------------------

//----------------------------------------------------------------------------------------------
// Include files
//----------------------------------------------------------------------------------------------

#include "StdAfx.h"
#include "SimplePredictor.h"

//----------------------------------------------------------------------------------------------
// Setup(): sets up the predictor
//----------------------------------------------------------------------------------------------

void CSimplePredictor::Setup(int nWindowSize, int nMinPerformance) { 
	// set up window
	WindowSize = nWindowSize; 
	Reset();
	MinPerformance = nMinPerformance; 
}

//----------------------------------------------------------------------------------------------
// GetPrediction(): determines the prediction using string-matchin prediction (complexity O^2)
//----------------------------------------------------------------------------------------------

bool CSimplePredictor::GetPrediction(int &Prediction) {
	int N = window.GetSize();
	int MaxStringSize = 0, MaxStringPosition = N-2;
	
	// if the window is empty, we are unable to make a prediction
	if (window.GetSize() > 0) {
				
		for (int i = N-2; i >= 0; i--) {
			int j = 0;
			while (i-j >= 0 && window[i-j] == window[N-1-j]) {
				j++;
			}
			if (j > MaxStringSize) {
				MaxStringPosition = i;
				MaxStringSize = j;
			}
		}
		Prediction = window[MaxStringPosition+1];		
	}

	// performance indicator: is the prediction good enough?
	return (MaxStringSize >= MinPerformance);
}

⌨️ 快捷键说明

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