8人过河oo-AI.html  返回列表

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>8人过河 过河游戏 AI.html</title>
<script type="text/javascript">
if(typeof window.console == 'undefined'){window.console={'log':function(){},'error':function(){}}}
Array.prototype.indexOf=function(value){for(var i=0,l=this.length;i<l;i++)if(this[i]==value)return i;return -1;};
var World=function(peoples,chuan){
	this.worldStatus=[];
	this.peoples=peoples;
	this.chuan=chuan;
	this.saveStatus();
}
World.prototype.saveStatus=function(){
	var str=[];
	var peoples=this.peoples;
	var worldStatus=this.worldStatus;
	var xflag=true; //船在岸边为真,船在河中为假
	for (var i = 0; i < peoples.length; i++) {
		str.push(peoples[i].tmpPos);
		if(peoples[i].tmpPos==0){
			xflag=false;
		}
	}
	str.push(this.chuan.pos);
	str=str.toString();
	for(var i=0;i<worldStatus.length;i++){
		if(str==worldStatus[i].key) return -1;
	}
	this.worldStatus.push({key:str,flag:true,num:0,xflag:xflag});
	return worldStatus.length-1;
}
World.prototype.setNowStatusFlag=function(nowIndex,flag){
	nowIndex=(typeof nowIndex=='undefined')?this.worldStatus.length-1:nowIndex;
	flag=(typeof flag=='undefined')?false:flag;
	this.worldStatus[nowIndex].flag=flag;
}
World.prototype.revertStatus=function(nowIndex){
	this.chuan.delPeople();
	var worldStatus=this.worldStatus
	var peoples=this.peoples;
	var str=worldStatus[this.findLastStatus(nowIndex)].key;
	var status=str.split(",");
	for (var i = 0; i < peoples.length; i++) {
		this.peoples[i]['pos']=status[i];
		this.peoples[i]['tmpPos']=status[i];
		if(status[i]==0){
			this.chuan.addPeople(this.peoples[i]);
		}
	}
	chuan.pos=status[peoples.length];
	//console.log("回滚现状:"+this.show());
}
World.prototype.findLastStatus=function(nowIndex){
	var worldStatus=this.worldStatus;
	var i=worldStatus.length-1;
	var peoplesL=this.peoples.length;
	if(typeof nowIndex=='undefined'){
		for(;i>0;i--){
			if (worldStatus[i].flag && worldStatus[i].xflag && worldStatus[i].num <peoplesL ) {
				this.worldStatus[i].num+=1;
				return i;
			}
		}
	}else{
		var lsk=worldStatus[nowIndex].key.split(',')[peoplesL];
		for(;i>0;i--){
			var _lsk=worldStatus[i].key.split(',')[peoplesL];
			if (worldStatus[i].flag && worldStatus[i].xflag && worldStatus[i].num <17 && _lsk!=lsk) {
				this.worldStatus[i].num+=1;
				return i;
			}
		}
	}
	//console.log(0);
	return 0;
}
World.prototype.isKill=function(){
	var peoples=this.peoples;
	for(var i =0 ;i<peoples.length;i++){
		if(peoples[i].doKill(peoples)) return true;
	}
	return false;
	
}
World.prototype.gameOver=function(){
	var peoples=this.peoples;
	for (var i = 0; i < peoples.length; i++) {
		if(peoples[i].tmpPos!=1) return false;
	}
	return true;
}
World.prototype.show=function(){
	var status={};
	status[-1]=[];
	status[0]=[];
	status[1]=[];
	var peoples=this.peoples;
	for (var i = 0; i < peoples.length; i++) {
		status[peoples[i].tmpPos].push(peoples[i].name);
	}
	return "左岸:"+status[-1].toString()+"\t\t\t\t船上:"+status[0].toString()+"\t\t右岸:"+status[1].toString();
}
World.prototype.display = function(key){
	key=key.split(',');
	var keyLength=key.length;
	var peoples=this.peoples;
	var str={};
	str[-1]=[];
	str[1]=[];
	for(var i=0;i<keyLength-1;i++){
		str[key[i]].push(peoples[i].name);
	}
	return ("左岸:"+str[-1].toString()+"┃右岸:"+str[1].toString()+"┃船在"+(key[keyLength-1]==-1?'左':'右')+"岸"+'\n');
}
//////////////////////////////////////////////////////////////
var Cruises=function(){
	this.pos=-1;
	this.peoples=[];
}
Cruises.prototype.addPeople=function(people){
	if(this.peoples.length>1) return false;
	people.tmpPos=0;
	this.peoples.push(people);
	return true;
}
Cruises.prototype.dock=function(){
	this.pos=-this.pos;
	for(var i=0;i<this.peoples.length;i++){
		this.peoples[i].tmpPos=this.pos;
		this.peoples[i].pos=this.pos;
	}
	this.peoples=[];
}
Cruises.prototype.delPeople=function(){
	for(var i=0;i<this.peoples.length;i++){
		this.peoples[i].tmpPos=this.peoples[i].pos;
	}
	this.peoples=[];
}
//////////////////////////////////////////////////////////////
var People=function(name){
	this.pos=-1;
	this.tmpPos=-1;
	this.name=name;
}
People.prototype.boating=function(){
	this.boat=true;
}
People.prototype.isBoat=function(){
	return (typeof this.boat=='undefined')?false:true;
}
People.prototype.killer=function(killers,nokill){
	this.killers=killers;
	this.nokill=nokill;
}
People.prototype.isKiller=function(){
	return (typeof this.nokill=='undefined')?false:true;
}
People.prototype.doKill=function(peoples){
	if (!this.isKiller()) return false;
	if(this.nokill.tmpPos==this.tmpPos) return false;
	for(var i=0;i<peoples.length;i++){
		if(this.killers.indexOf(peoples[i])>-1 && this.tmpPos==peoples[i].tmpPos) return true;
	}
	return false;
}
//////////////////////////////////////////////////////////////
function twoGo(){
	var flag=false;
	var nowIndex;
	var peoples=world.peoples;
	//chuan.delPeople();
	for(var i =0; i<peoples.length;i++){
		for (var j = 0; j < peoples.length; j++) {
			if (peoples[i].tmpPos==chuan.pos && peoples[j].tmpPos==chuan.pos) {
				if (peoples[i].isBoat() || peoples[j].isBoat()) {
					//chuan.delPeople();
					chuan.addPeople(peoples[i]);
					chuan.addPeople(peoples[j]);
					nowIndex=world.saveStatus();
					if (nowIndex>0 && world.isKill()) {
						//console.log("世界回滚【登船杀戮】:"+world.show());
						world.setNowStatusFlag(nowIndex,false);
						chuan.delPeople();
						//console.log("世界回滚【登船杀戮】:"+world.show());
						continue;
					}
					if (nowIndex==-1) {
						//console.log("世界回滚【重复登船】:"+world.show());
						chuan.delPeople();
						//console.log("世界回滚【重复登船】:"+world.show());
						continue;
					}
					
					//console.log((chuan.pos==1?'<-':'->')+world.show());
					chuan.dock();
					nowIndex=world.saveStatus();
					if (nowIndex>0 && world.isKill()) {
						//console.log("世界回滚【下船杀戮】:"+world.show());
						world.setNowStatusFlag(nowIndex,false);
						world.revertStatus(nowIndex);
						//console.log("世界回滚【下船杀戮】:"+world.show());
						continue;
					}
					if (nowIndex==-1){
						//console.log("世界回滚【重复下船】:"+world.show());
						world.revertStatus();
						//console.log("世界回滚【重复下船】:"+world.show());
						continue;
					}
					console.log(peoples[i].name+','+peoples[j].name+'===>登船:');
					console.log((chuan.pos==1?'【船在右岸】':'【船在左岸】')+'\t\t'+world.show());
					return true;
					
				}
				
			}
		}
	}
	return flag;
}
function Go(num){
	initGame(num);
	while(true){
		if(world.gameOver()){
			console.log("游戏结束:"+world.show());
			console.log("=======================================================");
			var msg='';
			for(var i=0,j=0;i<world.worldStatus.length;i++){
				if (world.worldStatus[i].flag && world.worldStatus[i].xflag && world.worldStatus[i].num < world.peoples.length-1) {
					msg+="["+(j++)+"]"+world.display(world.worldStatus[i].key);
				}
			}
			console.log(msg);
			alert(msg);
			break;
		}
		if(!twoGo()){
			world.revertStatus();
			twoGo();
		}
	}
}
function checkMask(mask) 
{ 
    obj=mask; 
    var exp=/^(254|252|248|240|224|192|128|0)\.0\.0\.0|255\.(254|252|248|240|224|192|128|0)\.0\.0|255\.255\.(254|252|248|240|224|192|128|0)\.0|255\.255\.255\.(254|252|248|240|224|192|128|0)$/; 
	var reg = obj.match(exp);
	if(reg==null){ 
       alert("非法!!"); 
	   return false; 
    }else{ 
       alert("合法!!"); 
       return true; 
    }
}
var chuan,world;
function initGame(num){
	chuan=new Cruises();
	switch(num){
		case 4:
			var nongfu=new People("猎人");
			var baicai=new People("白菜");
			var yang=new People("绵羊");
			var lang=new People("野狼");
			lang.killer([yang],nongfu);
			yang.killer([baicai],nongfu);
			nongfu.boating();
			world=new World([nongfu,baicai,yang,lang],chuan);
		break;
		case 5:
			var lieren=new People("猎人");
			var nongfu=new People("农夫");
			var baicai=new People("白菜");
			var yang=new People("绵羊");
			var lang=new People("野狼");
			yang.killer([baicai],nongfu);
			lang.killer([yang,nongfu],lieren);
			nongfu.boating();
			lieren.boating();
			world=new World([lieren,nongfu,baicai,yang,lang],chuan);
		break;
		case 6:
			var lieren=new People("猎人");
			var nongfu=new People("农夫");
			var baicai=new People("白菜");
			var yang=new People("绵羊");
			var lang=new People("野狼");
			var gou=new People("猎狗");
			yang.killer([baicai],nongfu);
			lang.killer([yang,nongfu],lieren);
			gou.killer([lang],lieren);
			nongfu.boating();
			lieren.boating();
			world=new World([lieren,nongfu,baicai,yang,lang,gou],chuan);
		break;
		case 8:
			var jingcha=new People("警察");
			var nver1=new People("大女儿");
			var nver2=new People("小女儿");
			var erzi1=new People("大儿子");
			var erzi2=new People("小儿子");
			var baba=new People("爸爸");
			var mama=new People("妈妈");
			var zuifan=new People("杀人犯");
			baba.boating();
			mama.boating();
			jingcha.boating();
			baba.killer([nver1,nver2],mama);
			mama.killer([erzi1,erzi2],baba);
			zuifan.killer([erzi1,erzi2,nver1,nver2,baba,mama],jingcha);
			world=new World([baba,mama,zuifan,jingcha,erzi1,erzi2,nver1,nver2],chuan);
		break;
	}
	world.saveStatus();
}
//////////////////////////////////////////////////////////////
</script>
<style type="text/css">
	body{ font-size:12px; padding:0; margin:0;}
	.gameinfo{ background-color:#005896; color:#ffffff; border:10px solid #1168a7; margin-bottom:10px; padding:10px; }
</style>
</head>
<body>
<div class="gameinfo">
	firebug console 可以看到较详细的过程,如需更详细的过程需要将代码中的console注释去掉。
</div>
<div class="gameinfo">现有一条河,共有八个人要过河,分别是爸爸,妈妈,两个儿子,两个女儿,一个警察,一个犯人.现有一条木伐,一次最多载两个人,在这八个人中,有妈妈,爸爸,警察会开船,即这个船上必须有爸爸,妈妈,警察三个中的一个,船才会开动.船过去无法自动回来.
	<br>并且要避免以下事件发生:
	<br>1,警察不在犯人会伤害一家六口.
	<br>2,爸爸不在,妈妈会伤害儿子.
	<br>3,妈妈不在,爸爸会伤害女儿.应当如何过河?
	<br><input type="button" onclick="Go(8)" value="八人过河 AI"/>
</div>
<div class="gameinfo">现有一条河一条船,<font color="red">猎人、狼、菜、羊</font>要過河,船只有两座,只有农夫会掌舵,如果猎人和其它三样没在一起会发生狼吃羊,羊吃菜的情况,请帮农夫无损过河。
	<br><input type="button" onclick="Go(4)" value="四人过河 AI"/>
</div>
<div class="gameinfo">现有一条河一条船,<font color="red">猎人、农夫、狼、菜、羊</font>要過河,船只有两座,只有农夫、猎人会掌舵,请帮他们无损过河。
	<br>并且要避免以下事件发生:
	<br>1,如果猎人不在狼会吃农夫和羊。
	<br>2,如果农夫不在羊会吃菜。
	<br><input type="button" onclick="Go(5)" value="⑤人过河 AI"/>
</div>
<div class="gameinfo">现有一条河一条船,<font color="red">猎人、农夫、野狼、猎狗、白菜、羊</font>要過河,船只有两座,只有农夫、猎人会掌舵,请帮他们无损过河。
	<br>并且要避免以下事件发生:
	<br>1,如果猎人不在狼会吃农夫和羊,猎狗会吃狼。
	<br>2,如果农夫不在羊会吃菜。
	<br><input type="button" onclick="Go(6)" value="六人过河 AI"/>
</div>
<div class="gameinfo">
	<input id="mask"> <input type="button" onclick="checkMask(document.getElementById('mask').value);" value="IP Mask效验"/>
</div>
</body>
</html>
Add New Content