Any explanations appropriate for a 5 year old?
For further actions, you may consider blocking this person and/or reporting abuse
Any explanations appropriate for a 5 year old?
For further actions, you may consider blocking this person and/or reporting abuse
Torequl Islam -
DevvEmeka -
転職カメ -
Ian -
Top comments (5)
I'm not sure if this would be a good way, but I will try anyways :
Think of game of snakes/chutes and ladders - and that is a state machine. You roll a dice and whatever comes on top, you move your piece by that much number. The piece will move from one square, to some other square depending on what was on that dice. Now sometimes, one can change the square without a dice, just because they are on that square, for example when the encounter snake/chute or when they encounter a ladder.
Now Imagine starting to watch a game being played some time after it has started. One of the pieces is on square number 11, but we have no way of telling how it got there. Maybe player rolled a five then a six, or maybe they rolled a five, another five and then a one, or maybe they are there because a snake/chute on 21 got them back to 6 and then they rolled 5. But in the game it doesn't matter how they got there, what matter, is that players have fun.... and also what players roll next on the dice, as the next movement depends only on that.
State machines are like that, too. A machine's state (position of piece) changes depending only on what is the next input (what comes next on dice), and there may be multiple ways of reaching a state (How it reached square 11), but which way it used does not affect its next transition (move). There can be ways to change state just because one is in a particular state, using epsilon transitions (one can move squares because they encountered snake/chute or ladder, without rolling dice). And in the end important thing is that players have fun (I think it is important 😄)
I think it would be possible to explain using hopscotch as it literally has jumping from one state to another, but my knowledge of that is non-existent 😅
Hope this is useful !
How about a stop light. The lights change from state to another; always following the same rules of when to change. But let's do an example:
There's a Red light, a Yellow light, and a Green light where a big road and a little road cross.
We want the big road to always have a Green light so everyone can keep going. Except sometimes when a car stops on the little road we'll want to change the light so they can go too. And we don't want someone to have to stand by the intersection all day to change the lights so we'll use a machine; a State Machine!
Let's first think about the different states a stop light can be in. We don't want both lights to be Green! The only safe ones are:
These are the states for our machine. But we aren't done, we need to decide when the light should change. Remember we wanted the big road to usually have a Green light; so let's start with that light Green and the other one Red; Green/Red (the first state).
The only time we want to change the light from Green/Red is when a car comes on the little road. When that happens we'll turn the big roads light Yellow to tell those people to slow down by changing to the Yellow/Red state.
After 5 seconds we'll change all the lights to Red to make sure everyone is safe; Red/Red. After another second we'll turn the little roads light Green to let them across; Red/Green.
After 10 seconds they've probably made it across, so let's start changing the light back by making it Red/Yellow. But wait; there's a little problem, do you see it? We need to change the light to Red/Red, but the rule for our Red/Red state is to go to Red/Green after 1 second. If we did that then the big road wouldn't get their turn to go! To remember whose turn is next we need a different state than our first Red/Red; both states have the same lights turned on (Red and Red) but they have a different rule: The first goes to Red/Green and the second goes to Green/Red after 1 second.
Here's a little picture of our "State Machine":
I only know about finite state machines, so I'll do that
Imagine a super cool robot transformer that can change to a few shapes. To change from one shape to the next you use a voice command (how cool is that). It can have a human-like form, it can be an animal, a car, and there is this secret command that makes it take the shape of a bucket. At any given time it can only have one shape, if it's a car, is car; if it's an animal, it is an animal. It can't be half car half animal, that's not a thing. To prevent any sort of weird invalid shapes, the designers made it so that you can only transform from human-like to one of the other forms (car or animal or bucket). And when the robot is an animal or car, it can only change to the human form. What's with the bucket thing? Okay, the reason it's a secret is because when the robot changes to a bucket it can't listen to any command, so you can't make it human again (why? I don't know. I didn't design the toy).
This robot is a finite state machine. At any given time it can only be in one state (the "shape"). It has specific transitions from one state to the other (human -> thing or thing -> human). And this transitions are triggered by events, the voice commands. The bucket thing? Well, if it can't listen to any event is not going to trigger any transitions (it also needs those transitions, even if you could listen to an event, but you have no transitions, you're still stuck). So if it gets to the bucket, that is going to be it's final state.
I bet someone here can make an xstate diagram representation thingy of the robot.
Edit: I almost forgot. I have post about finite state machines, it's mostly me playing around with a javascript library, but I think it could help someone (maybe a very smart 5 years old):
Taking a look at finite state machines
Are you hungry? When you are hungry, what do you do? You eat some bread, maybe drink some milk, right? Then are you full? Yes, you are? Good. But you don't feel hungry and full at the same time, right? Yeah. The feelings you have, you feel hungry or full, but not both at the same time, we can call them states. S-t-a-t-e, states. The things you do to change your feelings, like eating or drinking, we can call them transitions. But if you don't eat, you will become hungry again, right? But what are you doing when you are not eating? Does doing nothing also change how you feel? Yes? No, because you are not really doing nothing. Your stomach absorbs the food you eat. So only doing something changes how you feel, right? Doing nothing doesn't change how you feel. We call this only transitions can change the state. See, you are a little state machine! Yes, yes, you are not a machine, you are a child, not a robot. But we want to make robots which work like human, right? So they can help you do the housework, right? We describe how human do housework with this tool called state machine, just like you draw a house with the crayons. This machine can't work yet, like you can't live in your house on the paper. You can? Can you invite me into the house then? No? That's a pity, but don't worry. There are people who can look at your house, and build one on the ground that we can visit together. They are called engineers. There are also people who can look at the state machine, and build a robot that helps you do some housework! They are also called engineers. Do you want to become an engineer? No? What do you want to do? You like cooking? ...
If it's rainy you may want to
carry an umbrella
not water the garden
not wear sunglasses
play indoors
If it's sunny you may want to
use suncream
wear sunglasses
water the garden
play outside
You have become a state machine because what you choose to do depends on the state of the weather
That is a state machine explained to a 5 year old