




版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、狀態(tài)驅(qū)動(dòng)的游戲智能體設(shè)計(jì)(一)廣州匯眾出品Finite state machines, or FSMs as they are usually referred to, have for many years been the AI coders instrument of choice to imbue a game agent with the illusion of intelligence. You will find FSMs of one kind or another in just about every game to hit the shelves since the ea
2、rly days of video games, and despite the increasing popularity of more esoteric agent architectures, they are going to be around for a long time to come. Here are just some of the reasons why:就像經(jīng)常聽(tīng)到的,有限狀態(tài)機(jī)(簡(jiǎn)寫為FSM)早就被AI程序員用來(lái)實(shí)現(xiàn)游戲智能體以體現(xiàn)智能感。你會(huì)發(fā)現(xiàn)在FSM幾乎是所有視頻游戲的基礎(chǔ)構(gòu)架,不管正在出現(xiàn)和流行的越來(lái)越深?yuàn)W的智能體架構(gòu),F(xiàn)SM在長(zhǎng)久的未來(lái)仍然有武之地。這里
3、是一些為什么FSM如此強(qiáng)勁的原因:They are quick and simple to code.There are many ways of programming a finite state machine and almost all of them are reasonably simple to implement. Youll see several alternatives described in this article together with the pros and cons of using them.可以快速簡(jiǎn)單地編寫代碼。實(shí)現(xiàn)有限狀態(tài)機(jī)有多種途徑,而且都可
4、以簡(jiǎn)單實(shí)現(xiàn)。在本文你就能看到多種描述和贊成或者反對(duì)使用它們的理由。They are easy to debug.Because a game agents behavior is broken down into easily manageable chunks, if an agent starts acting strangely, it can be debugged by adding tracer code to each state. In this way, the AI programmer can easily follow the sequence of events th
5、at precedes the buggy behavior and take action accordingly.易于調(diào)試。因?yàn)橐粋€(gè)游戲智能體的行為由一個(gè)易于管理的代碼段來(lái)實(shí)現(xiàn),如果一個(gè)智能出現(xiàn)奇怪的行為,可以通過(guò)為每一個(gè)狀態(tài)增加Tracer來(lái)調(diào)試。這樣能夠容易地跟蹤事件序列,就可以針對(duì)之前的怪異行為修改代碼了。They have little computational overhead.Finite state machines use hardly any precious processor time because they essentially follow hard- co
6、ded rules. There is no real “thinking” involved beyond the if-this-then-that sort of thought process.需要付出一點(diǎn)計(jì)算代價(jià)。有限狀態(tài)機(jī)幾乎不使用寶貴的處理器時(shí)間,因?yàn)樗麄儽举|(zhì)上跟硬編碼是一樣的。在那種“如果這樣就那樣”的思考處理中根本不存在真正的“思考”。They are intuitive.Its human nature to think about things as being in one state or another and we often refer to ourselves
7、 as being in such and such a state. How many times have you “got yourself into a state” or found yourself in “the right state of mind”? Humans dont really work like finite state machines of course, but sometimes we find it useful to think of our behavior in this way. Similarly, it is fairly easy to
8、break down a game agents behavior into a number of states and to create the rules required for manipulating them. For the same reason, finite state machines also make it easy for you to discuss the design of your AI with non-programmers (with game producers and level designers for example), providin
9、g improved communication and exchange of ideas.符合直覺(jué)。人類天生就以當(dāng)前處以這種或者哪種狀態(tài)來(lái)思考事情,所以我們常常聽(tīng)到我們自己處于什么狀態(tài)的說(shuō)法。多少次你“讓你自己進(jìn)入狀態(tài)”或者發(fā)現(xiàn)你自己處于“正確的精神狀態(tài)”?盡管人類并非真的像有限狀態(tài)機(jī)那樣工作,但通常我們發(fā)現(xiàn)這樣有利于我們思考我們的行為。同樣地,這易于通過(guò)一系列的狀態(tài)和創(chuàng)造操作規(guī)則來(lái)實(shí)現(xiàn)一個(gè)游戲智能體的行為?;谕瑯拥脑?,有限狀態(tài)機(jī)能讓你和非程序員(如游戲策劃和關(guān)卡設(shè)計(jì)師等)更好地進(jìn)行關(guān)于你的AI設(shè)計(jì)的討論,改進(jìn)交流和交換觀點(diǎn)。They are flexible.A game agent
10、s finite state machine can easily be adjusted and tweaked by the programmer to provide the behavior required by the game designer. Its also a simple matter to expand the scope of an agents behavior by adding new states and rules. In addition, as your AI skills grow youll find that finite state machi
11、nes provide a solid backbone with which you can combine other techniques such as fuzzy logic or neural networks.可伸縮性。游戲智能體的有限狀態(tài)機(jī)易于調(diào)整,能夠很容易讓程序員實(shí)現(xiàn)游戲設(shè)計(jì)師需要的行為,也易于通過(guò)增加新的狀態(tài)和規(guī)則來(lái)擴(kuò)展智能體的行為。此外,隨著你AI技術(shù)的增進(jìn),你將發(fā)現(xiàn)有限狀態(tài)機(jī)提供堅(jiān)實(shí)的基礎(chǔ),讓你能夠把模糊邏輯和神經(jīng)網(wǎng)絡(luò)之類的技術(shù)組合到游戲中。What Exactly Is a Finite State Machine?有限狀態(tài)機(jī)定義Historically, a fi
12、nite state machine is a rigidly formalized device used by mathematicians to solve problems. The most famous finite state machine is probably Alan Turings hypothetical device: the Turing machine, which he wrote about in his 1936 paper, “On Computable Numbers.” This was a machine presaging modern-day
13、programmable computers that could perform any logical operation by reading, writing, and erasing symbols on an infinitely long strip of tape. Fortunately, as AI programmers, we can forgo the formal mathematical definition of a finite state machine; a descriptive one will suffice:從歷史觀點(diǎn)上來(lái)說(shuō),有限狀態(tài)機(jī)是一種嚴(yán)格的
14、公式化的被數(shù)學(xué)家用以解決難題的一種策略。最著名的有限狀態(tài)機(jī)可能是阿蘭圖靈在1936年發(fā)表的論文On Computable Numbers上寫下的的猜想圖靈機(jī)。這是現(xiàn)代計(jì)算機(jī)的雛形,能夠通過(guò)在無(wú)限長(zhǎng)的磁帶上進(jìn)行讀、寫和擦除符號(hào)來(lái)實(shí)現(xiàn)所有邏輯操作。幸運(yùn)的是,作為AI程序員,我們能夠?qū)交臄?shù)學(xué)定義不加理會(huì),如下描述已經(jīng)足夠:A finite state machine is a device, or a model of a device, which has a finite number of states it can be in at any given time and can ope
15、rate on input to either make transitions from one state to another or to cause an output or action to take place. A finite state machine can only be in one state at any moment in time.有限狀態(tài)機(jī)是一種策略或者一種策略模型,它由有限的一系列狀態(tài)構(gòu)成,在任一給定時(shí)刻,可以通過(guò)輸入操作作出從一種狀態(tài)到另一種狀態(tài)的轉(zhuǎn)換或者產(chǎn)生輸出或者發(fā)生動(dòng)作。有限狀態(tài)機(jī)在任一時(shí)刻都只能夠處于一種狀態(tài)中。The idea behind a
16、 finite state machine, therefore, is to decompose an objects behavior into easily manageable “chunks” or states. The light switch on your wall, for example, is a very simple finite state machine. It has two states: on and off. Transitions between states are made by the input of your finger. By flick
17、ing the switch up it makes the transition from off to on, and by flicking the switch down it makes the transition from on to off. There is no output or action associated with the off state (unless you consider the bulb being off as an action), but when it is in the on state electricity is allowed to
18、 flow through the switch and light up your room via the filament in a lightbulb. See Figure 2.1.因此,有限狀態(tài)機(jī)背后的思想就是把一個(gè)對(duì)象的行為分解為易于管理的“塊”或者狀態(tài)。例如墻上的電燈開(kāi)關(guān),就是一種非常簡(jiǎn)單的有限狀態(tài)機(jī)。它有兩個(gè)狀態(tài):開(kāi)與關(guān)。兩個(gè)狀態(tài)通過(guò)你指頭產(chǎn)生的輸入來(lái)切換。把開(kāi)關(guān)扳上,它就從關(guān)的狀態(tài)轉(zhuǎn)換到開(kāi)的狀態(tài),把開(kāi)關(guān)扳下,它就從開(kāi)的狀態(tài)轉(zhuǎn)換到關(guān)的狀態(tài)。在關(guān)的狀態(tài)沒(méi)有任何輸出或者動(dòng)作(除非你裝燈泡被關(guān)掉視為一種動(dòng)作),但當(dāng)在開(kāi)的狀態(tài)下時(shí)電流通過(guò)開(kāi)關(guān)并且通過(guò)燈泡里的燈絲照亮房間。如圖2.1
19、The ghosts behavior in Pac-Man is implemented as a finite state machine. There is one Evade state, which is the same for all ghosts, and then each ghost has its own Chase state, the actions of which are implemented differently for each ghost. The input of the player eating one of the power pills is
20、the condition for the transition from Chase to Evade. The input of a timer running down is the condition for the transition from Evade to Chase. Pac-Mac里的精靈的行為用有限狀態(tài)機(jī)實(shí)現(xiàn)。所有的精靈都有一種Evade(逃避)狀態(tài),它們的實(shí)現(xiàn)都是一樣的;但每一個(gè)精靈都一個(gè)Chase(追蹤)狀態(tài),它的實(shí)現(xiàn)各不相同。Quake-style bots are implemented as finite state machines. They have s
21、tates such as FindArmor, FindHealth, SeekCover, and RunAway. Even the weapons in Quake implement their own mini finite state machines. For example, a rocket may implement states such asMove, TouchObject, and Die. Quake系列的機(jī)器人以有限狀態(tài)機(jī)實(shí)現(xiàn)。它們FindArmor(找裝備)、FindHealth(找補(bǔ)血)、SeekCover(找掩護(hù))和RunAway(逃跑)等多種狀態(tài)。甚至
22、Quake里實(shí)現(xiàn)的武器都帶有小型有限狀態(tài)機(jī),例如一個(gè)火箭炮實(shí)現(xiàn)的狀態(tài)就有Move(移動(dòng))、TouchObject(觸到物體)和Die(死亡)等幾種狀態(tài)。 Players in sports simulations such as the soccer game FIFA2002 are implemented as state machines. They have states such as Strike, Dribble, ChaseBall, and MarkPlayer. In addition, the teams themselves are often implemented
23、as FSMs and can have states such as KickOff, Defend, or WalkOutOnField. FIFA2002之類的運(yùn)動(dòng)模擬游戲里的運(yùn)動(dòng)員是用狀態(tài)機(jī)實(shí)現(xiàn)的,它們有Strike(踢出)、Dribble(帶球)、ChaseBall(逐球)和MarkPlayer(盯人)等狀態(tài)。此外,整個(gè)球隊(duì)通常也是用FSM實(shí)現(xiàn)的,有KickOff(發(fā)球)、Defend(防守)和WalkOutOnField(不知道怎么翻譯,請(qǐng)足球達(dá)人告知一下)。 The NPCs (non-player characters) in RTSs (real-time strategy
24、games) such as Warcraft make use of finite state machines. They have states such as MoveToPosition, Patrol, andFollowPath. RTS(實(shí)時(shí)策略游戲)(例如Warcraft)中的NPC(非玩家角色)也利用有限狀態(tài)機(jī)。它們的狀態(tài)有MoveToPosition(移動(dòng)到某地)、Patrol(巡邏)和FollowPath(跟隨)等。Implementing a Finite State Machine有限狀態(tài)機(jī)實(shí)現(xiàn)There are a number of ways of implem
25、enting finite state machines. A naive approach is to use a series of if-then statements or the slightly tidier mechanism of a switch statement. Using a switch with an enumerated type to represent the states looks something like this:實(shí)現(xiàn)有限狀態(tài)機(jī)有許多方式。一個(gè)直觀的做法就是使用一系列的if-then語(yǔ)句或者稍顯整潔的switch語(yǔ)句。使用switch的實(shí)現(xiàn)看起來(lái)
26、就像這里的代碼:enum StateTypestate_RunAway, state_Patrol, state_Attack;void Agent:UpdateState(StateType CurrentState)switch(CurrentState)case state_RunAway:EvadeEnemy();if (Safe()ChangeState(state_Patrol);break;case state_Patrol:FollowPatrolPath();if (Threatened()if (StrongerThanEnemy()ChangeState(state_At
27、tack);elseChangeState(state_RunAway);break;case state_Attack:if (WeakerThanEnemy()ChangeState(state_RunAway);elseBashEnemyOverHead();break;/end switchAlthough at first glance this approach seems reasonable, when applied practically to anything more complicated than the simplest of game objects, the
28、switch/if-then solution becomes a monster lurking in the shadows waiting to pounce. As more states and conditions are added, this sort of structure ends up looking like spaghetti very quickly, making the program flow difficult to understand and creating a debugging nightmare. In addition, its inflex
29、ible and difficult to extend beyond the scope of its original design, should that be desirable and as we all know, it most often is. Unless you are designing a state machine to implement very simple behavior (or you are a genius), you will almost certainly find yourself first tweaking the agent to c
30、ope with unplanned-for circumstances before honing the behavior to get the results you thought you were going to get when you first planned out the state machine!盡管咋一看這個(gè)方案還可以,但只要將其應(yīng)用到比最簡(jiǎn)單的游戲?qū)ο笊詾閺?fù)雜的實(shí)際情況下,這個(gè)switch/if-then方案就變成了蟄伏在陰影下的怪物隨時(shí)都可能突襲你一下。隨著大量的狀態(tài)和條件的增加,那種結(jié)構(gòu)很快就會(huì)變得像意大利面條一樣,使用程序難以理解,并成為調(diào)試夢(mèng)魘。此外,它不
31、可伸縮并且難以在最初的設(shè)計(jì)范圍之外進(jìn)行擴(kuò)展,然而我們都知道,它極為常見(jiàn)。除非你用狀態(tài)機(jī)實(shí)現(xiàn)非常簡(jiǎn)單的行為(或者你是一個(gè)天才),否則當(dāng)你第一次策劃狀態(tài)機(jī)的時(shí)候,在你“磨合”取得的結(jié)果和你希望取得的結(jié)果之前,你幾乎肯定會(huì)發(fā)現(xiàn)你的智能體無(wú)法應(yīng)用未考慮到的環(huán)境。Additionally, as an AI coder, you will often require that a state perform a specific action (or actions) when its initially entered or when the state is exited. For example,
32、 when an agent enters the state RunAway you may want it to wave its arms in the air and scream “Arghhhhhhh!” When it finally escapes and changes state to Patrol, you may want it to emit a sigh, wipe its forehead, and say “Phew!” These are actions that only occur when the RunAway state is entered or
33、exited and not during the usual update step. Consequently, this additional functionality must ideally be built into your state machine architecture. To do this within the framework of a switch or if-then architecture would be accompanied by lots of teeth grinding and waves of nausea, and produce ver
34、y ugly code indeed.此外,作為一名AI程序員,你經(jīng)常需要在某一狀態(tài)實(shí)現(xiàn)一種特殊行為(或者一系列行為),比如在進(jìn)入或者離開(kāi)某一狀態(tài)的時(shí)候。例如當(dāng)一個(gè)智能體進(jìn)行RunAway(逃跑)狀態(tài)時(shí)你希望它把武器拋向空中并尖叫一聲“Arghhhhhh(啊)!”當(dāng)它成功逃脫并轉(zhuǎn)換到Patrol(巡邏)狀態(tài),你可能想讓它喘口氣、擦擦額頭然后說(shuō)一聲“Phew(呸)!”這些行為都只在進(jìn)入和離開(kāi)RunAway(逃跑)狀態(tài)時(shí)才會(huì)發(fā)生,而不是整個(gè)普通的update(更新)階段都會(huì)出現(xiàn)。因此這些額外的功能必須被完美地集成到你的狀態(tài)機(jī)架構(gòu)里。在switch或者if-then架構(gòu)里實(shí)現(xiàn)這些將讓人難以忍受,產(chǎn)生
35、的代碼將非常丑陋,不忍卒讀。State Transition Tables狀態(tài)轉(zhuǎn)換表A better mechanism for organizing states and affecting state transitions is a state transition table. This is just what it says it is: a table of conditions and the states those conditions lead to. Table 2.1 shows an example of the mapping for the states and
36、 conditions shown in the previous example.一個(gè)能夠更好地組織和進(jìn)行狀態(tài)轉(zhuǎn)換的機(jī)制是狀態(tài)轉(zhuǎn)換表。顧名思義這就是一個(gè)包含條件和條件導(dǎo)致的狀態(tài)的表。表2.1是前文代碼的狀態(tài)和條件影射表:Table 2.1. A simple state transition table表2.1 簡(jiǎn)單狀態(tài)轉(zhuǎn)換表This table can be queried by an agent at regular intervals, enabling it to make any necessary state transitions based on the stimulus i
37、t receives from the game environment. Each state can be modeled as a separate object or function existing external to the agent, providing a clean and flexible architecture. One that is much less prone to spaghettification than the if-then/switch approach discussed in the previous section.智能體隔一定時(shí)間查詢
38、這個(gè)表格,以使得它能夠基于從游戲環(huán)境接收到的消息來(lái)進(jìn)行必須的狀態(tài)轉(zhuǎn)換。每一個(gè)狀態(tài)都能夠?qū)崿F(xiàn)為彼此分離的與智能體不耦合的對(duì)象或函數(shù),以提供清晰和可伸縮的架構(gòu)。這一設(shè)計(jì)不再那么容易像前文討論的if-then/switch架構(gòu)那樣容易成為意大利面條。Someone once told me a vivid and silly visualization can help people to understand an abstract concept. Lets see if it works曾有人告訴我一個(gè)明晰而無(wú)聊的可視物能幫助人們理解抽象的理論,讓我們來(lái)看看當(dāng)它工作時(shí)Imagine a rob
39、ot kitten. Its shiny yet cute, has wire for whiskers and a slot in its stomach where cartridges analogous to its states can be plugged in. Each of these cartridges is programmed with logic, enabling the kitten to perform a specific set of actions. Each set of actions encodes a different behavior; fo
40、r example, play_with_string,eat_fish, or poo_on_carpet. Without a cartridge stuffed inside its belly the kitten is an inanimate metallic sculpture, only able to sit there and look cute in a Metal Mickey kind of way.想像存在一個(gè)機(jī)器貓,它閃閃發(fā)亮但是非??蓯?ài),有著金屬絲制作的胡須并且在胃部有一個(gè)插槽可以依照狀態(tài)插入可插入模組。每一個(gè)可插入模組都是一段邏輯程序,使得小貓能夠?qū)崿F(xiàn)一系列指
41、定的動(dòng)作。每一系列動(dòng)作都編碼為不同的行為,如play_with_string、eat_fish和poo_on_carpet。如果沒(méi)有在小貓的胃部插上可插入模組,它就是一件死物坐在那里,看起來(lái)蠻可受。The kitten is very dexterous and has the ability to autonomously exchange its cartridge for another if instructed to do so. By providing the rules that dictate when a cartridge should be switched, its
42、possible to string together sequences of cartridge insertions permitting the creation of all sorts of interesting and complicated behavior. These rules are programmed onto a tiny chip situated inside the kittens head, which is analogous to the state transition table we discussed earlier. The chip co
43、mmunicates with the kittens internal functions to retrieve the information necessary to process the rules (such as how hungry Kitty is or how playful its feeling). As a result, the state transition chip can be programmed with rules like:IF Kitty_Hungry AND NOT Kitty_Playful SWITCH_CARTRIDGE eat_fish
44、All the rules in the table are tested each time step and instructions are sent to Kitty to switch cartridges accordingly.這個(gè)小貓非常靈巧,并且能夠根據(jù)指令自動(dòng)地更換可插入模組。通過(guò)給定什么時(shí)候該更換可插入模組的規(guī)則,它能利用連貫地插入一系列的可插入模組創(chuàng)造所有有趣而復(fù)雜的行為。這些與前文討論的狀態(tài)轉(zhuǎn)換表相類似的規(guī)則被編成程序?qū)懭氲揭粋€(gè)極小的芯片,放置在小貓的頭部。芯片與小貓的內(nèi)部功能通信,以獲得處理規(guī)則時(shí)需要的信息(如Kitty有多餓和它感覺(jué)到的好玩度是多少)。狀態(tài)轉(zhuǎn)換芯片
45、可以用如下的方式編寫規(guī)則:IF Kitty_Hungry AND NOT Kitty_Playful SWITCH_CARTRIDGE eat_fish在每一個(gè)時(shí)間片都檢測(cè)表中的所有的規(guī)則,從而給Kitty發(fā)送指令以切換彈可插入模組。This type of architecture is very flexible, making it easy to expand the kittens repertoire by adding new cartridges. Each time a new cartridge is added, the owner is only required to
46、 take a screwdriver to the kittens head in order to remove and reprogram the state transition rule chip. It is not necessary to interfere with any other internal circuitry.這種架構(gòu)有良好的伸縮性,通過(guò)增加新的可插入模組就可以輕易擴(kuò)展小貓的指令表。每一次增加新的可插入模組,只需要用起子打開(kāi)小貓的頭殼,重編程狀態(tài)轉(zhuǎn)換規(guī)則芯片即可,不需要與其它內(nèi)部電路打交道。Embedded Rules規(guī)則內(nèi)嵌An alternative app
47、roach is to embed the rules for the state transitions within the states themselves. Applying this concept to Robo-Kitty, the state transition chip can be dispensed with and the rules moved directly into the cartridges. For instance, the cartridge for play_with_string can monitor the kittys level of
48、hunger and instruct it to switch cartridges for the eat_fish cartridge when it senses hunger rising. In turn the eat_fish cartridge can monitor the kittens bowel and instruct it to switch to the poo_on_carpet cartridge when it senses poo levels are running dangerously high.另一可選的方法是把狀態(tài)轉(zhuǎn)換規(guī)則內(nèi)嵌到狀態(tài)本身當(dāng)中。在
49、對(duì)機(jī)器貓應(yīng)用這個(gè)概念后,可以去除狀態(tài)轉(zhuǎn)換芯片,直接將規(guī)則內(nèi)置到可插入模組里。例如play_with_string可插入模組能夠監(jiān)視小貓的饑餓度并適時(shí)地命令它切換到eat_fish可插入模組。同樣地,eat_fish可插入模組能夠監(jiān)視小貓是否已經(jīng)吃飽,并在感覺(jué)到迫切的排泄感時(shí)命令它切換到poo_on_carpet可插入模組。Although each cartridge may be aware of the existence of any of the other cartridges, each is a self-contained unit and not reliant on any
50、 external logic to decide whether or not it should allow itself to be swapped for an alternative. As a consequence, its a straightforward matter to add states or even to swap the whole set of cartridges for a completely new set (maybe ones that make little Kitty behave like a raptor). Theres no need
51、 to take a screwdriver to the kittens head, only to a few of the cartridges themselves.盡管每一個(gè)可插入模組需要知道其它模組的存在,但它們都是自包含的,無(wú)論是否要其它模組來(lái)替換自己,都并不需要任何外部邏輯來(lái)幫助決策。從而可以推論出其中的簡(jiǎn)明關(guān)系,甚至可以用全新的模組集合替換已有的集合(可能這會(huì)使用小貓的行為像鳥類一樣)。使用這個(gè)方案不再需要用起子打開(kāi)小貓的頭部,只要改變可插入模組即可。Lets take a look at how this approach is implemented within the
52、 context of a video game. Just like Kittys cartridges, states are encapsulated as objects and contain the logic required to facilitate state transitions. In addition, all state objects share a common interface: a pure virtual class named State. Heres a version that provides a simple interface:現(xiàn)在來(lái)看看在
53、視頻游戲中如何實(shí)現(xiàn)這一方案。像剛才討論的小貓的可插入模組,可以封裝到一個(gè)對(duì)象里,其中包含輔助狀態(tài)轉(zhuǎn)換的的邏輯。另外,所有的狀態(tài)共享一個(gè)通用接口:一個(gè)命名為State的純虛類。這里有個(gè)接口簡(jiǎn)單的實(shí)現(xiàn):Class Statepublic:virtual void Execute (Troll* troll) = 0;Now imagine a Troll class that has member variables for attributes such as health, anger, stamina, etc., and an interface allowing a client to
54、query and adjust those values. A Troll can be given the functionality of a finite state machine by adding a pointer to an instance of a derived object of the Stateclass, and a method permitting a client to change the instance the pointer is pointing to.現(xiàn)在想象Troll類有一系列的數(shù)值成員變量:health、anger、stamina等,當(dāng)然也
55、有相應(yīng)的接口以查詢和設(shè)置這些變量值。Troll通過(guò)增加一個(gè)成員指針變量(指向State類派生類的實(shí)例)來(lái)增加有限狀態(tài)機(jī)的功能,還提供一個(gè)改變指針指向的實(shí)例的方法。class Troll/* ATTRIBUTES OMITTED */State* m_pCurrentState;public:/* INTERFACE TO ATTRIBUTES OMITTED */void Update()m_pCurrentState-Execute(this);void ChangeState(const State* pNewState)delete m_pCurrentState;m_pCurrentS
56、tate = pNewState;When the Update method of a Troll is called, it in turn calls the Executemethod of the current state type with the this pointer. The current state may then use the Troll interface to query its owner, to adjust its owners attributes, or to effect a state transition. In other words, h
57、ow a Troll behaves when updated can be made completely dependent on the logic in its current state. This is best illustrated with an example, so lets create a couple of states to enable a troll to run away from enemies when it feels threatened and to sleep when it feels safe.當(dāng)調(diào)用Troll的Update方法時(shí),它以thi
58、s指針為參數(shù)調(diào)用當(dāng)前狀態(tài)類型的Excecute方法。當(dāng)前狀態(tài)可能使用Troll的接口查詢它的擁有者,設(shè)置擁有者的屬性或者產(chǎn)生一個(gè)狀態(tài)轉(zhuǎn)換。換句話說(shuō),Troll能依賴當(dāng)前狀態(tài)的邏輯作出完整行為。這里有最好的例子表達(dá)這一觀點(diǎn),讓我們來(lái)完成兩個(gè)狀態(tài)實(shí)現(xiàn)以使得Troll能夠在危險(xiǎn)時(shí)逃跑,或者在安全時(shí)睡覺(jué)。/-State_Runawayclass State_RunAway : public Statepublic:void Execute(Troll* troll)if (troll-isSafe()troll-ChangeState(new State_Sleep();elsetroll-MoveAwayFromEnemy();/-State_Sleepclass State_Sleep : public Statepublic:void
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 武漢幼兒園家長(zhǎng)活動(dòng)方案
- 櫥柜打折促銷活動(dòng)方案
- 格雅裝飾公司團(tuán)購(gòu)活動(dòng)方案
- 湯店鋪營(yíng)銷活動(dòng)方案
- 歡樂(lè)聚會(huì)活動(dòng)方案
- 法國(guó)健康包發(fā)放活動(dòng)方案
- 漢字打字大賽活動(dòng)方案
- 河北主場(chǎng)活動(dòng)方案
- 模擬槍機(jī)活動(dòng)方案
- 四年級(jí)下冊(cè)語(yǔ)文期末考試作文押題及范文
- 禮來(lái)公司銷售講座
- 門店日常衛(wèi)生檢查表
- 神經(jīng)生物學(xué)試題(卷)與答案解析6套
- GB∕T 10544-2022 橡膠軟管及軟管組合件 油基或水基流體適用的鋼絲纏繞增強(qiáng)外覆橡膠液壓型 規(guī)范
- FANUC機(jī)器人R-2000iA機(jī)械單元維護(hù)手冊(cè)
- 中國(guó)當(dāng)代文學(xué)專題-國(guó)家開(kāi)放大學(xué)2022年1月期末考試復(fù)習(xí)資料-漢語(yǔ)言本科復(fù)習(xí)資料
- SHR-500A高速混合機(jī)
- 機(jī)動(dòng)車維修經(jīng)營(yíng)備案表
- 井下作業(yè)質(zhì)量管理制度
- 超星爾雅學(xué)習(xí)通《國(guó)際金融》2020章節(jié)測(cè)試含答案(上)
- 污水處理工程調(diào)試和試運(yùn)行手冊(cè)通用
評(píng)論
0/150
提交評(píng)論