[C-prog-lang-l] mountain art competition

Filip S hyppercrone at gmail.com
Fri Mar 15 21:46:21 CET 2019


Hello,

I used 3 functions. One for initialization, one for filling the array and
lastly for printing the mountain.
I also added some snowflakes and some random details on the mountains
themselves. I made it so there is an option to keep them.
Printing multiple mountains over each other makes it also a little more
detailed.
[image: Mountain.JPG]

The code is in the attachment. I'm sorry about the for-loops. :)

Regards,
Filip Sedlák

pá 15. 3. 2019 v 4:56 odesílatel Vladimír Kotal <vlada at devnull.cz> napsal:

> In addition to the creative enhancement (reminds me of
> https://ajmysmetatry.sk/), the code is nicely structured with a good
> style.
>
> Some minor nits:
>   - separate the defines and includes with blank line
>   - be consistent: rand() % 3 vs. rand()%3
>   - multiword identifiers can be made more readable somehow (either use
> https://en.wikipedia.org/wiki/Camel_case or underscores)
>   - you are cheating a little bit with those for loops :-) as we have not
> introduced them yet
>   - snow caps drawing can be refactored into its own function - it's extra
> feature, maybe later on can be governed by an option
>
> Regards,
>
> V. Kotal
>
> On Thu, Mar 14, 2019 at 12:32 PM Peter G <peter.grajcar131 at gmail.com>
> wrote:
>
>> Hello,
>>
>> I have used two functions. One for the printing the mountain and second
>> for filling the columns of the array. I also added mountain snow caps,
>> which are generated above certain height level.
>>
>> I put the code on GitHub. It can be found there:
>> https://github.com/peter-grajcar/the-c-programming-language/blob/master/src/mountains.c
>>
>> As an example I attached picture of my mountains.
>>
>> Regards,
>> Peter Grajcar
>>
>> .
>> _______________________________________________
>> c-prog-lang-l mailing list
>> c-prog-lang-l at mff.cuni.cz
>> http://mbox.ms.mff.cuni.cz/listserv/listinfo/c-prog-lang-l
>>
> _______________________________________________
> c-prog-lang-l mailing list
> c-prog-lang-l at mff.cuni.cz
> http://mbox.ms.mff.cuni.cz/listserv/listinfo/c-prog-lang-l
>
-------------- next part --------------
HTML attachment scrubbed and removed
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Screenshot 2019-03-14 at 11.46.13.png
Type: image/png
Size: 31344 bytes
Desc: not available
URL: <http://mbox.ms.mff.cuni.cz/listserv/archive/c-prog-lang-l/attachments/20190315/f7f4b2e2/attachment.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Mountain.JPG
Type: image/jpeg
Size: 86527 bytes
Desc: not available
URL: <http://mbox.ms.mff.cuni.cz/listserv/archive/c-prog-lang-l/attachments/20190315/f7f4b2e2/attachment.jpe>
-------------- next part --------------
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#define	HEIGHT 30
#define	WIDTH  200
#define	SNOW 1
#define	DETAILS 1


char mountain[HEIGHT][WIDTH];
char directions [] = {'\\', '_', '/'};


void
fillMountain(int maxHeight) {
	int myHeight = 0, direction = 0, position = 0;

	for (int j = 0; j < WIDTH; ++j)
	{
		while (myHeight == position) {
			myHeight = rand() % maxHeight;
		}

	direction = 1 - (position > myHeight) * 2;

	// putting in random details
		for (int i = 0; i < position; ++i)
			if (DETAILS && (rand() % 10) == 0) mountain[i][j] = '.';
			else mountain[i][j] = ' ';

		mountain[position][j] = directions[direction + 1];

	// mountain itself, sometimes growth(decrease) stagnates
		if ((rand() % 2) == 1) mountain[position][j] = directions[1];
			else
				position += direction;
	}

}


void
printMountain(void) {
	for (int i = HEIGHT - 1; i > 0; --i)
	{
		for (int j = 0; j < WIDTH; ++j)
		{
			printf("%c", mountain[i][j]);
		}
		printf("\n");
	}
}


void
init(void) {

	// it's snowing
	for (int i = 0; i < HEIGHT; ++i)
		for (int j = 0; j < WIDTH; ++j)
		{
			if (SNOW && (rand() % 20) == 1) mountain[i][j] = '*';
			else
				mountain[i][j] = ' ';
		}
	srand(time(NULL));
}


int
main(void) {
	init();

	// putting multiple mountains over themselves makes it more realistic
	fillMountain(HEIGHT);
	fillMountain((HEIGHT * 3) / 4);
	fillMountain(HEIGHT / 2);
	printMountain();
}


More information about the c-prog-lang-l mailing list