Skip to content

Instantly share code, notes, and snippets.

@jmcph4
Created January 10, 2015 14:38
Show Gist options
  • Select an option

  • Save jmcph4/8db906ecbf7ba100a236 to your computer and use it in GitHub Desktop.

Select an option

Save jmcph4/8db906ecbf7ba100a236 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <vector>
// https://upload.wikimedia.org/wikipedia/en/7/76/Busy_Beaver_1.JPG
class Turing
{
public:
std::vector<bool> tape; // our tape, with an alphabet of {0,1}
int state; // the machine's current state
Turing::Turing()
{
// initialising the tape the lazy way
this->tape.push_back(false);
this->tape.push_back(false);
this->tape.push_back(false);
this->tape.push_back(false);
this->tape.push_back(false);
this->tape.push_back(false);
this->tape.push_back(false);
this->tape.push_back(false);
this->tape.push_back(false);
this->tape.push_back(false);
this->tape.push_back(false);
this->tape.push_back(false);
this->state = 3; // starting state
}
int compute(int i)
{
if(i < this->tape.size()) // bounds check
{
if(this->state == 0) // halt
{
return 0;
}
else if(this->state == 1) // state A
{
if(this->tape[i] == false)
{
this->tape[i] = true;
this->state = 2;
compute(++i); // move right
}
else if(this->tape[i] == true)
{
this->state = 3;
compute(--i); // move left
}
}
else if(this->state == 2) // state B
{
if(this->tape[i] == false)
{
this->tape[i] = true;
this->state = 1;
compute(--i);
}
else if(this->tape[i] == true)
{
this->state = 2;
compute(++i);
}
}
else if(this->state == 3) // state C
{
if(this->tape[i] == false)
{
this->tape[i] = true;
this->state = 2;
compute(--i);
}
else if(this->tape[i] == true)
{
this->state = 0;
compute(i);
}
}
}
}
};
int main(void)
{
int i = 0;
Turing t;
t.compute(0); // go!
// print tape after operations performed
for(i=0;i<t.tape.size();i++)
{
std::cout << t.tape[i] << std::endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment