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