# Finite State Machines

[Finite State Machines](<https://gameprogrammingpatterns.com/state.html#finite-state-machines-to-the-rescue >) are one of the most frequently used State patterns for games because they are one of the most **simple** ways to represent and implement logic for event-driven systems. A Finite State Machine is model of a dynamic, event-driven system. FSM's allow for simplification of the logic of complex event-driven systems. The simplification comes from the structure of the FSM which is specified below.\
\
**Finite State Machine** model specifies that the event-driven-system can be represented by:

* a finite **set of states - represented by nodes**
* a finite **set of events** that cause the system to change state&#x20;
* a well-defined set of **state-event transitions (event-arrows)**&#x20;
* **memory to keep track of the current state** of the system
* identification of the **starting state**
* at any point in time, the **system must be in one**, **and only one state** of the system.

The benefit of using an FSM to model a system is that once a system has been represented using a FSM format, then it is straightforward to write code to implement logic of the system. &#x20;

An FSM is often represented as a **diagram with nodes** to represent each state and **arrows between nodes to represent valid transition events**.  A table can also be used to represent the same logic.

## **Finite State Machines**&#x20;

The image below shows a **Finite State Machine** diagram for managing **Event-Driven changes in GameState** which correspond to **Level-Change Events** for the MiniGame used in Project 3.

![](/files/-MFWLzityMIwkP1nS5fN)

![Event-Driven Logic for Project 3, Corresponds to FSM Above](/files/-MFb1P01dPUfc2Ams3t-)

Throughout this course we will look at the fundamental computing structures associated with games and stories.  The online book listed below "Game Programming Patterns" is a resource we'll use throughout the course. &#x20;

In computer science, **programming patterns** are an important way to understand and communicate about common abstractions. &#x20;

### **The State Pattern defines the behavior of a dynamic object.**

> Allow an object to alter its behavior when its internal state changes. The object will appear to change its class.&#x20;

{% embed url="<https://gameprogrammingpatterns.com/state.html#finite-state-machines-to-the-rescue>" %}

As discussed in the book shown below, a useful way to think about games is to consider that they can be represented as a hierarchy of "looping systems."

**In Computer Science, such looping systems are called State Machines.** &#x20;

{% embed url="<https://gameprogrammingpatterns.com/game-loop.html>" %}

![Game-Loops: Image below from Michael Sellers, Advanced Game Design ](/files/-M1_bVgTGaCAfekp-L1c)

{% embed url="<https://learning.oreilly.com/library/view/advanced-game-design/9780134668185/ch2.xhtml#ch2>" %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://kdoore.gitbook.io/cs2335/game-coding-structures/finite-state-machines.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
