patterncsharpMinor
Turing Machine in C#
Viewed 0 times
turingmachinestackoverflow
Problem
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 on my code?
Turing Machine Namespace
```
using System;
using System.Collections.Generic;
namespace TuringMachineNS
{
public class TuringMachine
{
byte[] tape;
public bool hasTerminated;
int currentIndex;
int currentCard;
List cards;
public TuringMachine() : this(500) { }
public TuringMachine(int size) : this(size, null) { }
public TuringMachine(int size, List cards)
{
this.hasTerminated = false;
this.tape = new byte[size];
this.currentIndex = tape.Length / 2;
this.cards = cards;
this.currentCard = 1;
}
public void NextInstruction()
{
if (this.currentCard == 0)
{
hasTerminated = true;
return;
}
if (this.currentIndex > this.tape.Length || this.currentIndex < 0)
{
hasTerminated = true;
return;
}
int value = this.tape[this.currentIndex];
Instruction Inst = this.cards[this.currentCard].GetInstruction(value);
this.ExecuteInstruction(Inst);
}
private void ExecuteInstruction(Instruction inst)
{
if (inst.ActionInst == RWInstructions.WRITE0)
{
this.tape[currentIndex] = 0;
}
else if (inst.ActionInst == RWInstructions.WRITE1)
{
this.tape[currentIndex] = 1;
}
if (inst.MovementInst == LRInstructions.MOVELEFT)
{
this.currentIndex--;
}
else if(inst.MovementInst == LRInstructions.MOVERIGHT)
{
this.currentIndex++;
}
Turing Machine Namespace
```
using System;
using System.Collections.Generic;
namespace TuringMachineNS
{
public class TuringMachine
{
byte[] tape;
public bool hasTerminated;
int currentIndex;
int currentCard;
List cards;
public TuringMachine() : this(500) { }
public TuringMachine(int size) : this(size, null) { }
public TuringMachine(int size, List cards)
{
this.hasTerminated = false;
this.tape = new byte[size];
this.currentIndex = tape.Length / 2;
this.cards = cards;
this.currentCard = 1;
}
public void NextInstruction()
{
if (this.currentCard == 0)
{
hasTerminated = true;
return;
}
if (this.currentIndex > this.tape.Length || this.currentIndex < 0)
{
hasTerminated = true;
return;
}
int value = this.tape[this.currentIndex];
Instruction Inst = this.cards[this.currentCard].GetInstruction(value);
this.ExecuteInstruction(Inst);
}
private void ExecuteInstruction(Instruction inst)
{
if (inst.ActionInst == RWInstructions.WRITE0)
{
this.tape[currentIndex] = 0;
}
else if (inst.ActionInst == RWInstructions.WRITE1)
{
this.tape[currentIndex] = 1;
}
if (inst.MovementInst == LRInstructions.MOVELEFT)
{
this.currentIndex--;
}
else if(inst.MovementInst == LRInstructions.MOVERIGHT)
{
this.currentIndex++;
}
Solution
I would store the value to write directly in the
I would also store the
It is very likely that the tape would need to grow. If the tape can't grow then you technically don't have a turing machine.
Also it may be handy to be able to initialize the state of the tape before starting.
Instruction. That way you don't need the if-else in ExecuteInstruction. Instead it's just an assignment.this.tape[currentIndex] = inst.ValueToWrite;I would also store the
NextCard value directly. This requires ControlCard to be a reference type but that's already the case. You do need to pass which state is the final state.It is very likely that the tape would need to grow. If the tape can't grow then you technically don't have a turing machine.
Also it may be handy to be able to initialize the state of the tape before starting.
Code Snippets
this.tape[currentIndex] = inst.ValueToWrite;Context
StackExchange Code Review Q#108762, answer score: 8
Revisions (0)
No revisions yet.