As of May 31, 2023, we have updated our Code of Conduct.

Questions tagged [state-machine]

A state machine is a model for designing systems which change based upon their current state and what input they receive.

Filter by
Sorted by
Tagged with
24 votes
4 answers
11k views

Simple Turing machine simulator

Yesterday I got a sudden urge to go from writing Python to something, well, more UNIX core-ish. I read some examples on here and decided I might as well put some of that stuff to use to test something ...
A.J.Rouvoet's user avatar
18 votes
10 answers
3k views

Increase difficulty based on score

What I'm trying to do is very straightforward and easy, but I am wondering whether there is a more elegant solution. Basically I have a game and I want the difficulty to increase the moment the ...
async's user avatar
  • 431
15 votes
1 answer
35k views

State pattern + C++ template

This finds its origin in the following reflection. In their book from 1995, the so-called gang of four (GoF) described the state pattern. What they were actually telling us in their description, is ...
14 votes
2 answers
16k views

Very simple finite state machine

I have beginner knowledge of C++ and using that and some self dummy design I came up with the above implementation. The FiniteStateMachine here first asks for the ...
Abhinav Agarwal's user avatar
13 votes
2 answers
282 views

Vending Machine in Idris

Type Driven Development with Idris demonstrates how Idris can be used to build a Finite State Machine for a vending machine. Please critique my implementation. I'm new to dependent types, so I'm not ...
Kevin Meredith's user avatar
12 votes
1 answer
2k views

DFA and NFA in Haskell

I've written a determinstic & non-deterministic finite state machine. I've scrubbed the code quite a bit but I wonder if it could perhaps be scrubbed even more. Suggestions for code clarity, ...
wei2912's user avatar
  • 1,223
12 votes
1 answer
5k views

Robot toy simulator

I have already flunked the test with this code so don't feel bad for cheating any employer. I didn't get any feedback though just wondering what could have gone wrong. You don't need to get into the ...
sethi's user avatar
  • 297
11 votes
1 answer
333 views

NotBF - A Brainfuck-ish like "language"

I've made an interpreted language that's like Brainfuck except it has keywords instead of characters. Here's an explanation of the commands, and how to run it. ...
Ethan Bierlein's user avatar
11 votes
2 answers
201 views

Using state transitions to filter C comments

This is my second attempt at K&R 1-23, Write a program to remove all comments from a C program. Don't forget to handle quoted strings and character constants properly. C comments don't nest....
ivan's user avatar
  • 795
10 votes
3 answers
1k views

The parking machine implementation

I am interesting in the topic of finite automata, so I started over by implementing a parking machine from this video: Computers Without Memory - Computerphile to understand conceptions. Didn't do ...
MiniMax's user avatar
  • 872
10 votes
2 answers
395 views

Excel-formula-analysis state machine

This is one of my first useful scripts. I run it on a highlighted text selection within Notepad++, but I've provided a sample formula and commented out the Scintilla-specific references. I'm a rank ...
Dane's user avatar
  • 223
10 votes
1 answer
8k views

Event-driven finite state machine DSL in Kotlin

I wrote a DSL for defining a finite state machine. My robotics team wants to use a state machine to manage the state of the subsystems on our robot, like the arms, drive train, lifter, etc. Each ...
Roth's user avatar
  • 103
10 votes
1 answer
341 views

Parse regular expression

Task: This function searches given null terminated string pStr by given subset of regular expression pMatch. Return value is ...
loudking's user avatar
  • 101
9 votes
5 answers
3k views

Turing Machine in C#

I've tried designing a Turing Machine model, it works quite well, however I think it could be confusing to maintain on in the future, and I'm not sure what of best practices. Can I get some feedback ...
WizardOfMenlo's user avatar
9 votes
2 answers
1k views

Hierarchical State Machine in F#

I appreciate any feedback on where I might alter or improve my code for this project. This is an old attempt at implementing a Hierarchical State Machine in F#. I'm from a C#/OO background mostly. <...
user38198's user avatar
9 votes
1 answer
1k views

Simple parsing for string formatting (version 2)

This is my second attempt (version 1) at writing a string formatting utility which uses parsing. This time I more explicitly designed it as a finite state machine. I have very little experience with ...
Chris_F's user avatar
  • 347
8 votes
2 answers
475 views

Does this simulation follow abstraction and encapsulation?

This below code implements a simulation of an ocean(as 2d-array of cells) which currently have Shark, fish or be Empty. User can run this simulation, as shown below: ...
overexchange's user avatar
  • 3,321
8 votes
2 answers
16k views

Designing a state machine in C++ using the STL

I have this working code for C++ state machine using STL (not using boost state chart). The purpose of posting is two-fold: To share a simple and working code C++ state machine using the STL for the ...
Shivaram Sitaram's user avatar
8 votes
2 answers
408 views

Playing a game throughout the house

Target I wrote this program in a few free hours when I was on an excursion as "youth leader". I had created a game to explore the house, with a 40-field mechanic, using dice for advancing and ...
Vogel612's user avatar
  • 25.2k
8 votes
3 answers
1k views

Efficient data-driven finite state machine

In creating this answer to a recent question, I thought I might write a fully worked example that implements a finite state machine in C. The purpose is to implement the following function: ...
Edward's user avatar
  • 65k
8 votes
1 answer
9k views

Deterministic finite automaton in Java

Formally, a deterministic finite automaton is a 5-tuple \$M = (Q, \Sigma, \delta, q_0, F)\$, where \$Q\$ is the set of all possible states \$\Sigma\$ is the alphabet \$\delta \colon Q \times \Sigma \...
coderodde's user avatar
  • 26.7k
8 votes
1 answer
641 views

Member function state machine

For various reasons, I wrote this. For one, I don't like to wrap every single function in a separate class, which is too verbose. It's a state machine based on GoTW 57 function pointer wrapping and ...
kert's user avatar
  • 183
8 votes
1 answer
325 views

Designing yet another a state machine

I wanted to design a state machine with a few goals in mind: Encapsulate state logic within individual classes to keep them decoupled. (I'm very curious what the community thinks about this in ...
Tom's user avatar
  • 183
8 votes
1 answer
532 views

Haskell Tic-Tac-Toe (with automation and GUI)

There are already many Tic Tac Toe posts. But as far as I can tell, none of the ones in Haskell are complete with a GUI Here is my implementation with Gloss. Gist Link for convenience ...
Agnishom Chattopadhyay's user avatar
8 votes
0 answers
348 views

STTCL FSM framework based on the GoF State Pattern, Part I: The basic interfaces

Preface I have decided to let my pet project created some years ago to undergo a code review here. The review will be broken into parts according to meta question Multiple reviews or one big review?...
πάντα ῥεῖ's user avatar
7 votes
5 answers
320 views

Simple tokenizer v2 - reading all matching chars at once

I have rewritten my tokenizer according to most of the suggestions from the previous question here. API It now reads all chars as long as they match the pattern. I use three types of attributes to ...
t3chb0t's user avatar
  • 44k
7 votes
1 answer
2k views

Implementation of tokenizer object and finite state machine in C

I was given an assignment to create an implementation of a tokenizer object (by object, I mean a struct with associated functions that operate on instances of the struct). This tokenizer retrieves ...
JRTN's user avatar
  • 111
7 votes
1 answer
2k views

Simulate Turing machine

I have written this code to simulate a Turing machine and wonder what could be improved. In particular, the --tape.end() seems a bit dodgy. ...
Andrew Hickman's user avatar
7 votes
1 answer
305 views

Implementation of interface within state machine

This particular example is verilog, but my question is more about the state machine structuring, which would be relevant to both VHDL and verilog. So if I have a state machine, this one is fairly ...
stanri's user avatar
  • 173
7 votes
1 answer
976 views

Finite State Machine supporting shortest path transitions

In our automated test framework, written in Java 8, there are different entities representing test data, having different states and transitions between them. To model this behavior, I started to ...
stuXnet's user avatar
  • 171
6 votes
4 answers
792 views

State machine for handling Telnet sequences for an FTP server

I've asked Code Climate to generate metrics for the ftpd Ruby gem. It correctly identified the God class; I know what to do about that. But one of the smaller classes has me stumped. This is telnet....
Wayne Conrad's user avatar
  • 3,274
6 votes
2 answers
20k views

Designing simple state machine in C++

I have designed a simple state machine for the following diagram: I am seeking improvements from the community. I especially don't like two things: When calling ...
Adrian's user avatar
  • 275
6 votes
2 answers
5k views

State Transition Table

I wrote a transition table which allows the client to: Create States Create a TransitionTable or some kind of ...
Dioxin's user avatar
  • 424
6 votes
2 answers
342 views

Finite state automata

This is a non-deterministic finite state automata (NFA) meant to be used with a Regex engine. To show some example usage, suppose you wanted to construct an NFA for the regex ...
User319's user avatar
  • 1,132
6 votes
2 answers
349 views

Design feedback for automaton to draw a checkerboard pattern

I've just completed the 3rd task of the 1st assignment offered by Stanford's programming methodologies (see the full resources for it here too). I just need some helpful feedback on how my code can ...
user3025998's user avatar
6 votes
1 answer
3k views

C++ CLI Console Menu Statemachine

I am trying to find the best way(/most efficent way) to setup my console program's menu. The way I have it set up is that I have a Menu class and then I use a menu pointer called currentMenu and I ...
Samuel Habtu's user avatar
6 votes
1 answer
2k views

Toy Robot Simulator

I know this code worked as I've tested it with the case tests commented below in the code, and even added a few of my own, but the developers who were testing me came back with the following critiques:...
Eliseo D'Annunzio's user avatar
6 votes
1 answer
110 views

Hardware Clock Divider

I have created a clock divider that works as follows: If the division factor is 0, pass the clock through unchanged. Otherwise, flip the output clock signal after the specified number of input clock ...
rlee827's user avatar
  • 313
6 votes
1 answer
93 views

Arduino controller to control the backlighting of a gear selector

Background The gear selector on a car allows the driver to select which state the transmission should be in. In the example picture, one of the five letters (P, R, N, D & S) lights up to ...
Zaid's user avatar
  • 163
6 votes
1 answer
81 views

F# pattern matching to determine the state of a bank account based on past events

I am writing an event sourced system in which I need to have the state based on the events that already occurred for a given bank account. The events are defined such as: ...
Natalie Perret's user avatar
6 votes
1 answer
1k views

A variant of state pattern + C++ template FSM without state creation/destruction penalty

This is a variant of the solution posted here, without state creation/destruction penalty. Note that my comment in the original question about the simplification provided by C++11's inherited ...
nilo's user avatar
  • 507
6 votes
2 answers
3k views

Simple ini file parser

The below is a C++ parser for a reasonably simple INI grammar (although my understanding is that there isn't an official spec as such). My grammar is roughly: ...
cloakedlearning's user avatar
6 votes
1 answer
126 views

Input processing/state machine function

I'm making a Mario clone in pygame and I have just wrote a function proccesses inputs and acts as a state machine for Mario. Everything is working just as I want it, but I was wondering if you can ...
Robbie's user avatar
  • 253
6 votes
1 answer
342 views

Automata theory, state reachability in Haskell

I wrote a small library to learn Haskell while studying Automata theory. I have some concerns on my algorithm to evaluate state reachability. That is, I think it might be improved if I can avoid ...
bigstones's user avatar
  • 177
6 votes
1 answer
401 views

Chat room using GenServer

It seems odd that state must be passed around all the time. Can't State module hold itself, so I could do something like ...
roberts's user avatar
  • 61
6 votes
0 answers
193 views

Character-based transitions (part of a lexer)

As part of my prep for the Code Review community-challenge (which looks like it will be Write your own programming language), I've been working on an LL(1) lexer generator in Rust. Yes, lexer ...
CAD97's user avatar
  • 1,934
5 votes
2 answers
128 views

Finding if there exists an input for a given automata for which no matter what the starting state is, final state would be same everytime

I am solving a puzzle (Finding if there exists an input for a given automata for which no matter what the starting state is, final state would be same everytime) and have written following Python code....
h_t's user avatar
  • 51
5 votes
3 answers
5k views

State machine implementation

I have written a simple state machine. Comments and feedback are welcome. ...
cslrnr's user avatar
  • 275
5 votes
2 answers
571 views

Alternative to nonlocal for local variables in a function with nested functions in Python

Introduction for future readers I posted the code below in the hope that it would be a good example to discuss the topic "Alternative to nonlocal for local variables in a function with nested ...
nilo's user avatar
  • 507
5 votes
1 answer
132 views

UTF8 Codepoint decode and length v.2

This is the complete rewrite of a function to decode an UTF-8 codepoints and return the encoding length (or 0 if the codepoint is ...
Remo.D's user avatar
  • 335