C/C++教程

写学习abcde的简单AI(C++实现)

本文主要是介绍写学习abcde的简单AI(C++实现),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
#include <iostream>
#include <time.h> 
#include <stdlib.h>
#include <cmath>

using namespace std;
#define random(a,b) (rand() % (b-a+1))+ a
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
const int association = 70;//产生联想的概率
const int divergence = 29;//产生发散的概率
bool Forget = false;//是否会发生战争
const int lenth = 5;

int main() {
	srand(time(NULL));
	int map[lenth][lenth];
	for(int i = 0;i < lenth;++i) {
		for(int j = 0;j < lenth;++j) {
			map[i][j] = -1;
		}
	}
	string show[lenth][lenth];
	for(int i = 0;i < lenth;++i) {
		for(int j = 0;j < lenth;++j) {
			show[i][j] = "warn";
		}
	}
	int x, y;//坐标点
	int tempX, tempY;
	bool first = false;//记录是否为第一次
	int development;//记录发展的趋势	
	string learn[lenth] = {"a", "b", "c", "d", "e"};
	string answer[lenth];
	int i = 0;
	while(true) {
	    while(true) {
	        if(!first) {//随机初始化坐标
	            i = 0;
	            x = random(0,lenth-1);
	            y = random(0,lenth-1);
	            map[x][y] = i;
	            show[x][y] = learn[i];
	            first = true;
	            ++i;
	        }
	        else {
	            development = random(1,100);//随机出发展的趋势
	            if(development > 0 && development <= association) {//如果随机的发展趋势为百分之70,则进入联想模式
	                tempX = random(0,2) - 1;
	                x = x + tempX;
	                tempY = random(0,2) - 1;
	                y = y + tempY;
	                while((x < 0 || x >= lenth) || (y < 0 || y >= lenth)) {
	                    x = x - tempX;
	                    tempX = random(0,2) - 1;
	                    x = x + tempX;
	                    y = y - tempY;
	                    tempY = random(0,2) - 1;
	                     y = y + tempY;
	                }
	                map[x][y] = i;
			        show[x][y] = learn[i];
			        ++i;
	            }
	            else if(development > association && development <= (association + divergence)) {//如果随机的发展趋势为百分之29,则进入发散模式
	                tempX = x;
	                x = random(0,lenth-1);
	                tempY = y;
	                y = random(0,lenth-1);
	                while(((x > (tempX - 1))&&(x < (tempX + 1)))||((y > (tempY - 1))&&(y < (tempY + 1)))){
	                    x = random(0,lenth-1);
	                    y = random(0,lenth-1);
	                }
	                map[x][y] = i;
				    show[x][y] = learn[i];
				    ++i;	    
	            }   
	            else {//是否遗忘 
	                x = random(0,lenth-1);
	                y = random(0,lenth-1);
	                map[x][y] = -1;
				    show[x][y] = "warn";
				    ++i;
	                Forget = true;
	            }
	            if(i >= lenth || Forget == true) {
				    break;
		    	}
	        }
	    }
	    if(Forget == true) {
	    	Forget = false;
	    	first = false;
			continue;
		}
		else {
			break;
		}
		
	}
	for(int i = 0;i < lenth;++i) {
		for(int j = 0;j < lenth;++j) {
			if(map[i][j] != -1){
				answer[map[i][j]] = show[i][j];
			}
		}
	}
	for(int i = 0;i < lenth;++i) {
		cout << answer[i] << endl;
	}
	
	return 0;
}

  

这篇关于写学习abcde的简单AI(C++实现)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!