游戏人工智能-状态驱动智能体设计-Miner

以下代码改编自《游戏人工智能编程案例精粹(修订版)》([美]Mat buckland,人民邮电出版社,2012)。将其C++代码改为javascript实现 

<!DOCTYPE html>
<html>
<head>
<script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
<script src="myMiner.js"></script>
<style>
#screen{
text-align:left;
background-color:darkcyan;
}
</style>
</head>
<body>
<div id='screen'></div>
</body>
</html>
//myMiner.js
$(document).ready(function () {
	var Miner = {
		name: "Bob",
		state: "",
		Location: "",
		m_iGoldCarried: 0, //矿工的包中装了多少的金块
		m_iMoneyInBank: 0, //矿工在银行存了多少钱
		m_iThirst: 0, //值越高,矿工越口渴
		m_iFatigue: 0, //值越高,矿工越劳累

		ComfortLevel: 5,
		MaxNuggets: 3,
		ThirstLevel: 5,
		TirednessThreshold: 5,

		AddToGoldCarried: function (v) {
			this.m_iGoldCarried += v;
			if (this.m_iGoldCarried < 0)
				this.m_iGoldCarried = 0;
		},
		AddToWealth: function (v) {
			this.m_iMoneyInBank += v;
			if (this.m_iMoneyInBank < 0)
				this.m_iMoneyInBank = 0;
		},
		Thirsty: function () {
			if (this.m_iThirst >= this.ThirstLevel) {
				return true;
			}

			return false;
		},
		GoldCarried: function () {
			return this.m_iGoldCarried;
		},
		SetGoldCarried: function (v) {
			this.m_iGoldCarried = v;
		},
		PocketsFull: function () {
			return this.m_iGoldCarried >= this.MaxNuggets;
		},

		Fatigued: function () {
			if (this.m_iFatigue > this.TirednessThreshold) {
				return true;
			}

			return false;
		},
		DecreaseFatigue: function () {
			this.m_iFatigue -= 1;
		},
		IncreaseFatigue: function () {
			this.m_iFatigue += 1;
		},

		Wealth: function () {
			return this.m_iMoneyInBank;
		},
		SetWealth: function (v) {
			this.m_iMoneyInBank = v;
		},

		BuyAndDrinkAWhiskey: function () {
			this.m_iThirst = 0;
			this.m_iMoneyInBank -= 2;
		},
		//
		Update: function () {
			this.m_iThirst += 1;
			this.FSM.Update();
		},
		FSM: null,
	};
	var View = {
		View: function (s) {
			var html = $("#screen").html();
			html += "<p>Miner:" + s + "</p>";
			$("#screen").html(html);
		},
	};
	var StateMachine = {
		m_PreviousState: {
			name: "",
			Enter: function () {},
			Exit: function () {},
			Update: function (e) {},
		},
		m_CurrentState: {
			name: "",
			Enter: function () {},
			Exit: function () {},
			Update: function (e) {},
		},
		Update: function (e) {
			if (this.m_CurrentState != null)
				this.m_CurrentState.Update(e);
		},
		ChangeState: function (s) {
			document.title = s.name;
			this.m_PreviousState = this.m_CurrentState;
			this.m_CurrentState.Exit();
			this.m_CurrentState = s;
			this.m_CurrentState.Enter();
		},
		RevertToPreviousState: function () {
			this.ChangeState(this.m_PreviousState);
		},

	};
	var EnterMineAndDigForNuggetState = {
		name: "EnterMineAndDigForNuggetState",
		Enter: function () {
			if (Miner.Location != "goldmine") {
				View.View("Walkin' to the goldmine");
				Miner.Location = "goldmine";
			}
		},
		Exit: function () {
			View.View("Ah'm leavin' the gold mine with mah pockets full o' sweet gold");

		},
		Update: function () {
			Miner.AddToGoldCarried(1);
			Miner.IncreaseFatigue();
			View.View("Pickin' up a nugget.");

			if (Miner.PocketsFull()) {
				Miner.FSM.ChangeState(VistBankAndDepositGoldState);
			} else if (Miner.Thirsty()) {
				Miner.FSM.ChangeState(QuenchThirstState);
			}
		},
	};
	var VistBankAndDepositGoldState = {
		name: "VistBankAndDepositGoldState",
		Enter: function () {
			if (Miner.Location != "bank") {
				View.View("Goin' to the bank. Yes siree");
				Miner.Location = "bank";
			}
		},
		Exit: function () {
			View.View("Leavin' the bank");
		},
		Update: function () {
			Miner.AddToWealth(Miner.GoldCarried());
			View.View("Depositing gold. Total savings now");
			//wealthy enough to have a well earned rest?
			if (Miner.Wealth() >= Miner.ComfortLevel) {
				View.View("WooHoo! Rich enough for now. Back home to mah li'lle lady");
				Miner.FSM.ChangeState(GoHomeAndSleepTilRestedState);
			} else {
				Miner.FSM.ChangeState(EnterMineAndDigForNuggetState);
			}
		},
	};
	var GoHomeAndSleepTilRestedState = {
		name: "GoHomeAndSleepTilRestedState",
		Enter: function () {
			if (Miner.Location != "shack") {
				View.View("Walkin' home");
				Miner.Location = "shack";
			}
		},
		Exit: function () {
			View.View("Leaving the house");
		},
		Update: function () {
			if (Miner.Fatigued()) {
				Miner.DecreaseFatigue();
				View.View("ZZZZ... ");

			} else {
				View.View("What a God darn fantastic nap! Time to find more gold");
				Miner.FSM.ChangeState(EnterMineAndDigForNuggetState); ;
			}
		},
	};
	var QuenchThirstState = {
		name: "QuenchThirstState",
		Enter: function () {
			if (Miner.Location != "saloon") {
				View.View("Boy, ah sure is thusty! Walking to the saloon");
				Miner.Location = "saloon";
			}
		},
		Exit: function () {
			View.View("Leaving the saloon, feelin' good");
		},
		Update: function () {
			if (Miner.Thirsty()) {

				View.View("That's mighty fine sippin liquer");
				Miner.FSM.ChangeState(EnterMineAndDigForNuggetState); ;
			} else {
				View.View("\nERROR!\nERROR!\nERROR!");
			}
		},
	};
	
	Miner.FSM = StateMachine;
	Miner.FSM.ChangeState(EnterMineAndDigForNuggetState);
	setInterval(function () {
		Miner.Update();
	}, 1000);
});

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值