Presentation is loading. Please wait.

Presentation is loading. Please wait.

Finite State Machines introduction to hunter-prey 10/8/10

Similar presentations


Presentation on theme: "Finite State Machines introduction to hunter-prey 10/8/10"— Presentation transcript:

1 Finite State Machines introduction to hunter-prey 10/8/10
PROJECT introduction to hunter-prey DATE 10/8/10 PRESENTERS JOSEPH LEE, ALEX ZIRBEL

2 So I hear you like Graph Theory.

3 Introduction to Finite State Machines
Overview Implementation in C Hunter-Prey An Example Questions?

4 Introduction to Finite State Machines
Overview Implementation in C Hunter-Prey An Example Questions?

5 Overview Sometimes it is best to draw a diagram. Initial State State 1
Carnegie Mellon Overview Initial State State 1 State 2 State 3 Sometimes it is best to draw a diagram.

6 Overview When in doubt, do this: 1. Draw Diagram (Design)
Carnegie Mellon Overview When in doubt, do this: 1. Draw Diagram (Design) 2. Then Code (Implement) int state = 1 while(1) { switch(state) { case 1: printf(“hello!\n”); state++; break; case 2: printf(“good-bye.\n”); state--; default: printf(“AHH! You shouldn’t be here!\n”); }

7 Introduction to Finite State Machines
Overview Implementation in C Hunter-Prey An Example Questions?

8 Implementation in C while(1){ switch(state){ #define HUNTER 1
Carnegie Mellon Implementation in C A FSM in C consists of two parts: A While Loop while(1){ A Switch Statement switch(state){ You should define your states #define HUNTER 1

9 Implementation in C Code Example: #define PREY 0 #define HUNTER 1
Carnegie Mellon Implementation in C Code Example: #define PREY 0 #define HUNTER 1 #define WAIT -1 ... int state = PREY; while(1){ switch(state){ case PREY: /* prey code */ break; case WAIT: /* waiting code */ case HUNTER: /* hunter code */ default: break; }

10 Implementation in C Code Example: Define the states #define PREY 0
Carnegie Mellon Implementation in C Code Example: #define PREY 0 #define HUNTER 1 #define WAIT -1 ... int state = PREY; while(1){ switch(state){ case PREY: /* prey code */ break; case WAIT: /* waiting code */ case HUNTER: /* hunter code */ default: break; } Define the states

11 Implementation in C Code Example: Make the while loop #define PREY 0
Carnegie Mellon Implementation in C Code Example: #define PREY 0 #define HUNTER 1 #define WAIT -1 ... int state = PREY; while(1){ switch(state){ case PREY: /* prey code */ break; case WAIT: /* waiting code */ case HUNTER: /* hunter code */ default: break; } Make the while loop

12 Implementation in C Code Example: Add the switch statement
Carnegie Mellon Implementation in C Code Example: #define PREY 0 #define HUNTER 1 #define WAIT -1 ... int state = PREY; while(1){ switch(state){ case PREY: /* prey code */ break; case WAIT: /* waiting code */ case HUNTER: /* hunter code */ default: break; } Add the switch statement

13 Implementation in C Code Example: You should have break statements
Carnegie Mellon Implementation in C Code Example: #define PREY 0 #define HUNTER 1 #define WAIT -1 ... int state = PREY; while(1){ switch(state){ case PREY: /* prey code */ break; case WAIT: /* waiting code */ case HUNTER: /* hunter code */ default: break; } You should have break statements

14 Implementation in C Things to look out for
Carnegie Mellon Implementation in C Things to look out for Do not have states that go nowhere Bad State Conversely, do not have unreachable states Bad State

15 Introduction to Finite State Machines
Overview Implementation in C Hunter-Prey An Example Questions?

16 Hunter-Prey The Prey has simple requirements
Carnegie Mellon Hunter-Prey The Prey has simple requirements Design a prey that will run from the hunters If the prey is tagged, it will first acknowledge the tag After the prey acknowledges, it will change state to Waiting After waiting, the bot will move to the Hunter state

17 Hunter-Prey The Hunter is just as simple
Carnegie Mellon Hunter-Prey The Hunter is just as simple Design a hunter that will catch the prey When the hunter is close enough to the prey, it will tag the prey If the prey acknowledges the tag, then the hunter becomes the Prey If another hunter becomes the prey, your hunter changes state to Waiting

18 Introduction to Finite State Machines
Overview Implementation in C Hunter-Prey An Example Questions?

19 An Example: Zombie Design
Carnegie Mellon An Example: Zombie Design hunting for food hiding in corners blinded by light Zombie becomes hungry human shines flashlight Zombie recovers Zombie decides hunting is not worth it

20 An Example: Zombie Implementation
Carnegie Mellon An Example: Zombie Implementation #define HIDING 0 #define HUNTING 1 #define BLINDED 2 hunting for food case HUNTING: //locate flesh //go towards flesh //grab blindly break; human shines flashlight case HUNTING: //same as above if(light_shine) state=BLINDED; break;

21 An Example: Zombie Implementation
Carnegie Mellon An Example: Zombie Implementation #define HIDING 0 #define HUNTING 1 #define BLINDED 2 int main(){ int state = HIDING; int motivated = 0; while(1){ switch(state){ case HIDING: motivated++; if(motivated >= 2) state = HUNTING; break; case HUNTING: if(light_shine){ motivated--; state = BLINDED; } case BLINDED: if(!motivated) state = HIDING; else

22 An Example: Zombie Implementation
Carnegie Mellon An Example: Zombie Implementation #define HIDING 0 #define HUNTING 1 #define BLINDED 2 int main(){ int state = HIDING; int motivated = 0; while(1){ switch(state){ case HIDING: motivated++; if(motivated >= 2) state = HUNTING; break; case HUNTING: if(light_shine){ motivated--; state = BLINDED; } case BLINDED: if(!motivated) state = HIDING; else

23 An Example: Zombie Implementation
Carnegie Mellon An Example: Zombie Implementation #define HIDING 0 #define HUNTING 1 #define BLINDED 2 int main(){ int state = HIDING; int motivated = 0; while(1){ switch(state){ case HIDING: motivated++; if(motivated >= 2) state = HUNTING; break; case HUNTING: if(light_shine){ motivated--; state = BLINDED; } case BLINDED: if(!motivated) state = HIDING; else

24 An Example: Zombie Implementation
Carnegie Mellon An Example: Zombie Implementation #define HIDING 0 #define HUNTING 1 #define BLINDED 2 int main(){ int state = HIDING; int motivated = 0; while(1){ switch(state){ case HIDING: motivated++; if(motivated >= 2) state = HUNTING; break; case HUNTING: if(light_shine){ motivated--; state = BLINDED; } case BLINDED: if(!motivated) state = HIDING; else

25 An Example: Tracing Zombie
Carnegie Mellon An Example: Tracing Zombie #define HIDING 0 #define HUNTING 1 #define BLINDED 2 int main(){ int state = HIDING; int motivated = 0; while(1){ switch(state){ case HIDING: motivated++; if(motivated >= 2) state = HUNTING; break; case HUNTING: if(light_shine){ motivated--; state = BLINDED; } case BLINDED: if(!motivated) state = HIDING; else state = HIDING motivated = 0

26 An Example: Tracing Zombie
Carnegie Mellon An Example: Tracing Zombie #define HIDING 0 #define HUNTING 1 #define BLINDED 2 int main(){ int state = HIDING; int motivated = 0; while(1){ switch(state){ case HIDING: motivated++; if(motivated >= 2) state = HUNTING; break; case HUNTING: if(light_shine){ motivated--; state = BLINDED; } case BLINDED: if(!motivated) state = HIDING; else Before state = HIDING motivated = 0 After state = HIDING motivated = 1

27 An Example: Tracing Zombie
Carnegie Mellon An Example: Tracing Zombie #define HIDING 0 #define HUNTING 1 #define BLINDED 2 int main(){ int state = HIDING; int motivated = 0; while(1){ switch(state){ case HIDING: motivated++; if(motivated >= 2) state = HUNTING; break; case HUNTING: if(light_shine){ motivated--; state = BLINDED; } case BLINDED: if(!motivated) state = HIDING; else Before state = HIDING motivated = 1 After state = HUNTING motivated = 2

28 An Example: Tracing Zombie
Carnegie Mellon An Example: Tracing Zombie #define HIDING 0 #define HUNTING 1 #define BLINDED 2 int main(){ int state = HIDING; int motivated = 0; while(1){ switch(state){ case HIDING: motivated++; if(motivated >= 2) state = HUNTING; break; case HUNTING: if(light_shine){ motivated--; state = BLINDED; } case BLINDED: if(!motivated) state = HIDING; else Before state = HUNTING motivated = 2 After state = BLINDED motivated = 1

29 An Example: Tracing Zombie
Carnegie Mellon An Example: Tracing Zombie #define HIDING 0 #define HUNTING 1 #define BLINDED 2 int main(){ int state = HIDING; int motivated = 0; while(1){ switch(state){ case HIDING: motivated++; if(motivated >= 2) state = HUNTING; break; case HUNTING: if(light_shine){ motivated--; state = BLINDED; } case BLINDED: if(!motivated) state = HIDING; else Before state = BLINDED motivated = 1 After state = HUNTING motivated = 1

30 An Example: Tracing Zombie
Carnegie Mellon An Example: Tracing Zombie #define HIDING 0 #define HUNTING 1 #define BLINDED 2 int main(){ int state = HIDING; int motivated = 0; while(1){ switch(state){ case HIDING: motivated++; if(motivated >= 2) state = HUNTING; break; case HUNTING: if(light_shine){ motivated--; state = BLINDED; } case BLINDED: if(!motivated) state = HIDING; else Before state = HUNTING motivated = 1 After state = BLINDED motivated = 0

31 An Example: Tracing Zombie
Carnegie Mellon An Example: Tracing Zombie #define HIDING 0 #define HUNTING 1 #define BLINDED 2 int main(){ int state = HIDING; int motivated = 0; while(1){ switch(state){ case HIDING: motivated++; if(motivated >= 2) state = HUNTING; break; case HUNTING: if(light_shine){ motivated--; state = BLINDED; } case BLINDED: if(!motivated) state = HIDING; else Before state = BLINDED motivated = 0 After state = HIDING motivated = 0

32 An Example: Tracing Zombie
Carnegie Mellon An Example: Tracing Zombie #define HIDING 0 #define HUNTING 1 #define BLINDED 2 int main(){ int state = HIDING; int motivated = 0; while(1){ switch(state){ case HIDING: motivated++; if(motivated >= 2) state = HUNTING; break; case HUNTING: if(light_shine){ motivated--; state = BLINDED; } case BLINDED: if(!motivated) state = HIDING; else Before state = HIDING motivated = 0 After state = HIDING motivated = 1 Back to Square 1

33 Introduction to Finite State Machines
Overview Implementation in C Hunter-Prey Going Further Questions?


Download ppt "Finite State Machines introduction to hunter-prey 10/8/10"

Similar presentations


Ads by Google