1 / 33

Finite State Machines

Finite State Machines. PROJECT. introduction to hunter-prey. 10/8/10. JOSEPH LEE, ALEX ZIRBEL. DATE. PRESENTERS. So I hear you like Graph Theory. Introduction to Finite State Machines. Overview. Implementation in C. Hunter-Prey. An Example. Questions?.

roden
Download Presentation

Finite State Machines

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  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. Carnegie Mellon Overview Initial State State 1 State 2 State 3 Sometimes it is best to draw a diagram.

  6. 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--; • break; • default: • printf(“AHH! You shouldn’t be here!\n”); • break; • } • }

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

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

  9. 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 */ • break; • default: break; • } • }

  10. 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 */ • break; • default: break; • } • } Define the states

  11. 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 */ • break; • default: break; • } • } Make the while loop

  12. 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 */ • break; • default: break; • } • } Add the switch statement

  13. 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 */ • break; • default: break; • } • } You should have break statements

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

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

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

  17. Carnegie Mellon 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 Hunter-Prey The Hunter is just as simple

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

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

  20. Carnegie Mellon 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; An Example: Zombie Implementation #define HIDING 0 #define HUNTING 1 #define BLINDED 2

  21. 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; • } • break; • case BLINDED: • if(!motivated) • state = HIDING; • else • state = HUNTING; • break; • } • } • }

  22. 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; • } • break; • case BLINDED: • if(!motivated) • state = HIDING; • else • state = HUNTING; • break; • } • } • }

  23. 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; • } • break; • case BLINDED: • if(!motivated) • state = HIDING; • else • state = HUNTING; • break; • } • } • }

  24. 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; • } • break; • case BLINDED: • if(!motivated) • state = HIDING; • else • state = HUNTING; • break; • } • } • }

  25. 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; • } • break; • case BLINDED: • if(!motivated) • state = HIDING; • else • state = HUNTING; • break; • } • } • } state = HIDING motivated = 0

  26. 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; • } • break; • case BLINDED: • if(!motivated) • state = HIDING; • else • state = HUNTING; • break; • } • } • } Before state = HIDING motivated = 0 After state = HIDING motivated = 1

  27. 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; • } • break; • case BLINDED: • if(!motivated) • state = HIDING; • else • state = HUNTING; • break; • } • } • } Before state = HIDING motivated = 1 After state = HUNTING motivated = 2

  28. 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; • } • break; • case BLINDED: • if(!motivated) • state = HIDING; • else • state = HUNTING; • break; • } • } • } Before state = HUNTING motivated = 2 After state = BLINDED motivated = 1

  29. 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; • } • break; • case BLINDED: • if(!motivated) • state = HIDING; • else • state = HUNTING; • break; • } • } • } Before state = BLINDED motivated = 1 After state = HUNTING motivated = 1

  30. 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; • } • break; • case BLINDED: • if(!motivated) • state = HIDING; • else • state = HUNTING; • break; • } • } • } Before state = HUNTING motivated = 1 After state = BLINDED motivated = 0

  31. 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; • } • break; • case BLINDED: • if(!motivated) • state = HIDING; • else • state = HUNTING; • break; • } • } • } Before state = BLINDED motivated = 0 After state = HIDING motivated = 0

  32. 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; • } • break; • case BLINDED: • if(!motivated) • state = HIDING; • else • state = HUNTING; • break; • } • } • } 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?

More Related