Skip to content

Instantly share code, notes, and snippets.

Created June 2, 2014 20:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/dbaa15928ee5cd2136b6 to your computer and use it in GitHub Desktop.
Save anonymous/dbaa15928ee5cd2136b6 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <vector>
#include <math.h>
#include <time.h>
#include <stdlib.h>
enum Dir{LEFT, RIGHT, UP, DOWN};
const int MAX_HEIGHT = 25;
const int MAX_WIDTH = 80;
std::vector<long> inputStack;
int inputPlace;
//Function will pop the stack and return
//what was popped. If array is empty it will
//return 0.
long Pop(std::vector<long> &stack)
{
if(stack.empty()) return 0;
else
{
long pop = stack.back();
stack.pop_back();
return pop;
}
}
long NextInput()
{
if(inputPlace < inputStack.size())
{
long num = inputStack[inputPlace];
inputPlace++;
return num;
}
else return 0;
}
//this moves the counter for the program
//forward based on its Dir.
void Move(int &xPos, int &yPos, Dir dir)
{
//move the counter of where we are in program forward
//if whichever direction it should go
if(dir == RIGHT) xPos++;
else if(dir == LEFT) xPos--;
else if(dir == DOWN) yPos++;
else if(dir == UP) yPos--;
//make sure that the pointer isn't now outside of
//its bounds, if it is make it start at the oppisate side
if(xPos <0)
{
xPos = MAX_WIDTH-1;
//yPos--;
}
else if(xPos >= MAX_WIDTH)
{
xPos = 0;
//yPos++;
}
if(yPos <0) yPos = MAX_HEIGHT-1;
else if(yPos >= MAX_HEIGHT) yPos = 0;
}
//These are the functions that can happen in Befunge
void Plus(std::vector<long> &stack)
{
stack.push_back(Pop(stack) + Pop(stack));
}
void Minus(std::vector<long> &stack)
{
long a = Pop(stack);
stack.push_back(Pop(stack) -a);
}
void Multi(std::vector<long> &stack)
{
stack.push_back(Pop(stack) * Pop(stack));
}
void Divide(std::vector<long> &stack)
{
long a = Pop(stack);
while(a == 0)
{
std::cout <<"What number do you want to divide by? Not 0." << std::endl;
std::cin >> a;
}
stack.push_back((Pop(stack)/(long)a));
}
void Modulo(std::vector<long> &stack)
{
long a = Pop(stack);
while(a == 0)
{
std::cout <<"What number do you want to modulate by? Not 0." << std::endl;
std::cin >> a;
}
stack.push_back(Pop(stack)%(long)a);
}
void Not(std::vector<long> &stack)
{
if(Pop(stack))
stack.push_back(1);
else
stack.push_back(0);
}
void GreaterThan(std::vector<long> &stack)
{
char a = Pop(stack);
if(Pop(stack) > a) stack.push_back(1);
else stack.push_back(0);
}
void Right(Dir &dir)
{
dir = RIGHT;
}
void Left(Dir &dir)
{
dir = LEFT;
}
void Up(Dir &dir)
{
dir = UP;
}
void Down(Dir &dir)
{
dir = DOWN;
}
void RandDir(Dir &dir)
{
dir = Dir(rand() % 4);
}
void PopMoveRL(std::vector<long> &stack, Dir &dir)
{
if(Pop(stack) == 0) dir = RIGHT;
else dir = LEFT;
}
void PopMoveUD(std::vector<long> &stack, Dir &dir)
{
if(Pop(stack) == 0) dir = DOWN;
else dir = UP;
}
void StringPush(std::vector<long> &stack, int &xPos, int &yPos, char input[MAX_HEIGHT][MAX_WIDTH], Dir &dir)
{
Move(xPos, yPos, dir);
while(input[yPos][xPos] != '"')
{
stack.push_back(input[yPos][xPos]);
Move(xPos, yPos, dir);
}
}
//duplicates the top of the stack(which is
//the back of a vector
void Duplicate(std::vector<long> &stack)
{
char temp = Pop(stack);
stack.push_back(temp);
stack.push_back(temp);
}
//switches the top 2 values on the stack
void SwitchTop(std::vector<long> &stack)
{
long top = Pop(stack);
long under = Pop(stack);
stack.push_back(top);
stack.push_back(under);
}
void Discard(std::vector<long> &stack)
{
Pop(stack);
}
void PopInt(std::vector<long> &stack)
{
std::cout << Pop(stack);
}
void PopChar(std::vector<long> &stack)
{
std::cout << (char)Pop(stack);
}
void Tramplone(std::vector<long> &stack, int &xPos, int &yPos, Dir dir)
{
Move(xPos, yPos, dir);
}
void Put(std::vector<long> &stack, char input[MAX_HEIGHT][MAX_WIDTH])
{
long x,y;
char v;
y = Pop(stack);
x = Pop(stack);
v = (char)Pop(stack);
input[y][x] = v;
}
void Get(std::vector<long> &stack, char input[MAX_HEIGHT][MAX_WIDTH])
{
long x,y;
y = Pop(stack);
x = Pop(stack);
stack.push_back(input[y][x]);
}
void AskInt(std::vector<long> &stack)
{
long num;
num = NextInput();
stack.push_back(num);
}
void AskChar(std::vector<long> &stack)
{
char inp;
inp = (char)NextInput();
stack.push_back(inp);
}
void EndProgram()
{
}
void RunProgram(char input[MAX_HEIGHT][MAX_WIDTH])
{
std::vector<long> stack;
int inputX = 0;
int inputY = 1;
char command;
Dir currDir = RIGHT;
while(0==0){
command = input[inputY][inputX];
switch(command)
{
case '+':
Plus(stack);
break;
case '-':
Minus(stack);
break;
case '*':
Multi(stack);
break;
case '/':
Divide(stack);
break;
case '%':
Modulo(stack);
break;
case '!':
Not(stack);
break;
case '`':
GreaterThan(stack);
break;
case '>':
Right(currDir);
break;
case '<':
Left(currDir);
break;
case '^':
Up(currDir);
break;
case 'v':
Down(currDir);
break;
case '?':
RandDir(currDir);
break;
case '_':
PopMoveRL(stack, currDir);
break;
case '|':
PopMoveUD(stack, currDir);
break;
case '"':
StringPush(stack, inputX, inputY, input,currDir);
break;
case ':':
Duplicate(stack);
break;
case '\\':
SwitchTop(stack);
break;
case '$':
Discard(stack);
break;
case '.':
PopInt(stack);
break;
case ',':
PopChar(stack);
break;
case '#':
Tramplone(stack, inputX, inputY, currDir);
break;
case 'p':
Put(stack, input);
break;
case 'g':
Get(stack, input);
break;
case '&':
AskInt(stack);
break;
case '~':
AskChar(stack);
break;
case '@':
return;
break;
}
if((int)command >= 48 && (int)command <= 57)
{
stack.push_back(command-48);
}
Move(inputX, inputY, currDir);
}
}
std::vector<std::string> SplitString(std::string string, std::string splitChars)
{
int beg = 0;
std::vector<std::string> splitStrings;
int i =0;
while(i < string.length())
{
bool split = false;
for(int j =0; j < splitChars.length(); j++)
{
if(string[i] == splitChars[j] || i == string.length() -1)
{
split = true;
break;
}
}
if(split)
{
std::string sub = string.substr(beg, i-beg);
if(sub.length() == 1)
{
bool dontAdd = false;
for(int j =0; j < splitChars.length(); j++)
{
if(sub[j] == splitChars[j])
{
dontAdd = true;
}
}
if(!dontAdd)
{
splitStrings.push_back(sub);
beg = i+1;
}
}
else
{
splitStrings.push_back(sub);
beg = i+1;
}
}
i++;
}
return splitStrings;
};
int main(int argc, const char * argv[])
{
srand (time(NULL));
std::vector<std::string> inputs;
char program[MAX_HEIGHT][MAX_WIDTH];
for(int i = 0; i < MAX_HEIGHT; i++)
{
for(int j = 0; j < MAX_WIDTH; j++)
{
program[i][j] = ' ';
}
}
char progLine[MAX_WIDTH];
char inputChar[256];
int PROGRAM_LAST_LINE;
if(argc > 1)
{
FILE * file;
file = fopen(argv[1], "r");
if(file != NULL)
{
if(fgets(inputChar,256, file) != NULL)
{
std::string inp = inputChar;
inputs = SplitString(inp, " ");
for(int i =0; i < inputs.size(); i++)
{
inputStack.push_back(atoi(inputs[i].c_str()));
}
int i = 0;
while(i < MAX_HEIGHT)
{
if(fgets(progLine, MAX_WIDTH, file) != NULL)
{
int j = 0;
while(progLine[j] != '\0' && progLine[j] != '\n')
{
program[i][j] = progLine[j];
j++;
}
}
else
{
PROGRAM_LAST_LINE = i-1;
break;
}
i++;
}
}
}
else
{
std::cout << "Error opening file. Exiting." << std::endl;
return 1;
}
}
else
{
std::cout << "No file input, exiting." << std::endl;
return 1;
}
RunProgram(program);
// insert code here...
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment