以下代码改编自《游戏人工智能编程案例精粹(修订版)》([美]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="myMinerAndhisWife.js"></script>
<style>
#screen{
text-align:left;
}
#Bob{
backgroud-color:yellow;
}
#Elsa{
background-color:darkcyan;
}
</style>
</head>
<body>
<div id='screen'></div>
</body>
</html>
//myMinerAndhisWife.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();
var id = s.charAt(0) == "B" ? "Bob" : "Elsa";
html += "<div id='" + id + "'> " + s + "</div>";
$("#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) {},
},
m_GlobalState: {
name: "",
Enter: function () {},
Exit: function () {},
Update: function (e) {},
},
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);
},
};
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);
}
},
};
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);
}
},
};
var GoHomeAndSleepTilRestedState = {
name: "GoHomeAndSleepTilRestedState",
Enter: function () {
if (Miner.Location != "shack") {
View.View(Miner.name + ":Walkin' home");
Miner.Location = "shack";
}
},
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); ;
}
},
};
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!");
}
},
};
//
var Wife = {
name: "Elsa",
state: "",
Location: "",
Update: function () {
this.FSM.Update();
},
FSM: {},
};
var WifeGlobalState = {
name: "WifeGlobalState",
Enter: function () {},
Exit: function () {},
Update: function () {
if (Math.floor(Math.random() + 1.5) < 2) {
Wife.FSM.ChangeState(VisitBathroomState);
}
},
};
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;
}
},
};
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!");
},
};
//
Miner.FSM = StateMachine;
$.extend(Wife.FSM, StateMachine);
Miner.FSM.ChangeState(EnterMineAndDigForNuggetState);
Wife.FSM.SetGlobalState(WifeGlobalState);
Wife.FSM.ChangeState(DoHouseWorkState);
setInterval(function () {
Miner.Update();
Wife.Update();
}, 100);
});