Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

too many to summarize #5

Merged
merged 2 commits into from Jun 11, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
101 changes: 69 additions & 32 deletions triangular.c
Expand Up @@ -10,7 +10,16 @@ int jumps;
extern int verbose, display_code;

extern char buf[1000][1000];
int direction, skip, x, y;
int direction, skip, x, y, dx, dy;

const int NORTHWEST = 1;
const int NORTH = 2;
const int NORTHEAST = 3;
const int WEST = 4;
const int EAST = 5;
const int SOUTHWEST = 6;
const int SOUTH = 7;
const int SOUTHEAST = 8;

void parse(char);

Expand All @@ -26,17 +35,22 @@ void triangular(void)
parse(buf[y][x]);

switch (direction) {
case 1: x--; y--; break;
case 2: y -= 2; x--; break;
case 3: y--; break;
case 4: x--; break;
case 5: x++; break;
case 6: y++; break;
case 7: y += 2; x++; break;
case 8: x++, y++; break;
case NORTHWEST: dx = -1; dy = -1; break;
case NORTH: dx = -1; dy = 2; break;
case NORTHEAST: dx = 0; dy = -1; break;
case WEST: dx = -1; dy = 0; break;
case EAST: dx = 1; dy = 0; break;
case SOUTHWEST: dx = 0; dy = 1; break;
case SOUTH: dx = 1; dy = 2; break;
case SOUTHEAST: dx = 1; dy = 1; break;
}

skip = skip < 0 ? 0 : skip;
x += dx * (skip + 1);
y += dy * (skip + 1);
skip = 0;

if ((direction == 4 && x < 0) || (direction == 2 && y < 0) || (direction == 1 && (y < 0 || x < 0)))
if ((direction == WEST && x < 0) || (direction == NORTH && y < 0) || (direction == NORTHWEST && (y < 0 || x < 0)))
exit(EXIT_SUCCESS);

if (x < 0)
Expand All @@ -48,30 +62,53 @@ void triangular(void)
}
}

void parse(char command)
int next_direction(int direction, int change)
{
if (skip) {
skip--;
return;
if (change == 1){
switch (direction) {
case NORTH: return NORTHEAST;
case NORTHEAST: return EAST;
case EAST: return SOUTHEAST;
case SOUTHEAST: return SOUTH;
case SOUTH: return SOUTHWEST;
case SOUTHWEST: return WEST;
case WEST: return NORTHWEST;
case NORTHWEST: return NORTH;
}
}else{
switch (direction) {
case NORTH: return NORTHWEST;
case NORTHEAST: return NORTH;
case EAST: return NORTHEAST;
case SOUTHEAST: return EAST;
case SOUTH: return SOUTHEAST;
case SOUTHWEST: return SOUTH;
case WEST: return SOUTHWEST;
case NORTHWEST: return WEST;
}
}
return -1;
}

void parse(char command)
{
if (display_code)
putchar(command);

switch (command) {
/* directional */
case '`': direction = 1; break;
case '^': direction = 2; break;
case '/': direction = 3; break;
case '<': direction = 4; break;
case '>': direction = 5; break;
case ',': direction = 6; break;
case '|': direction = 7; break;
case '\\':direction = 8; break;
case 'o': direction = direction == 8 ? 1 : direction + 1; break;
case 'e': direction = direction == 1 ? 8 : direction - 1; break;
case 'c': direction = direction == 8 ? 1 : direction + 1; buf[y][x] = 'z'; break;
case 'z': direction = direction == 1 ? 8 : direction - 1; buf[y][x] = 'c'; break;
case '`': direction = NORTHWEST; break;
case '^': direction = NORTH; break;
case '/': direction = NORTHEAST; break;
case '<': direction = WEST; break;
case '>': direction = EAST; break;
case ',': direction = SOUTHWEST; break;
case 'v': direction = SOUTH; break;
case '\\':direction = SOUTHEAST; break;
case 'o': direction = next_direction(direction, 1); break;
case 'e': direction = next_direction(direction, -1); break;
case 'c': direction = next_direction(direction, 1); buf[y][x] = 'z'; break;
case 'z': direction = next_direction(direction, -1); buf[y][x] = 'c'; break;

/* program */
case '&': exit(EXIT_SUCCESS);
Expand All @@ -87,17 +124,17 @@ void parse(char command)

case '$': stack[size++] = getchar() - '0'; break;
case '~': stack[size++] = getchar(); break;
case '%': printf("%d",(size && stack[size-1])); break;
case '@': putchar((size && stack[size-1])); break;
case '%': printf("%d",size ? stack[size-1] : 0); break;
case '@': putchar(size ? stack[size-1] : 0); break;

case '1': case '2': case '3': case '4': case '5': case '6': case '7': case'8': case '9':
stack[size++] = command - '0'; break;

/* conditionals */
case '?': skip = (size && stack[size-1]) < 1; break;
case '!': skip = (size && stack[size-1]) > 0; break;
case 's': skip = (size && stack[size-1]); break;
case ';': if ((size && stack[size-1]) < 1) exit(EXIT_SUCCESS); break;
case '?': skip = (size ? stack[size-1] : 0) < 1; break;
case '!': skip = (size ? stack[size-1] : 0) > 0; break;
case 's': skip = (size ? stack[size-1] : 0); break;
case ';': if ((size ? stack[size-1] : 0) < 1) exit(EXIT_SUCCESS); break;

case 'x': jumps--;
case '(':
Expand Down