以下代码改编自《游戏人工智能编程案例精粹(修订版)》([美]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="myMinerAndhisWifeWithMessage.js"></script>
<style>
#screen{
text-align:left;
}
.Bob{
background-color:white;
}
.Elsa{
background-color:darkcyan;
}
.Message{
background-color:yellow;
}
</style>
</head>
<body>
<div id='screen'></div>
</body>
</html>
//myMinerAndhisWifeWithMessage.js
$(document).ready(function () {
/*
Message={
sender:0,
receiver:0,
msg:0,
DisptchTime:0,
}
*/
var MessageDispatcher = {
Message: [],
DisCharge: function (msg) { //发送消息
if (!msg.receiver.FSM.HandleMessage(msg)) {
View.View("Message:Not handled");
}
},
DispatchMessage: function (delay, msg) {
var currentTime = (new Date()).getTime();
msg.DisptchTime = currentTime;
if (delay <= 0.0) {
View.View("Message:Instant telegram dispatched at time:" + currentTime);
View.View("Message:from " + msg.sender.name);
View.View("Message:to " + msg.receiver.name);
View.View("Message:" + msg.msg);
this.DisCharge(msg);
} else {
msg.DisptchTime = (new Date()).getTime()
View.View("Message:Delayed telegram at time:" + msg.DisptchTime);
View.View("Message:from " + msg.sender.name);
View.View("Message:to " + msg.receiver.name);
View.View("Message:" + msg.msg);
msg.DisptchTime = parseInt(msg.DisptchTime) + delay;
this.Message.push(msg);
this.Message.sort(function (a, b) {
return a.DisptchTime - b.DisptchTime;
});
}
},
DispatchDelayedMessages: function () {
var currentTime = (new Date()).getTime();
while (MessageDispatcher.Message.length > 0 && MessageDispatcher.Message[0].DisptchTime < currentTime && MessageDispatcher.Message[0].DisptchTime < currentTime > 0) {
MessageDispatcher.DisCharge(MessageDispatcher.Message[0]);
MessageDispatcher.Message.shift();
}
},
};
var StateMachine = {
m_PreviousState: {
name: "p",
Enter: function () {},
Exit: function () {},
Update: function (e) {},
HandleMessage: function (msg) {
return false;
},
},
m_CurrentState: {
name: "C",
Enter: function () {},
Exit: function () {},
Update: function (e) {},
HandleMessage: function (msg) {
return false;
},
},
m_GlobalState: {
name: "G",
Enter: function () {},
Exit: function () {},
Update: function (e) {},
HandleMessage: function (msg) {
return false;
},
},
Update: function (e) {
if (this.m_GlobalState != null)
this.m_GlobalState.Update(e);
if (this.m_CurrentState != null)
this.m_CurrentState.Update(e);
},
SetGlobalState: function (s) {
this.m_GlobalState = s;
},
SetCurrentState: function (s) {
this.m_CurrentState = s;
},
SetPreviousState: function (s) {
this.m_PreviousState = s;
},
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);
},
HandleMessage: function (msg) {
if (this.m_CurrentState && this.m_CurrentState.HandleMessage(msg)) {
return true;
}
if (typeof this.m_GlobalState.HandleMessage != "function") {
console.log(this.m_GlobalState.HandleMessage);
console.log(this.m_GlobalState.name);
}
if (this.m_GlobalState && this.m_GlobalState.HandleMessage(msg)) {
return true;
}
return false;
},
};
var View = {
View: function (s) {
var html = $("#screen").html();
var id = 'Bob';
switch (s.charAt(0)) {
case "B":
id = "Bob";
break;
case "E":
id = "Elsa";
break;
case "M":
id = "Message";
break;
};
html += "<div class='" + id + "'> " + s + "</div>";
$("#screen").html(html);
},
};
var Controller = {
timer: null,
Init: function () {
$("body").click(function () {
clearInterval(Controller.timer);
});
},
};
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 -= 0.2;
},
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 EnterMineAndDigForNuggetState = {
name: "EnterMineAndDigForNuggetState",
Enter: function () {
if (Miner.Location != "goldmine") {
View.View(Miner.name + ":Walkin' to the goldmine");
Miner.Location = "goldmine";
}
},
Exit: function () {
View.View(Miner.name + ":Ah'm leavin' the gold mine with mah pockets full o' sweet gold");
},
Update: function () {
Miner.AddToGoldCarried(1);
Miner.IncreaseFatigue();
View.View(Miner.name + ":Pickin' up a nugget.");
if (Miner.PocketsFull()) {
Miner.FSM.ChangeState(VistBankAndDepositGoldState);
} else if (Miner.Thirsty()) {
Miner.FSM.ChangeState(QuenchThirstState);
}
},
HandleMessage: function (msg) {
return false;
},
};
var VistBankAndDepositGoldState = {
name: "VistBankAndDepositGoldState",
Enter: function () {
if (Miner.Location != "bank") {
View.View(Miner.name + ":Goin' to the bank. Yes siree");
Miner.Location = "bank";
}
},
Exit: function () {
View.View(Miner.name + ":Leavin' the bank");
},
Update: function () {
Miner.AddToWealth(Miner.GoldCarried());
View.View(Miner.name + ":Depositing gold. Total savings now");
//wealthy enough to have a well earned rest?
if (Miner.Wealth() >= Miner.ComfortLevel) {
View.View(Miner.name + ":WooHoo! Rich enough for now. Back home to mah li'lle lady");
Miner.FSM.ChangeState(GoHomeAndSleepTilRestedState);
} else {
Miner.FSM.ChangeState(EnterMineAndDigForNuggetState);
}
},
HandleMessage: function (msg) {
return false;
},
HandleMessage: function (msg) {
return false;
},
};
var GoHomeAndSleepTilRestedState = {
name: "GoHomeAndSleepTilRestedState",
Enter: function () {
if (Miner.Location != "shack") {
View.View(Miner.name + ":Walkin' home");
Miner.Location = "shack";
MessageDispatcher.DispatchMessage(-10, {
sender: Miner,
receiver: Wife,
msg: "HiHoneyImHome"
});
}
},
Exit: function () {
View.View(Miner.name + ":Leaving the house");
},
Update: function () {
if (Miner.Fatigued()) {
Miner.DecreaseFatigue();
View.View(Miner.name + ":ZZZZ... ");
} else {
View.View(Miner.name + ":What a God darn fantastic nap! Time to find more gold");
Miner.FSM.ChangeState(EnterMineAndDigForNuggetState);
}
},
HandleMessage: function (msg) {
if (msg.msg == "StewReady") {
View.View("Message:Message handled by " + Miner.name + " at " + (new Date()).toTimeString());
View.View(Miner.name + ":Okay Hun, ahm a comin'! ");
Miner.FSM.ChangeState(EatStewState);
return true;
}
return false;
},
};
var QuenchThirstState = {
name: "QuenchThirstState",
Enter: function () {
if (Miner.Location != "saloon") {
View.View(Miner.name + ":Boy, ah sure is thusty! Walking to the saloon");
Miner.Location = "saloon";
}
},
Exit: function () {
View.View(Miner.name + ":Leaving the saloon, feelin' good");
},
Update: function () {
if (Miner.Thirsty()) {
View.View(Miner.name + ":That's mighty fine sippin liquer");
Miner.FSM.ChangeState(EnterMineAndDigForNuggetState); ;
} else {
View.View("\nERROR!\nERROR!\nERROR!");
}
},
HandleMessage: function (msg) {
return false;
},
};
var EatStewState = {
name: "EatStewState",
Enter: function () {
View.View(Miner.name + ":Smells Reaaal goood Elsa!");
},
Exit: function () {
View.View(Miner.name + ":Thankya li'lle lady. Ah better get back to whatever ah wuz doin'");
},
Update: function () {
View.View(Miner.name + ":Tastes real good too!");
Miner.FSM.RevertToPreviousState();
},
HandleMessage: function (msg) {
return false;
},
};
//
var Wife = {
name: "Elsa",
state: "",
Location: "",
_cooking: false,
Update: function () {
this.FSM.Update();
},
Cooking: function () {
return this._cooking;
},
SetCooking: function (b) {
this._cooking = b;
},
FSM: {},
};
var WifeGlobalState = {
name: "WifeGlobalState",
Enter: function () {},
Exit: function () {},
Update: function () {
if (Math.floor(Math.random() + 5.5) < 2) {
Wife.FSM.ChangeState(VisitBathroomState);
}
},
HandleMessage: function (msg) {
if (msg.msg == "HiHoneyImHome") {
View.View("Message:Message handled by " + Wife.name + " at " + (new Date()).toTimeString());
View.View(Wife.name + ": Hi honey. Let me make you some of mah fine country stew");
Wife.FSM.ChangeState(CookStewState);
return true;
}
return false;
},
};
var DoHouseWorkState = {
name: "DoHouseWorkState",
Enter: function () {},
Exit: function () {},
Update: function () {
switch (Math.floor((Math.random() * 2))) {
case 0:
View.View(Wife.name + ":Moppin ' the floor");
break;
case 1:
View.View(Wife.name + ":Washin' the dishes ");
break;
case 2:
View.View(Wife.name + ":Makin' the bed");
break;
}
},
HandleMessage: function (msg) {
return false;
},
};
var VisitBathroomState = {
name: "VisitBathroomState",
Enter: function () {
View.View(Wife.name + ":Walkin' to the can. Need to powda mah pretty li'lle nose");
},
Exit: function () {
View.View(Wife.name + ":Leavin' the Jon");
},
Update: function () {
View.View(Wife.name + ": Ahhhhhh! Sweet relief!");
},
HandleMessage: function (msg) {
return false;
},
};
var CookStewState = {
name: "CookStewState",
Enter: function () {
if (!Wife.Cooking()) {
View.View(Wife.name + ": Putting the stew in the oven");
MessageDispatcher.DispatchMessage(1500, {
sender: Wife,
receiver: Wife,
msg: "StewReady"
});
Wife.SetCooking(true);
}
},
Exit: function () {
View.View(Wife.name + ":Puttin' the stew on the table");
},
Update: function () {
View.View(Wife.name + ":Fussin' over food");
},
HandleMessage: function (msg) {
if (msg.msg == "StewReady") {
View.View("Message:Message handled by " + Wife.name + " at " + (new Date()).toTimeString());
View.View(Wife.name + ":StewReady! Lets eat");
MessageDispatcher.DispatchMessage(-10, {
sender: Wife,
receiver: Miner,
msg: "StewReady"
});
Wife.SetCooking(false);
Wife.FSM.ChangeState(DoHouseWorkState);
return true;
}
return false;
},
};
//
Miner.FSM = StateMachine;
$.extend(Wife.FSM, StateMachine);
Miner.FSM.ChangeState(EnterMineAndDigForNuggetState);
Wife.FSM.SetGlobalState(WifeGlobalState);
Wife.FSM.ChangeState(DoHouseWorkState);
Controller.Init();
Controller.timer = setInterval(function () {
Miner.Update();
Wife.Update();
MessageDispatcher.DispatchDelayedMessages();
}, 800);
});