Skip to content

Instantly share code, notes, and snippets.

@sdwvit sdwvit/mario.c
Last active May 3, 2019

Embed
What would you like to do?
mario
#include <stdio.h>
#include <stdlib.h>
#include <cs50.h>
int getHeight(void) {
int height;
do {
height = get_int("Height: ");
} while (
height <= 0 || height > 8
);
return height;
}
int getLowerBound(int height, int gap_width, int j) {
return height - j - 1;
}
int getUpperBound(int height, int gap_width, int j) {
return height + j + gap_width;
}
int isGap(int height, int gap_width, int i) {
return i > height - 1 && i < height + gap_width;
}
int main(void)
{
int height = getHeight();
int gap_width = 2;
int line_len;
for (int j = 0; j < height; j++) {
line_len = getUpperBound(height, gap_width, j) + 1;
char line[line_len + 1];
for (int i = 0; i < line_len; i++) {
line[i] = ' ';
if (!(i >= getLowerBound(height, gap_width, j) && i <= getUpperBound(height, gap_width, j))) {
continue;
}
if (isGap(height, gap_width, i)) {
continue;
}
line[i] = '#';
}
line[line_len] = '\0'; // terminate char array: wtf c, that's why noone uses you
printf("%s\n", line);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.