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

Slides:



Advertisements
Similar presentations
Java Control Statements
Advertisements

Control Structures.
Switch code for Lab 4.2 switch (input) { /* input is a variable that we will test. */ case 'M': printf("The prefix is equal to 1E6.\n"); break; case 'k':
Introduction to C Programming
Recitation 1 Session 5 + handout Ishani Chakraborty.
Introduction to Assembly language
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Lab7: Introduction to Arduino
Introduction to C# Erick Pranata © Sekolah Tinggi Teknik Surabaya 1.
1 Java Programming Basics SE-1011 Dr. Mark L. Hornick.
C# Console Application
CS 4800 By Brandon Andrews.  Specifications  Goals  Applications  Design Steps  Testing.
1 Conditionals In many cases we want our program to make a decision about whether a piece of code should be executed or not, based on the truth of a condition.
Writing algorithms using the while-statement. Previously discussed Syntax of while-statement:
UNIT II Decision Making And Branching Decision Making And Looping
ENGG1100 Lecture7: Introduction To Engineering Design (Digital Logic) Part 2 Kin Hong Wong ENGG1100. Ch7-Digital Logic (part 2) 16/02/15 1.
ENGG1100 Introduction to Engineering Design Digital Logic (Part 2) Prof. Kin Hong Wong Department of Computer Science and Engineering.
J-1 University of Washington Computer Programming I Switch Statement © 2000 UW CSE.
CP104 Introduction to Programming Selection Structures_3 Lecture 11 __ 1 The Switch Statement The switch statement provides another means to select one.
The switch StatementtMyn1 The switch Statement Sometimes there can be a multiple-choice situation, in which you need to execute a particular set of statements.
CIS 068 Welcome to CIS 068 ! Stacks and Recursion.
1 Relational Expressions Relational expressions: –Expressions that compare operands –Sometimes called conditions –Evaluated to yield a result –Typically.
CSE 251 Dr. Charles B. Owen Programming in C1 States and State Machines.
1 Game AI Finite State Machine. Finite State Machine (FSM) is the most commonly used Game AI technology Finite State Machine (FSM) is the most commonly.
1 Debugging. 2 A Lot of Time is Spent Debugging Programs Debugging. Cyclic process of editing, compiling, and fixing errors. n Always a logical explanation.
1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture of a compiler PART II:
Programming Design ROBOTC Software Principles of Engineering
1-Dec-15 Additional control structures. 2 The if-else statement The if-else statement chooses which of two statements to execute The if-else statement.
1 Enhancing Program Comprehension with recovered State Models Stéphane S. Somé Timothy C. Lethbridge SITE, University of Ottawa.
CSC 107 – Programming For Science. Science Means Solving Problems  Physics – How does an atom work?
1 Chapter 3: Loops and Logic. 2 Control Statements If statement Example NumberCheck.java Relational operators (, >=, ==, !=) Using code blocks with If.
Dr Nick Mitchell (Room CM 224)
INTRO TO STATE MACHINES CIS 4350 Rolf Lakaemper. State Machines A state machine represents a system as a set of states, the transitions between them,
Finite State Machines using Alice Stephen Cano CIS 4914 Senior Project Wednesday December 5th.
JavaScript JavaScript ( Condition and Loops ). Conditional Statements If Statement If...else Statement if (condition) { code to be executed if condition.
C syntax (simplified) BNF. Program ::= [ ] Directives ::= [ ] ::= | |… ::=#include > ::=#define.
Presentation By :- Nikhil R. Anande ( ) Electronic & Communication Engineering. 3 nd Year / 5 th Semester FACULTY GUIDE : RAHIUL PATEL SIR MICROCONTROLLER.
1 COMP541 Sequential Logic – 2: Finite State Machines Montek Singh Feb 29, 2016.
Math Expression Evaluation With RegEx and Finite State Machines.
Programming Design ROBOTC Software. Behavior-Based Programming A behavior is anything your robot does –Turning on a single motor or servo Three main types.
Programming Design ROBOTC Software Principles Of Engineering
CHAPTER 4 DECISIONS & LOOPS
© 2016, Mike Murach & Associates, Inc.
Compound Condition Break , Continue Switch Statements in Java
Unit-1 Introduction to Java
Control Structures.
Problem Solving and Programming CS140: Introduction to Computing 1 8/21/13.
2.0 FUNDAMENTALS OF JAVA PROGRAMMING LANGUAGE
Finite State Machines Computer theory covers several types of abstract machines, including Finite State Machines.
Sensors Training.
Switch Statements, Do While, Break, Continue, Goto, Comma Operator
الكلية الجامعية للعلوم التطبيقية
David S. Touretzky Carnegie Mellon University
While Loops and If-Else Structures
Carnegie Mellon Wrap-up of Machine-Level Programming II: Control : Introduction to Computer Systems Sept. 18, 2018.
Series Circuits Lesson 8.
محاضرة 1: مقدمة للمسـاق و مراجعـة للأساسيـات
Lecture Notes – Week 3 Lecture-2
The switch statement: an alternative to writing a lot of conditionals
Finite State Machines in Games
Topics: Programming Constructs: loops & conditionals Digital Input
Programming Design ROBOTC Software Principles Of Engineering
Guest Lecture by David Johnston
CSE 373 Data Structures and Algorithms
Programming Fundamentals (750113) Ch1. Problem Solving
if-else Structures Principles of Engineering
Programming Fundamentals (750113) Ch1. Problem Solving
There many situations comes in real life when we need to make some decisions and based on these decisions, we decide what should we do next. Similar situations.
Welcome back to Software Development!
Lesson 3. Controlling program flow. Loops. Methods. Arrays.
Presentation transcript:

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

So I hear you like Graph Theory.

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

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

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.

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”); }

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

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

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; }

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

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

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

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

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

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

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

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

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

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

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;

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

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

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

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

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

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

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

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

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

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

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

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

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