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
22
votes
4answers
10k 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 ...
16
votes
9answers
2k 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 ...
14
votes
1answer
30k 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 ...
13
votes
2answers
12k 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
2answers
235 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
1answer
209 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. ...
12
votes
1answer
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
1answer
4k 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
2answers
164 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
5answers
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 ...
10
votes
2answers
293 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
1answer
275 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
2answers
856 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
1answer
865 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
2answers
420 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
2answers
401 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
1answer
460 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 ...
7
votes
2answers
15k 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 ...
7
votes
3answers
908 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: ...
7
votes
1answer
7k 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 \...
7
votes
1answer
1k 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
1answer
269 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
1answer
225 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 ...
7
votes
0answers
303 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?...
6
votes
5answers
248 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 ...
6
votes
4answers
578 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
2answers
231 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
2answers
301 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
1answer
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. ...
6
votes
1answer
53 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
1answer
73 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
1answer
780 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
1answer
753 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
2answers
2k 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
1answer
105 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
1answer
275 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
1answer
251 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 ...
5
votes
2answers
17k 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 ...
5
votes
2answers
115 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
2answers
4k views

State Transition Table

I wrote a transition table which allows the client to: Create States Create a TransitionTable or some kind of ...
5
votes
3answers
4k views

State machine implementation

I have written a simple state machine. Comments and feedback are welcome. ...
5
votes
2answers
189 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
1answer
78 views

Logon state management

I'm in the process of writing a simple text based MUD. First up is making sure that it's possible to create new players / logon with existing players. Since everything is text based, there are ...
5
votes
1answer
85 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 ...
5
votes
1answer
930 views

Model of an mp3 player with state and prototype design patterns

After reading about design patterns in general, I decided to try and incorporate them in an actual program. I chose a State machine for my first exercise, or, more concretely, an MP3 player. The ...
5
votes
0answers
2k views

Reducing complexity in NSView event handling code in Swift

My Cocoa event code always tends to descend into madness, I need to advice on how to reduce complexity. I've spend a few hours putting this example together. CanvasView is a layer hosting subclass of ...
4
votes
1answer
4k 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 ...
4
votes
2answers
47 views

Extract lines under the specified heading

I wrote the following working code, which I just can't stomach: ...
4
votes
3answers
594 views

Finite State Machine in C++

I made a state machine. You can press 0 and 1 to switch between the hypothetical menu and playing state. I hope you can help me improve it further. StateManager.h ...
4
votes
1answer
446 views

Mars Rover problem in J

I've written an implementation of the "famous" Mars Rover simulation, (another question on this) whose problem statement is this: The first line of input is the upper-right coordinates of the ...