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