Exemplo n.º 1
0
void
maze_gen(short int *grid, int TotalCells) {
	int CurrentCell = random() / (RAND_MAX / TotalCells + 1);
	int VisitedCells = 1;
	int AdCell; /* Adiacent cell */
	int *stack, top = 0;
	int i;

	if(! (stack = malloc(sizeof(int) * TotalCells)))
		return;

	for(i = 0; i < rows * cols; i++)
		grid[i] = A << WALLS;

	while(VisitedCells < TotalCells) {
		if((AdCell = nextcell(grid, CurrentCell)) != -1) {
			pulldown(grid, CurrentCell, AdCell);

			push(stack, &top, CurrentCell);
			CurrentCell = AdCell;
			++VisitedCells;
		}
		else
			CurrentCell = pop(stack, &top);
	}
}
Exemplo n.º 2
0
static void execute(void)
{
    context.pos.x = context.pos.y = context.stack.pos = context.stringmode = 0;
    context.direction = RIGHT;
    memset(context.stack.stck, 0, sizeof(context.stack));

    while(process() != EOP)
        nextcell();
}
Exemplo n.º 3
0
static int process(void)
{
    int val1, val2;

    if(context.stringmode)
    {
        if(matrix[context.pos.y][context.pos.x] == '"')
            context.stringmode = 0;
        else
            push(matrix[context.pos.y][context.pos.x]);

        return 0;
    }

    if(matrix[context.pos.y][context.pos.x] == ' ')
        return 0;

    switch(matrix[context.pos.y][context.pos.x])
    {
    /* @ (end) ends program */
    case '@':
        return EOP;

    case '>': case '<': case '^': case 'v':
        context.direction = car2dir(matrix[context.pos.y][context.pos.x]);
        break;

    /* _ (horizontal if) <boolean value> PC->left if <value>, else PC->right */
    case '_':
        context.direction = (pop() ? LEFT : RIGHT);
        break;

    /* | (vertical if) <boolean value> PC->up if <value>, else PC->down */
    case '|':
        context.direction = (pop() ? UP : DOWN);
        break;

    /* : (dup)  <value>   <value> <value> */
    case ':':
        push(val1 = pop());
        push(val1);
        break;

    /* ? (random) PC -> right? left? up? down? ??? */
    case '?':
        context.direction = rand() % DIRMAX;
        break;

    case '+': case '-': case '*': case '/': case '%':
        val1 = pop(), val2 = pop();
        push(binaryfuncs[binaryopcar2index(matrix[context.pos.y][context.pos.x])](val2, val1));
        break;

    case '!':
        push(!pop());
        break;

    /* ` (greater)  <value1> <value2>   <1 if value1 > value2, 0 otherwise> */
    case '`':
        push(pop() < pop());
        break;

    /* \ (swap) <value1> <value2>  <value2> <value1> */
    case '\\':
        val1 = pop(), val2 = pop();
        push(val1);
        push(val2);
        break;

    /* $ (pop) <value>   pops <value> but does nothing */
    case '$':
        (void) pop();
        break;

    /* . (pop) <value>   outputs <value> as integer */
    case '.':
        printf("%d", pop());
        break;

    /* , (pop)         <value>                 outputs <value> as ASCII */
    case ',':
        putchar(pop());
        break;

    /* # (bridge)  'jumps' PC one farther; skips over next command */
    case '#':
        nextcell();
        break;

    /* ~ (input character)   <character user entered> */
    case '~':
        push(xgetchar());
        break;

    case '"':
        context.stringmode = !context.stringmode;
        break;

    /* & (input value)   <value user entered> */
    case '&':
        push(getint());
        break;

    case 'p':
        val1 = pop(), val2 = pop();
        matrix[val1][val2] = pop();
        break;

    case 'g':
        val1 = pop(), val2 = pop();
        push(matrix[val1][val2]);
        break;

    default:
        if(isdigit(matrix[context.pos.y][context.pos.x]))
            push(matrix[context.pos.y][context.pos.x] - '0');
    }

    return 0;
}