Skip to content

Instantly share code, notes, and snippets.

@iporsut
Created March 3, 2011 19:47
Show Gist options
  • Save iporsut/853365 to your computer and use it in GitHub Desktop.
Save iporsut/853365 to your computer and use it in GitHub Desktop.
Cromartie Mountain
#include <stdio.h>
typedef struct _mountain{
int s;
int h;
} MOUNTAIN;
void sort_mountain(MOUNTAIN *m,int c) {
int i,j;
MOUNTAIN tmp;
for(i = 0; i < c-1;i++) {
for(j = 1; j < c; j++) {
if (m[i].s > m[j].s) {
tmp.s = m[j].s;
tmp.h = m[j].h;
m[j].s = m[i].s;
m[j].h = m[i].h;
m[i].s = tmp.s;
m[i].h = tmp.h;
}
}
}
}
int main(int argc, char *argv[]) {
unsigned int screen[10][60];
MOUNTAIN input[60];
int count = 0;
int i,j,m,k,l,s,h,maxs=0,maxh=0;
/* input */
printf("Input mountains(limit 21, 0 0 for finish input)\n");
do {
printf("Enter mountain S and H: ");
scanf("%d %d",&s,&h);
if (s == 0) break;
maxs = maxs < s?s:maxs;
maxh = maxh < h?h:maxh;
input[count].s = s;
input[count].h = h;
count++;
} while(count < 21);
/* qsort input */
sort_mountain(input,count);
/* fill screen with '.' */
for(i = 0; i < maxh; i++) {
for(j=0; j < (input[count-1].s+input[count-1].h*2-1); j++) {
screen[i][j] = '.';
}
}
/* draw mountain */
for(m = 0; m < count; m++) {
for(l = 0; l < input[m].h; l++) {
for(k = l; k < input[m].h; k++) {
if (k == l) {
if(screen[maxh-l-1][k+(input[m].s-1)] == '.')
screen[maxh-l-1][k+(input[m].s-1)] = '/';
else if(screen[maxh-l-1][k+(input[m].s-1)] == '\\')
screen[maxh-l-1][k+(input[m].s-1)] = 'v';
else
screen[maxh-l-1][k+(input[m].s-1)] = 'X';
if(screen[maxh-l-1][(input[m].h*2-k-1)+(input[m].s-1)] == '.')
screen[maxh-l-1][(input[m].h*2-k-1)+(input[m].s-1)] = '\\';
else if(screen[maxh-l-1][(input[m].h*2-k-1)+(input[m].s-1)] == '/')
screen[maxh-l-1][(input[m].h*2-k-1)+(input[m].s-1)] = 'v';
else
screen[maxh-l-1][(input[m].h*2-k-1)+(input[m].s-1)] = 'X';
} else {
screen[maxh-l-1][k+(input[m].s-1)] = 'X';
screen[maxh-l-1][(input[m].h*2-k-1)+(input[m].s-1)] = 'X';
}
}
}
}
/* render mountain */
for(i = 0; i < maxh; i++) {
for(j=0; j < (input[count-1].s+input[count-1].h*2-1); j++) {
printf("%c",screen[i][j]);
}
printf("\n");
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment