Initial Commit
This commit is contained in:
commit
f94f903987
|
|
@ -0,0 +1,117 @@
|
||||||
|
#include "sort.h"
|
||||||
|
#include "utils.h"
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <time.h>
|
||||||
|
// https://texteditor.com/multiline-text-art/
|
||||||
|
const int EASY_SCORE_DECREMENT = 10;
|
||||||
|
const int MEDIUM_SCORE_DECREMENT = 20;
|
||||||
|
const int LOWER = 1;
|
||||||
|
const int UPPER = 3;
|
||||||
|
|
||||||
|
enum Difficulty {
|
||||||
|
Easy,
|
||||||
|
Medium,
|
||||||
|
Hard,
|
||||||
|
};
|
||||||
|
|
||||||
|
int list[] = {40, 78, 94, 62, 68, 74, 56, 55, 88, 55, 59, 73,
|
||||||
|
19, 32, 81, 95, 71, 63, 15, 41, 11, 38, 86};
|
||||||
|
|
||||||
|
int score = 100;
|
||||||
|
|
||||||
|
void decrement_score(enum Difficulty diff);
|
||||||
|
enum Difficulty get_difficulty();
|
||||||
|
|
||||||
|
int main(int argc, char *argv[]) {
|
||||||
|
// Get a random number to run a random algorithm
|
||||||
|
int guess;
|
||||||
|
srand(time(0));
|
||||||
|
int random_number = (rand() % (UPPER - LOWER + 1)) + LOWER;
|
||||||
|
|
||||||
|
printf(COLOR_RED);
|
||||||
|
print_ascii("./assets/banner.txt");
|
||||||
|
int size;
|
||||||
|
enum Difficulty diff = get_difficulty();
|
||||||
|
switch (diff) {
|
||||||
|
case Easy:
|
||||||
|
size = 5;
|
||||||
|
break;
|
||||||
|
case Medium:
|
||||||
|
size = 10;
|
||||||
|
break;
|
||||||
|
case Hard:
|
||||||
|
size = 23;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
printf(BAR);
|
||||||
|
switch (random_number) {
|
||||||
|
case 1:
|
||||||
|
bubblesort(list, size);
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
insertionsort(list, size);
|
||||||
|
break;
|
||||||
|
case 3:
|
||||||
|
selectionsort(list, size);
|
||||||
|
break;
|
||||||
|
// case 4: radixsort(list); break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
printf(BAR);
|
||||||
|
printf("1. BubbleSort\n");
|
||||||
|
printf("2. InsertionSort\n");
|
||||||
|
printf("3. SelectionSort\n");
|
||||||
|
printf("4. RadixSort\n");
|
||||||
|
printf("Enter your guess: ");
|
||||||
|
scanf("%d", &guess);
|
||||||
|
if (guess == random_number) {
|
||||||
|
printf("Congratulations!!! Your answer was right!!\n");
|
||||||
|
score += 10;
|
||||||
|
} else {
|
||||||
|
decrement_score(diff);
|
||||||
|
}
|
||||||
|
printf("Score: %d\n", score);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void decrement_score(enum Difficulty diff) {
|
||||||
|
if (diff == Easy) {
|
||||||
|
printf("Wrong Answer!! 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");
|
||||||
|
score -= MEDIUM_SCORE_DECREMENT;
|
||||||
|
} else {
|
||||||
|
printf("Wrong Answer!! The score will be reseted to 0\n");
|
||||||
|
score = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
enum Difficulty get_difficulty() {
|
||||||
|
int choice;
|
||||||
|
enum 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("Enter difficulty: ");
|
||||||
|
scanf("%d", &choice);
|
||||||
|
switch (choice) {
|
||||||
|
case 1:
|
||||||
|
difficulty = Easy;
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
difficulty = Medium;
|
||||||
|
break;
|
||||||
|
case 3:
|
||||||
|
difficulty = Hard;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return difficulty;
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,41 @@
|
||||||
|
#include "utils.h"
|
||||||
|
#include <stdio.h>
|
||||||
|
void bubblesort(int a[], int n) {
|
||||||
|
int comparisons = 0;
|
||||||
|
for (int i = 0; i < n; i++) {
|
||||||
|
for (int j = 1; j < n - i; j++) {
|
||||||
|
comparisons++;
|
||||||
|
if (a[j - 1] > a[j]) {
|
||||||
|
swap(a, j, j - 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
printarr(a, n);
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void selectionsort(int a[], int n) {
|
||||||
|
for (int i = 0; i < n; i++) {
|
||||||
|
int small = i;
|
||||||
|
for (int j = i; j < n; j++) {
|
||||||
|
if (a[j] < a[small]) {
|
||||||
|
small = j;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
swap(a, i, small);
|
||||||
|
printarr(a, n);
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void insertionsort(int a[], int n) {
|
||||||
|
for (int i = 1; i < n; i++) {
|
||||||
|
int j = i;
|
||||||
|
while (j > 0 && a[j - 1] > a[j]) {
|
||||||
|
swap(a, j, j - 1);
|
||||||
|
j--;
|
||||||
|
}
|
||||||
|
printarr(a, n);
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,7 @@
|
||||||
|
#include <stdio.h>
|
||||||
|
#include "utils.h"
|
||||||
|
|
||||||
|
void bubblesort(int a[], int n);
|
||||||
|
void selectionsort(int a[], int n);
|
||||||
|
void insertionsort(int a[], int n);
|
||||||
|
void radixsort(int a[]);
|
||||||
|
|
@ -0,0 +1,26 @@
|
||||||
|
#include "utils.h"
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
void printarr(int a[], int n) {
|
||||||
|
for (int i = 0; i < n; i++) {
|
||||||
|
printf("%d ", a[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void print_ascii(char *filename) {
|
||||||
|
char read_string[MAX_LEN];
|
||||||
|
FILE *fptr = NULL;
|
||||||
|
if ((fptr = fopen(filename, "r")) == NULL) {
|
||||||
|
fprintf(stderr, "error opening %s\n", filename);
|
||||||
|
}
|
||||||
|
while (fgets(read_string, sizeof(read_string), fptr) != NULL) {
|
||||||
|
printf("%s", read_string);
|
||||||
|
}
|
||||||
|
fclose(fptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
void swap(int a[], int i, int j) {
|
||||||
|
int tmp = a[i];
|
||||||
|
a[i] = a[j];
|
||||||
|
a[j] = tmp;
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,19 @@
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
#define MAX_LEN 128
|
||||||
|
#define BAR "────────────────────────────────\n"
|
||||||
|
|
||||||
|
// COLORS
|
||||||
|
#define COLOR_RED "\x1b[31m"
|
||||||
|
#define COLOR_GREEN "\x1b[32m"
|
||||||
|
#define COLOR_YELLOW "\x1b[33m"
|
||||||
|
#define COLOR_BLUE "\x1b[34m"
|
||||||
|
#define COLOR_MAGENTA "\x1b[35m"
|
||||||
|
#define COLOR_CYAN "\x1b[36m"
|
||||||
|
#define COLOR_RESET "\x1b[0m"
|
||||||
|
#define COLOR_BOLD "\e[1m"
|
||||||
|
#define COLOR_OFF "\e[m"
|
||||||
|
|
||||||
|
void printarr(int a[], int n);
|
||||||
|
void print_ascii(char *filename);
|
||||||
|
void swap(int a[], int i, int j);
|
||||||
Reference in New Issue