From f8fc421e0b0d988e3c85c3a7e4b33a532f3c2827 Mon Sep 17 00:00:00 2001 From: krolyxon Date: Fri, 1 Sep 2023 11:22:33 +0530 Subject: [PATCH] beautification --- README.md | 17 +++++++---------- src/main.c | 39 ++++++++++++++++++++++----------------- src/utils.c | 16 +++++++++++++++- src/utils.h | 1 + 4 files changed, 45 insertions(+), 28 deletions(-) diff --git a/README.md b/README.md index 37d0257..65d2e35 100644 --- a/README.md +++ b/README.md @@ -1,11 +1,13 @@ ## WIP WIP WIP # TODO -- [] Implement Score system -Difficulties: Easy, Medium, Hard\ -Easy = +10, -10\ -Medium = +10, -20\ -Hard = +10, score = 0 +- [] Algorithms to be implemented: + - [x] Bubblesort + - [x] SelectionSort + - [x] InsertionSort + - [] Radix Sort +- [x] Implement Score system +- [] Code Refactoring/Cleaning ## Levels: 1: 5 elems\ @@ -20,9 +22,4 @@ Hard = +10, score = 0 10: 30 elems\ -# Algorithms to be implemented: -- [x] Bubblesort -- [x] SelectionSort -- [x] InsertionSort -- [] Radix Sort diff --git a/src/main.c b/src/main.c index 542b6b3..c177290 100644 --- a/src/main.c +++ b/src/main.c @@ -4,6 +4,7 @@ #include #include // https://texteditor.com/multiline-text-art/ + const int EASY_SCORE_DECREMENT = 10; const int MEDIUM_SCORE_DECREMENT = 20; const int LOWER = 1; @@ -21,6 +22,7 @@ int score = 100; int level = 1; int size = 5; +// Declaring Functions void decrement_score(Difficulty diff); void level_up(); void level_down(); @@ -33,13 +35,14 @@ int main(int argc, char *argv[]) { srand(time(0)); int guess; - printf(COLOR_RED); + clearscreen(); + printf(COLOR_CYAN); print_ascii("./assets/banner.txt"); Difficulty diff = get_difficulty(); while (level > 0 && level <= 10) { int random_number = getrand(); - printf(BAR); + printf(COLOR_GREEN BAR COLOR_OFF); switch (random_number) { case 1: getarr(size); @@ -58,22 +61,21 @@ int main(int argc, char *argv[]) { break; } - printf(BAR); - printf("1. BubbleSort\n"); - printf("2. InsertionSort\n"); - printf("3. SelectionSort\n"); - printf("4. RadixSort\n"); - printf("%d", random_number); + printf(COLOR_GREEN BAR COLOR_OFF); + printf(COLOR_BOLD "[1] " COLOR_OFF "Bubble Sort\n"); + printf(COLOR_BOLD "[2] " COLOR_OFF "Insertion Sort\n"); + printf(COLOR_BOLD "[3] " COLOR_OFF "Selection Sort\n"); + printf(COLOR_BOLD "[4] " COLOR_OFF "Radix Sort\n"); printf("Enter your guess: "); scanf("%d", &guess); if (guess == random_number) { - printf("Congratulations!!! Your answer was right!!\n"); score += 10; level_up(); } else { decrement_score(diff); level_down(); } + printf("Score: %d\n", score); } return 0; @@ -83,7 +85,8 @@ int getrand() { return (rand() % (UPPER - LOWER + 1)) + LOWER; } void decrement_score(Difficulty diff) { if (diff == Easy) { - printf("Wrong Answer!! The score will be decremented by 10\n"); + printf(COLOR_BOLD COLOR_RED "Wrong Answer!!" COLOR_OFF + "The score will be decremented by 10\n"); score -= EASY_SCORE_DECREMENT; } else if (diff == Medium) { printf("Wrong Answer!! The score will be decremented by 20\n"); @@ -97,12 +100,12 @@ void decrement_score(Difficulty diff) { Difficulty get_difficulty() { int choice; Difficulty difficulty; - printf(COLOR_CYAN " CHOOSE DIFFICULTY\n" COLOR_OFF); - printf(COLOR_RED BAR COLOR_OFF); - printf("1. Easy\n"); - printf("2. Medium\n"); - printf("3. Hard\n"); - printf(BAR); + printf(COLOR_BOLD COLOR_RED " CHOOSE DIFFICULTY\n"); + printf(COLOR_GREEN BAR COLOR_OFF); + printf(COLOR_BOLD "[1]" COLOR_OFF " Easy\n"); + printf(COLOR_BOLD "[2]" COLOR_OFF " Medium\n"); + printf(COLOR_BOLD "[3]" COLOR_OFF " Hard\n" COLOR_OFF); + printf(COLOR_GREEN BAR COLOR_OFF); printf("Enter difficulty: "); scanf("%d", &choice); switch (choice) { @@ -121,13 +124,15 @@ Difficulty get_difficulty() { void level_up() { if (level == 10) { + printf(COLOR_YELLOW); print_ascii("./assets/winner.txt"); + printf(COLOR_OFF); printf("Congratulations!! You WON the game"); exit(1); } level++; size += 3; - printf(COLOR_BOLD COLOR_RED + printf(COLOR_BOLD COLOR_GREEN BAR "You have been leveled up to Level %d\n" COLOR_OFF, level); } diff --git a/src/utils.c b/src/utils.c index 390aa7c..37b50bf 100644 --- a/src/utils.c +++ b/src/utils.c @@ -1,9 +1,10 @@ #include "utils.h" #include +#include void printarr(int a[], int n) { for (int i = 0; i < n; i++) { - printf("%d ", a[i]); + printf(COLOR_BOLD "%d " COLOR_OFF, a[i]); } } @@ -24,3 +25,16 @@ void swap(int a[], int i, int j) { a[i] = a[j]; a[j] = tmp; } + + +void clearscreen(void) { +#ifdef _WIN32 + system("cls"); +#elif defined(unix) || defined(__unix__) || defined(__unix) || \ + (defined(__APPLE__) && defined(__MACH__)) + system("clear"); +#else +#error "OS not supported." +#endif +} + diff --git a/src/utils.h b/src/utils.h index a667b73..20473ef 100644 --- a/src/utils.h +++ b/src/utils.h @@ -17,3 +17,4 @@ void printarr(int a[], int n); void print_ascii(char *filename); void swap(int a[], int i, int j); +void clearscreen();