写学习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;
}

  

上一篇:你对云存储的安全性了解多少?


下一篇:LeetCode 27. 移除数组元素