Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Extract out tape class in BrainmessShort
  • Loading branch information
michaelgwelch committed Mar 23, 2012
1 parent 746f4e3 commit c75cc6a
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 41 deletions.
1 change: 1 addition & 0 deletions csharp/BrainmessShort/BrainmessShort.csproj
Expand Up @@ -66,6 +66,7 @@
<Compile Include="StringExtensionsTests.cs" />
<Compile Include="Program.cs" />
<Compile Include="ProgramTests.cs" />
<Compile Include="Tape.cs" />
</ItemGroup>
<ItemGroup>
<BootstrapperPackage Include="Microsoft.Net.Client.3.5">
Expand Down
52 changes: 11 additions & 41 deletions csharp/BrainmessShort/Main.cs
Expand Up @@ -6,12 +6,12 @@ namespace BrainmessShort
public class Brainmess
{
private readonly Program _program;

private readonly int[] tape = new int[5000];
private int tc = 2500;
private readonly Tape _tape;

public Brainmess(string programString)
{
_program = new Program(programString);
_tape = new Tape();
}

public static void Main(string[] args)
Expand All @@ -21,37 +21,7 @@ public static void Main(string[] args)
reader.Close();
}

int MoveForward()
{
return tc++;
}

int MoveBackward()
{
return tc--;
}

int Increment()
{
return tape[tc]++;
}

int Decrement()
{
return tape[tc]--;
}

int Current
{
get
{
return tape[tc];
}
set
{
tape[tc] = value;
}
}

public void Run()
{
Expand All @@ -61,31 +31,31 @@ public void Run()
switch(instruction)
{
case '>':
MoveForward();
_tape.MoveForward();
break;
case '<':
MoveBackward();
_tape.MoveBackward();
break;
case '+':
Increment();
_tape.Increment();
break;
case '-':
Decrement();
_tape.Decrement();
break;
case '.':
Console.Write((char)Current);
Console.Write((char)_tape.Current);
break;
case ',':
Current = Console.Read();
_tape.Current = Console.Read();
break;
case '[':
if (Current == 0)
if (_tape.Current == 0)
{
_program.JumpForward();
}
break;
case ']':
if (Current != 0)
if (_tape.Current != 0)
{
_program.JumpBackward();
}
Expand Down
43 changes: 43 additions & 0 deletions csharp/BrainmessShort/Tape.cs
@@ -0,0 +1,43 @@
using System;

namespace BrainmessShort
{
public class Tape
{
private readonly int[] tape = new int[5000];
private int tc = 2500;

public int MoveForward()
{
return tc++;
}

public int MoveBackward()
{
return tc--;
}

public int Increment()
{
return tape[tc]++;
}

public int Decrement()
{
return tape[tc]--;
}

public int Current
{
get
{
return tape[tc];
}
set
{
tape[tc] = value;
}
}
}
}

0 comments on commit c75cc6a

Please sign in to comment.