Getting StartedGiving Commands to ObjectsPrograms 1Programs 2CalculatorVariables: Remembering StuffStrings 1Strings 2InputIfWhile: Loops and Repeating ThingsRandom Numbers and ChooseFSM: The Finite State Machine
FSM: The Finite State Machine

Crazy picture of a scary Finite State Machine with widgets everywhere and lots of scientists tinkering with it or something

A finite state machine isn't a crazy type of machine. A finite state machine is one way to write programs. A finite state machine is usually just called a FSM.

FSM: You see a fly: squish it? run away?

A FSM is made up of two things. First, it has some writing about what's happening. Then, it has some arrows that show different choices you can make. You should make a choice and follow the arrow.

Giant FSM--with numbered states?

FSMs are good for making games and stories. Here is one game where you have to find some treasure. First, go to Start. Then, you just follow the arrows until you get to the end.

3-state FSM for a house

Here is a simpler FSM that lets you explore the rooms of a house.

Numbering the FSM states

Let's try making a program that lets you explore the house. First, number all of the rooms.

room = 1;

Now we'll start programming. When programming a FSM, you need the computer to know which room you are in. You should start in room 1.

room = 1;
  if (room == 1) {
    show("You are in the living room");
  } else if (room == 2) {
    show("You are in the bedroom");
  } else if (room == 3) {
    show("You are in the kitchen");
  }

The computer should tell you something different depending on which room you are in.

room = 1;
  if (room == 1) {
    show("You are in the living room");
    a = input("Type 1 to go down or 2 to go right");
    if (a == "1") {room = 2;}
    else if (a == "2") {room = 3;}
  } else if (room == 2) {
    show("You are in the bedroom");
    a = input("Type 1 to go left or 2 to go down");
    if (a == "1") {room = 1;}
    else if (a == "2") {room = 3;}
  } else if (room == 3) {
    show("You are in the kitchen");
    a = input("Type 1 to go up and left or 2 to go down and right");
    if (a == "1") {room = 1;}
    else if (a == "2") {room = 2;}
  }

The computer should also ask you where you want to go. The computer should then change the room number depending on your answer.

room = 1;
while(true) {
  if (room == 1) {
    show("You are in the living room");
    a = input("Type 1 to go down or 2 to go right");
    if (a == "1") {room = 2;}
    else if (a == "2") {room = 3;}
  } else if (room == 2) {
    show("You are in the bedroom");
    a = input("Type 1 to go left or 2 to go down");
    if (a == "1") {room = 1;}
    else if (a == "2") {room = 3;}
  } else if (room == 3) {
    show("You are in the kitchen");
    a = input("Type 1 to go up and left or 2 to go down and right");
    if (a == "1") {room = 1;}
    else if (a == "2") {room = 2;}
  }
}

After your room has changed, the computer should go back to the beginning and tell you about the new room. You can use a loop for this.

Sample interaction with the FSM

That's it! You can run the program now. You will be able to move through the different rooms.

Someone drawing a FSM

Try taking the FSM for the treasure game and programming it into the computer. Then, try making your own FSM game.

Programming Basics

Programming Basics: Beginners

E-mail | About | Privacy Policy