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.
139
questions
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 ...
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 ...
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 ...
13
votes
2
answers
283
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 ...
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, ...
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 ...
11
votes
1
answer
337
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.
...
11
votes
2
answers
202
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....
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 ...
10
votes
2
answers
401
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 ...
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 ...
10
votes
1
answer
357
views
Parse regular expression
Task:
This function searches given null terminated string pStr by given subset of
regular expression pMatch.
Return value is ...
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 ...
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.
<...
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 ...
8
votes
2
answers
476
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:
...
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 ...
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 ...
8
votes
3
answers
2k
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:
...
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 \...
8
votes
1
answer
657
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 ...
8
votes
1
answer
330
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 ...
8
votes
1
answer
549
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
...
8
votes
0
answers
349
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?...
7
votes
5
answers
332
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 ...
7
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 ...
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 ...
7
votes
1
answer
3k
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.
...
7
votes
1
answer
310
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 ...
7
votes
1
answer
991
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 ...
6
votes
4
answers
809
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....
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 ...
6
votes
2
answers
347
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 ...
6
votes
2
answers
352
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 ...
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 ...
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:...
6
votes
1
answer
111
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 ...
6
votes
1
answer
94
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 ...
6
votes
1
answer
82
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:
...
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 ...
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:
...
6
votes
1
answer
130
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 ...
6
votes
1
answer
345
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 ...
6
votes
1
answer
403
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
...
6
votes
0
answers
194
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 ...
5
votes
2
answers
130
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....
5
votes
3
answers
5k
views
State machine implementation
I have written a simple state machine. Comments and feedback are welcome.
...
5
votes
2
answers
606
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
...
5
votes
1
answer
135
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 ...