Krolyxon
Commit f8fc421

Branches

Refs →

trosf8fc421

beautification

Author
krolyxon
Date
2023-09-01 11:22 +0530
Parent
c6e829d
Commit
f8fc421e0b0d988e3c85c3a7e4b33a532f3c2827

+45 −28

4 files changed

  1. M README.md +7 −10
  2. M src/main.c +22 −17
  3. M src/utils.c +15 −1
  4. M src/utils.h +1 −0
M README.md View
@@ -1,11 +1,13 @@
11 ## WIP WIP WIP
22
33 # TODO
4−- [] Implement Score system
5−Difficulties: Easy, Medium, Hard\
6−Easy = +10, -10\
7−Medium = +10, -20\
8−Hard = +10, score = 0
4+- [] Algorithms to be implemented:
5+ - [x] Bubblesort
6+ - [x] SelectionSort
7+ - [x] InsertionSort
8+ - [] Radix Sort
9+- [x] Implement Score system
10+- [] Code Refactoring/Cleaning
911
1012 ## Levels:
1113 1: 5 elems\
@@ -20,9 +22,4 @@ Hard = +10, score = 0
2022 10: 30 elems\
2123
2224
23−# Algorithms to be implemented:
24−- [x] Bubblesort
25−- [x] SelectionSort
26−- [x] InsertionSort
27−- [] Radix Sort
2825
M src/main.c View
@@ -4,6 +4,7 @@
44 #include <stdlib.h>
55 #include <time.h>
66 // https://texteditor.com/multiline-text-art/
7+
78 const int EASY_SCORE_DECREMENT = 10;
89 const int MEDIUM_SCORE_DECREMENT = 20;
910 const int LOWER = 1;
@@ -21,6 +22,7 @@ int score = 100;
2122 int level = 1;
2223 int size = 5;
2324
25+// Declaring Functions
2426 void decrement_score(Difficulty diff);
2527 void level_up();
2628 void level_down();
@@ -33,13 +35,14 @@ int main(int argc, char *argv[]) {
3335 srand(time(0));
3436 int guess;
3537
36− printf(COLOR_RED);
38+ clearscreen();
39+ printf(COLOR_CYAN);
3740 print_ascii("./assets/banner.txt");
3841 Difficulty diff = get_difficulty();
3942
4043 while (level > 0 && level <= 10) {
4144 int random_number = getrand();
42− printf(BAR);
45+ printf(COLOR_GREEN BAR COLOR_OFF);
4346 switch (random_number) {
4447 case 1:
4548 getarr(size);
@@ -58,22 +61,21 @@ int main(int argc, char *argv[]) {
5861 break;
5962 }
6063
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);
64+ printf(COLOR_GREEN BAR COLOR_OFF);
65+ printf(COLOR_BOLD "[1] " COLOR_OFF "Bubble Sort\n");
66+ printf(COLOR_BOLD "[2] " COLOR_OFF "Insertion Sort\n");
67+ printf(COLOR_BOLD "[3] " COLOR_OFF "Selection Sort\n");
68+ printf(COLOR_BOLD "[4] " COLOR_OFF "Radix Sort\n");
6769 printf("Enter your guess: ");
6870 scanf("%d", &guess);
6971 if (guess == random_number) {
70− printf("Congratulations!!! Your answer was right!!\n");
7172 score += 10;
7273 level_up();
7374 } else {
7475 decrement_score(diff);
7576 level_down();
7677 }
78+
7779 printf("Score: %d\n", score);
7880 }
7981 return 0;
@@ -83,7 +85,8 @@ int getrand() { return (rand() % (UPPER - LOWER + 1)) + LOWER; }
8385
8486 void decrement_score(Difficulty diff) {
8587 if (diff == Easy) {
86− printf("Wrong Answer!! The score will be decremented by 10\n");
88+ printf(COLOR_BOLD COLOR_RED "Wrong Answer!!" COLOR_OFF
89+ "The score will be decremented by 10\n");
8790 score -= EASY_SCORE_DECREMENT;
8891 } else if (diff == Medium) {
8992 printf("Wrong Answer!! The score will be decremented by 20\n");
@@ -97,12 +100,12 @@ void decrement_score(Difficulty diff) {
97100 Difficulty get_difficulty() {
98101 int choice;
99102 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);
103+ printf(COLOR_BOLD COLOR_RED " CHOOSE DIFFICULTY\n");
104+ printf(COLOR_GREEN BAR COLOR_OFF);
105+ printf(COLOR_BOLD "[1]" COLOR_OFF " Easy\n");
106+ printf(COLOR_BOLD "[2]" COLOR_OFF " Medium\n");
107+ printf(COLOR_BOLD "[3]" COLOR_OFF " Hard\n" COLOR_OFF);
108+ printf(COLOR_GREEN BAR COLOR_OFF);
106109 printf("Enter difficulty: ");
107110 scanf("%d", &choice);
108111 switch (choice) {
@@ -121,13 +124,15 @@ Difficulty get_difficulty() {
121124
122125 void level_up() {
123126 if (level == 10) {
127+ printf(COLOR_YELLOW);
124128 print_ascii("./assets/winner.txt");
129+ printf(COLOR_OFF);
125130 printf("Congratulations!! You WON the game");
126131 exit(1);
127132 }
128133 level++;
129134 size += 3;
130− printf(COLOR_BOLD COLOR_RED
135+ printf(COLOR_BOLD COLOR_GREEN BAR
131136 "You have been leveled up to Level %d\n" COLOR_OFF,
132137 level);
133138 }
M src/utils.c View
@@ -1,9 +1,10 @@
11 #include "utils.h"
22 #include <stdio.h>
3+#include <stdlib.h>
34
45 void printarr(int a[], int n) {
56 for (int i = 0; i < n; i++) {
6− printf("%d ", a[i]);
7+ printf(COLOR_BOLD "%d " COLOR_OFF, a[i]);
78 }
89 }
910
@@ -24,3 +25,16 @@ void swap(int a[], int i, int j) {
2425 a[i] = a[j];
2526 a[j] = tmp;
2627 }
28+
29+
30+void clearscreen(void) {
31+#ifdef _WIN32
32+ system("cls");
33+#elif defined(unix) || defined(__unix__) || defined(__unix) || \
34+ (defined(__APPLE__) && defined(__MACH__))
35+ system("clear");
36+#else
37+#error "OS not supported."
38+#endif
39+}
40+
M src/utils.h View
@@ -17,3 +17,4 @@
1717 void printarr(int a[], int n);
1818 void print_ascii(char *filename);
1919 void swap(int a[], int i, int j);
20+void clearscreen();