| 1 | #include "sort.h" |
| 2 | #include "utils.h" |
| 3 | #include <stdio.h> |
| 4 | #include <stdlib.h> |
| 5 | #include <time.h> |
| 6 | // https://texteditor.com/multiline-text-art/ |
| 7 | |
| 8 | const char BANNER[][300] = { |
| 9 | " ╔════╗ ╔═══╗ ╔═══╗ ╔═══╗", " ║╔╗╔╗║ ║╔═╗║ ║╔═╗║ ║╔═╗║", |
| 10 | " ╚╝║║╚╝ ║╚═╝║ ║║ ║║ ║╚══╗", " ║║ ║╔╗╔╝ ║║ ║║ ╚══╗║", |
| 11 | " ╔╝╚╗ ║║║╚╗ ║╚═╝║ ║╚═╝║", " ╚══╝ ╚╝╚═╝ ╚═══╝ ╚═══╝", |
| 12 | }; |
| 13 | |
| 14 | const char WINNER[][300] = { |
| 15 | "──────────────────────────────────────────", |
| 16 | " ╔╗╔╗╔╗╔══╗╔═╗ ╔╗╔═╗ ╔╗╔═══╗╔═══╗╔╗╔╗", |
| 17 | " ║║║║║║╚╣╠╝║║╚╗║║║║╚╗║║║╔══╝║╔═╗║║║║║", |
| 18 | " ║║║║║║ ║║ ║╔╗╚╝║║╔╗╚╝║║╚══╗║╚═╝║║║║║", |
| 19 | " ║╚╝╚╝║ ║║ ║║╚╗║║║║╚╗║║║╔══╝║╔╗╔╝╚╝╚╝", |
| 20 | " ╚╗╔╗╔╝╔╣╠╗║║ ║║║║║ ║║║║╚══╗║║║╚╗╔╗╔╗", |
| 21 | " ╚╝╚╝ ╚══╝╚╝ ╚═╝╚╝ ╚═╝╚═══╝╚╝╚═╝╚╝╚╝", |
| 22 | "──────────────────────────────────────────", |
| 23 | }; |
| 24 | |
| 25 | static const int EASY_SCORE_DECREMENT = 10; |
| 26 | static const int MEDIUM_SCORE_DECREMENT = 20; |
| 27 | static const int LOWER = 1; |
| 28 | static const int UPPER = 4; |
| 29 | typedef enum { |
| 30 | Easy, |
| 31 | Medium, |
| 32 | Hard, |
| 33 | } Difficulty; |
| 34 | |
| 35 | int list[35]; |
| 36 | |
| 37 | static int score = 0; |
| 38 | static int level = 1; |
| 39 | static int size = 5; |
| 40 | |
| 41 | // Declaring Functions |
| 42 | void decrement_score(Difficulty diff); |
| 43 | void level_up(); |
| 44 | void level_down(); |
| 45 | int getrand(); |
| 46 | void getarr(int size); |
| 47 | Difficulty get_difficulty(); |
| 48 | |
| 49 | int main(int argc, char *argv[]) { |
| 50 | // Get a random number to run a random algorithm |
| 51 | srand(time(0)); |
| 52 | int guess; |
| 53 | |
| 54 | clearscreen(); |
| 55 | printf(COLOR_CYAN BAR); |
| 56 | for (int i = 0; i < 6; i++) { |
| 57 | printf("%s\n", BANNER[i]); |
| 58 | } |
| 59 | Difficulty diff = get_difficulty(); |
| 60 | |
| 61 | while (level > 0 && level <= 10) { |
| 62 | int random_number = getrand(); |
| 63 | printf(COLOR_GREEN BAR COLOR_OFF); |
| 64 | switch (random_number) { |
| 65 | case 1: |
| 66 | getarr(size); |
| 67 | bubblesort(list, size); |
| 68 | break; |
| 69 | case 2: |
| 70 | getarr(size); |
| 71 | insertionsort(list, size); |
| 72 | break; |
| 73 | case 3: |
| 74 | getarr(size); |
| 75 | selectionsort(list, size); |
| 76 | break; |
| 77 | case 4: |
| 78 | getarr(size); |
| 79 | radixsort(list, size); |
| 80 | break; |
| 81 | default: |
| 82 | break; |
| 83 | } |
| 84 | |
| 85 | printf(COLOR_GREEN BAR COLOR_OFF); |
| 86 | printf(COLOR_BOLD "[1] " COLOR_OFF "Bubble Sort\n"); |
| 87 | printf(COLOR_BOLD "[2] " COLOR_OFF "Insertion Sort\n"); |
| 88 | printf(COLOR_BOLD "[3] " COLOR_OFF "Selection Sort\n"); |
| 89 | printf(COLOR_BOLD "[4] " COLOR_OFF "Radix Sort\n"); |
| 90 | printf("Enter your guess: "); |
| 91 | scanf("%d", &guess); |
| 92 | if (guess == random_number) { |
| 93 | score += 10; |
| 94 | level_up(); |
| 95 | } else { |
| 96 | decrement_score(diff); |
| 97 | level_down(); |
| 98 | } |
| 99 | |
| 100 | printf("Score: %d\n", score); |
| 101 | } |
| 102 | return 0; |
| 103 | } |
| 104 | |
| 105 | int getrand() { return (rand() % (UPPER - LOWER + 1)) + LOWER; } |
| 106 | |
| 107 | void decrement_score(Difficulty diff) { |
| 108 | if (diff == Easy) { |
| 109 | printf(COLOR_BOLD COLOR_RED "Wrong Answer!!" COLOR_OFF |
| 110 | "The score will be decremented by 10\n"); |
| 111 | score -= EASY_SCORE_DECREMENT; |
| 112 | } else if (diff == Medium) { |
| 113 | printf("Wrong Answer!! The score will be decremented by 20.\n"); |
| 114 | score -= MEDIUM_SCORE_DECREMENT; |
| 115 | } else { |
| 116 | printf(COLOR_RED COLOR_BOLD "Wrong Answer!! You Lose!!\n" COLOR_OFF); |
| 117 | exit(1); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | Difficulty get_difficulty() { |
| 122 | int choice; |
| 123 | Difficulty difficulty; |
| 124 | printf(COLOR_GREEN BAR COLOR_OFF); |
| 125 | printf(COLOR_BOLD COLOR_RED " CHOOSE DIFFICULTY\n"); |
| 126 | printf(COLOR_GREEN BAR COLOR_OFF); |
| 127 | printf(COLOR_BOLD "[1]" COLOR_OFF " Easy\n"); |
| 128 | printf(COLOR_BOLD "[2]" COLOR_OFF " Medium\n"); |
| 129 | printf(COLOR_BOLD "[3]" COLOR_OFF " Hard\n" COLOR_OFF); |
| 130 | printf(COLOR_GREEN BAR COLOR_OFF); |
| 131 | printf("Enter difficulty: "); |
| 132 | scanf("%d", &choice); |
| 133 | switch (choice) { |
| 134 | case 1: |
| 135 | difficulty = Easy; |
| 136 | break; |
| 137 | case 2: |
| 138 | difficulty = Medium; |
| 139 | break; |
| 140 | case 3: |
| 141 | difficulty = Hard; |
| 142 | break; |
| 143 | default: |
| 144 | printf(COLOR_RED "Invalid Choice. Defaulting to Medium\n" COLOR_OFF); |
| 145 | difficulty = Medium; |
| 146 | break; |
| 147 | } |
| 148 | return difficulty; |
| 149 | } |
| 150 | |
| 151 | void level_up() { |
| 152 | if (level == 10) { |
| 153 | printf(COLOR_YELLOW); |
| 154 | for (int i = 0; i < 8; i++) { |
| 155 | printf("%s\n", WINNER[i]); |
| 156 | } |
| 157 | printf(COLOR_OFF); |
| 158 | printf("Congratulations!! You WON the game"); |
| 159 | exit(1); |
| 160 | } |
| 161 | level++; |
| 162 | size += 3; |
| 163 | printf(COLOR_BOLD COLOR_GREEN BAR |
| 164 | "You have been leveled up to Level %d\n" COLOR_OFF, |
| 165 | level); |
| 166 | } |
| 167 | |
| 168 | void level_down() { |
| 169 | if (level >= 2) { |
| 170 | level--; |
| 171 | size -= 3; |
| 172 | } else { |
| 173 | printf("Too many wrong answers!! " COLOR_RED COLOR_BOLD |
| 174 | "You Lose!!\n" COLOR_OFF); |
| 175 | exit(1); |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | void getarr(int size) { |
| 180 | for (int i = 0; i < size; i++) { |
| 181 | list[i] = rand() % 100; |
| 182 | } |
| 183 | } |