Skip to content

Instantly share code, notes, and snippets.

@himanshugoel2797
Created January 26, 2018 23:12
Show Gist options
  • Save himanshugoel2797/ee159512ebc2d25c337e0be14ac8dada to your computer and use it in GitHub Desktop.
Save himanshugoel2797/ee159512ebc2d25c337e0be14ac8dada to your computer and use it in GitHub Desktop.
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define SIDE 128
#define PATH_LEN 10
#define MIN_PATH_LEN 8
char grid[SIDE * SIDE];
char x = 0;
char y = 0;
void direction_calc(int direction, int *x_d, int *y_d) {
switch (direction) {
case 0:
*x_d = 1;
*y_d = 0;
return;
case 1:
*x_d = -1;
*y_d = 0;
return;
case 2:
*x_d = 0;
*y_d = 1;
return;
case 3:
*x_d = 0;
*y_d = -1;
return;
}
}
void set_grid(char x, char y) {
if (x < 0 | y < 0)
return;
if (x >= SIDE | y >= SIDE)
return;
grid[x * SIDE + y] = 0;
}
int get_grid(char x, char y) {
if (x < 0 | y < 0)
return 0;
if (x >= SIDE | y >= SIDE)
return 0;
return grid[x * SIDE + y];
}
void set_grid_alt(char x, char y, int dir) {
//Make sure that the lines don't cross over something else
int x_dir = 0;
int y_dir = 0;
direction_calc(dir, &x_dir, &y_dir);
if (x_dir == 0) {
if (get_grid(x - 1, y) == 0)
return;
if (get_grid(x + 1, y) == 0)
return;
set_grid(x, y);
}
else if (y_dir == 0) {
if (get_grid(x, y - 1) == 0)
return;
if (get_grid(x, y + 1) == 0)
return;
set_grid(x, y);
}
}
int draw_path(char len, int direction) {
int x_dir = 0;
int y_dir = 0;
direction_calc(direction, &x_dir, &y_dir);
for (int i = 0; i < len; i++) {
set_grid(x, y);
x += x_dir;
y += y_dir;
if (x < 0)
x = 0;
if (x >= SIDE)
x = SIDE - 1;
if (y < 0)
y = 0;
if (y >= SIDE)
y = SIDE - 1;
}
return 0;
}
int draw_path_alt(char len, int direction) {
int x_dir = 0;
int y_dir = 0;
direction_calc(direction, &x_dir, &y_dir);
for (int i = 0; i < len; i++) {
if (i == 0)set_grid(x, y);
else set_grid_alt(x, y, direction);
x += x_dir;
y += y_dir;
if (x < 0)
x = 0;
if (x >= SIDE)
x = SIDE - 1;
if (y < 0)
y = 0;
if (y >= SIDE)
y = SIDE - 1;
}
return 0;
}
void draw_grid() {
for (int i = 0; i < SIDE; i++) {
for (int j = 0; j < SIDE; j++) {
switch (grid[i * SIDE + j]) {
case 0:
printf(" ");
break;
case 1:
printf("%c", 219);
break;
case 2:
printf("S");
break;
case 3:
printf("E");
break;
}
}
printf("\r\n");
}
printf("\r\n");
}
int main(int argc, char *argv[]) {
if(argc >= 2)srand(atoi(argv[1]));
else srand(time(NULL));
memset(grid, 1, SIDE * SIDE);
//pick a starting location
x = 0;
y = rand() % SIDE;
int start_dir = 0;
int len = rand() % PATH_LEN;
if (len < MIN_PATH_LEN)len = MIN_PATH_LEN;
int orig_x = x;
int orig_y = y;
while (x < SIDE - 1) {
draw_path((char)len, start_dir);
len = rand() % PATH_LEN;
if (len < MIN_PATH_LEN)len = MIN_PATH_LEN;
if (start_dir == 0 | start_dir == 1)
start_dir = rand() % 2 + 2;
else
start_dir = rand() % 2;
if (start_dir & 1)
{
if (x == 0 | y == 0)
start_dir = (start_dir & 2);
}
else {
if (x == SIDE | y == SIDE)
start_dir = start_dir | 1;
}
}
int end_x = x;
int end_y = y;
for (int j = 0; j < 20; j++) {
x = rand() % SIDE;
y = rand() % SIDE;
for (int i = 0; i < 5; i++) {
start_dir = rand() % 4;
len = rand() % PATH_LEN;
if (len < MIN_PATH_LEN)len = MIN_PATH_LEN;
draw_path_alt(len, start_dir);
if (start_dir & 1)
{
if (x == 0 | y == 0)
start_dir = (start_dir & 2);
}
else {
if (x == SIDE | y == SIDE)
start_dir = start_dir | 1;
}
}
}
grid[end_x * SIDE + end_y] = 3;
grid[orig_x * SIDE + orig_y] = 2;
draw_grid();
while (true)
;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment